forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ice_waitset_trigger.cpp
252 lines (223 loc) · 8.11 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
// 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/wait_set.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include <iostream>
#include <thread>
// The two events the MyTriggerClass offers
enum class MyTriggerClassEvents
{
PERFORMED_ACTION,
ACTIVATE
};
// Triggerable class which has two events an both events 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) origin pointer. The same goes for
// the resetCallback which is used when the WaitSet goes out of scope
// and is pointing also to the old origin.
MyTriggerClass(const MyTriggerClass&) = delete;
MyTriggerClass(MyTriggerClass&&) = delete;
MyTriggerClass& operator=(const MyTriggerClass&) = delete;
MyTriggerClass& operator=(MyTriggerClass&&) = delete;
// 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();
}
uint64_t getActivationCode() const noexcept
{
return m_activationCode;
}
// 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;
}
// reset PERFORMED_ACTION and ACTIVATE event
void reset(const MyTriggerClassEvents event) noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORMED_ACTION:
m_hasPerformedAction = false;
break;
case MyTriggerClassEvents::ACTIVATE:
m_isActivated = false;
break;
}
}
static void callOnAction(MyTriggerClass* const triggerClassPtr)
{
// Ignore unused variable warning
(void)triggerClassPtr;
std::cout << "action performed" << std::endl;
}
friend iox::popo::EventAttorney;
private:
/// @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 a eventId to group multiple instances and a custom callback.
void enableEvent(iox::popo::TriggerHandle&& triggerHandle, const MyTriggerClassEvents event) noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORMED_ACTION:
m_onActionTrigger = std::move(triggerHandle);
break;
case MyTriggerClassEvents::ACTIVATE:
m_activateTrigger = std::move(triggerHandle);
break;
}
}
/// @brief Only usable by the WaitSet, not for public use
// we offer the waitset a method to invalidate trigger if it goes
// out of scope
void invalidateTrigger(const uint64_t uniqueTriggerId)
{
if (m_onActionTrigger.getUniqueId() == uniqueTriggerId)
{
m_onActionTrigger.invalidate();
}
else if (m_activateTrigger.getUniqueId() == uniqueTriggerId)
{
m_activateTrigger.invalidate();
}
}
void disableEvent(const MyTriggerClassEvents event) noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORMED_ACTION:
m_onActionTrigger.reset();
break;
case MyTriggerClassEvents::ACTIVATE:
m_activateTrigger.reset();
break;
}
}
/// @brief Only usable by the WaitSet, not for public use
iox::popo::WaitSetHasTriggeredCallback
getHasTriggeredCallbackForEvent(const MyTriggerClassEvents event) const noexcept
{
switch (event)
{
case MyTriggerClassEvents::PERFORMED_ACTION:
return {*this, &MyTriggerClass::hasPerformedAction};
case MyTriggerClassEvents::ACTIVATE:
return {*this, &MyTriggerClass::isActivated};
}
return {};
}
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::cxx::optional<iox::popo::WaitSet<>> waitset;
iox::cxx::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 events.
void eventLoop()
{
while (true)
{
auto eventVector = waitset->wait();
for (auto& event : eventVector)
{
if (event->getEventId() == ACTIVATE_ID)
{
// reset MyTriggerClass instance state
event->getOrigin<MyTriggerClass>()->reset(MyTriggerClassEvents::ACTIVATE);
// call the callback attached to the trigger
(*event)();
}
else if (event->getEventId() == ACTION_ID)
{
// reset MyTriggerClass instance state
event->getOrigin<MyTriggerClass>()->reset(MyTriggerClassEvents::PERFORMED_ACTION);
// call the callback attached to the trigger
(*event)();
}
}
}
}
int main()
{
iox::runtime::PoshRuntime::initRuntime("iox-ex-waitset-trigger");
// we create a waitset and a triggerClass instance inside of the two
// global optional's
waitset.emplace();
triggerClass.emplace();
// attach both events to a waitset and assign a callback
waitset->attachEvent(*triggerClass, MyTriggerClassEvents::ACTIVATE, ACTIVATE_ID, &callOnActivate).or_else([](auto) {
std::cerr << "failed to attach MyTriggerClass::ACTIVATE event " << std::endl;
std::terminate();
});
waitset
->attachEvent(*triggerClass, MyTriggerClassEvents::PERFORMED_ACTION, ACTION_ID, &MyTriggerClass::callOnAction)
.or_else([](auto) {
std::cerr << "failed to attach MyTriggerClass::PERFORMED_ACTION event " << std::endl;
std::terminate();
});
// start the event loop which is handling the events
std::thread eventLoopThread(eventLoop);
// start a thread which will trigger a event every second
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();
}
});
triggerThread.join();
eventLoopThread.join();
return (EXIT_SUCCESS);
}