From e5107e7cfa8bc17c64fca257d8e440421de82849 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Mon, 1 Apr 2024 11:34:52 +0200 Subject: [PATCH] add ready check --- .gitignore | 48 +++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 2 -- TCPServer.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-- TCPServer.h | 5 ++++ utils.h | 17 +++++++++---- 5 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98fc262 --- /dev/null +++ b/.gitignore @@ -0,0 +1,48 @@ +### C++ template +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +### CMake template +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +cmake-build-debug +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index fc18c7c..c67a6b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,4 @@ set(CMAKE_CXX_STANDARD 17) add_executable(socketServer main.cpp TCPServer.cpp - TCPServer.h - utils.h ) diff --git a/TCPServer.cpp b/TCPServer.cpp index 52ad09c..8710578 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -1,7 +1,5 @@ #include "TCPServer.h" -#include "utils.h" - ClientHandler::ClientHandler(int clientSocket, TCPServer* server) : clientSocket(clientSocket), server(server) {}; void ClientHandler::handle() { @@ -68,6 +66,39 @@ TCPServer::TCPServer(int port) } std::cout << "Server started on port " << port << std::endl; + + clients.reserve(7); + ClientTCP tirette; + tirette.name = "tirette"; + + ClientTCP aruco; + aruco.name = "aruco"; + + ClientTCP ihm; + ihm.name = "ihm"; + + ClientTCP lidar; + lidar.name = "lidar"; + + ClientTCP arduino; + arduino.name = "arduino"; + arduino.isReady = true; + + ClientTCP servo_pot; + servo_pot.name = "servo_pot"; + servo_pot.isReady = true; + + ClientTCP servo_panneaux_solaire; + servo_panneaux_solaire.name = "servo_ps"; + servo_panneaux_solaire.isReady = true; + + clients.push_back(tirette); + clients.push_back(aruco); + clients.push_back(ihm); + clients.push_back(lidar); + clients.push_back(arduino); + clients.push_back(servo_pot); + clients.push_back(servo_panneaux_solaire); } void TCPServer::acceptConnections() @@ -115,6 +146,19 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) { this->broadcastMessage(message.c_str(), clientSocket); } + if (tokens[2] == "ready") + { + for (ClientTCP& client : clients) + { + if (client.name == tokens[0]) + { + client.isReady = true; + client.socket = clientSocket; + break; + } + } + checkIfAllClientsReady(); + } std::cout << "Received: " << message << std::endl; } @@ -163,3 +207,20 @@ void TCPServer::start() { std::thread([this]() { acceptConnections(); }).detach(); } + +void TCPServer::checkIfAllClientsReady() +{ + bool allReady = true; + for (ClientTCP& client : clients) + { + if (!client.isReady) + { + allReady = false; + } + } + + if (allReady) + { + this->broadcastMessage("strat;all;ready;1"); + } +} diff --git a/TCPServer.h b/TCPServer.h index 787e54b..490e312 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -9,6 +9,8 @@ #include #include +#include "utils.h" + class TCPServer; // Forward declaration class ClientHandler { @@ -33,6 +35,7 @@ private: std::vector clientSockets; // Store connected client sockets int connectedClients = 0; // Track the number of connected clients bool shouldStop = false; // Flag to indicate if the server should stop + std::vector clients; // Store connected clients public: explicit TCPServer(int port); @@ -52,5 +55,7 @@ public: int nbClients(); + void checkIfAllClientsReady(); + ~TCPServer(); }; diff --git a/utils.h b/utils.h index 8f23da5..cb4bd82 100644 --- a/utils.h +++ b/utils.h @@ -3,12 +3,12 @@ #include #include -bool startWith(const std::string& str, const std::string& start) +inline bool startWith(const std::string& str, const std::string& start) { return str.rfind(start, 0) == 0; } -bool endsWith(const std::string& str, const std::string& end) +inline bool endsWith(const std::string& str, const std::string& end) { if (str.length() >= end.length()) { @@ -17,12 +17,12 @@ bool endsWith(const std::string& str, const std::string& end) return false; } -bool contains(const std::string& str, const std::string& sub) +inline bool contains(const std::string& str, const std::string& sub) { return str.find(sub) != std::string::npos; } -std::vector split(const std::string& str, const std::string& delimiter) +inline std::vector split(const std::string& str, const std::string& delimiter) { std::vector tokens; size_t prev = 0, pos = 0; @@ -35,4 +35,11 @@ std::vector split(const std::string& str, const std::string& delimi prev = pos + delimiter.length(); } while (pos < str.length() && prev < str.length()); return tokens; -} \ No newline at end of file +} + +struct ClientTCP +{ + std::string name; + int socket = -1; + bool isReady = false; +}; \ No newline at end of file