From 7a7943b3b3a7b0574f44c786ac8bfeff5bffc92b Mon Sep 17 00:00:00 2001 From: Henri Casanova Date: Mon, 30 Sep 2024 19:52:00 -1000 Subject: [PATCH] Added a "fixed simulation port" option to the wrench-daemon --- .../wrench-daemon/include/WRENCHDaemon.h | 2 ++ .../wrench/wrench-daemon/src/WRENCHDaemon.cpp | 12 ++++++-- tools/wrench/wrench-daemon/src/main.cpp | 29 ++++++++++++++----- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/tools/wrench/wrench-daemon/include/WRENCHDaemon.h b/tools/wrench/wrench-daemon/include/WRENCHDaemon.h index db0c44ddfc..b88bdf706b 100644 --- a/tools/wrench/wrench-daemon/include/WRENCHDaemon.h +++ b/tools/wrench/wrench-daemon/include/WRENCHDaemon.h @@ -35,6 +35,7 @@ class WRENCHDaemon { WRENCHDaemon(bool simulation_logging, bool daemon_logging, int port_number, + int fixed_simulation_port_number, const std::string &allowed_origin, int sleep_us); @@ -64,6 +65,7 @@ class WRENCHDaemon { bool simulation_logging; bool daemon_logging; int port_number; + int fixed_simulation_port_number; std::string allowed_origin; int sleep_us; diff --git a/tools/wrench/wrench-daemon/src/WRENCHDaemon.cpp b/tools/wrench/wrench-daemon/src/WRENCHDaemon.cpp index da31c086a1..67874c78d3 100644 --- a/tools/wrench/wrench-daemon/src/WRENCHDaemon.cpp +++ b/tools/wrench/wrench-daemon/src/WRENCHDaemon.cpp @@ -36,7 +36,8 @@ std::vector WRENCHDaemon::allowed_origins; * @brief Constructor * @param simulation_logging true if simulation logging should be printed * @param daemon_logging true if daemon logging should be printed -* @param port_number port number on which to listen +* @param port_number port number on which to listen for 'start simulation' requests +* @param simulation_port_number port number on which to listen for a new simulation (0 means: use a random port each time) * @param allowed_origin allowed origin for http connection * @param sleep_us number of micro-seconds or real time that the simulation daemon's simulation execution_controller * thread should sleep at each iteration @@ -44,10 +45,12 @@ std::vector WRENCHDaemon::allowed_origins; WRENCHDaemon::WRENCHDaemon(bool simulation_logging, bool daemon_logging, int port_number, + int simulation_port_number, const std::string &allowed_origin, int sleep_us) : simulation_logging(simulation_logging), daemon_logging(daemon_logging), port_number(port_number), + fixed_simulation_port_number(simulation_port_number), sleep_us(sleep_us) { WRENCHDaemon::allowed_origins.push_back(allowed_origin); } @@ -165,8 +168,11 @@ void WRENCHDaemon::startSimulation(const Request &req, Response &res) { // Find an available port number on which the simulation daemon will be able to run int simulation_port_number; - while (isPortTaken(simulation_port_number = PORT_MIN + rand() % (PORT_MAX - PORT_MIN))) - ; + if (this->fixed_simulation_port_number == 0) { + while (isPortTaken(simulation_port_number = PORT_MIN + rand() % (PORT_MAX - PORT_MIN))); + } else { + simulation_port_number = this->fixed_simulation_port_number; + } // Create a shared memory segment, to which an error message will be written by // the child process (the simulation daemon) in case it fails on startup diff --git a/tools/wrench/wrench-daemon/src/main.cpp b/tools/wrench/wrench-daemon/src/main.cpp index 3c6889ca04..16ba67be48 100644 --- a/tools/wrench/wrench-daemon/src/main.cpp +++ b/tools/wrench/wrench-daemon/src/main.cpp @@ -38,14 +38,21 @@ int main(int argc, char **argv) { // Define command-line argument options po::options_description desc("Allowed options"); - desc.add_options()("help", "Show this help message")("simulation-logging", po::bool_switch()->default_value(false), - "Show full simulation log during execution")("daemon-logging", po::bool_switch()->default_value(false), - "Show full daemon log during execution")("port", po::value()->default_value(8101)->notifier(in(1024, 49151, "port")), - "port number, between 1024 and 4951, on which this daemon will listen")("allow-origin", po::value()->default_value(""), - "allow origin for http connections to avoid CORS errors if needed (e.g., --allow-origin http://localhost:8000)")("sleep-us", po::value()->default_value(200)->notifier(in(0, 1000000, "sleep-us")), - "number of micro-seconds, between 0 and 1000000, that the simulation " - "thread sleeps at each iteration of its main loop (smaller means faster " - "simulation, larger means less CPU load)"); + desc.add_options()("help", "Show this help message") + ("simulation-logging", po::bool_switch()->default_value(false), + "Show full simulation log during execution") + ("daemon-logging", po::bool_switch()->default_value(false), + "Show full daemon log during execution") + ("port", po::value()->default_value(8101)->notifier(in(1024, 49151, "port")), + "A port number, between 1024 and 4951, on which this daemon will listen for 'start simulation' requests") + ("allow-origin", po::value()->default_value(""), + "Allow origin for http connections to avoid CORS errors if needed (e.g., --allow-origin http://localhost:8000)") + ("simulation-port", po::value()->notifier(in(1024, 49151, "simulation-port")), + "A fixed port number to be use for all simulations (prevents concurrent simulations, use at your own risk)") + ("sleep-us", po::value()->default_value(200)->notifier(in(0, 1000000, "sleep-us")), + "A number of micro-seconds, between 0 and 1000000, that the simulation " + "thread sleeps at each iteration of its main loop (smaller means faster " + "simulation, larger means less CPU load)"); // Parse command-line arguments po::variables_map vm; @@ -63,10 +70,16 @@ int main(int argc, char **argv) { exit(1); } + int simulation_port = 0; + if (vm.count("simulation-port")) { + simulation_port = vm["simulation-port"].as(); + } + // Create and run the WRENCH daemon WRENCHDaemon daemon(vm["simulation-logging"].as(), vm["daemon-logging"].as(), vm["port"].as(), + simulation_port, vm["allow-origin"].as(), vm["sleep-us"].as());