RTI Routing Service Version 7.7.0
EntityListener.hpp
1/*
2 * (c) Copyright, Real-Time Innovations, 2017.
3 * All rights reserved.
4 *
5 * No duplications, whole or partial, manual or electronic, may be made
6 * without express written permission. Any such copies, or
7 * revisions thereof, must display this notice unaltered.
8 * This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10
11#ifndef RTI_ROUTING_ENTITY_LISTENER_HPP_
12#define RTI_ROUTING_ENTITY_LISTENER_HPP_
13
14#include "routingservice/routingservice_service_impl.h"
15
16#include <rti/apputils/ServiceException.hpp>
17#include <rti/apputils/log/LogConfig.hpp>
18#include <rti/routing/StreamInfo.hpp>
19
20namespace rti { namespace routing {
21
24struct EntityKind_def {
28 enum type {
29 SERVICE = RTI_ROUTING_SERVICE_SERVICE_ENTITY,
30 DOMAIN_ROUTE = RTI_ROUTING_SERVICE_DOMAIN_ROUTE_ENTITY,
31 CONNECTION = RTI_ROUTING_SERVICE_CONNECTION_ENTITY,
32 SESSION = RTI_ROUTING_SERVICE_SESSION_ENTITY,
33 AUTO_TOPIC_ROUTE = RTI_ROUTING_SERVICE_AUTO_TOPIC_ROUTE_ENTITY,
34 TOPIC_ROUTE = RTI_ROUTING_SERVICE_TOPIC_ROUTE_ENTITY,
35 INPUT = RTI_ROUTING_SERVICE_INPUT_ENTITY,
36 OUTPUT = RTI_ROUTING_SERVICE_OUTPUT_ENTITY,
37 count_
38 };
39
40};
41
44typedef dds::core::safe_enum<EntityKind_def> EntityKind;
45
46
49struct EntityStateKind_def {
53 enum type {
54 INVALID = RTI_SERVICE_INVALID_ENTITY_STATE,
55 ENABLED = RTI_SERVICE_ENABLED_ENTITY_STATE,
56 DISABLED = RTI_SERVICE_DISABLED_ENTITY_STATE,
57 STARTED = RTI_SERVICE_STARTED_ENTITY_STATE,
58 STOPPED = RTI_SERVICE_STOPPED_ENTITY_STATE,
59 RUNNING = RTI_SERVICE_RUNNING_ENTITY_STATE,
60 PAUSED = RTI_SERVICE_PAUSED_ENTITY_STATE
61 };
62
63};
64
65typedef dds::core::safe_enum<EntityStateKind_def> EntityStateKind;
66
67/*
68 * @brief string representation of EntityStateKind
69 */
70inline
71const std::string to_string(const rti::routing::EntityStateKind& kind)
72{
73 static const char * ENTITY_STATE_KIND_AS_STRING[] = {
74 "INVALID",
75 "ENABLED",
76 "DISABLED",
77 "STARTED",
78 "STOPPED",
79 "RUNNING",
80 "PAUSED"
81 };
82
83 return ENTITY_STATE_KIND_AS_STRING[kind.underlying()];
84}
85
86inline
87const char* to_action_native_string(const rti::routing::EntityStateKind& kind)
88{
89 static const char * ENTITY_STATE_ACTION_AS_STRING[] = {
90 "INVALID",
91 "ENABLE",
92 "DISABLE",
93 "START",
94 "STOP",
95 "RUN",
96 "PAUSE"
97 ""
98 };
99
100 return ENTITY_STATE_ACTION_AS_STRING[kind.underlying()];
101}
102
103inline
104const std::string to_action_string(const rti::routing::EntityStateKind& kind)
105{
106 return to_action_native_string(kind);
107}
108
109class EntityListener {
110public:
111
112 class Context {
113 public:
114
115 friend EntityListener::Context from_native(
116 RTI_RoutingServiceEntityListenerContext*);
117
118 const rti::routing::EntityKind& entity_kind() const
119 {
120 return entity_kind_;
121 }
122
123 const std::string& entity_name() const
124 {
125 return entity_name_;
126 }
127
128 const rti::routing::EntityStateKind& entity_state_kind() const
129 {
130 return entity_state_kind_;
131 }
132
133 bool ok()
134 {
135 return ok_;
136 }
137
138 rti::advlog::Entries logging_context_entries()
139 {
140 return logging_context_entries_;
141 }
142
143 private:
144
145 rti::routing::EntityKind entity_kind_;
146
147 std::string entity_name_;
148
149 rti::routing::EntityStateKind entity_state_kind_;
150
151 bool ok_;
152
153 /*
154 * We need to keep the entries of the context because they are entered
155 * in a function and left in another function.
156 * The number of entries is two:
157 * - An entry for the resource.
158 * - An entry for the activity.
159 *
160 * Recorder:
161 * - The entries are entered into the context using the macro
162 * RTI_ENTITY_LISTENER_CONTEXT_ENTER_ON_BEFORE in a function.
163 * - The entries are left using the macro
164 * RTI_ENTITY_LISTENER_CONTEXT_LEAVE_ON_AFTER in another function.
165 *
166 * For example:
167 * - Enter in EntityListener::on_before_create
168 * - Leave in EntityListener::on_after_create
169 */
170 rti::advlog::Entries logging_context_entries_;
171 };
172
173 virtual void on_before_create( EntityListener::Context&)
174 {
175 }
176
177 virtual void on_after_create(EntityListener::Context&)
178 {
179 }
180
181 virtual void on_before_state_change(EntityListener::Context&)
182 {
183 }
184
185 virtual void on_after_state_change(EntityListener::Context&)
186 {
187 }
188
189 virtual void on_before_delete(EntityListener::Context&)
190 {
191 }
192
193 virtual void on_after_delete(EntityListener::Context&)
194 {
195 }
196
197 virtual void on_before_stream_event(
198 EntityListener::Context&,
200 {
201 }
202
203 virtual void on_after_stream_event(
204 EntityListener::Context&,
206 {
207 }
208
209 virtual ~EntityListener() {}
210
211};
212
213 inline
214 EntityListener::Context from_native(
215 RTI_RoutingServiceEntityListenerContext *native)
216 {
217 EntityListener::Context context;
218 context.entity_kind_ = static_cast<EntityKind::type> (
219 native->entity_kind);
220 context.entity_name_ = native->entity_name;
221 context.entity_state_kind_ = static_cast<EntityStateKind::type> (
222 native->entity_state_kind);
223 context.ok_ = native->ok ? true : false;
224 context.logging_context_entries_.from_native(
225 native->logging_context_entries,
226 native->logging_context_params);
227
228 return context;
229 }
230
231class EntityListenerForwarder {
232public:
233
234 typedef RTI_RoutingServiceEntityListener native;
235
236 static native create(
237 EntityListener *listener)
238 {
239 native native_listener = RTI_RoutingServiceEntityListener_INITIALIZER;
240 native_listener.on_before_create =
241 on_before_create_forwarder;
242 native_listener.on_after_create =
243 on_after_create_forwarder;
244 native_listener.on_before_state_change =
245 on_before_state_change_forwarder;
246 native_listener.on_after_state_change =
247 on_after_state_change_forwarder;
248 native_listener.on_before_delete =
249 on_before_delete_forwarder;
250 native_listener.on_after_delete =
251 on_after_delete_forwarder;
252 native_listener.on_before_stream_event =
253 on_before_stream_event_forwarder;
254 native_listener.on_after_stream_event =
255 on_after_stream_event_forwarder;
256 native_listener.listener_data = static_cast<void*>(listener);
257
258 return native_listener;
259 }
260
261private:
262 static void on_before_create_forwarder(
263 void *listener_data,
264 RTI_RoutingServiceEntityListenerContext *context_native)
265 {
266 EntityListener *listener =
267 static_cast<EntityListener *>(listener_data);
268 try {
269 EntityListener::Context context =
270 rti::routing::from_native(context_native);
271 listener->on_before_create(context);
272 } catch (rti::apputils::ServiceException& ex) {
273 SERVICELog_fromException(ex);
274 } catch (std::exception& ex) {
275 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
276 } catch (...) {
277 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
278 }
279 }
280 static void on_after_create_forwarder(
281 void *listener_data,
282 RTI_RoutingServiceEntityListenerContext *context_native)
283 {
284 EntityListener *listener =
285 static_cast<EntityListener *>(listener_data);
286 try {
287 EntityListener::Context context =
288 rti::routing::from_native(context_native);
289 listener->on_after_create(context);
290 } catch (rti::apputils::ServiceException& ex) {
291 SERVICELog_fromException(ex);
292 } catch (std::exception& ex) {
293 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
294 } catch (...) {
295 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
296 }
297 }
298
299 static void on_before_state_change_forwarder(
300 void *listener_data,
301 RTI_RoutingServiceEntityListenerContext *context_native)
302 {
303 EntityListener *listener =
304 static_cast<EntityListener *>(listener_data);
305 try {
306 EntityListener::Context context =
307 rti::routing::from_native(context_native);
308 listener->on_before_state_change(context);
309 } catch (rti::apputils::ServiceException& ex) {
310 SERVICELog_fromException(ex);
311 } catch (std::exception& ex) {
312 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
313 } catch (...) {
314 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
315 }
316 }
317
318 static void on_after_state_change_forwarder(
319 void *listener_data,
320 RTI_RoutingServiceEntityListenerContext *context_native)
321 {
322 EntityListener *listener =
323 static_cast<EntityListener *>(listener_data);
324 try {
325 EntityListener::Context context =
326 rti::routing::from_native(context_native);
327 listener->on_after_state_change(context);
328 } catch (rti::apputils::ServiceException& ex) {
329 SERVICELog_fromException(ex);
330 } catch (std::exception& ex) {
331 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
332 } catch (...) {
333 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
334 }
335 }
336
337 static void on_before_delete_forwarder(
338 void *listener_data,
339 RTI_RoutingServiceEntityListenerContext *context_native)
340 {
341 EntityListener *listener =
342 static_cast<EntityListener *>(listener_data);
343 try {
344 EntityListener::Context context =
345 rti::routing::from_native(context_native);
346 listener->on_before_delete(context);
347 } catch (rti::apputils::ServiceException& ex) {
348 SERVICELog_fromException(ex);
349 } catch (std::exception& ex) {
350 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
351 } catch (...) {
352 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
353 }
354 }
355
356 static void on_after_delete_forwarder(
357 void *listener_data,
358 RTI_RoutingServiceEntityListenerContext *context_native)
359 {
360 EntityListener *listener =
361 static_cast<EntityListener *>(listener_data);
362 try {
363 EntityListener::Context context =
364 rti::routing::from_native(context_native);
365 listener->on_after_delete(context);
366 } catch (rti::apputils::ServiceException& ex) {
367 SERVICELog_fromException(ex);
368 } catch (std::exception& ex) {
369 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
370 } catch (...) {
371 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
372 }
373 }
374
375 static void on_before_stream_event_forwarder(
376 void *listener_data,
377 RTI_RoutingServiceEntityListenerContext *context_native,
378 const RTI_RoutingServiceStreamInfo *stream_info)
379 {
380 EntityListener *listener =
381 static_cast<EntityListener *>(listener_data);
382 try {
383 EntityListener::Context context =
384 rti::routing::from_native(context_native);
385 listener->on_before_stream_event(
386 context,
387 *stream_info);
388 } catch (rti::apputils::ServiceException& ex) {
389 SERVICELog_fromException(ex);
390 } catch (std::exception& ex) {
391 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
392 } catch (...) {
393 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
394 }
395 }
396
397 static void on_after_stream_event_forwarder(
398 void *listener_data,
399 RTI_RoutingServiceEntityListenerContext *context_native,
400 const RTI_RoutingServiceStreamInfo *stream_info)
401 {
402 EntityListener *listener =
403 static_cast<EntityListener *>(listener_data);
404 try {
405 EntityListener::Context context =
406 rti::routing::from_native(context_native);
407 listener->on_after_stream_event(
408 context,
409 *stream_info);
410 } catch (rti::apputils::ServiceException& ex) {
411 SERVICELog_fromException(ex);
412 } catch (std::exception& ex) {
413 SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
414 } catch (...) {
415 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
416 }
417 }
418};
419
420}}
421
422
423
424
425
426#endif // RTI_ROUTING_ENTITY_LISTENER_HPP_
Definition of the stream information that RTI Routing Service needs to manage user data streams.
Definition: StreamInfo.hpp:106