create interface

This commit is contained in:
Rémi Heredero 2023-11-29 11:53:20 +01:00
parent e8cb3ab9b9
commit 847393cec3
9 changed files with 148 additions and 1 deletions

View File

@ -2,7 +2,13 @@ QT += network gui widgets
CONFIG += c++14 console
CONFIG -= app_bundle
HEADERS += \
app.h \
interface/iStompObserver.h \
interface/iStompSubject.h \
stomp.h \
stompframe.h
SOURCES += \
app.cpp \
main.cpp \
stomp.cpp \
stompframe.cpp

5
app.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "app.h"
App::App() {
}

12
app.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef APP_H
#define APP_H
#include <QObject>
#include "interface/iStompObserver.h"
class App : public QObject, public interface::iStompObserver {
public:
App();
};
#endif // APP_H

View File

@ -0,0 +1,16 @@
#ifndef ISTOMPOBSERVER_H
#define ISTOMPOBSERVER_H
#include <QString>
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();
};
}
#endif // ISTOMPOBSERVER_H

29
interface/iStompSubject.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef ISTOMPSUBJECT_H
#define ISTOMPSUBJECT_H
#include "iStompObserver.h"
#include <QString>
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();
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();
};
} // namespace
#endif // ISTOMPSUBJECT_H

View File

@ -1,7 +1,30 @@
#include <QApplication>
#include <QSslSocket>
#include "stompframe.h"
int main(int argc, char *argv[]) {
QApplication application(argc, argv);
QSslSocket socket;
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");
});
socket.connectToHostEncrypted("sdi.hevs.ch", 61614);
if(!socket.waitForConnected()) return -1;
STOMPFrame(STOMPFrame::STOMP, {
{"accept-version", "1.2"},
{"host", "/"},
{"login", "sdi10"},
{"passcode", "809c02f36becb0868da98761fe3209f6"}
}).send(socket);
return application.exec();
}

16
stomp.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "stomp.h"
Stomp::Stomp() {
}
bool Stomp::subscribe(interface::iStompObserver obs) {
for(int i = 0; i < MAX_OBSERVER; i++) {
if (observer_[i] == nullptr) {
observer_[i] = obs;
return true;
}
}
return false;

35
stomp.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef STOMP_H
#define STOMP_H
#include <QObject>
#include "interface/iStompSubject.h"
#define MAX_OBSERVER 5
class Stomp : public QObject, public interface::iStompSubject{
public:
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);
void sendRequest(QString destination, QString body);
void subscribeRequest(QString destination, int id);
void disconnectRequest();
protected:
void notifyConnectConfirmation(bool success, int version);
void notifySendConfirmation(bool success);
void notifySubscribeConfirmation(bool success);
void notifyReceiveIndication(int id, QString destination, QString body);
void notifyDisconnectConfirmation();
void notifyDisconnectIndication();
protected:
interface::iStompObserver* observer_[MAX_OBSERVER];
};
#endif // STOMP_H

View File

@ -2,21 +2,26 @@
STOMPFrame::STOMPFrame(Command command, std::initializer_list<QPair<QString, QString>> headers, const QByteArray& body):
command_(command), body_(body) {
for (const auto& header: headers) {
headers_[header.first] = header.second;
}
}
qint64 STOMPFrame::send(QIODevice& device) {
QByteArray encoded;
QTextStream out {&encoded};
out << command_ << "\n";
for (const auto& key: headers_.keys()) {
out << key << ":" << headers_[key] << "\n";
}
if (!headers_.contains("content-length")) {
out << "content-length" << ":" << body_.size() << "\n";
}
out << "\n" << body_ << '\0';
out.flush();
return device.write(encoded);