diff --git a/xhal/client/src/XHALInterface.cpp b/xhal/client/src/XHALInterface.cpp index 9de167bbbb9f9b03f9d4fa5661d1dbf8d03057d5..78276048facaa17918dc2a909c7d2e0b5d1616e7 100644 --- a/xhal/client/src/XHALInterface.cpp +++ b/xhal/client/src/XHALInterface.cpp @@ -2,12 +2,30 @@ #include <xhal/client/XHALInterface.h> +#include <regex> +#include <string> + xhal::client::XHALInterface::XHALInterface(const std::string& board_url, log4cplus::Logger& logger) : m_board_url(board_url) , m_logger(logger) { + // Split the URL into hostname and port + std::regex re("([^:]+)(?::([0-9]+))?"); + std::smatch m; + + if (!std::regex_match(board_url, m, re)) + throw std::runtime_error("Invalid board URL: " + board_url); + + // First capture group -> hostname + const std::string board_hostname = m[1].str(); + + // Second capture group -> port + short board_port = 9811; + if (m[2].matched) + board_port = std::stoi(m[2].str()); + try { - m_rpc.connect(board_url); + m_rpc.connect(board_hostname, board_port); XHAL_INFO("Connected to " << m_board_url); } catch (wisc::RPCSvc::ConnectionFailedException& e) { XHAL_ERROR("Caught RPCErrorException: " << e.message.data()); diff --git a/xhal/server/CMakeLists.txt b/xhal/server/CMakeLists.txt index 90b6691d2e8e414b4b1a97c7e9da1877052ca93b..7910a605302980bf38e2b55ebdde8a6d469131db 100644 --- a/xhal/server/CMakeLists.txt +++ b/xhal/server/CMakeLists.txt @@ -25,6 +25,7 @@ target_include_directories(xhal_server PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/_inte target_link_libraries(xhal_server PUBLIC xhal_common) target_link_libraries(xhal_server PUBLIC cereal::cereal) +target_link_libraries(xhal_server PRIVATE CLI11::CLI11) target_link_libraries(xhal_server PRIVATE rt) target_link_libraries(xhal_server PRIVATE ZLIB::ZLIB) target_link_libraries(xhal_server PRIVATE ${CMAKE_DL_LIBS}) diff --git a/xhal/server/src/rpcsvc.cpp b/xhal/server/src/rpcsvc.cpp index 39a86d4ebf7b3040feff019b344dddff89a692e0..35e9d2f91f6bb067278c70ce287395a1c30a272f 100644 --- a/xhal/server/src/rpcsvc.cpp +++ b/xhal/server/src/rpcsvc.cpp @@ -1,5 +1,7 @@ /// @file +#include <CLI/CLI.hpp> + #include <dirent.h> #include <errno.h> #include <netinet/in.h> @@ -15,36 +17,32 @@ #include <sys/wait.h> #include <unistd.h> -#include <xhal/server/LogManager.h> - #define MAX_CLIENTS 50 -#undef NODAEMON - //#define dprintf(...) printf(__VA_ARGS__) #define dprintf(...) int run_client(int clientfd); // run_client.cpp -void do_fork() -{ - if (fork()) - exit(0); - setsid(); - if (fork()) - exit(0); - - // Close all standard file descriptors - // This avoid unexpected output or termination by signal - close(0); - close(1); - close(2); -} - void handle_client_connection(int clientfd, struct sockaddr_in& client_addr, socklen_t client_addr_len, int& numchildren); +void do_fork(); + int main(int argc, char* argv[]) { + // Define the options + bool foreground = false; + short port = 9811; + + // Parse the command line + { + CLI::App app { "RPC server application\n" }; + app.option_defaults()->always_capture_default(); + app.add_option("-p,--port", port, "Port on which to listen"); + app.add_flag("-f,--foreground", foreground, "Not not daemonize the server")->multi_option_policy(CLI::MultiOptionPolicy::Throw); + CLI11_PARSE(app, argc, argv); + } + int listenfd; struct sockaddr_in serv_addr; @@ -56,7 +54,7 @@ int main(int argc, char* argv[]) serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = htons(9811); + serv_addr.sin_port = htons(port); int optval = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)); @@ -69,9 +67,8 @@ int main(int argc, char* argv[]) dprintf("Listening...\n"); listen(listenfd, 5); -#ifndef NODAEMON - do_fork(); -#endif + if (!foreground) + do_fork(); int numchildren = 0; while (1) { @@ -151,3 +148,18 @@ void handle_client_connection(int clientfd, struct sockaddr_in& client_addr, soc numchildren++; close(clientfd); // parent has no use for this. } + +void do_fork() +{ + if (fork()) + exit(0); + setsid(); + if (fork()) + exit(0); + + // Close all standard file descriptors + // This avoid unexpected output or termination by signal + close(0); + close(1); + close(2); +}