implements oberver pattern

This commit is contained in:
Rémi Heredero 2023-11-30 09:53:53 +01:00
parent 0907d3373c
commit 5388d154da
7 changed files with 164 additions and 21 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
*.pro.user *.pro.user
cmake-build-debug
.idea
cmake-build-debug-visual-studio

24
app.cpp
View File

@ -3,3 +3,27 @@
App::App() { 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() {
}

10
app.h
View File

@ -7,6 +7,16 @@
class App : public QObject, public interface::iStompObserver { class App : public QObject, public interface::iStompObserver {
public: public:
App(); 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 #endif // APP_H

View File

@ -5,12 +5,20 @@
namespace interface { namespace interface {
class iStompObserver { class iStompObserver {
virtual void connectConfirmation(bool success, int version);
virtual void sendConfirmation(bool success); public:
virtual void subscribeConfirmation(bool success); virtual ~iStompObserver() {}
virtual void receiveIndication(int id, QString destination, QString body);
virtual void disconnectConfirmation(); protected:
virtual void disconnectIndication(); 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 #endif // ISTOMPOBSERVER_H

View File

@ -6,22 +6,29 @@
namespace interface { namespace interface {
class iStompSubject { 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); public:
virtual void sendRequest(QString destination, QString body); virtual ~iStompSubject() {}
virtual void subscribeRequest(QString destination, int id);
virtual void disconnectRequest();
protected: protected:
virtual void notifyConnectConfirmation(bool success, int version); iStompSubject() {}
virtual void notifySendConfirmation(bool success);
virtual void notifySubscribeConfirmation(bool success); public:
virtual void notifyReceiveIndication(int id, QString destination, QString body); virtual bool subscribe(iStompObserver* obs) = 0;
virtual void notifyDisconnectConfirmation(); virtual void unsubscribe(iStompObserver* obs) = 0;
virtual void notifyDisconnectIndication();
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 } // namespace

View File

@ -4,6 +4,11 @@ Stomp::Stomp() {
} }
Stomp::~Stomp() {
}
bool Stomp::subscribe(interface::iStompObserver* obs) { bool Stomp::subscribe(interface::iStompObserver* obs) {
for(int i = 0; i < MAX_OBSERVER; i++) { for(int i = 0; i < MAX_OBSERVER; i++) {
if (observer_[i] == nullptr) { if (observer_[i] == nullptr) {
@ -14,4 +19,79 @@ bool Stomp::subscribe(interface::iStompObserver* obs) {
return false; 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() {
}

15
stomp.h
View File

@ -3,25 +3,28 @@
#include <QObject> #include <QObject>
#include "interface/iStompSubject.h" #include "interface/iStompSubject.h"
#include <QSslSocket>
#include "stompframe.h"
#define MAX_OBSERVER 5 #define MAX_OBSERVER 5
class Stomp : public QObject, public interface::iStompSubject{ class Stomp : public QObject, public interface::iStompSubject{
public: public:
Stomp(); Stomp();
~Stomp();
// iStompSubject interface // iStompSubject interface
public: public:
bool subscribe(interface::iStompObserver* obs); bool subscribe(interface::iStompObserver* obs);
void unsubscribe(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 sendRequest(QString destination, QString body);
void subscribeRequest(QString destination, int id); void subscribeRequest(QString destination, int id);
void disconnectRequest(); void disconnectRequest();
protected: protected:
void notifyConnectConfirmation(bool success, int version); void notifyConnectConfirmation(bool success, QString version);
void notifySendConfirmation(bool success); void notifySendConfirmation(bool success);
void notifySubscribeConfirmation(bool success); void notifySubscribeConfirmation(bool success);
void notifyReceiveIndication(int id, QString destination, QString body); void notifyReceiveIndication(int id, QString destination, QString body);
@ -30,6 +33,14 @@ protected:
protected: protected:
interface::iStompObserver* observer_[MAX_OBSERVER]; 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 #endif // STOMP_H