Skip to content

Commit

Permalink
demos: Add --sleep command line option (#170)
Browse files Browse the repository at this point in the history
* demos: Add --sleep command line option

* configs: Remote Logging configs with info level
  • Loading branch information
KonradBkd authored Jan 24, 2025
1 parent 380693a commit 0c329aa
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
74 changes: 62 additions & 12 deletions Demos/communication/include/ApplicationBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SilKit::Config::IParticipantConfiguration> _participantConfiguration{nullptr};
Expand Down Expand Up @@ -89,7 +90,8 @@ class ApplicationBase
Async,
Autonomous,
Duration,
AsFastAsPossible
AsFastAsPossible,
Sleep
};

private:
Expand All @@ -98,21 +100,25 @@ 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<DefaultArg, std::string> 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<DefaultArg, std::string> 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
std::unique_ptr<IParticipant> _participant;
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
Expand Down Expand Up @@ -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<CommandlineParser::Option>(
defaultArgName.at(DefaultArg::Duration), "d", defaultValue,
"-d, --" + defaultArgName.at(DefaultArg::Duration) + " <us>",
Expand All @@ -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<CommandlineParser::Option>(
defaultArgName.at(DefaultArg::Sleep), "s", defaultValue,
"-s, --" + defaultArgName.at(DefaultArg::Sleep) + " <ms>",
std::vector<std::string>{
"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
Expand Down Expand Up @@ -320,6 +338,21 @@ class ApplicationBase
}
}

bool hasSleepOption = false;
if (!excludedCommandLineArgs.count(DefaultArg::Sleep))
{
hasSleepOption =
_commandLineParser->Get<CommandlineParser::Option>(defaultArgName.at(DefaultArg::Sleep)).HasValue();
if (hasSleepOption)
{
int sleepDurationMs = std::stoi(
_commandLineParser->Get<CommandlineParser::Option>(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 '--"
Expand All @@ -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))
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -545,6 +591,10 @@ class ApplicationBase
_participant->GetLogger()->Info(ss.str());

DoWorkSync(now);
if (_sleepingEnabled)
{
std::this_thread::sleep_for(_arguments.sleep);
}
}, _arguments.duration);
}

Expand Down
7 changes: 0 additions & 7 deletions Demos/configs/FileLog_Trace_FromRemotes.silkit.yaml

This file was deleted.

File renamed without changes.
6 changes: 6 additions & 0 deletions Demos/configs/Stdout_Info_FromRemotes.silkit.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions docs/demos/communication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ms> | 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:

Expand Down

0 comments on commit 0c329aa

Please sign in to comment.