11 #ifndef RTI_SERVICE_MONITORING_MONITORABLE_HPP_
12 #define RTI_SERVICE_MONITORING_MONITORABLE_HPP_
14 #include "routingservice/routingservice_service_impl.h"
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>
22 namespace rti {
namespace routing {
namespace monitoring {
27 virtual void publish_status(
28 const dds::core::Duration& now)
30 RTIOsapiUtility_unusedParameter(now);
33 virtual void sample_status(
34 const dds::core::Duration& now)
36 RTIOsapiUtility_unusedParameter(now);
39 virtual ~Monitorable()
44 class MonitorableWrapper :
public Monitorable {
50 void native(RTI_RoutingServiceMonitorable *native_monitorable)
52 native_monitorable_ = native_monitorable;
55 RTI_RoutingServiceMonitorable * native()
57 return native_monitorable_;
61 const dds::core::Duration& now)
63 DDS_Duration_t native_now;
64 rti::core::native_conversions::to_native(native_now, now);
66 DDS_Duration_to_ntp_time(&native_now, &ntp_now);
67 native_monitorable_->publish_status(
68 native_monitorable_->monitorable_data,
73 virtual void sample_status(
74 const dds::core::Duration& now)
76 DDS_Duration_t native_now;
77 rti::core::native_conversions::to_native(native_now, now);
79 DDS_Duration_to_ntp_time(&native_now, &ntp_now);
80 native_monitorable_->sample_status(
81 native_monitorable_->monitorable_data,
87 RTI_RoutingServiceMonitorable *native_monitorable_;
90 class MonitorableForwarder {
93 typedef RTI_RoutingServiceMonitorable native;
96 Monitorable *monitorable)
98 native native_monitorable;
99 native_monitorable.publish_status =
101 native_monitorable.sample_status =
103 native_monitorable.monitorable_data =
104 static_cast<void*
>(monitorable);
106 return native_monitorable;
110 static void publish_status(
111 void *monitorable_data,
112 const struct RTINtpTime *now)
114 Monitorable *monitorable =
115 static_cast<Monitorable *
>(monitorable_data);
117 DDS_Duration_t native_duration = DDS_DURATION_ZERO;
118 DDS_Duration_from_ntp_time(&native_duration, now);
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());
127 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
131 static void sample_status(
132 void *monitorable_data,
133 const struct RTINtpTime *now)
135 Monitorable *monitorable =
136 static_cast<Monitorable *
>(monitorable_data);
138 DDS_Duration_t native_duration = DDS_DURATION_ZERO;
139 DDS_Duration_from_ntp_time(&native_duration, now);
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());
148 SERVICELog_exception(&RTI_LOG_UNEXPECTED_EXCEPTION);
155 void register_monitorable(
157 rti::routing::monitoring::Monitorable *monitorable,
158 const dds::core::Duration& sampling_period,
159 const dds::core::Duration& publication_period)
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(
171 &native_publication) == -1) {
172 RTI_THROW_SERVICE_EXCEPTION(
173 &RTI_LOG_ANY_FAILURE_s,
174 "register native monitorable");
179 void unregister_monitorable(
181 rti::routing::monitoring::Monitorable *monitorable)
183 MonitorableForwarder::native forwarder =
184 MonitorableForwarder::create(monitorable);
185 RTI_RoutingService_unregister_monitorable(router->native(), &forwarder);
189 class MonitorableAccessGuard {
195 RTI_RoutingService_begin_monitorable_access(service_->native());
199 ~MonitorableAccessGuard()
207 RTI_RoutingService_end_monitorable_access(service_->native());
221 #endif // RTI_SERVICE_MONITORING_MONITORABLE_HPP_