Segmentation fault

This commit is contained in:
Rémi Heredero 2023-12-26 18:10:52 +01:00
parent ac7411aa58
commit 2418aafa97
5 changed files with 101 additions and 20 deletions

48
app.cpp
View File

@ -1,12 +1,14 @@
#include "app.h" #include "app.h"
App::App() { App::App(Stomp* st) {
st.connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6"); st_ = st;
st.sendRequest("/topic/sdi10.gem.command", "right"); st_->connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6");
st.subscribeRequest("/topic/sdi10.gem.field", 1); 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) { 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) { 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() { void App::disconnectConfirmation() {
@ -29,3 +34,32 @@ void App::disconnectConfirmation() {
void App::disconnectIndication() { 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<body.length(); i++) {
const QChar c = body.at(i);
if(c == 'g') addGem(x, y, 100);
if(c == 'G') addGem(x, y, 250);
if(c == 'D') addGem(x, y, 500);
if(c == 'Y') {
myVehicle_.x = x;
myVehicle_.y = y;
myVehicle_.me = true;
}
if(c == '\n') {
y++;
x = 0;
}
x++;
}
}

24
app.h
View File

@ -7,12 +7,12 @@
class App : public QObject, public interface::iStompObserver { class App : public QObject, public interface::iStompObserver {
public: public:
App(); App(Stomp* st);
~App() = default; ~App() = default;
// iStompObserver interface // iStompObserver interface
private: private:
void connectConfirmation(bool success, int version); void connectConfirmation(bool success, QString version);
void sendConfirmation(bool success); void sendConfirmation(bool success);
void subscribeConfirmation(bool success); void subscribeConfirmation(bool success);
void receiveIndication(int id, QString destination, QString body); void receiveIndication(int id, QString destination, QString body);
@ -20,7 +20,25 @@ private:
void disconnectIndication(); void disconnectIndication();
private: private:
Stomp st; Stomp* st_;
typedef struct {
int x;
int y;
int pts;
} Gem;
typedef struct {
int x;
int y;
bool me;
} Vehicle;
QVector<Gem> gems_;
Vehicle myVehicle_;
void addGem(int x, int y, int pts);
void fillField(QString body);
}; };
#endif // APP_H #endif // APP_H

View File

@ -13,7 +13,7 @@ protected:
iStompObserver() {} iStompObserver() {}
public: 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 sendConfirmation(bool success) = 0;
virtual void subscribeConfirmation(bool success) = 0; virtual void subscribeConfirmation(bool success) = 0;
virtual void receiveIndication(int id, QString destination, QString body) = 0; virtual void receiveIndication(int id, QString destination, QString body) = 0;

View File

@ -2,14 +2,15 @@
#include <QSslSocket> #include <QSslSocket>
#include <QThread> #include <QThread>
#include "stompframe.h"
#include "stomp.h" #include "stomp.h"
#include "app.h" #include "app.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QApplication application(argc, argv); QApplication application(argc, argv);
App app; Stomp st;
App app(&st);
st.subscribe(&app);
return application.exec(); return application.exec();
} }

View File

@ -1,9 +1,12 @@
#include "stomp.h" #include "stomp.h"
Stomp::Stomp() { Stomp::Stomp() {
for(int i = 0; i< MAX_OBSERVER; i++) {
observer_[i] = nullptr;
}
socket_.setPeerVerifyMode(QSslSocket::VerifyNone); socket_.setPeerVerifyMode(QSslSocket::VerifyNone);
QObject::connect(&socket_, &QSslSocket::readyRead, [&] { QObject::connect(&socket_, &QSslSocket::readyRead, [&] {
auto frame = STOMPFrame::receive(socket_); STOMPFrame frame = STOMPFrame::receive(socket_);
switch(frame.command()) { switch(frame.command()) {
case STOMPFrame::CONNECTED: case STOMPFrame::CONNECTED:
@ -13,6 +16,7 @@ Stomp::Stomp() {
break; break;
case STOMPFrame::MESSAGE: case STOMPFrame::MESSAGE:
notifyReceiveIndication(1, frame.headers().value("destination"), frame.body());
qDebug() << "Message" << Qt::endl; qDebug() << "Message" << Qt::endl;
break; break;
@ -104,26 +108,50 @@ void Stomp::disconnectRequest() {
} }
void Stomp::notifyConnectConfirmation(bool success, QString version) { 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) { 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) { 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) { 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() { void Stomp::notifyDisconnectConfirmation() {
for(int i = 0; i < MAX_OBSERVER; i++) {
if (observer_[i] != nullptr){
observer_[i]->disconnectConfirmation();
}
}
} }
void Stomp::notifyDisconnectIndication() { void Stomp::notifyDisconnectIndication() {
for(int i = 0; i < MAX_OBSERVER; i++) {
if (observer_[i] != nullptr){
observer_[i]->disconnectIndication();
}
}
} }