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] 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(); + } + } }