forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ice_waitset_trigger.cpp
341 lines (306 loc) · 10.9 KB
/
ice_waitset_trigger.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#include "iceoryx_posh/popo/enum_trigger_type.hpp"
#include "iceoryx_posh/popo/wait_set.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iox/optional.hpp"
#include <iostream>
#include <thread>
std::atomic_bool keepRunning{true};
// The two states and events the MyTriggerClass offers
//! [state enum]
enum class MyTriggerClassStates : iox::popo::StateEnumIdentifier
{
HAS_PERFORMED_ACTION,
IS_ACTIVATED
};
//! [state enum]
//! [event enum]
enum class MyTriggerClassEvents : iox::popo::EventEnumIdentifier
{
PERFORM_ACTION_CALLED,
ACTIVATE_CALLED
};
//! [event enum]
// Triggerable class which has two states and events which can be
// attached to a WaitSet.
class MyTriggerClass
{
public:
MyTriggerClass() = default;
~MyTriggerClass() = default;
// IMPORTANT: For now the WaitSet does not support that the origin is moved
// or copied. To support that we have to inform the waitset about
// our new origin, otherwise the WaitSet would end up in the wrong
// memory location when it calls the 'hasTriggerCallback' with the
// old origin (already moved) pointer. The same applies to
// the resetCallback which is used when the WaitSet goes out of scope
// and is pointing also to the old origin.
//! [no move and copy]
MyTriggerClass(const MyTriggerClass&) = delete;
MyTriggerClass(MyTriggerClass&&) = delete;
MyTriggerClass& operator=(const MyTriggerClass&) = delete;
MyTriggerClass& operator=(MyTriggerClass&&) = delete;
//! [no move and copy]
//! [activate and performAction]
// When you call this method you will trigger the ACTIVATE event
void activate(const uint64_t activationCode) noexcept
{
m_activationCode = activationCode;
m_isActivated = true;
m_activateTrigger.trigger();
}
// Calling this method will trigger the PERFORMED_ACTION event
void performAction() noexcept
{
m_hasPerformedAction = true;
m_onActionTrigger.trigger();
}
//! [activate and performAction]
uint64_t getActivationCode() const noexcept
{
return m_activationCode;
}
//! [state checks]
// required by the m_onActionTrigger to ask the class if it was triggered
bool hasPerformedAction() const noexcept
{
return m_hasPerformedAction;
}
// required by the m_activateTrigger to ask the class if it was triggered
bool isActivated() const noexcept
{
return m_isActivated;
}
//! [state checks]
// reset PERFORMED_ACTION and ACTIVATE event
void reset(const MyTriggerClassStates event) noexcept
{
switch (event)
{
case MyTriggerClassStates::HAS_PERFORMED_ACTION:
m_hasPerformedAction = false;
break;
case MyTriggerClassStates::IS_ACTIVATED:
m_isActivated = false;
break;
}
}
static void callOnAction(MyTriggerClass* const triggerClassPtr)
{
// Ignore unused variable warning
(void)triggerClassPtr;
std::cout << "action performed" << std::endl;
}
//! [attorney]
friend iox::popo::NotificationAttorney;
//! [attorney]
private:
/// @brief Only usable by the WaitSet, not for public use
// This method attaches a state of the class to a waitset.
// The state is choosen by the state parameter. Additionally, you can
// set an eventId to group multiple instances and a custom callback.
//! [enableState]
void enableState(iox::popo::TriggerHandle&& triggerHandle, const MyTriggerClassStates state) noexcept
{
switch (state)
{
case MyTriggerClassStates::HAS_PERFORMED_ACTION:
m_onActionTrigger = std::move(triggerHandle);
break;
case MyTriggerClassStates::IS_ACTIVATED:
m_activateTrigger = std::move(triggerHandle);
break;
}
}
//! [enableState]
/// @brief Only usable by the WaitSet, not for public use
// This method attaches an event of the class to a waitset.
// The event is choosen by the event parameter. Additionally, you can
// set an eventId to group multiple instances and a custom callback.
//! [enableEvent]
void enableEvent(iox::popo::TriggerHandle&& triggerHandle, const MyTriggerClassEvents event) noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORM_ACTION_CALLED:
m_onActionTrigger = std::move(triggerHandle);
break;
case MyTriggerClassEvents::ACTIVATE_CALLED:
m_activateTrigger = std::move(triggerHandle);
break;
}
}
//! [enableEvent]
/// @brief Only usable by the WaitSet, not for public use
// we offer the waitset a method to invalidate a trigger if it goes
// out of scope
//! [invalidateTrigger]
void invalidateTrigger(const uint64_t uniqueTriggerId)
{
if (m_onActionTrigger.getUniqueId() == uniqueTriggerId)
{
m_onActionTrigger.invalidate();
}
else if (m_activateTrigger.getUniqueId() == uniqueTriggerId)
{
m_activateTrigger.invalidate();
}
}
//! [invalidateTrigger]
//! [disableState]
void disableState(const MyTriggerClassStates state) noexcept
{
switch (state)
{
case MyTriggerClassStates::HAS_PERFORMED_ACTION:
m_onActionTrigger.reset();
break;
case MyTriggerClassStates::IS_ACTIVATED:
m_activateTrigger.reset();
break;
}
}
//! [disableState]
//! [disableEvent]
void disableEvent(const MyTriggerClassEvents event) noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORM_ACTION_CALLED:
m_onActionTrigger.reset();
break;
case MyTriggerClassEvents::ACTIVATE_CALLED:
m_activateTrigger.reset();
break;
}
}
//! [disableEvent]
/// @brief Only usable by the WaitSet, not for public use
//! [condition satisfied]
iox::popo::WaitSetIsConditionSatisfiedCallback
getCallbackForIsStateConditionSatisfied(const MyTriggerClassStates event) const noexcept
{
switch (event)
{
case MyTriggerClassStates::HAS_PERFORMED_ACTION:
return iox::popo::WaitSetIsConditionSatisfiedCallback(
iox::in_place, *this, &MyTriggerClass::hasPerformedAction);
case MyTriggerClassStates::IS_ACTIVATED:
return iox::popo::WaitSetIsConditionSatisfiedCallback(iox::in_place, *this, &MyTriggerClass::isActivated);
}
return iox::nullopt;
}
//! [condition satisfied]
private:
uint64_t m_activationCode = 0U;
bool m_hasPerformedAction = false;
bool m_isActivated = false;
iox::popo::TriggerHandle m_onActionTrigger;
iox::popo::TriggerHandle m_activateTrigger;
};
iox::optional<iox::popo::WaitSet<>> waitset;
iox::optional<MyTriggerClass> triggerClass;
constexpr uint64_t ACTIVATE_ID = 0U;
constexpr uint64_t ACTION_ID = 1U;
void callOnActivate(MyTriggerClass* const triggerClassPtr)
{
std::cout << "activated with code: " << triggerClassPtr->getActivationCode() << std::endl;
}
// The global event loop. It will create an infinite loop and
// will work on the incoming notifications.
//! [event loop]
void eventLoop()
{
while (keepRunning)
{
auto notificationVector = waitset->wait();
for (auto& notification : notificationVector)
{
if (notification->getNotificationId() == ACTIVATE_ID)
{
// reset MyTriggerClass instance state
notification->getOrigin<MyTriggerClass>()->reset(MyTriggerClassStates::IS_ACTIVATED);
// call the callback attached to the trigger
(*notification)();
}
else if (notification->getNotificationId() == ACTION_ID)
{
// reset is not required since we attached an notification here. we will be notified once
(*notification)();
}
}
}
}
//! [event loop]
int main()
{
iox::runtime::PoshRuntime::initRuntime("iox-cpp-waitset-trigger");
// we create a waitset and a triggerClass instance inside of the two
// global optionals
//! [create]
waitset.emplace();
triggerClass.emplace();
//! [create]
//! [attach]
// attach the IS_ACTIVATED state to the waitset and assign a callback
waitset
->attachState(*triggerClass,
MyTriggerClassStates::IS_ACTIVATED,
ACTIVATE_ID,
iox::popo::createNotificationCallback(callOnActivate))
.or_else([](auto) {
std::cerr << "failed to attach MyTriggerClassStates::IS_ACTIVATED state " << std::endl;
std::exit(EXIT_FAILURE);
});
// attach the PERFORM_ACTION_CALLED event to the waitset and assign a callback
waitset
->attachEvent(*triggerClass,
MyTriggerClassEvents::PERFORM_ACTION_CALLED,
ACTION_ID,
iox::popo::createNotificationCallback(MyTriggerClass::callOnAction))
.or_else([](auto) {
std::cerr << "failed to attach MyTriggerClassEvents::PERFORM_ACTION_CALLED event " << std::endl;
std::exit(EXIT_FAILURE);
});
//! [attach]
// start the event loop which is handling the events
//! [start event loop]
std::thread eventLoopThread(eventLoop);
//! [start event loop]
// start a thread which will trigger an event every second
//! [start trigger]
std::thread triggerThread([&] {
uint64_t activationCode = 1U;
for (auto i = 0U; i < 10; ++i)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
triggerClass->activate(activationCode++);
std::this_thread::sleep_for(std::chrono::seconds(1));
triggerClass->performAction();
}
std::cout << "Sending final trigger" << std::endl;
keepRunning = false;
triggerClass->activate(activationCode++);
triggerClass->performAction();
});
//! [start trigger]
triggerThread.join();
eventLoopThread.join();
triggerClass.reset();
waitset.reset();
return (EXIT_SUCCESS);
}