RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
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/service/ServiceException.hpp>
17 #include <rti/service/log/LogConfig.hpp>
18 #include <rti/routing/StreamInfo.hpp>
19 
20 namespace rti { namespace routing {
21 
24 struct 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 
44 typedef dds::core::safe_enum<EntityKind_def> EntityKind;
45 
46 
49 struct 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 
65 typedef dds::core::safe_enum<EntityStateKind_def> EntityStateKind;
66 
67 /*
68  * @brief string representation of EntityStateKind
69  */
70 inline
71 const 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 
86 inline
87 const 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 
103 inline
104 const std::string to_action_string(const rti::routing::EntityStateKind& kind)
105 {
106  return to_action_native_string(kind);
107 }
108 
109 class EntityListener {
110 public:
111 
112  class Context {
113  public:
114 
115  friend EntityListener::Context from_native(
116  const 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  private:
139  rti::routing::EntityKind entity_kind_;
140  std::string entity_name_;
141  rti::routing::EntityStateKind entity_state_kind_;
142  bool ok_;
143  };
144 
145  virtual void on_before_create(
146  const EntityListener::Context&)
147  {
148  }
149 
150  virtual void on_after_create(
151  const EntityListener::Context&)
152  {
153  }
154 
155  virtual void on_before_state_change(
156  const EntityListener::Context&)
157  {
158  }
159 
160  virtual void on_after_state_change(
161  const EntityListener::Context&)
162  {
163  }
164 
165  virtual void on_before_delete(
166  const EntityListener::Context&)
167  {
168  }
169 
170  virtual void on_after_delete(
171  const EntityListener::Context&)
172  {
173  }
174 
175  virtual void on_before_stream_event(
176  const EntityListener::Context&,
178  {
179  }
180 
181  virtual void on_after_stream_event(
182  const EntityListener::Context&,
184  {
185  }
186 
187  virtual ~EntityListener() {}
188 
189 };
190 
191 inline
192 EntityListener::Context from_native(
193  const RTI_RoutingServiceEntityListenerContext *native)
194  {
195  EntityListener::Context context;
196  context.entity_kind_ = static_cast<EntityKind::type> (
197  native->entity_kind);
198  context.entity_name_ = native->entity_name;
199  context.entity_state_kind_ = static_cast<EntityStateKind::type> (
200  native->entity_state_kind);
201  context.ok_ = native->ok ? true : false;
202 
203  return context;
204  }
205 
206 class EntityListenerForwarder {
207 public:
208 
209  typedef RTI_RoutingServiceEntityListener native;
210 
211  static native create(
212  EntityListener *listener)
213  {
214  native native_listener = RTI_RoutingServiceEntityListener_INITIALIZER;
215  native_listener.on_before_create =
216  on_before_create_forwarder;
217  native_listener.on_after_create =
218  on_after_create_forwarder;
219  native_listener.on_before_state_change =
220  on_before_state_change_forwarder;
221  native_listener.on_after_state_change =
222  on_after_state_change_forwarder;
223  native_listener.on_before_delete =
224  on_before_delete_forwarder;
225  native_listener.on_after_delete =
226  on_after_delete_forwarder;
227  native_listener.on_before_stream_event =
228  on_before_stream_event_forwarder;
229  native_listener.on_after_stream_event =
230  on_after_stream_event_forwarder;
231  native_listener.listener_data = static_cast<void*>(listener);
232 
233  return native_listener;
234  }
235 
236 private:
237  static void on_before_create_forwarder(
238  void *listener_data,
239  const RTI_RoutingServiceEntityListenerContext *context)
240  {
241  EntityListener *listener =
242  static_cast<EntityListener *>(listener_data);
243  try {
244  listener->on_before_create(rti::routing::from_native(context));
245  } catch (rti::service::ServiceException& ex) {
246  SERVICELog_fromException(ex);
247  } catch (std::exception& ex) {
248  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
249  } catch (...) {
250  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
251  }
252  }
253  static void on_after_create_forwarder(
254  void *listener_data,
255  const RTI_RoutingServiceEntityListenerContext *context)
256  {
257  EntityListener *listener =
258  static_cast<EntityListener *>(listener_data);
259  try {
260  listener->on_after_create(rti::routing::from_native(context));
261  } catch (rti::service::ServiceException& ex) {
262  SERVICELog_fromException(ex);
263  } catch (std::exception& ex) {
264  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
265  } catch (...) {
266  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
267  }
268  }
269 
270  static void on_before_state_change_forwarder(
271  void *listener_data,
272  const RTI_RoutingServiceEntityListenerContext *context)
273  {
274  EntityListener *listener =
275  static_cast<EntityListener *>(listener_data);
276  try {
277  listener->on_before_state_change(rti::routing::from_native(context));
278  } catch (rti::service::ServiceException& ex) {
279  SERVICELog_fromException(ex);
280  } catch (std::exception& ex) {
281  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
282  } catch (...) {
283  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
284  }
285  }
286 
287  static void on_after_state_change_forwarder(
288  void *listener_data,
289  const RTI_RoutingServiceEntityListenerContext *context)
290  {
291  EntityListener *listener =
292  static_cast<EntityListener *>(listener_data);
293  try {
294  listener->on_after_state_change(rti::routing::from_native(context));
295  } catch (rti::service::ServiceException& ex) {
296  SERVICELog_fromException(ex);
297  } catch (std::exception& ex) {
298  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
299  } catch (...) {
300  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
301  }
302  }
303 
304  static void on_before_delete_forwarder(
305  void *listener_data,
306  const RTI_RoutingServiceEntityListenerContext *context)
307  {
308  EntityListener *listener =
309  static_cast<EntityListener *>(listener_data);
310  try {
311  listener->on_before_delete(rti::routing::from_native(context));
312  } catch (rti::service::ServiceException& ex) {
313  SERVICELog_fromException(ex);
314  } catch (std::exception& ex) {
315  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
316  } catch (...) {
317  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
318  }
319  }
320 
321  static void on_after_delete_forwarder(
322  void *listener_data,
323  const RTI_RoutingServiceEntityListenerContext *context)
324  {
325  EntityListener *listener =
326  static_cast<EntityListener *>(listener_data);
327  try {
328  listener->on_after_delete(rti::routing::from_native(context));
329  } catch (rti::service::ServiceException& ex) {
330  SERVICELog_fromException(ex);
331  } catch (std::exception& ex) {
332  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
333  } catch (...) {
334  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
335  }
336  }
337 
338  static void on_before_stream_event_forwarder(
339  void *listener_data,
340  const RTI_RoutingServiceEntityListenerContext *context,
341  const RTI_RoutingServiceStreamInfo *stream_info)
342  {
343  EntityListener *listener =
344  static_cast<EntityListener *>(listener_data);
345  try {
346  listener->on_before_stream_event(
347  rti::routing::from_native(context),
348  *stream_info);
349  } catch (rti::service::ServiceException& ex) {
350  SERVICELog_fromException(ex);
351  } catch (std::exception& ex) {
352  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
353  } catch (...) {
354  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
355  }
356  }
357 
358  static void on_after_stream_event_forwarder(
359  void *listener_data,
360  const RTI_RoutingServiceEntityListenerContext *context,
361  const RTI_RoutingServiceStreamInfo *stream_info)
362  {
363  EntityListener *listener =
364  static_cast<EntityListener *>(listener_data);
365  try {
366  listener->on_after_stream_event(
367  rti::routing::from_native(context),
368  *stream_info);
369  } catch (rti::service::ServiceException& ex) {
370  SERVICELog_fromException(ex);
371  } catch (std::exception& ex) {
372  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
373  } catch (...) {
374  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
375  }
376  }
377 };
378 
379 }}
380 
381 
382 
383 
384 
385 #endif // RTI_ROUTING_ENTITY_LISTENER_HPP_

RTI Routing Service Version 6.0.1 Copyright © Sun Nov 17 2019 Real-Time Innovations, Inc