-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathSimStep.cpp
More file actions
71 lines (56 loc) · 2.62 KB
/
SimStep.cpp
File metadata and controls
71 lines (56 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH
//
// SPDX-License-Identifier: MIT
#include <iostream>
#include "silkit/SilKit.hpp"
using namespace std::chrono_literals;
std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp)
{
out << std::chrono::duration_cast<std::chrono::milliseconds>(timestamp).count() << "ms";
return out;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl;
return -1;
}
std::string participantName(argv[1]);
try
{
// Setup participant, lifecycle, time synchronization and logging.
const std::string registryUri = "silkit://localhost:8500";
const std::string configString = R"({"Logging":{"Sinks":[{"Type":"Stdout","Level":"Info"}]}})";
auto participantConfiguration = SilKit::Config::ParticipantConfigurationFromString(configString);
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri);
auto logger = participant->GetLogger();
auto* lifecycleService =
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated});
auto* timeSyncService = lifecycleService->CreateTimeSyncService();
const auto stepSize = 2ms;
timeSyncService->SetSimulationStepHandler(
[logger](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) {
// The invocation of this handler marks the beginning of a simulation step.
{
std::stringstream ss;
ss << "--------- Simulation step T=" << now << ", duration=" << duration << " ---------";
logger->Info(ss.str());
}
std::this_thread::sleep_for(500ms);
// All messages sent here are guaranteed to arrive at other participants before their next simulation step is called.
// So here, we can rely on having received all messages from the past (< now).
// Note that this guarantee only holds for messages sent within a simulation step,
// not for messages send outside of this handler (e.g. directly in a reception handler).
// Returning from the handler marks the end of a simulation step.
}, stepSize);
auto finalStateFuture = lifecycleService->StartLifecycle();
finalStateFuture.get();
}
catch (const std::exception& error)
{
std::cerr << "Something went wrong: " << error.what() << std::endl;
return -2;
}
return 0;
}