From 852d59b0b3a97259beccd934603cf45f1be46576 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Thu, 23 Nov 2023 08:19:32 +0000 Subject: [PATCH 01/11] Initial commit --- .gitignore | 1 + STOMPClient.pro | 8 +++ main.cpp | 7 +++ stompframe.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ stompframe.h | 62 ++++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 .gitignore create mode 100644 STOMPClient.pro create mode 100644 main.cpp create mode 100644 stompframe.cpp create mode 100644 stompframe.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75c107b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pro.user diff --git a/STOMPClient.pro b/STOMPClient.pro new file mode 100644 index 0000000..e3e96e3 --- /dev/null +++ b/STOMPClient.pro @@ -0,0 +1,8 @@ +QT += network gui widgets +CONFIG += c++14 console +CONFIG -= app_bundle +HEADERS += \ + stompframe.h +SOURCES += \ + main.cpp \ + stompframe.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..bf2b52a --- /dev/null +++ b/main.cpp @@ -0,0 +1,7 @@ +#include + +int main(int argc, char *argv[]) { + QApplication application(argc, argv); + + return application.exec(); +} diff --git a/stompframe.cpp b/stompframe.cpp new file mode 100644 index 0000000..0b40080 --- /dev/null +++ b/stompframe.cpp @@ -0,0 +1,151 @@ +#include "stompframe.h" + +STOMPFrame::STOMPFrame(Command command, std::initializer_list> 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); +} + +STOMPFrame STOMPFrame::receive(QIODevice& device) { + STOMPFrame frame; + QTextStream in(&device); + + QString commandStr; + if (!in.readLineInto(&commandStr)) { + return {}; + } + if (commandStr == "STOMP") { + frame.command_ = STOMPFrame::STOMP; + } else if (commandStr == "SEND") { + frame.command_ = STOMPFrame::SEND; + } else if (commandStr == "SUBSCRIBE") { + frame.command_ = STOMPFrame::SUBSCRIBE; + } else if (commandStr == "UNSUBSCRIBE") { + frame.command_ = STOMPFrame::UNSUBSCRIBE; + } else if (commandStr == "BEGIN") { + frame.command_ = STOMPFrame::BEGIN; + } else if (commandStr == "COMMIT") { + frame.command_ = STOMPFrame::COMMIT; + } else if (commandStr == "ABORT") { + frame.command_ = STOMPFrame::ABORT; + } else if (commandStr == "ACK") { + frame.command_ = STOMPFrame::ACK; + } else if (commandStr == "NACK") { + frame.command_ = STOMPFrame::NACK; + } else if (commandStr == "DISCONNECT") { + frame.command_ = STOMPFrame::DISCONNECT; + } else if (commandStr == "CONNECTED") { + frame.command_ = STOMPFrame::CONNECTED; + } else if (commandStr == "MESSAGE") { + frame.command_ = STOMPFrame::MESSAGE; + } else if (commandStr == "RECEIPT") { + frame.command_ = STOMPFrame::RECEIPT; + } else if (commandStr == "ERROR") { + frame.command_ = STOMPFrame::ERROR; + } else { + return {}; + } + + QString headerLine; + while (in.readLineInto(&headerLine) && headerLine != "") { + auto components = headerLine.split(':'); + if (components.count() != 2) { + return {}; + } + frame.headers_.insert(components[0].trimmed(), components[1].trimmed()); + } + + if (frame.headers_.contains("content-length")) { + bool conversionOk; + qint64 length = frame.headers_["content-length"].toLongLong(&conversionOk); + if (!conversionOk) { + return {}; + } + while (frame.body_.length() < length) { + frame.body_ += in.read(length - frame.body_.length()).toUtf8(); + } + } else { + in >> frame.body_; + } + + return frame; +} + +QTextStream& operator <<(QTextStream& out, STOMPFrame::Command command) { + switch (command) { + case STOMPFrame::INVALID: + break; + + case STOMPFrame::STOMP: + out << "STOMP"; + break; + + case STOMPFrame::SEND: + out << "SEND"; + break; + + case STOMPFrame::SUBSCRIBE: + out << "SUBSCRIBE"; + break; + + case STOMPFrame::UNSUBSCRIBE: + out << "UNSUBSCRIBE"; + break; + + case STOMPFrame::BEGIN: + out << "BEGIN"; + break; + + case STOMPFrame::COMMIT: + out << "COMMIT"; + break; + + case STOMPFrame::ABORT: + out << "ABORT"; + break; + + case STOMPFrame::ACK: + out << "ACK"; + break; + + case STOMPFrame::NACK: + out << "NACK"; + break; + + case STOMPFrame::DISCONNECT: + out << "DISCONNECT"; + break; + + case STOMPFrame::CONNECTED: + out << "CONNECTED"; + break; + + case STOMPFrame::MESSAGE: + out << "MESSAGE"; + break; + + case STOMPFrame::RECEIPT: + out << "RECEIPT"; + break; + case STOMPFrame::ERROR: + out << "ERROR"; + break; + } + return out; +} diff --git a/stompframe.h b/stompframe.h new file mode 100644 index 0000000..d3352b6 --- /dev/null +++ b/stompframe.h @@ -0,0 +1,62 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +class STOMPFrame { + public: + enum Command { + INVALID, + + // Client messages + STOMP, SEND, SUBSCRIBE, UNSUBSCRIBE, BEGIN, COMMIT, ABORT, ACK, NACK, DISCONNECT, + + // Server messages + CONNECTED, MESSAGE, RECEIPT, ERROR + }; + + explicit STOMPFrame(Command command, std::initializer_list> headers = {}, const QByteArray& body = {}); + + bool isNull() const { + return command_ == INVALID; + } + + Command command() const { + return command_; + } + + void setCommand(Command command) { + command_ = command; + } + + const QMap& headers() const { + return headers_; + } + + void setHeaders(const QMap& headers) { + headers_ = headers; + } + + const QByteArray& body() const { + return body_; + } + + void setBody(const QByteArray& body) { + body_ = body; + } + + qint64 send(QIODevice& device); + static STOMPFrame receive(QIODevice& device); + + private: + STOMPFrame() = default; + + Command command_ = INVALID; + QMap headers_; + QByteArray body_; +}; + +QTextStream& operator <<(QTextStream& out, STOMPFrame::Command command); From e8cb3ab9b9f0639646f2a058f35120a23cb0b9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 29 Nov 2023 08:32:55 +0100 Subject: [PATCH 02/11] add CMake --- CMakeLists.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..49112ee --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.22) +project(ProtocolDeveloppement) + +set(CMAKE_CXX_STANDARD 14) + +set(CMAKE_PREFIX_PATH "/Qt/6.5.0/android_arm64_v8a/lib/cmake") + + +find_package(Qt6Widgets REQUIRED) +find_package(Qt6Network REQUIRED) +find_package(Qt6Gui REQUIRED) + +add_executable(${PROJECT_NAME} + main.cpp + stompframe.cpp +) + +link_directories( ${PCL_LIBRARY_DIRS} ) + +target_link_libraries(${PROJECT_NAME} + Qt6::Network + Qt6::Gui + Qt6::Widgets +) From 847393cec38fb294a737597e1925ec7d786d6c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 29 Nov 2023 11:53:20 +0100 Subject: [PATCH 03/11] create interface --- STOMPClient.pro | 6 ++++++ app.cpp | 5 +++++ app.h | 12 ++++++++++++ interface/iStompObserver.h | 16 ++++++++++++++++ interface/iStompSubject.h | 29 +++++++++++++++++++++++++++++ main.cpp | 25 ++++++++++++++++++++++++- stomp.cpp | 16 ++++++++++++++++ stomp.h | 35 +++++++++++++++++++++++++++++++++++ stompframe.cpp | 5 +++++ 9 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 app.cpp create mode 100644 app.h create mode 100644 interface/iStompObserver.h create mode 100644 interface/iStompSubject.h create mode 100644 stomp.cpp create mode 100644 stomp.h diff --git a/STOMPClient.pro b/STOMPClient.pro index e3e96e3..804c68a 100644 --- a/STOMPClient.pro +++ b/STOMPClient.pro @@ -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 diff --git a/app.cpp b/app.cpp new file mode 100644 index 0000000..5f73395 --- /dev/null +++ b/app.cpp @@ -0,0 +1,5 @@ +#include "app.h" + +App::App() { + +} diff --git a/app.h b/app.h new file mode 100644 index 0000000..4254ebd --- /dev/null +++ b/app.h @@ -0,0 +1,12 @@ +#ifndef APP_H +#define APP_H + +#include +#include "interface/iStompObserver.h" + +class App : public QObject, public interface::iStompObserver { +public: + App(); +}; + +#endif // APP_H diff --git a/interface/iStompObserver.h b/interface/iStompObserver.h new file mode 100644 index 0000000..e81ce3c --- /dev/null +++ b/interface/iStompObserver.h @@ -0,0 +1,16 @@ +#ifndef ISTOMPOBSERVER_H +#define ISTOMPOBSERVER_H + +#include + +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 diff --git a/interface/iStompSubject.h b/interface/iStompSubject.h new file mode 100644 index 0000000..d9278da --- /dev/null +++ b/interface/iStompSubject.h @@ -0,0 +1,29 @@ +#ifndef ISTOMPSUBJECT_H +#define ISTOMPSUBJECT_H + +#include "iStompObserver.h" +#include + +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 diff --git a/main.cpp b/main.cpp index bf2b52a..d351f99 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,30 @@ #include +#include + +#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(); } diff --git a/stomp.cpp b/stomp.cpp new file mode 100644 index 0000000..2f947fb --- /dev/null +++ b/stomp.cpp @@ -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; + + diff --git a/stomp.h b/stomp.h new file mode 100644 index 0000000..46e4b4e --- /dev/null +++ b/stomp.h @@ -0,0 +1,35 @@ +#ifndef STOMP_H +#define STOMP_H + +#include +#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 diff --git a/stompframe.cpp b/stompframe.cpp index 0b40080..9e8b8d0 100644 --- a/stompframe.cpp +++ b/stompframe.cpp @@ -2,21 +2,26 @@ STOMPFrame::STOMPFrame(Command command, std::initializer_list> 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); From 0907d3373c0e4a06863bb2f70eb133a71a495549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 29 Nov 2023 11:55:52 +0100 Subject: [PATCH 04/11] implement subscribe in STOMP --- main.cpp | 6 ++++++ stomp.cpp | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index d351f99..8f49b72 100644 --- a/main.cpp +++ b/main.cpp @@ -26,5 +26,11 @@ int main(int argc, char *argv[]) { {"login", "sdi10"}, {"passcode", "809c02f36becb0868da98761fe3209f6"} }).send(socket); + + + + STOMPFrame(STOMPFrame::MESSAGE, { + {}, + }).send(socket); return application.exec(); } diff --git a/stomp.cpp b/stomp.cpp index 2f947fb..f5900d3 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -4,13 +4,14 @@ Stomp::Stomp() { } -bool Stomp::subscribe(interface::iStompObserver obs) { +bool Stomp::subscribe(interface::iStompObserver* obs) { for(int i = 0; i < MAX_OBSERVER; i++) { - if (observer_[i] == nullptr) { - observer_[i] = obs; - return true; - } + if (observer_[i] == nullptr) { + observer_[i] = obs; + return true; } - return false; + } + return false; +} From 5388d154da1ecee23845e9ee46dd61e9223ccc42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 30 Nov 2023 09:53:53 +0100 Subject: [PATCH 05/11] implements oberver pattern --- .gitignore | 3 ++ app.cpp | 24 ++++++++++++ app.h | 10 +++++ interface/iStompObserver.h | 20 +++++++--- interface/iStompSubject.h | 33 +++++++++------- stomp.cpp | 80 ++++++++++++++++++++++++++++++++++++++ stomp.h | 15 ++++++- 7 files changed, 164 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 75c107b..5c0a0da 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.pro.user +cmake-build-debug +.idea +cmake-build-debug-visual-studio diff --git a/app.cpp b/app.cpp index 5f73395..714f481 100644 --- a/app.cpp +++ b/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() { + +} diff --git a/app.h b/app.h index 4254ebd..2a2759e 100644 --- a/app.h +++ b/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 diff --git a/interface/iStompObserver.h b/interface/iStompObserver.h index e81ce3c..5fcfe66 100644 --- a/interface/iStompObserver.h +++ b/interface/iStompObserver.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 diff --git a/interface/iStompSubject.h b/interface/iStompSubject.h index d9278da..b08562a 100644 --- a/interface/iStompSubject.h +++ b/interface/iStompSubject.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 diff --git a/stomp.cpp b/stomp.cpp index f5900d3..0fb7e8e 100644 --- a/stomp.cpp +++ b/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() { + +} diff --git a/stomp.h b/stomp.h index 46e4b4e..e2185b1 100644 --- a/stomp.h +++ b/stomp.h @@ -3,25 +3,28 @@ #include #include "interface/iStompSubject.h" +#include +#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 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 06/11] 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(); From ac7411aa58a963d7036868b8f78a7f66165852bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Fri, 22 Dec 2023 12:44:28 +0100 Subject: [PATCH 07/11] some update --- UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m | 0 app.cpp | 6 ++++-- app.h | 4 ++++ main.cpp | 12 ++---------- stomp.cpp | 6 +++++- 5 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m diff --git a/UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m b/UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m new file mode 100644 index 0000000..e69de29 diff --git a/app.cpp b/app.cpp index 714f481..6c379c1 100644 --- a/app.cpp +++ b/app.cpp @@ -1,7 +1,9 @@ #include "app.h" App::App() { - + st.connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); + st.sendRequest("/topic/sdi10.gem.command", "right"); + st.subscribeRequest("/topic/sdi10.gem.field", 1); } void App::connectConfirmation(bool success, int version) { @@ -13,7 +15,7 @@ void App::sendConfirmation(bool success) { } void App::subscribeConfirmation(bool success) { - + st.sendRequest("/topic/sdi10.gem.command", "up"); } void App::receiveIndication(int id, QString destination, QString body) { diff --git a/app.h b/app.h index 2a2759e..b1b81b3 100644 --- a/app.h +++ b/app.h @@ -3,6 +3,7 @@ #include #include "interface/iStompObserver.h" +#include "stomp.h" class App : public QObject, public interface::iStompObserver { public: @@ -17,6 +18,9 @@ private: void receiveIndication(int id, QString destination, QString body); void disconnectConfirmation(); void disconnectIndication(); + +private: + Stomp st; }; #endif // APP_H diff --git a/main.cpp b/main.cpp index 85a4c25..228f127 100644 --- a/main.cpp +++ b/main.cpp @@ -4,20 +4,12 @@ #include "stompframe.h" #include "stomp.h" +#include "app.h" int main(int argc, char *argv[]) { QApplication application(argc, argv); - Stomp st; - st.connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); - - st.sendRequest("/topic/sdi10.gem.command", "right"); - - st.sendRequest("/topic/sdi10.gem.command", "up"); - - st.sendRequest("/topic/sdi10.gem.command", "left"); - - st.sendRequest("/topic/sdi10.gem.command", "down"); + App app; return application.exec(); } diff --git a/stomp.cpp b/stomp.cpp index 78f2510..3828592 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -92,7 +92,11 @@ void Stomp::sendRequest(QString destination, const QByteArray& body) { } void Stomp::subscribeRequest(QString destination, int id) { - + QString sid = QString::number(id); + STOMPFrame(STOMPFrame::SUBSCRIBE, { + {"destination", destination}, + {"id", sid} + }).send(socket_); } void Stomp::disconnectRequest() { From 2418aafa9722a2df4bc229bd8e1f78099519e631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 26 Dec 2023 18:10:52 +0100 Subject: [PATCH 08/11] Segmentation fault --- app.cpp | 48 ++++++++++++++++++++++++++++++++------ app.h | 24 ++++++++++++++++--- interface/iStompObserver.h | 2 +- main.cpp | 5 ++-- stomp.cpp | 42 +++++++++++++++++++++++++++------ 5 files changed, 101 insertions(+), 20 deletions(-) diff --git a/app.cpp b/app.cpp index 6c379c1..cbbb67b 100644 --- a/app.cpp +++ b/app.cpp @@ -1,12 +1,14 @@ #include "app.h" -App::App() { - st.connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); - st.sendRequest("/topic/sdi10.gem.command", "right"); - st.subscribeRequest("/topic/sdi10.gem.field", 1); +App::App(Stomp* st) { + st_ = st; + st_->connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); + st_->sendRequest("/topic/sdi10.gem.command", "right"); + st_->subscribeRequest("/topic/sdi10.gem.field", 1); + } -void App::connectConfirmation(bool success, int version) { +void App::connectConfirmation(bool success, QString version) { } @@ -15,11 +17,14 @@ void App::sendConfirmation(bool success) { } void App::subscribeConfirmation(bool success) { - st.sendRequest("/topic/sdi10.gem.command", "up"); + st_->sendRequest("/topic/sdi10.gem.command", "up"); } void App::receiveIndication(int id, QString destination, QString body) { - + //qDebug() << "Indication " << id << " : " << destination << Qt::endl << body << Qt::endl; + if(destination.contains("field")){ + fillField(body); + } } void App::disconnectConfirmation() { @@ -29,3 +34,32 @@ void App::disconnectConfirmation() { void App::disconnectIndication() { } + +void App::addGem(int x, int y, int pts) { + Gem g; + g.x = x; + g.y = y; + g.pts = pts; + gems_.append(g); +} + +void App::fillField(QString body) { + static int x = 0; + static int y = 0; + for(int i = 0; i gems_; + Vehicle myVehicle_; + void addGem(int x, int y, int pts); + void fillField(QString body); + }; #endif // APP_H diff --git a/interface/iStompObserver.h b/interface/iStompObserver.h index 5fcfe66..28185a6 100644 --- a/interface/iStompObserver.h +++ b/interface/iStompObserver.h @@ -13,7 +13,7 @@ protected: iStompObserver() {} public: - virtual void connectConfirmation(bool success, int version) = 0; + virtual void connectConfirmation(bool success, QString version) = 0; virtual void sendConfirmation(bool success) = 0; virtual void subscribeConfirmation(bool success) = 0; virtual void receiveIndication(int id, QString destination, QString body) = 0; diff --git a/main.cpp b/main.cpp index 228f127..8439daf 100644 --- a/main.cpp +++ b/main.cpp @@ -2,14 +2,15 @@ #include #include -#include "stompframe.h" #include "stomp.h" #include "app.h" int main(int argc, char *argv[]) { QApplication application(argc, argv); - App app; + Stomp st; + App app(&st); + st.subscribe(&app); return application.exec(); } diff --git a/stomp.cpp b/stomp.cpp index 3828592..8f4288b 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -1,9 +1,12 @@ #include "stomp.h" Stomp::Stomp() { + for(int i = 0; i< MAX_OBSERVER; i++) { + observer_[i] = nullptr; + } socket_.setPeerVerifyMode(QSslSocket::VerifyNone); QObject::connect(&socket_, &QSslSocket::readyRead, [&] { - auto frame = STOMPFrame::receive(socket_); + STOMPFrame frame = STOMPFrame::receive(socket_); switch(frame.command()) { case STOMPFrame::CONNECTED: @@ -13,6 +16,7 @@ Stomp::Stomp() { break; case STOMPFrame::MESSAGE: + notifyReceiveIndication(1, frame.headers().value("destination"), frame.body()); qDebug() << "Message" << Qt::endl; break; @@ -104,26 +108,50 @@ void Stomp::disconnectRequest() { } void Stomp::notifyConnectConfirmation(bool success, QString version) { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->connectConfirmation(success, version); + } + } } void Stomp::notifySendConfirmation(bool success) { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->sendConfirmation(success); + } + } } void Stomp::notifySubscribeConfirmation(bool success) { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->subscribeConfirmation(success); + } + } } void Stomp::notifyReceiveIndication(int id, QString destination, QString body) { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->receiveIndication(id, destination, body); + } + } } void Stomp::notifyDisconnectConfirmation() { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->disconnectConfirmation(); + } + } } void Stomp::notifyDisconnectIndication() { - + for(int i = 0; i < MAX_OBSERVER; i++) { + if (observer_[i] != nullptr){ + observer_[i]->disconnectIndication(); + } + } } From 17ba18a200b95d7567397e4b6d519329dc906872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 26 Dec 2023 22:09:42 +0100 Subject: [PATCH 09/11] finish mandatory part of the lab --- CMakeLists.txt | 5 ++++- app.cpp | 38 ++++++++++++++++++++++++++++++++------ app.h | 5 +++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49112ee..74d79e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,8 @@ project(ProtocolDeveloppement) set(CMAKE_CXX_STANDARD 14) -set(CMAKE_PREFIX_PATH "/Qt/6.5.0/android_arm64_v8a/lib/cmake") +#set(CMAKE_PREFIX_PATH "/Qt/6.5.0/android_arm64_v8a/lib/cmake") +set(CMAKE_PREFIX_PATH "D:/programme/Qt/6.5.0/mingw_64/lib/cmake") find_package(Qt6Widgets REQUIRED) @@ -13,6 +14,8 @@ find_package(Qt6Gui REQUIRED) add_executable(${PROJECT_NAME} main.cpp stompframe.cpp + stomp.cpp + app.cpp ) link_directories( ${PCL_LIBRARY_DIRS} ) diff --git a/app.cpp b/app.cpp index cbbb67b..ec96429 100644 --- a/app.cpp +++ b/app.cpp @@ -25,6 +25,7 @@ void App::receiveIndication(int id, QString destination, QString body) { if(destination.contains("field")){ fillField(body); } + computeMove(); } void App::disconnectConfirmation() { @@ -40,26 +41,51 @@ void App::addGem(int x, int y, int pts) { g.x = x; g.y = y; g.pts = pts; + g.relativePts = computeRelativePts(g); gems_.append(g); } +void App::printGem() { + for (Gem g: gems_) { + qDebug() << g.x << ":" << g.y << " - " << g.pts << Qt::endl; + } +} + void App::fillField(QString body) { - static int x = 0; - static int y = 0; + int x = 0; + int y = 0; + gems_.clear(); for(int i = 0; i myGem.pts) myGem = g; + } + if(myGem.x>myVehicle_.x) st_->sendRequest("/topic/sdi10.gem.command", "right"); + else if(myGem.xsendRequest("/topic/sdi10.gem.command", "left"); + else if(myGem.y>myVehicle_.y) st_->sendRequest("/topic/sdi10.gem.command", "down"); + else if(myGem.ysendRequest("/topic/sdi10.gem.command", "up"); +} + diff --git a/app.h b/app.h index aada023..0b3d136 100644 --- a/app.h +++ b/app.h @@ -4,6 +4,7 @@ #include #include "interface/iStompObserver.h" #include "stomp.h" +#include class App : public QObject, public interface::iStompObserver { public: @@ -26,6 +27,7 @@ private: int x; int y; int pts; + QVector2D relativePts; } Gem; typedef struct { @@ -37,7 +39,10 @@ private: QVector gems_; Vehicle myVehicle_; void addGem(int x, int y, int pts); + void printGem(); void fillField(QString body); + QVector2D computeRelativePts(Gem g); + void computeMove(); }; From 633592a4eacf909c8f243d5098adee0b07f8619d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 28 Dec 2023 14:57:13 +0100 Subject: [PATCH 10/11] use vector for choose direction --- CMakeLists.txt | 3 +- UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m | 0 Vector2D.cpp | 49 ++++++++++++++ Vector2D.h | 41 +++++++++++ app.cpp | 79 ++++++++++++++++------ app.h | 18 +++-- stomp.cpp | 2 +- 7 files changed, 162 insertions(+), 30 deletions(-) delete mode 100644 UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m create mode 100644 Vector2D.cpp create mode 100644 Vector2D.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 74d79e3..74bd1a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(ProtocolDeveloppement) set(CMAKE_CXX_STANDARD 14) #set(CMAKE_PREFIX_PATH "/Qt/6.5.0/android_arm64_v8a/lib/cmake") -set(CMAKE_PREFIX_PATH "D:/programme/Qt/6.5.0/mingw_64/lib/cmake") +#set(CMAKE_PREFIX_PATH "D:/programme/Qt/6.5.0/mingw_64/lib/cmake") find_package(Qt6Widgets REQUIRED) @@ -16,6 +16,7 @@ add_executable(${PROJECT_NAME} stompframe.cpp stomp.cpp app.cpp + Vector2D.cpp ) link_directories( ${PCL_LIBRARY_DIRS} ) diff --git a/UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m b/UsersREMI~1.HERAppDataLocalTemptmp8h6pg12m deleted file mode 100644 index e69de29..0000000 diff --git a/Vector2D.cpp b/Vector2D.cpp new file mode 100644 index 0000000..b2889e8 --- /dev/null +++ b/Vector2D.cpp @@ -0,0 +1,49 @@ +#include "Vector2D.h" + +Vector2D::Vector2D(Point point) { + point_.x = point.x; + point_.y = point.y; + lenght_ = sqrt(pow(point.x, 2) + pow(point.y, 2)); + angle_ = atan2(point.y, point.x); +} + +Vector2D::Vector2D(double lenght, double angle) { + lenght_ = lenght; + angle_ = angle; + point_.x = lenght * cos(angle); + point_.y = lenght * sin(angle); +} + +Vector2D::Point Vector2D::CreatePoint(double x, double y) { + Point p; + p.x = x; + p.y = y; + return p; +} + +void Vector2D::normalize(double max) { + lenght_ = (1/lenght_); + point_.x = lenght_ * cos(angle_); + point_.y = lenght_ * sin(angle_); +} + +Vector2D Vector2D::reverse() { + Point p; + p.x = -point_.x; + p.y = -point_.y; + return Vector2D(p); +} + +Vector2D Vector2D::operator +(Vector2D v) const { + Point p; + p.x = point_.x + v.x(); + p.y = point_.y + v.y(); + return Vector2D(p); +} + +Vector2D Vector2D::operator -(Vector2D v) const { + Point p; + p.x = point_.x - v.x(); + p.y = point_.y - v.y(); + return Vector2D(p); +} \ No newline at end of file diff --git a/Vector2D.h b/Vector2D.h new file mode 100644 index 0000000..2997e03 --- /dev/null +++ b/Vector2D.h @@ -0,0 +1,41 @@ +#ifndef VECTOR2D_H +#define VECTOR2D_H + +#include + +class Vector2D { + +public: + typedef struct { + double x; + double y; + } Point; + Vector2D() = default; + Vector2D(Point point); + Vector2D(double lenght, double angle); + ~Vector2D() = default; + + static Point CreatePoint(double x, double y); + + double x() {return point_.x;} + double y() {return point_.y;} + Point point() {return point_;} + double lenght() {return lenght_;} + double angle() {return angle_;} + + void normalize(double max); + Vector2D reverse(); + + Vector2D operator +(Vector2D v) const; + Vector2D operator -(Vector2D v) const; + +private: + Point point_; + double lenght_; + double angle_; + +}; + + + +#endif //VECTOR2D_H diff --git a/app.cpp b/app.cpp index ec96429..b55728b 100644 --- a/app.cpp +++ b/app.cpp @@ -1,9 +1,9 @@ #include "app.h" + App::App(Stomp* st) { st_ = st; st_->connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); - st_->sendRequest("/topic/sdi10.gem.command", "right"); st_->subscribeRequest("/topic/sdi10.gem.field", 1); } @@ -38,19 +38,11 @@ void App::disconnectIndication() { void App::addGem(int x, int y, int pts) { Gem g; - g.x = x; - g.y = y; + g.coordinate = Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y)); g.pts = pts; - g.relativePts = computeRelativePts(g); gems_.append(g); } -void App::printGem() { - for (Gem g: gems_) { - qDebug() << g.x << ":" << g.y << " - " << g.pts << Qt::endl; - } -} - void App::fillField(QString body) { int x = 0; int y = 0; @@ -58,9 +50,7 @@ void App::fillField(QString body) { for(int i = 0; i myGem.pts) myGem = g; + myRelativePts = myRelativePts+computeRelativePts(g); + } + double angle = myRelativePts.angle(); + + if(angle <0) angle = angle+2*PI; + angle *= 180/PI; + + qDebug() << "Angle: " << angle << "°"; + qDebug() << "Length: " << myRelativePts.lenght() << Qt::endl; + + if(angle > 360) { + qDebug() << "Error angle: " << angle << Qt::endl; + } + else if(angle <= -360) { + qDebug() << "Error angle: " << angle << Qt::endl; + } + + else if(angle > 315) { // angle > 315° + st_->sendRequest("/topic/sdi10.gem.command", "right"); + } + else if(angle <= -315) { // angle <= -315° || angle <= 45° + st_->sendRequest("/topic/sdi10.gem.command", "right"); + } + + else if(angle > 225) { // angle > 225° + st_->sendRequest("/topic/sdi10.gem.command", "down"); + } + else if(angle <= -225) { // angle <= -225° || angle <= 135° + st_->sendRequest("/topic/sdi10.gem.command", "up"); + } + + else if(angle > 135) { // angle > 135° + st_->sendRequest("/topic/sdi10.gem.command", "left"); + } + else if(angle <= -135) { // angle <= -135° || angle <= 225° + st_->sendRequest("/topic/sdi10.gem.command", "left"); + } + + else if(angle > 45) { // angle > 45° + st_->sendRequest("/topic/sdi10.gem.command", "up"); + } + else if(angle <= -45) { // angle <= -45° || angle <= 315° + st_->sendRequest("/topic/sdi10.gem.command", "down"); + } + + else { + st_->sendRequest("/topic/sdi10.gem.command", "right"); } - if(myGem.x>myVehicle_.x) st_->sendRequest("/topic/sdi10.gem.command", "right"); - else if(myGem.xsendRequest("/topic/sdi10.gem.command", "left"); - else if(myGem.y>myVehicle_.y) st_->sendRequest("/topic/sdi10.gem.command", "down"); - else if(myGem.ysendRequest("/topic/sdi10.gem.command", "up"); } diff --git a/app.h b/app.h index 0b3d136..dab66aa 100644 --- a/app.h +++ b/app.h @@ -5,12 +5,19 @@ #include "interface/iStompObserver.h" #include "stomp.h" #include +#include + +#include "Vector2D.h" + +#define GRID_SIZE 32 + class App : public QObject, public interface::iStompObserver { public: App(Stomp* st); ~App() = default; + const double MAX_LENGHT = sqrt(GRID_SIZE*GRID_SIZE + GRID_SIZE*GRID_SIZE); // iStompObserver interface private: void connectConfirmation(bool success, QString version); @@ -20,14 +27,12 @@ private: void disconnectConfirmation(); void disconnectIndication(); -private: +protected: Stomp* st_; typedef struct { - int x; - int y; + Vector2D coordinate; int pts; - QVector2D relativePts; } Gem; typedef struct { @@ -37,11 +42,10 @@ private: } Vehicle; QVector gems_; - Vehicle myVehicle_; + Vector2D myVehicle_; void addGem(int x, int y, int pts); - void printGem(); void fillField(QString body); - QVector2D computeRelativePts(Gem g); + Vector2D computeRelativePts(Gem g); void computeMove(); }; diff --git a/stomp.cpp b/stomp.cpp index 8f4288b..38a4616 100644 --- a/stomp.cpp +++ b/stomp.cpp @@ -17,7 +17,7 @@ Stomp::Stomp() { case STOMPFrame::MESSAGE: notifyReceiveIndication(1, frame.headers().value("destination"), frame.body()); - qDebug() << "Message" << Qt::endl; + //qDebug() << "Message" << Qt::endl; break; case STOMPFrame::RECEIPT: From 53a2a09399325c678dc442777bc30bde0fdb435c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 28 Dec 2023 18:15:53 +0100 Subject: [PATCH 11/11] take care of other player --- STOMPClient.pro | 14 -------------- app.cpp | 27 ++++++++++++++++++++++++--- app.h | 4 ++++ 3 files changed, 28 insertions(+), 17 deletions(-) delete mode 100644 STOMPClient.pro diff --git a/STOMPClient.pro b/STOMPClient.pro deleted file mode 100644 index 804c68a..0000000 --- a/STOMPClient.pro +++ /dev/null @@ -1,14 +0,0 @@ -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 diff --git a/app.cpp b/app.cpp index b55728b..6c7bea6 100644 --- a/app.cpp +++ b/app.cpp @@ -17,7 +17,6 @@ void App::sendConfirmation(bool success) { } void App::subscribeConfirmation(bool success) { - st_->sendRequest("/topic/sdi10.gem.command", "up"); } void App::receiveIndication(int id, QString destination, QString body) { @@ -52,6 +51,9 @@ void App::fillField(QString body) { if(c == 'Y') { myVehicle_ = Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y)); } + if(c == 'h') { + otherVehicles_.append(Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y))); + } if(c == 'g') addGem(x, y, 100); if(c == 'G') addGem(x, y, 250); if(c == 'D') addGem(x, y, 500); @@ -69,14 +71,33 @@ Vector2D App::computeRelativePts(Gem g) { v.reverse(); v.normalize(MAX_LENGHT); - return Vector2D(v.lenght()*g.pts*v.lenght(), v.angle()); + return Vector2D(v.lenght()*g.pts, v.angle()); +} + +void App::computeRelativeDistance(Gem* g) { + int deltaX = abs(g->coordinate.x() - myVehicle_.x()); + int deltaY = abs(g->coordinate.y() - myVehicle_.y()); + g->relativeDistanceToMe = deltaX+deltaY; + g->relativeDistanceToOtherPlayer = MAX_LENGHT*2; + for(Vector2D v : otherVehicles_) { + deltaX = abs(g->coordinate.x() - v.x()); + deltaY = abs(g->coordinate.y() - v.y()); + if(deltaX+deltaY < g->relativeDistanceToOtherPlayer) { + g->relativeDistanceToOtherPlayer = deltaX+deltaY; + } + } } void App::computeMove() { static const double PI = 3.14159265358979323846; Vector2D myRelativePts; for(Gem g : gems_) { - myRelativePts = myRelativePts+computeRelativePts(g); + computeRelativeDistance(&g); + Vector2D v = computeRelativePts(g); + myRelativePts = myRelativePts+v; + if(g.relativeDistanceToMe <= g.relativeDistanceToOtherPlayer) { + myRelativePts = myRelativePts+v; + } } double angle = myRelativePts.angle(); diff --git a/app.h b/app.h index dab66aa..52e558d 100644 --- a/app.h +++ b/app.h @@ -32,6 +32,8 @@ protected: typedef struct { Vector2D coordinate; + int relativeDistanceToMe; + int relativeDistanceToOtherPlayer; int pts; } Gem; @@ -43,9 +45,11 @@ protected: QVector gems_; Vector2D myVehicle_; + QVector otherVehicles_; void addGem(int x, int y, int pts); void fillField(QString body); Vector2D computeRelativePts(Gem g); + void computeRelativeDistance(Gem* g); void computeMove(); };