44
55#include < iostream>
66#include < random>
7+ #include < thread>
78
89#include " silkit/SilKit.hpp"
910
@@ -17,13 +18,74 @@ std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp)
1718
1819int main (int argc, char ** argv)
1920{
20- if (argc != 2 )
21+ if (argc != 5 && argc != 6 )
2122 {
22- std::cerr << " Wrong number of arguments! Start demo with: " << argv[0 ] << " <ParticipantName>" << std::endl;
23+ std::cerr << " Wrong number of arguments! Start demo with: " << argv[0 ] <<
24+ " <ParticipantName> "
25+ " <StepSize> "
26+ " [-A (Autonomous) | -C (Coordinated)] "
27+ " [-M (ByMinimalDuration) | -D (ByOwnDuration)] "
28+ " [-R (Optional; Randomize StepSize (1ms to 10ms) with 10% probability every step)]" << std::endl;
2329 return -1 ;
2430 }
31+ // Arg 1: Participant Name
2532 std::string participantName (argv[1 ]);
2633
34+ // Arg 2: Step Size
35+ auto stepSize = std::chrono::milliseconds (std::stoi (argv[2 ]));
36+ std::cout << " Starting with stepSize=" << stepSize << std::endl;
37+
38+ // Arg 3: Operation Mode
39+ auto operationMode = SilKit::Services::Orchestration::OperationMode::Coordinated;
40+ if (std::string (argv[3 ]) == " -A" )
41+ {
42+ std::cout << " Using OperationMode::Autonomous" << std::endl;
43+ operationMode = SilKit::Services::Orchestration::OperationMode::Autonomous;
44+ }
45+ else if (std::string (argv[3 ]) == " -C" )
46+ {
47+ std::cout << " Using OperationMode::Coordinated" << std::endl;
48+ }
49+ else
50+ {
51+ std::cerr << " Unknown third argument '" << argv[3 ] << " '. Did you mean '-A' for autonomous mode or '-C' for coordinated mode?" << std::endl;
52+ return -1 ;
53+ }
54+
55+ // Arg 4: Time Advance Mode
56+ auto timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration;
57+ if (std::string (argv[4 ]) == " -M" )
58+ {
59+ std::cout << " Using TimeAdvanceMode::ByMinimalDuration" << std::endl;
60+ }
61+ else if (std::string (argv[4 ]) == " -D" )
62+ {
63+ timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByOwnDuration;
64+ std::cout << " Using TimeAdvanceMode::ByOwnDuration" << std::endl;
65+ }
66+ else
67+ {
68+ std::cerr << " Unknown argument '" << argv[4 ]
69+ << " '. Did you mean '-M' for TimeAdvanceMode::ByMinimalDuration or '-D' for TimeAdvanceMode::ByOwnDuration?" << std::endl;
70+ return -1 ;
71+ }
72+
73+ // Arg 5: Optional Randomize Step Size
74+ bool randomizeStepSize = false ;
75+ if (argc == 6 )
76+ {
77+ if (std::string (argv[5 ]) == " -R" )
78+ {
79+ randomizeStepSize = true ;
80+ std::cout << " Randomizing step size every 10 steps." << std::endl << std::endl;
81+ }
82+ else
83+ {
84+ std::cerr << " Unknown argument '" << argv[5 ] << " '. Did you mean '-R' to randomize the step size every 10 steps?" << std::endl;
85+ return -1 ;
86+ }
87+ }
88+
2789 try
2890 {
2991 // Setup participant, lifecycle, time synchronization and logging.
@@ -34,23 +96,20 @@ int main(int argc, char** argv)
3496 auto participant = SilKit::CreateParticipant (participantConfiguration, participantName, registryUri);
3597 auto logger = participant->GetLogger ();
3698
37- auto * lifecycleService =
38- participant->CreateLifecycleService ({SilKit::Services::Orchestration::OperationMode::Coordinated});
99+ auto * lifecycleService = participant->CreateLifecycleService ({operationMode});
39100
40- auto * timeSyncService = lifecycleService->CreateTimeSyncService (SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration );
101+ auto * timeSyncService = lifecycleService->CreateTimeSyncService (timeAdvanceMode );
41102
42- const auto stepSize = 10ms;
43- static int stepCounter = 0 ;
44103 std::random_device rd;
45104 std::mt19937 rng (rd ());
46105 auto bounded_rand = [&rng](unsigned range) {
47106 std::uniform_int_distribution<unsigned > dist (1 , range);
48107 return dist (rng);
49108 };
50109
51-
52110 timeSyncService->SetSimulationStepHandler (
53- [logger, timeSyncService, participantName, bounded_rand](std::chrono::nanoseconds now,
111+ [randomizeStepSize, logger, timeSyncService, participantName, bounded_rand](
112+ std::chrono::nanoseconds now,
54113 std::chrono::nanoseconds duration) {
55114 // The invocation of this handler marks the beginning of a simulation step.
56115 {
@@ -59,7 +118,7 @@ int main(int argc, char** argv)
59118 logger->Info (ss.str ());
60119 }
61120
62- if (bounded_rand (10 ) == 1 ) // && participantName == "P1" )
121+ if (randomizeStepSize && bounded_rand (10 ) == 1 )
63122 {
64123 auto rndStepDuration = bounded_rand (10 );
65124 timeSyncService->SetStepDuration (std::chrono::milliseconds (rndStepDuration));
@@ -69,12 +128,7 @@ int main(int argc, char** argv)
69128 }
70129
71130 std::this_thread::sleep_for (500ms);
72- // All messages sent here are guaranteed to arrive at other participants before their next simulation step is called.
73- // So here, we can rely on having received all messages from the past (< now).
74- // Note that this guarantee only holds for messages sent within a simulation step,
75- // not for messages send outside of this handler (e.g. directly in a reception handler).
76131
77- // Returning from the handler marks the end of a simulation step.
78132 }, stepSize);
79133
80134 auto finalStateFuture = lifecycleService->StartLifecycle ();
0 commit comments