@@ -17,13 +17,74 @@ std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp)
1717
1818int main (int argc, char ** argv)
1919{
20- if (argc != 2 )
20+ if (argc != 5 && argc != 6 )
2121 {
22- std::cerr << " Wrong number of arguments! Start demo with: " << argv[0 ] << " <ParticipantName>" << std::endl;
22+ std::cerr << " Wrong number of arguments! Start demo with: " << argv[0 ] <<
23+ " <ParticipantName> "
24+ " <StepSize> "
25+ " [-A (Autonomous) | -C (Coordinated)] "
26+ " [-M (ByMinimalDuration) | -D (ByOwnDuration)] "
27+ " [-R (Optional; Randomize StepSize (1ms to 10ms) with 10% probability every step)]" << std::endl;
2328 return -1 ;
2429 }
30+ // Arg 1: Participant Name
2531 std::string participantName (argv[1 ]);
2632
33+ // Arg 2: Step Size
34+ auto stepSize = std::chrono::milliseconds (std::stoi (argv[2 ]));
35+ std::cout << " Starting with stepSize=" << stepSize << std::endl;
36+
37+ // Arg 3: Operation Mode
38+ auto operationMode = SilKit::Services::Orchestration::OperationMode::Coordinated;
39+ if (std::string (argv[3 ]) == " -A" )
40+ {
41+ std::cout << " Using OperationMode::Autonomous" << std::endl;
42+ operationMode = SilKit::Services::Orchestration::OperationMode::Autonomous;
43+ }
44+ else if (std::string (argv[3 ]) == " -C" )
45+ {
46+ std::cout << " Using OperationMode::Coordinated" << std::endl;
47+ }
48+ else
49+ {
50+ std::cerr << " Unknown third argument '" << argv[3 ] << " '. Did you mean '-A' for autonomous mode or '-C' for coordinated mode?" << std::endl;
51+ return -1 ;
52+ }
53+
54+ // Arg 4: Time Advance Mode
55+ auto timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration;
56+ if (std::string (argv[4 ]) == " -M" )
57+ {
58+ std::cout << " Using TimeAdvanceMode::ByMinimalDuration" << std::endl;
59+ }
60+ else if (std::string (argv[4 ]) == " -D" )
61+ {
62+ timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByOwnDuration;
63+ std::cout << " Using TimeAdvanceMode::ByOwnDuration" << std::endl;
64+ }
65+ else
66+ {
67+ std::cerr << " Unknown argument '" << argv[4 ]
68+ << " '. Did you mean '-M' for TimeAdvanceMode::ByMinimalDuration or '-D' for TimeAdvanceMode::ByOwnDuration?" << std::endl;
69+ return -1 ;
70+ }
71+
72+ // Arg 5: Optional Randomize Step Size
73+ bool randomizeStepSize = false ;
74+ if (argc == 6 )
75+ {
76+ if (std::string (argv[5 ]) == " -R" )
77+ {
78+ randomizeStepSize = true ;
79+ std::cout << " Randomizing step size every 10 steps." << std::endl << std::endl;
80+ }
81+ else
82+ {
83+ std::cerr << " Unknown argument '" << argv[5 ] << " '. Did you mean '-R' to randomize the step size every 10 steps?" << std::endl;
84+ return -1 ;
85+ }
86+ }
87+
2788 try
2889 {
2990 // Setup participant, lifecycle, time synchronization and logging.
@@ -34,12 +95,10 @@ int main(int argc, char** argv)
3495 auto participant = SilKit::CreateParticipant (participantConfiguration, participantName, registryUri);
3596 auto logger = participant->GetLogger ();
3697
37- auto * lifecycleService =
38- participant->CreateLifecycleService ({SilKit::Services::Orchestration::OperationMode::Coordinated});
98+ auto * lifecycleService = participant->CreateLifecycleService ({operationMode});
3999
40- auto * timeSyncService = lifecycleService->CreateTimeSyncService (SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration );
100+ auto * timeSyncService = lifecycleService->CreateTimeSyncService (timeAdvanceMode );
41101
42- const auto stepSize = 10ms;
43102 static int stepCounter = 0 ;
44103 std::random_device rd;
45104 std::mt19937 rng (rd ());
@@ -50,7 +109,8 @@ int main(int argc, char** argv)
50109
51110
52111 timeSyncService->SetSimulationStepHandler (
53- [logger, timeSyncService, participantName, bounded_rand](std::chrono::nanoseconds now,
112+ [randomizeStepSize, logger, timeSyncService, participantName, bounded_rand](
113+ std::chrono::nanoseconds now,
54114 std::chrono::nanoseconds duration) {
55115 // The invocation of this handler marks the beginning of a simulation step.
56116 {
@@ -59,7 +119,7 @@ int main(int argc, char** argv)
59119 logger->Info (ss.str ());
60120 }
61121
62- if (bounded_rand (10 ) == 1 ) // && participantName == "P1" )
122+ if (randomizeStepSize && bounded_rand (10 ) == 1 )
63123 {
64124 auto rndStepDuration = bounded_rand (10 );
65125 timeSyncService->SetStepDuration (std::chrono::milliseconds (rndStepDuration));
@@ -69,12 +129,7 @@ int main(int argc, char** argv)
69129 }
70130
71131 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).
76132
77- // Returning from the handler marks the end of a simulation step.
78133 }, stepSize);
79134
80135 auto finalStateFuture = lifecycleService->StartLifecycle ();
0 commit comments