diff --git a/.gitignore b/.gitignore index 75c107b..5c0a0da 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.pro.user +cmake-build-debug +.idea +cmake-build-debug-visual-studio diff --git a/app.cpp b/app.cpp index 5f73395..714f481 100644 --- a/app.cpp +++ b/app.cpp @@ -3,3 +3,27 @@ App::App() { } + +void App::connectConfirmation(bool success, int version) { + +} + +void App::sendConfirmation(bool success) { + +} + +void App::subscribeConfirmation(bool success) { + +} + +void App::receiveIndication(int id, QString destination, QString body) { + +} + +void App::disconnectConfirmation() { + +} + +void App::disconnectIndication() { + +} diff --git a/app.h b/app.h index 4254ebd..2a2759e 100644 --- a/app.h +++ b/app.h @@ -7,6 +7,16 @@ class App : public QObject, public interface::iStompObserver { public: App(); + ~App() = default; + + // iStompObserver interface +private: + void connectConfirmation(bool success, int version); + void sendConfirmation(bool success); + void subscribeConfirmation(bool success); + void receiveIndication(int id, QString destination, QString body); + void disconnectConfirmation(); + void disconnectIndication(); }; #endif // APP_H diff --git a/interface/iStompObserver.h b/interface/iStompObserver.h index e81ce3c..5fcfe66 100644 --- a/interface/iStompObserver.h +++ b/interface/iStompObserver.h @@ -5,12 +5,20 @@ namespace interface { class iStompObserver { - virtual void connectConfirmation(bool success, int version); - virtual void sendConfirmation(bool success); - virtual void subscribeConfirmation(bool success); - virtual void receiveIndication(int id, QString destination, QString body); - virtual void disconnectConfirmation(); - virtual void disconnectIndication(); + +public: + virtual ~iStompObserver() {} + +protected: + iStompObserver() {} + +public: + virtual void connectConfirmation(bool success, int version) = 0; + virtual void sendConfirmation(bool success) = 0; + virtual void subscribeConfirmation(bool success) = 0; + virtual void receiveIndication(int id, QString destination, QString body) = 0; + virtual void disconnectConfirmation() = 0; + virtual void disconnectIndication() = 0; }; } #endif // ISTOMPOBSERVER_H diff --git a/interface/iStompSubject.h b/interface/iStompSubject.h index d9278da..b08562a 100644 --- a/interface/iStompSubject.h +++ b/interface/iStompSubject.h @@ -6,22 +6,29 @@ namespace interface { class iStompSubject { -public: - virtual bool subscribe(iStompObserver* obs); - virtual void unsubscribe(iStompObserver* obs); - virtual void connectRequest(QString host, int port, QString vhost, QString username, QString password); - virtual void sendRequest(QString destination, QString body); - virtual void subscribeRequest(QString destination, int id); - virtual void disconnectRequest(); +public: + virtual ~iStompSubject() {} protected: - virtual void notifyConnectConfirmation(bool success, int version); - virtual void notifySendConfirmation(bool success); - virtual void notifySubscribeConfirmation(bool success); - virtual void notifyReceiveIndication(int id, QString destination, QString body); - virtual void notifyDisconnectConfirmation(); - virtual void notifyDisconnectIndication(); + iStompSubject() {} + +public: + virtual bool subscribe(iStompObserver* obs) = 0; + virtual void unsubscribe(iStompObserver* obs) = 0; + + virtual int connectRequest(QString host, int port, QString vhost, QString username, QString password) = 0; + virtual void sendRequest(QString destination, QString body) = 0; + virtual void subscribeRequest(QString destination, int id) = 0; + virtual void disconnectRequest() = 0; + +protected: + virtual void notifyConnectConfirmation(bool success, QString version) = 0; + virtual void notifySendConfirmation(bool success) = 0; + virtual void notifySubscribeConfirmation(bool success) = 0; + virtual void notifyReceiveIndication(int id, QString destination, QString body) = 0; + virtual void notifyDisconnectConfirmation() = 0; + virtual void notifyDisconnectIndication() = 0; }; } // namespace diff --git a/stomp.cpp b/stomp.cpp index f5900d3..0fb7e8e 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -4,6 +4,11 @@ Stomp::Stomp() { } +Stomp::~Stomp() { + +} + + bool Stomp::subscribe(interface::iStompObserver* obs) { for(int i = 0; i < MAX_OBSERVER; i++) { if (observer_[i] == nullptr) { @@ -14,4 +19,79 @@ bool Stomp::subscribe(interface::iStompObserver* obs) { return false; } +void Stomp::unsubscribe(interface::iStompObserver* obs) { + for(int i = 0; i < MAX_OBSERVER; i++) { + if(observer_[i] == obs) { + observer_[i] = nullptr; + } + } +} + +int Stomp::connectRequest(QString host, int port, QString vhost, QString username, QString password) { + socket_.setPeerVerifyMode(QSslSocket::VerifyNone); + + + QObject::connect(&socket_, &QSslSocket::readyRead, [&]() { + auto frame = STOMPFrame::receive(socket_); + + qDebug() << "Connected:" << (frame.command() == STOMPFrame::CONNECTED) << Qt::endl + << "Version:" << frame.headers().value("version"); + + notifyConnectConfirmation((frame.command() == STOMPFrame::CONNECTED), + frame.headers().value("version")); + }); + + socket_.connectToHostEncrypted(host, port); + if(!socket_.waitForConnected()) return -1; + + STOMPFrame(STOMPFrame::STOMP, { + {"accept-version", "1.2"}, + {"host", vhost}, + {"login", username}, + {"passcode", password} + }).send(socket_); + + return 0; + +} + +void Stomp::sendRequest(QString destination, QString body) { + + QObject::connect(&socket_, &QSslSocket::readyRead, [&] { + auto frame = STOMPFrame::receive(socket_); + notifySendConfirmation(frame.command() == STOMPFrame::RECEIPT); + }); +} + +void Stomp::subscribeRequest(QString destination, int id) { + +} + +void Stomp::disconnectRequest() { + +} + +void Stomp::notifyConnectConfirmation(bool success, QString version) { + +} + +void Stomp::notifySendConfirmation(bool success) { + +} + +void Stomp::notifySubscribeConfirmation(bool success) { + +} + +void Stomp::notifyReceiveIndication(int id, QString destination, QString body) { + +} + +void Stomp::notifyDisconnectConfirmation() { + +} + +void Stomp::notifyDisconnectIndication() { + +} diff --git a/stomp.h b/stomp.h index 46e4b4e..e2185b1 100644 --- a/stomp.h +++ b/stomp.h @@ -3,25 +3,28 @@ #include #include "interface/iStompSubject.h" +#include +#include "stompframe.h" #define MAX_OBSERVER 5 class Stomp : public QObject, public interface::iStompSubject{ public: Stomp(); + ~Stomp(); // iStompSubject interface public: bool subscribe(interface::iStompObserver* obs); void unsubscribe(interface::iStompObserver* obs); - void connectRequest(QString host, int port, QString vhost, QString username, QString password); + int connectRequest(QString host, int port, QString vhost, QString username, QString password); void sendRequest(QString destination, QString body); void subscribeRequest(QString destination, int id); void disconnectRequest(); protected: - void notifyConnectConfirmation(bool success, int version); + void notifyConnectConfirmation(bool success, QString version); void notifySendConfirmation(bool success); void notifySubscribeConfirmation(bool success); void notifyReceiveIndication(int id, QString destination, QString body); @@ -30,6 +33,14 @@ protected: protected: interface::iStompObserver* observer_[MAX_OBSERVER]; + +private: + QSslSocket socket_; + QString host_ = "sdi.hevs.ch"; + QString vHost_ = "/"; + int port_ = 61614; + QString user_ = "sdi10"; + QString password_ = "809c02f36becb0868da98761fe3209f6"; }; #endif // STOMP_H