-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTimeSyncService.hpp
More file actions
202 lines (149 loc) · 7.45 KB
/
TimeSyncService.hpp
File metadata and controls
202 lines (149 loc) · 7.45 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-FileCopyrightText: 2023 Vector Informatik GmbH
//
// SPDX-License-Identifier: MIT
#pragma once
#include "silkit/capi/Orchestration.h"
#include "silkit/participant/exception.hpp"
#include "silkit/services/orchestration/ITimeSyncService.hpp"
#include "silkit/experimental/services/orchestration/TimeSyncDatatypesExtensions.hpp"
#include <unordered_map>
namespace SilKit {
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN
namespace Impl {
namespace Services {
namespace Orchestration {
class TimeSyncService : public SilKit::Services::Orchestration::ITimeSyncService
{
public:
inline explicit TimeSyncService(SilKit_LifecycleService* lifecycleService);
inline explicit TimeSyncService(SilKit_LifecycleService* lifecycleService,
SilKit::Services::Orchestration::TimeAdvanceMode timeAdvanceMode);
inline ~TimeSyncService() override = default;
inline void SetSimulationStepHandler(SimulationStepHandler task, std::chrono::nanoseconds initialStepSize) override;
inline void SetSimulationStepHandlerAsync(SimulationStepHandler task,
std::chrono::nanoseconds initialStepSize) override;
inline void CompleteSimulationStep() override;
inline auto Now() const -> std::chrono::nanoseconds override;
inline void SetStepDuration(std::chrono::nanoseconds stepDuration) override;
public:
inline auto ExperimentalAddOtherSimulationStepsCompletedHandler(
SilKit::Experimental::Services::Orchestration::OtherSimulationStepsCompletedHandler) -> SilKit::Util::HandlerId;
inline void ExperimentalRemoveOtherSimulationStepsCompletedHandler(SilKit::Util::HandlerId handlerId);
private:
template <typename HandlerFunction>
struct HandlerData
{
HandlerFunction handler{};
};
template <typename HandlerFunction>
using HandlerDataMap = std::unordered_map<SilKit::Util::HandlerId, std::unique_ptr<HandlerData<HandlerFunction>>>;
private:
SilKit_TimeSyncService* _timeSyncService{nullptr};
std::unique_ptr<SimulationStepHandler> _simulationStepHandler;
std::unique_ptr<SimulationStepHandler> _simulationStepHandlerAsync;
HandlerDataMap<SilKit::Experimental::Services::Orchestration::OtherSimulationStepsCompletedHandler>
_otherSimulationStepsCompletedHandlers;
};
} // namespace Orchestration
} // namespace Services
} // namespace Impl
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE
} // namespace SilKit
// ================================================================================
// Inline Implementations
// ================================================================================
#include "silkit/detail/impl/ThrowOnError.hpp"
namespace SilKit {
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN
namespace Impl {
namespace Services {
namespace Orchestration {
TimeSyncService::TimeSyncService(SilKit_LifecycleService* lifecycleService)
{
const auto returnCode = SilKit_TimeSyncService_Create(&_timeSyncService, lifecycleService);
ThrowOnError(returnCode);
}
TimeSyncService::TimeSyncService(SilKit_LifecycleService* lifecycleService, SilKit::Services::Orchestration::TimeAdvanceMode timeAdvanceMode)
{
const auto returnCode = SilKit_TimeSyncService_Create_With_TimeAdvanceMode(&_timeSyncService, lifecycleService, static_cast<SilKit_TimeAdvanceMode>(timeAdvanceMode));
ThrowOnError(returnCode);
}
void TimeSyncService::SetSimulationStepHandler(SimulationStepHandler task, std::chrono::nanoseconds initialStepSize)
{
auto ownedHandlerPtr = std::make_unique<SimulationStepHandler>(std::move(task));
const auto cSimulationStepHandler = [](void* context, SilKit_TimeSyncService* timeSyncService,
SilKit_NanosecondsTime now, SilKit_NanosecondsTime duration) {
SILKIT_UNUSED_ARG(timeSyncService);
const auto handlerPtr = static_cast<SimulationStepHandler*>(context);
(*handlerPtr)(std::chrono::nanoseconds{now}, std::chrono::nanoseconds{duration});
};
const auto returnCode = SilKit_TimeSyncService_SetSimulationStepHandler(
_timeSyncService, ownedHandlerPtr.get(), cSimulationStepHandler, initialStepSize.count());
ThrowOnError(returnCode);
_simulationStepHandler = std::move(ownedHandlerPtr);
}
void TimeSyncService::SetSimulationStepHandlerAsync(SimulationStepHandler task,
std::chrono::nanoseconds initialStepSize)
{
auto ownedHandlerPtr = std::make_unique<SimulationStepHandler>(std::move(task));
const auto cSimulationStepHandler = [](void* context, SilKit_TimeSyncService* timeSyncService,
SilKit_NanosecondsTime now, SilKit_NanosecondsTime duration) {
SILKIT_UNUSED_ARG(timeSyncService);
const auto handlerPtr = static_cast<SimulationStepHandler*>(context);
(*handlerPtr)(std::chrono::nanoseconds{now}, std::chrono::nanoseconds{duration});
};
const auto returnCode = SilKit_TimeSyncService_SetSimulationStepHandlerAsync(
_timeSyncService, ownedHandlerPtr.get(), cSimulationStepHandler, initialStepSize.count());
ThrowOnError(returnCode);
_simulationStepHandlerAsync = std::move(ownedHandlerPtr);
}
void TimeSyncService::CompleteSimulationStep()
{
const auto returnCode = SilKit_TimeSyncService_CompleteSimulationStep(_timeSyncService);
ThrowOnError(returnCode);
}
auto TimeSyncService::Now() const -> std::chrono::nanoseconds
{
SilKit_NanosecondsTime nanosecondsTime;
const auto returnCode = SilKit_TimeSyncService_Now(_timeSyncService, &nanosecondsTime);
ThrowOnError(returnCode);
return std::chrono::nanoseconds{nanosecondsTime};
}
void TimeSyncService::SetStepDuration(std::chrono::nanoseconds stepDuration)
{
const auto returnCode = SilKit_TimeSyncService_SetStepDuration(_timeSyncService, stepDuration.count());
ThrowOnError(returnCode);
}
inline auto TimeSyncService::ExperimentalAddOtherSimulationStepsCompletedHandler(std::function<void()> handler)
-> SilKit::Util::HandlerId
{
const auto cHandler = [](void* context, SilKit_TimeSyncService* cTimeSyncService) {
SILKIT_UNUSED_ARG(cTimeSyncService);
const auto data = static_cast<
HandlerData<SilKit::Experimental::Services::Orchestration::OtherSimulationStepsCompletedHandler>*>(context);
data->handler();
};
SilKit_HandlerId handlerId;
auto handlerData = std::make_unique<
HandlerData<SilKit::Experimental::Services::Orchestration::OtherSimulationStepsCompletedHandler>>();
handlerData->handler = std::move(handler);
const auto returnCode = SilKit_Experimental_TimeSyncService_AddOtherSimulationStepsCompletedHandler(
_timeSyncService, handlerData.get(), cHandler, &handlerId);
ThrowOnError(returnCode);
_otherSimulationStepsCompletedHandlers.emplace(static_cast<SilKit::Util::HandlerId>(handlerId),
std::move(handlerData));
return static_cast<SilKit::Services::HandlerId>(handlerId);
}
inline void TimeSyncService::ExperimentalRemoveOtherSimulationStepsCompletedHandler(
const SilKit::Util::HandlerId cppHandlerId)
{
const auto cHandlerId = static_cast<SilKit_HandlerId>(cppHandlerId);
const auto returnCode =
SilKit_Experimental_TimeSyncService_RemoveOtherSimulationStepsCompletedHandler(_timeSyncService, cHandlerId);
ThrowOnError(returnCode);
}
} // namespace Orchestration
} // namespace Services
} // namespace Impl
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE
} // namespace SilKit