2023-11-29 10:53:20 +00:00
|
|
|
#include "stomp.h"
|
|
|
|
|
|
|
|
Stomp::Stomp() {
|
2023-12-06 11:00:27 +00:00
|
|
|
socket_.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
QObject::connect(&socket_, &QSslSocket::readyRead, [&] {
|
|
|
|
auto frame = STOMPFrame::receive(socket_);
|
2023-11-29 10:53:20 +00:00
|
|
|
|
2023-12-06 11:00:27 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2023-11-29 10:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 08:53:53 +00:00
|
|
|
Stomp::~Stomp() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-29 10:55:52 +00:00
|
|
|
bool Stomp::subscribe(interface::iStompObserver* obs) {
|
2023-11-29 10:53:20 +00:00
|
|
|
for(int i = 0; i < MAX_OBSERVER; i++) {
|
2023-11-29 10:55:52 +00:00
|
|
|
if (observer_[i] == nullptr) {
|
|
|
|
observer_[i] = obs;
|
|
|
|
return true;
|
2023-11-29 10:53:20 +00:00
|
|
|
}
|
2023-11-29 10:55:52 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-29 10:53:20 +00:00
|
|
|
|
2023-11-30 08:53:53 +00:00
|
|
|
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_.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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:00:27 +00:00
|
|
|
void Stomp::sendRequest(QString destination, const QByteArray& body) {
|
2023-11-30 08:53:53 +00:00
|
|
|
|
2023-12-06 11:00:27 +00:00
|
|
|
STOMPFrame(STOMPFrame::SEND, {
|
|
|
|
{"destination", destination},
|
|
|
|
}, body).send(socket_);
|
2023-11-30 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stomp::subscribeRequest(QString destination, int id) {
|
2023-12-22 11:44:28 +00:00
|
|
|
QString sid = QString::number(id);
|
|
|
|
STOMPFrame(STOMPFrame::SUBSCRIBE, {
|
|
|
|
{"destination", destination},
|
|
|
|
{"id", sid}
|
|
|
|
}).send(socket_);
|
2023-11-30 08:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
|
|
|
}
|
2023-11-29 10:53:20 +00:00
|
|
|
|