15#include "sockpp/tcp_acceptor.h"
16#include "sockpp/tcp_connector.h"
28#define PROGRAM_NAME "client-server-interaction-demo"
38 return std::thread([&app]() ->
void {
40 IRSOL_LOG_FATAL(
"Failed to start server.");
48 std::string clientName,
50 std::vector<std::string> commands)
52 std::random_device rd;
53 std::mt19937 gen(rd());
54 std::uniform_int_distribution<> waitDist(40, 1000);
58 std::string host =
"127.0.0.1";
61 IRSOL_LOG_WARN(
"Failed to connect to server at {}:{}: {}", host, port, ec.message());
66 for(
const auto& command : commands) {
67 auto waitMs = waitDist(gen);
68 std::this_thread::sleep_for(std::chrono::milliseconds(waitMs));
70 conn.write(command.data(), command.size());
73 auto result = conn.read(buf,
sizeof(buf) - 1);
75 buf[
result.value()] =
'\0';
88 const std::string& clientName,
90 const std::vector<std::string> commands)
115 args::HelpFlag help(parser,
"help",
"Display this help menu", {
'h',
"help"});
117 args::ValueFlag<uint32_t> numClientsArg(
118 parser,
"#clients",
"Number of clients to run concurrently", {
'c',
"clients"});
121 parser.ParseCLI(argc, argv);
122 }
catch(args::Help) {
123 std::cout << parser.Help() << std::endl;
125 }
catch(args::ParseError& e) {
126 std::cerr <<
"Error parsing command-line arguments:\n" << e.what() <<
"\n\n" << parser.Help();
147 std::vector<std::thread> clientThreads;
148 std::vector<std::string> sampleCommands = {
"fr?\n",
157 for(
size_t i = 0; i < numClients; ++i) {
158 std::string clientName =
"Client" + std::to_string(i + 1);
160 std::vector<std::string> shuffledCommands = sampleCommands;
162 shuffledCommands.begin(), shuffledCommands.end(), std::mt19937(std::random_device()()));
163 clientThreads.emplace_back(
runClientThread(clientName, port, shuffledCommands));
167 for(
auto& t : clientThreads) {
174 if(serverThread.joinable())
Main server application that manages client connections and camera streaming.
void stop()
Stops the server.
bool start()
Starts the server.
const std::string getProgramName()
Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time,...
Params getParams(int argc, char **argv)
Parses command-line arguments using args library.
std::thread runClientThread(const std::string &clientName, irsol::types::port_t port, const std::vector< std::string > commands)
TCP client that connects to the server and sends commands at random intervals.
void runDemo(uint32_t numClients)
void clientThreadBody(std::string clientName, irsol::types::port_t port, std::vector< std::string > commands)
The function executed by each client thread.
std::thread runServerThread(irsol::server::App &app)
Demonstrates handler creation, registration, and dispatch.
void initAssertHandler()
Initializes the assertion handler system.
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
#define IRSOL_NAMED_LOG_WARN(name,...)
Logs a warning-level message using a named logger.
void initLogging(const char *fileSinkFilename="logs/irsol.log", std::optional< spdlog::level::level_enum > minLogLevel=std::nullopt)
Initializes the irsol logging system.
uint16_t port_t
Represents a network port number. Typically used to specify TCP or UDP ports.
sockpp::tcp_connector connector_t
Alias for the TCP client connector type.
Command-line parameters for the program.
uint32_t numClients
Number of clients to run concurrently (default: 1)