diff --git a/CMakeLists.txt b/CMakeLists.txt index 644dd2a..2e499e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,8 +43,8 @@ set(SOURCES preparation/Lidar.cpp preparation/OneItemPreparation.cpp preparation/TiretteState.cpp - tcp/TCPClient.cpp - tcp/TCPClient.h + tcp/MyTCPClient.cpp + tcp/MyTCPClient.h ) add_executable(ihm_robot resource.qrc ${HEADERS} ${SOURCES}) diff --git a/MainWindow.h b/MainWindow.h index 673d27b..730756a 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "homeButton.h" @@ -13,12 +12,16 @@ #include "PreparationMatch.h" #include "TeamChooser.h" #include "TestMode.h" +#include "tcp/MyTCPClient.h" -class MainWindow : public QMainWindow, public TCPClient { +class MainWindow : public QMainWindow { Q_OBJECT public: - MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent), TCPClient(address, port) + MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent) { + this->socketClient = new MyTCPSocket(this); + this->socketClient->connectToServer(address, port); + connect(this->socketClient, &MyTCPSocket::messageReceived, this, &MainWindow::handleMessage); this->centralWidget = new QWidget(this); this->setCentralWidget(centralWidget); @@ -49,7 +52,7 @@ public: this->setFixedSize(QSize(480, 320)); - this->home = new homeButton(centralWidget); + this->home = new homeButton(centralidget); connect(this->home, &homeButton::homologationClicked, this, &MainWindow::onHomologationPressed); @@ -68,7 +71,7 @@ public: connect(this->preparationMatch, &PreparationMatch::startGame, this, &MainWindow::onStartGame); // connect(this->preparationMatch, &PreparationMatch::askTCPServer, this, &MainWindow::broadcastTCPMessage); connect(this->preparationMatch, &PreparationMatch::askTCPServer, [&](const std::string& message) { - this->sendMessage(message.c_str()); + this->socketClient->sendMessage(message.c_str()); }); this->testMode = new TestMode(centralWidget); @@ -109,26 +112,6 @@ public: this->stackedWidget->setCurrentIndex(index); } - void handleMessage(const std::string& message) override - { - std::cout << "Received message: " << message << std::endl; - - std::vector list = TCPSocket::split(message, ";"); - - if (TCPSocket::startWith(list[2], "pong")) - { - preparationMatch->responseFromPing(QString::fromStdString(message)); - } - if (TCPSocket::contains(list[0], "tirette") && TCPSocket::contains(list[2], "set state")) - { - preparationMatch->responseTiretteState(QString::fromStdString(message)); - } - if (TCPSocket::contains(list[0], "lidar")) - { - preparationMatch->responseLidar(QString::fromStdString(message)); - } - } - protected slots: void onHomePressed() { @@ -170,6 +153,26 @@ protected slots: emit replierRobot(); } + + void handleMessage(const std::string& message) + { + std::vector list = TCPSocket::split(message, ";"); + + if (TCPSocket::startWith(list[2], "pong")) + { + preparationMatch->responseFromPing(QString::fromStdString(message)); + } + if (TCPSocket::contains(list[0], "tirette") && TCPSocket::contains(list[2], "set state")) + { + preparationMatch->responseTiretteState(QString::fromStdString(message)); + } + if (TCPSocket::contains(list[0], "lidar")) + { + preparationMatch->responseLidar(QString::fromStdString(message)); + } + } + + signals: void deplierRobot(); void replierRobot(); @@ -189,4 +192,6 @@ private: PreparationMatch* preparationMatch; TestMode* testMode; InGame* inGame; + + MyTCPSocket* socketClient; }; diff --git a/tcp/MyTCPClient.cpp b/tcp/MyTCPClient.cpp new file mode 100644 index 0000000..dd83631 --- /dev/null +++ b/tcp/MyTCPClient.cpp @@ -0,0 +1,47 @@ +#include "MyTCPClient.h" +#include + +MyTCPSocket::MyTCPSocket(QObject *parent) : QObject(parent) +{ + socket = new QTcpSocket(this); + + connect(socket, &QTcpSocket::connected, this, &MyTCPSocket::onConnected); + connect(socket, &QTcpSocket::disconnected, this, &MyTCPSocket::onDisconnected); + connect(socket, &QTcpSocket::readyRead, this, &MyTCPSocket::onReadyRead); + connect(socket, QOverload::of(&QTcpSocket::errorOccurred), + this, &MyTCPSocket::onErrorOccurred); +} + +void MyTCPSocket::connectToServer(const QString &host, quint16 port) +{ + socket->connectToHost(host, port); +} + +void MyTCPSocket::sendMessage(const QByteArray &message) +{ + if (socket->state() == QTcpSocket::ConnectedState) { + socket->write(message); + } +} + +void MyTCPSocket::onReadyRead() +{ + QByteArray message = socket->readAll(); + emit messageReceived(message); +} + +void MyTCPSocket::onConnected() +{ + emit connected(); +} + +void MyTCPSocket::onDisconnected() +{ + emit disconnected(); +} + +void MyTCPSocket::onErrorOccurred(QAbstractSocket::SocketError socketError) +{ + Q_UNUSED(socketError) + emit errorOccurred(socket->errorString()); +} diff --git a/tcp/TCPClient.h b/tcp/MyTCPClient.h similarity index 86% rename from tcp/TCPClient.h rename to tcp/MyTCPClient.h index f26dfcb..69fe1a6 100644 --- a/tcp/TCPClient.h +++ b/tcp/MyTCPClient.h @@ -3,11 +3,11 @@ #include #include -class TCPSocket : public QObject +class MyTCPSocket : public QObject { Q_OBJECT public: - explicit TCPSocket(QObject *parent = nullptr); + explicit MyTCPSocket(QObject *parent = nullptr); void connectToServer(const QString &host, quint16 port); void sendMessage(const QByteArray &message); diff --git a/tcp/TCPClient.cpp b/tcp/TCPClient.cpp deleted file mode 100644 index 8f059d0..0000000 --- a/tcp/TCPClient.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "TCPClient.h" -#include - -TCPSocket::TCPSocket(QObject *parent) : QObject(parent) -{ - socket = new QTcpSocket(this); - - connect(socket, &QTcpSocket::connected, this, &TCPSocket::onConnected); - connect(socket, &QTcpSocket::disconnected, this, &TCPSocket::onDisconnected); - connect(socket, &QTcpSocket::readyRead, this, &TCPSocket::onReadyRead); - connect(socket, QOverload::of(&QTcpSocket::errorOccurred), - this, &TCPSocket::onErrorOccurred); -} - -void TCPSocket::connectToServer(const QString &host, quint16 port) -{ - socket->connectToHost(host, port); -} - -void TCPSocket::sendMessage(const QByteArray &message) -{ - if (socket->state() == QTcpSocket::ConnectedState) { - socket->write(message); - } -} - -void TCPSocket::onReadyRead() -{ - QByteArray message = socket->readAll(); - emit messageReceived(message); -} - -void TCPSocket::onConnected() -{ - emit connected(); -} - -void TCPSocket::onDisconnected() -{ - emit disconnected(); -} - -void TCPSocket::onErrorOccurred(QAbstractSocket::SocketError socketError) -{ - Q_UNUSED(socketError) - emit errorOccurred(socket->errorString()); -}