diff --git a/Demos/communication/include/ApplicationBase.hpp b/Demos/communication/include/ApplicationBase.hpp index 545f04db1..ac6de2a8b 100644 --- a/Demos/communication/include/ApplicationBase.hpp +++ b/Demos/communication/include/ApplicationBase.hpp @@ -35,6 +35,7 @@ struct Arguments bool runAutonomous{false}; bool runAsync{false}; std::chrono::nanoseconds duration = 1ms; + std::chrono::nanoseconds sleep = 1000ms; bool asFastAsPossible{false}; }; std::shared_ptr _participantConfiguration{nullptr}; @@ -89,7 +90,8 @@ class ApplicationBase Async, Autonomous, Duration, - AsFastAsPossible + AsFastAsPossible, + Sleep }; private: @@ -98,14 +100,17 @@ class ApplicationBase // Names of the default command line arguments. // Note that the 'short name' (e.g. -n) and description are defined at runtime. - const std::unordered_map defaultArgName = {{DefaultArg::Name, "name"}, - {DefaultArg::Uri, "registry-uri"}, - {DefaultArg::Log, "log"}, - {DefaultArg::Config, "config"}, - {DefaultArg::Async, "async"}, - {DefaultArg::Autonomous, "autonomous"}, - {DefaultArg::Duration, "sim-step-duration"}, - {DefaultArg::AsFastAsPossible, "fast"}}; + const std::unordered_map defaultArgName = { + {DefaultArg::Name, "name"}, + {DefaultArg::Uri, "registry-uri"}, + {DefaultArg::Log, "log"}, + {DefaultArg::Config, "config"}, + {DefaultArg::Async, "async"}, + {DefaultArg::Autonomous, "autonomous"}, + {DefaultArg::Duration, "sim-step-duration"}, + {DefaultArg::AsFastAsPossible, "fast"}, + {DefaultArg::Sleep, "sleep"}, + }; Arguments _arguments; // SIL Kit API @@ -113,6 +118,7 @@ class ApplicationBase ILifecycleService* _lifecycleService{nullptr}; ITimeSyncService* _timeSyncService{nullptr}; ISystemMonitor* _systemMonitor{nullptr}; + bool _sleepingEnabled = false; // For sync: wait for sil-kit-system-controller start/abort or manual user abort enum struct SystemControllerResult @@ -217,7 +223,7 @@ class ApplicationBase if (!excludedCommandLineArgs.count(DefaultArg::Duration)) { - auto defaultValue = std::to_string(defaultArgs.duration.count() / 1000000); + auto defaultValue = std::to_string(defaultArgs.duration.count() / 1000); _commandLineParser->Add( defaultArgName.at(DefaultArg::Duration), "d", defaultValue, "-d, --" + defaultArgName.at(DefaultArg::Duration) + " ", @@ -237,6 +243,18 @@ class ApplicationBase "By default, the execution is slowed down to two work cycles per second.", "Cannot be used together with '--" + defaultArgName.at(DefaultArg::Config) + "'."}); } + + if (!excludedCommandLineArgs.count(DefaultArg::Sleep)) + { + auto defaultValue = std::to_string(defaultArgs.sleep.count() / 1000000); + _commandLineParser->Add( + defaultArgName.at(DefaultArg::Sleep), "s", defaultValue, + "-s, --" + defaultArgName.at(DefaultArg::Sleep) + " ", + std::vector{ + "The sleep duration per work cycle in milliseconds.", "Default is no sleeping.", + "Using this options overrides the default execution slow down.", + "Cannot be used together with '--" + defaultArgName.at(DefaultArg::AsFastAsPossible) + "'."}); + } } auto ToLowerCase(std::string s) -> std::string @@ -320,6 +338,21 @@ class ApplicationBase } } + bool hasSleepOption = false; + if (!excludedCommandLineArgs.count(DefaultArg::Sleep)) + { + hasSleepOption = + _commandLineParser->Get(defaultArgName.at(DefaultArg::Sleep)).HasValue(); + if (hasSleepOption) + { + int sleepDurationMs = std::stoi( + _commandLineParser->Get(defaultArgName.at(DefaultArg::Sleep)) + .Value()); + _arguments.sleep = std::chrono::milliseconds(sleepDurationMs); + _sleepingEnabled = true; + } + } + if (hasAsyncFlag && hasDurationOption) { std::cerr << "Error: Options '--" << defaultArgName.at(DefaultArg::Async) << "' and '--" @@ -336,6 +369,14 @@ class ApplicationBase .Value(); } + if (hasAsFastAsPossibleFlag && hasSleepOption) + { + std::cerr << "Error: Options '--" << defaultArgName.at(DefaultArg::AsFastAsPossible) << "' and '--" + << defaultArgName.at(DefaultArg::Sleep) << "' cannot be used simultaneously" << std::endl; + _commandLineParser->PrintUsageInfo(std::cerr); + exit(-1); + } + bool hasLogOption = false; if (!excludedCommandLineArgs.count(DefaultArg::Log)) { @@ -408,7 +449,7 @@ class ApplicationBase ss << "{"; ss << R"("Logging":{"Sinks":[{"Type":"Stdout","Level":")" << configLogLevel << R"("}]})"; - if (!_arguments.runAsync && !_arguments.asFastAsPossible) + if (!_arguments.runAsync && !_arguments.asFastAsPossible && !_sleepingEnabled) { // For async: sleep 0.5s per cycle // For sync: set the animation factor to 0.5/duration(s) here, resulting in two simulation step per second @@ -457,12 +498,17 @@ class ApplicationBase { DoWorkAsync(); - auto wait = _arguments.asFastAsPossible ? 0ms : 500ms; + auto wait = _arguments.asFastAsPossible || _sleepingEnabled ? 0ms : 500ms; auto futureStatus = _stopWorkFuture.wait_for(wait); if (futureStatus == std::future_status::ready) { break; } + + if (_sleepingEnabled) + { + std::this_thread::sleep_for(_arguments.sleep); + } } } @@ -545,6 +591,10 @@ class ApplicationBase _participant->GetLogger()->Info(ss.str()); DoWorkSync(now); + if (_sleepingEnabled) + { + std::this_thread::sleep_for(_arguments.sleep); + } }, _arguments.duration); } diff --git a/Demos/configs/FileLog_Trace_FromRemotes.silkit.yaml b/Demos/configs/FileLog_Trace_FromRemotes.silkit.yaml deleted file mode 100644 index c0301dad7..000000000 --- a/Demos/configs/FileLog_Trace_FromRemotes.silkit.yaml +++ /dev/null @@ -1,7 +0,0 @@ -Description: Log to file with Level Trace from participants that use a Sink of Type Remote -Logging: - LogFromRemotes: True - Sinks: - - Level: Trace - Type: File - LogName: TraceLogFromRemotes diff --git a/Demos/configs/Trace_ToRemote.silkit.yaml b/Demos/configs/Remote_Info.silkit.yaml similarity index 100% rename from Demos/configs/Trace_ToRemote.silkit.yaml rename to Demos/configs/Remote_Info.silkit.yaml diff --git a/Demos/configs/Stdout_Info_FromRemotes.silkit.yaml b/Demos/configs/Stdout_Info_FromRemotes.silkit.yaml new file mode 100644 index 000000000..827dd5a38 --- /dev/null +++ b/Demos/configs/Stdout_Info_FromRemotes.silkit.yaml @@ -0,0 +1,6 @@ +Description: Log to Stdout with Level Info from participants that use a Sink of Type Remote +Logging: + LogFromRemotes: True + Sinks: + - Level: Info + Type: Stdout diff --git a/docs/demos/communication.rst b/docs/demos/communication.rst index 1efadeabf..1ec5a87f9 100644 --- a/docs/demos/communication.rst +++ b/docs/demos/communication.rst @@ -42,6 +42,10 @@ The following arguments are available for all demos described in this chapter: -f, --fast | Run the simulation as fast as possible. By default, the execution is slowed down to two work cycles per second. Cannot be used together with '--config'. + -s, --sleep | The sleep duration per work cycle in milliseconds. + Default is no sleeping. + Using this options overrides the default execution slow down. + Cannot be used together with '--fast'. The default behavior of these options is: