implements oberver pattern
This commit is contained in:
parent
0907d3373c
commit
5388d154da
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,4 @@
|
||||
*.pro.user
|
||||
cmake-build-debug
|
||||
.idea
|
||||
cmake-build-debug-visual-studio
|
||||
|
24
app.cpp
24
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() {
|
||||
|
||||
}
|
||||
|
10
app.h
10
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
80
stomp.cpp
80
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() {
|
||||
|
||||
}
|
||||
|
||||
|
15
stomp.h
15
stomp.h
@ -3,25 +3,28 @@
|
||||
|
||||
#include <QObject>
|
||||
#include "interface/iStompSubject.h"
|
||||
#include <QSslSocket>
|
||||
#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
|
||||
|
Reference in New Issue
Block a user