RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
Monitorable.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_SERVICE_MONITORING_MONITORABLE_HPP_
12 #define RTI_SERVICE_MONITORING_MONITORABLE_HPP_
13 
14 #include "routingservice/routingservice_service_impl.h"
15 
16 #include <dds/core/Duration.hpp>
17 #include <rti/core/detail/NativeConversion.hpp>
18 #include <rti/service/ServiceException.hpp>
19 #include <rti/service/log/LogConfig.hpp>
20 #include <rti/routing/RoutingService.hpp>
21 
22 namespace rti { namespace routing { namespace monitoring {
23 
24 class Monitorable {
25 public:
26 
27  virtual void publish_status(
28  const dds::core::Duration& now)
29  {
30  RTIOsapiUtility_unusedParameter(now);
31  }
32 
33  virtual void sample_status(
34  const dds::core::Duration& now)
35  {
36  RTIOsapiUtility_unusedParameter(now);
37  }
38 
39  virtual ~Monitorable()
40  {
41  }
42 };
43 
44 class MonitorableWrapper : public Monitorable {
45 public:
46  MonitorableWrapper()
47  {
48  }
49 
50  void native(RTI_RoutingServiceMonitorable *native_monitorable)
51  {
52  native_monitorable_ = native_monitorable;
53  }
54 
55  RTI_RoutingServiceMonitorable * native()
56  {
57  return native_monitorable_;
58  }
59 
60  void publish_status(
61  const dds::core::Duration& now)
62  {
63  DDS_Duration_t native_now;
64  rti::core::native_conversions::to_native(native_now, now);
65  RTINtpTime ntp_now;
66  DDS_Duration_to_ntp_time(&native_now, &ntp_now);
67  native_monitorable_->publish_status(
68  native_monitorable_->monitorable_data,
69  &ntp_now);
70 
71  }
72 
73  virtual void sample_status(
74  const dds::core::Duration& now)
75  {
76  DDS_Duration_t native_now;
77  rti::core::native_conversions::to_native(native_now, now);
78  RTINtpTime ntp_now;
79  DDS_Duration_to_ntp_time(&native_now, &ntp_now);
80  native_monitorable_->sample_status(
81  native_monitorable_->monitorable_data,
82  &ntp_now);
83  }
84 
85 
86 private:
87  RTI_RoutingServiceMonitorable *native_monitorable_;
88 };
89 
90 class MonitorableForwarder {
91 public:
92 
93  typedef RTI_RoutingServiceMonitorable native;
94 
95  static native create(
96  Monitorable *monitorable)
97  {
98  native native_monitorable;
99  native_monitorable.publish_status =
100  publish_status;
101  native_monitorable.sample_status =
102  sample_status;
103  native_monitorable.monitorable_data =
104  static_cast<void*>(monitorable);
105 
106  return native_monitorable;
107  }
108 
109 private:
110  static void publish_status(
111  void *monitorable_data,
112  const struct RTINtpTime *now)
113  {
114  Monitorable *monitorable =
115  static_cast<Monitorable *>(monitorable_data);
116  try {
117  DDS_Duration_t native_duration = DDS_DURATION_ZERO;
118  DDS_Duration_from_ntp_time(&native_duration, now);
119 
120  monitorable->publish_status(
121  rti::core::native_conversions::from_native(native_duration));
122  } catch (rti::service::ServiceException& ex) {
123  SERVICELog_fromException(ex);
124  } catch (std::exception& ex) {
125  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
126  } catch (...) {
127  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
128  }
129  }
130 
131  static void sample_status(
132  void *monitorable_data,
133  const struct RTINtpTime *now)
134  {
135  Monitorable *monitorable =
136  static_cast<Monitorable *>(monitorable_data);
137  try {
138  DDS_Duration_t native_duration = DDS_DURATION_ZERO;
139  DDS_Duration_from_ntp_time(&native_duration, now);
140 
141  monitorable->sample_status(
142  rti::core::native_conversions::from_native(native_duration));
143  } catch (rti::service::ServiceException& ex) {
144  SERVICELog_fromException(ex);
145  } catch (std::exception& ex) {
146  SERVICELog_exception(&RTI_LOG_ANY_s, ex.what());
147  } catch (...) {
148  SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
149  }
150  }
151 
152 };
153 
154 inline
155 void register_monitorable(
157  rti::routing::monitoring::Monitorable *monitorable,
158  const dds::core::Duration& sampling_period,
159  const dds::core::Duration& publication_period)
160 {
161  MonitorableForwarder::native forwarder =
162  MonitorableForwarder::create(monitorable);
163  DDS_Duration_t native_sampling;
164  rti::core::native_conversions::to_native(native_sampling, sampling_period);
165  DDS_Duration_t native_publication;
166  rti::core::native_conversions::to_native(native_publication, publication_period);
167  if (RTI_RoutingService_register_monitorable(
168  router->native(),
169  &forwarder,
170  &native_sampling,
171  &native_publication) == -1) {
172  RTI_THROW_SERVICE_EXCEPTION(
173  &RTI_LOG_ANY_FAILURE_s,
174  "register native monitorable");
175  }
176 }
177 
178 inline
179 void unregister_monitorable(
181  rti::routing::monitoring::Monitorable *monitorable)
182 {
183  MonitorableForwarder::native forwarder =
184  MonitorableForwarder::create(monitorable);
185  RTI_RoutingService_unregister_monitorable(router->native(), &forwarder);
186 }
187 
188 
189 class MonitorableAccessGuard {
190 public:
191  MonitorableAccessGuard(rti::routing::RoutingService& service)
192  : service_(service),
193  is_taken_(false)
194  {
195  RTI_RoutingService_begin_monitorable_access(service_->native());
196  is_taken_ = true;
197  }
198 
199  ~MonitorableAccessGuard()
200  {
201  end();
202  }
203 
204  void end()
205  {
206  if (is_taken_) {
207  RTI_RoutingService_end_monitorable_access(service_->native());
208  is_taken_ = false;
209  }
210  }
211 
212 private:
214  bool is_taken_;
215 };
216 
217 
218 }}}
219 
220 
221 #endif // RTI_SERVICE_MONITORING_MONITORABLE_HPP_

RTI Routing Service Version 6.0.0 Copyright © Sun Mar 3 2019 Real-Time Innovations, Inc