Skip to content

Commit

Permalink
Merge branch 'master' into wrench-fsmod
Browse files Browse the repository at this point in the history
  • Loading branch information
henricasanova committed Oct 9, 2024
2 parents 92708ca + 7a7943b commit 246cc9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 2 additions & 0 deletions tools/wrench/wrench-daemon/include/WRENCHDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
12 changes: 9 additions & 3 deletions tools/wrench/wrench-daemon/src/WRENCHDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ std::vector<std::string> 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
*/
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);
}
Expand Down Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions tools/wrench/wrench-daemon/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>()->default_value(8101)->notifier(in(1024, 49151, "port")),
"port number, between 1024 and 4951, on which this daemon will listen")("allow-origin", po::value<std::string>()->default_value(""),
"allow origin for http connections to avoid CORS errors if needed (e.g., --allow-origin http://localhost:8000)")("sleep-us", po::value<int>()->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<int>()->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<std::string>()->default_value(""),
"Allow origin for http connections to avoid CORS errors if needed (e.g., --allow-origin http://localhost:8000)")
("simulation-port", po::value<int>()->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<int>()->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;
Expand All @@ -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<int>();
}

// Create and run the WRENCH daemon
WRENCHDaemon daemon(vm["simulation-logging"].as<bool>(),
vm["daemon-logging"].as<bool>(),
vm["port"].as<int>(),
simulation_port,
vm["allow-origin"].as<std::string>(),
vm["sleep-us"].as<int>());

Expand Down

0 comments on commit 246cc9f

Please sign in to comment.