mirror of
https://github.com/modelec/TCPSocketServer.git
synced 2026-01-18 16:37:29 +01:00
add ready check
This commit is contained in:
48
.gitignore
vendored
Normal file
48
.gitignore
vendored
Normal file
@@ -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
|
||||
@@ -5,6 +5,4 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(socketServer main.cpp
|
||||
TCPServer.cpp
|
||||
TCPServer.h
|
||||
utils.h
|
||||
)
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
class TCPServer; // Forward declaration
|
||||
|
||||
class ClientHandler {
|
||||
@@ -33,6 +35,7 @@ private:
|
||||
std::vector<int> 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<ClientTCP> clients; // Store connected clients
|
||||
|
||||
public:
|
||||
explicit TCPServer(int port);
|
||||
@@ -52,5 +55,7 @@ public:
|
||||
|
||||
int nbClients();
|
||||
|
||||
void checkIfAllClientsReady();
|
||||
|
||||
~TCPServer();
|
||||
};
|
||||
|
||||
17
utils.h
17
utils.h
@@ -3,12 +3,12 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
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<std::string> split(const std::string& str, const std::string& delimiter)
|
||||
inline std::vector<std::string> split(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
size_t prev = 0, pos = 0;
|
||||
@@ -35,4 +35,11 @@ std::vector<std::string> split(const std::string& str, const std::string& delimi
|
||||
prev = pos + delimiter.length();
|
||||
} while (pos < str.length() && prev < str.length());
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
|
||||
struct ClientTCP
|
||||
{
|
||||
std::string name;
|
||||
int socket = -1;
|
||||
bool isReady = false;
|
||||
};
|
||||
Reference in New Issue
Block a user