From 356cb3e54159ac8eaa1c33dae7589af2d75f4759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 6 Dec 2023 12:00:27 +0100 Subject: [PATCH] implement send command --- interface/iStompSubject.h | 2 +- main.cpp | 29 +++++------------- stomp.cpp | 62 ++++++++++++++++++++++++++++----------- stomp.h | 2 +- 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/interface/iStompSubject.h b/interface/iStompSubject.h index b08562a..1924fd6 100644 --- a/interface/iStompSubject.h +++ b/interface/iStompSubject.h @@ -18,7 +18,7 @@ public: 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 sendRequest(QString destination, const QByteArray& body) = 0; virtual void subscribeRequest(QString destination, int id) = 0; virtual void disconnectRequest() = 0; diff --git a/main.cpp b/main.cpp index 8f49b72..85a4c25 100644 --- a/main.cpp +++ b/main.cpp @@ -1,36 +1,23 @@ #include #include +#include #include "stompframe.h" +#include "stomp.h" int main(int argc, char *argv[]) { QApplication application(argc, argv); - QSslSocket socket; - socket.setPeerVerifyMode(QSslSocket::VerifyNone); + Stomp st; + st.connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); + st.sendRequest("/topic/sdi10.gem.command", "right"); - QObject::connect(&socket, &QSslSocket::readyRead, [&]() { - auto frame = STOMPFrame::receive(socket); + st.sendRequest("/topic/sdi10.gem.command", "up"); - qDebug() << "Connected:" << (frame.command() == STOMPFrame::CONNECTED) << Qt::endl - << "Version:" << frame.headers().value("version"); - }); + st.sendRequest("/topic/sdi10.gem.command", "left"); - socket.connectToHostEncrypted("sdi.hevs.ch", 61614); - if(!socket.waitForConnected()) return -1; + st.sendRequest("/topic/sdi10.gem.command", "down"); - STOMPFrame(STOMPFrame::STOMP, { - {"accept-version", "1.2"}, - {"host", "/"}, - {"login", "sdi10"}, - {"passcode", "809c02f36becb0868da98761fe3209f6"} - }).send(socket); - - - - STOMPFrame(STOMPFrame::MESSAGE, { - {}, - }).send(socket); return application.exec(); } diff --git a/stomp.cpp b/stomp.cpp index 0fb7e8e..78f2510 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -1,7 +1,48 @@ #include "stomp.h" Stomp::Stomp() { + socket_.setPeerVerifyMode(QSslSocket::VerifyNone); + QObject::connect(&socket_, &QSslSocket::readyRead, [&] { + auto frame = STOMPFrame::receive(socket_); + switch(frame.command()) { + case STOMPFrame::CONNECTED: + qDebug() << "Connected !" << Qt::endl + << "Version:" << frame.headers().value("version"); + notifyConnectConfirmation(true,frame.headers().value("version")); + break; + + case STOMPFrame::MESSAGE: + qDebug() << "Message" << Qt::endl; + break; + + case STOMPFrame::RECEIPT: + notifySendConfirmation(true); + qDebug() << "Succesfully send" << Qt::endl; + break; + + case STOMPFrame::ERROR: + notifyConnectConfirmation(false,frame.headers().value("version")); + notifySendConfirmation(false); + qDebug() << Qt::endl + << "-----ERROR-----" << Qt::endl + << "Command: " << frame.command() << Qt::endl + << "Header: " << frame.headers() << Qt::endl + << "Body: " << frame.body() << Qt::endl + << Qt::endl; + break; + + default: + qDebug() << Qt::endl + << "-----Other STOMP frame-----" << Qt::endl + << "Command: " << frame.command() << Qt::endl + << "Header: " << frame.headers() << Qt::endl + << "Body: " << frame.body() << Qt::endl + << Qt::endl; + break; + + } + }); } Stomp::~Stomp() { @@ -28,18 +69,6 @@ void Stomp::unsubscribe(interface::iStompObserver* obs) { } 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; @@ -55,12 +84,11 @@ int Stomp::connectRequest(QString host, int port, QString vhost, QString usernam } -void Stomp::sendRequest(QString destination, QString body) { +void Stomp::sendRequest(QString destination, const QByteArray& body) { - QObject::connect(&socket_, &QSslSocket::readyRead, [&] { - auto frame = STOMPFrame::receive(socket_); - notifySendConfirmation(frame.command() == STOMPFrame::RECEIPT); - }); + STOMPFrame(STOMPFrame::SEND, { + {"destination", destination}, + }, body).send(socket_); } void Stomp::subscribeRequest(QString destination, int id) { diff --git a/stomp.h b/stomp.h index e2185b1..201ab7d 100644 --- a/stomp.h +++ b/stomp.h @@ -19,7 +19,7 @@ public: bool subscribe(interface::iStompObserver* obs); void unsubscribe(interface::iStompObserver* obs); int connectRequest(QString host, int port, QString vhost, QString username, QString password); - void sendRequest(QString destination, QString body); + void sendRequest(QString destination, const QByteArray& body); void subscribeRequest(QString destination, int id); void disconnectRequest();