RTI Routing Service Version 7.7.0
RoutingServiceImpl.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_ROUTING_SERVICE_IMPL_HPP_
12#define RTI_ROUTING_ROUTING_SERVICE_IMPL_HPP_
13
14#include <dds/core/refmacros.hpp>
15#include <rti/core/detail/SelfReference.hpp>
16#include "routingservice/routingservice_adapter.h"
17#include "routingservice/routingservice_service.h"
18#include "routingservice/routingservice_log.h"
19#include "routingservice/routingservice_remote_config.h"
20#include "routingservice/ServiceAdmin.h"
21
22#include <rti/routing/ServiceProperty.hpp>
26#include <rti/routing/detail/ShutdownHookForwarder.hpp>
27#include <rti/idlgen/ServiceAdmin.hpp>
28
29namespace rti { namespace routing {
30class RoutingServiceImpl
31 : public rti::core::detail::RetainableType<RoutingServiceImpl> {
32public:
33
34 typedef RTI::Service::Admin::CommandRequest CommandRequest;
35 typedef RTI::Service::Admin::CommandReply CommandReply;
36
37 template <typename HookFunc>
38 explicit RoutingServiceImpl(
39 const ServiceProperty& property,
40 HookFunc& shutdown_hook)
41 : native_(RTI_RoutingService_new(&property.native())),
42 shutdown_hook_fwd_(
43 new rti::routing::detail::ShutdownHookForwarder<HookFunc>(
44 shutdown_hook))
45 {
46 rti::core::check_create_entity(native_, "RoutingService");
47 RTI_RoutingService_set_remote_shutdown_hook(
48 native_,
49 shutdown_hook_fwd_->native());
50 }
51
52 explicit RoutingServiceImpl(const ServiceProperty& property)
53 : native_(RTI_RoutingService_new(&property.native())),
54 shutdown_hook_fwd_(nullptr)
55 {
56 rti::core::check_create_entity(native_, "RoutingService");
57 }
58
59 RoutingServiceImpl(const RTI_RoutingServiceProperty& property)
60 : native_(RTI_RoutingService_new(&property)),
61 shutdown_hook_fwd_(NULL)
62 {
63 rti::core::check_create_entity(native_, "RoutingService");
64 }
65
66 RoutingServiceImpl(RTI_RoutingService *native)
67 : native_(native), shutdown_hook_fwd_(NULL)
68 {
69 rti::core::check_create_entity(native_, "RoutingService");
70 }
71
72 ~RoutingServiceImpl()
73 {
74 RTI_RoutingService_delete(native_);
75 delete shutdown_hook_fwd_;
76 }
77
78 void start()
79 {
80 if (!RTI_RoutingService_start(native_)) {
81 throw dds::core::Error("failed to start RoutingService");
82 }
83 }
84
85 void stop()
86 {
87 if (!RTI_RoutingService_stop(native_)) {
88 throw dds::core::Error("failed to stop RoutingService");
89 }
90 }
91
92 void attach_adapter_plugin(
94 const std::string& registered_name)
95 {
96 RTI_RoutingServiceAdapterPluginExt *native_plugin =
97 adapter::detail::AdapterForwarder::create_plugin(adapter_plugin);
98 if (!RTI_RoutingService_attach_adapter_plugin(
99 native_,
100 native_plugin,
101 registered_name.c_str())) {
102 throw dds::core::Error("failed to attach native adapter");
103 }
104 }
105
106 void attach_transformation_plugin(
107 rti::routing::transf::TransformationPlugin *transformation_plugin,
108 const std::string& registered_name)
109 {
110 RTI_RoutingServiceTransformationPlugin *native_plugin =
111 transf::detail::TransformationPluginForwarder::create_plugin(
112 transformation_plugin);
113 if (!RTI_RoutingService_attach_transformation_plugin(
114 native_,
115 native_plugin,
116 registered_name.c_str())) {
117 throw dds::core::Error("failed to attach native transformation");
118 }
119 }
120
121 void attach_processor_plugin(
123 const std::string& registered_name)
124 {
125 RTI_RoutingServiceProcessorPlugin *native_plugin =
126 processor::detail::ProcessorPluginForwarder::create_plugin(
127 processor_plugin);
128 if (!RTI_RoutingService_attach_processor_plugin(
129 native_,
130 native_plugin,
131 registered_name.c_str())) {
132 throw dds::core::Error("failed to attach native processor");
133 }
134 }
135
136 CommandReply& execute_command(
137 CommandReply& reply,
138 const CommandRequest& request)
139 {
140 RTI_Service_Admin_CommandRequest native_request =
141 RTI_Service_Admin_CommandRequest_INITIALIZER;
142 RTI_Service_Admin_CommandReply *native_reply_ptr = nullptr;
143
144 if (!RTI_Service_Admin_CommandRequest_initialize(&native_request)) {
145 RTI_Service_Admin_CommandRequest_finalize(&native_request);
146 throw dds::core::Error("failed to initialize CommandRequest");
147 }
148 std::shared_ptr<RTI_Service_Admin_CommandRequest> request_ptr(
149 &native_request,
150 [](RTI_Service_Admin_CommandRequest *native_req_ptr) {
151 RTI_Service_Admin_CommandRequest_finalize(native_req_ptr);
152 });
153
154 to_native(native_request, request);
155 RTI_RoutingService_execute_command(
156 native_,
157 &native_reply_ptr,
158 &native_request);
159 from_native(reply, *native_reply_ptr);
160 RTI_RoutingService_return_reply(native_, native_reply_ptr);
161 return reply;
162 }
163
164 CommandReply execute_command(const CommandRequest& request)
165 {
166 CommandReply reply;
167 return execute_command(reply, request);
168 }
169
170
171 RTI_RoutingService* native() const
172 {
173 return native_;
174 }
175
176private:
177
178 RTI_RoutingService *native_;
179
180 rti::routing::detail::AbstractShutdownHookForwarder *shutdown_hook_fwd_;
181
182 static RTI_Service_Admin_CommandRequest& to_native(
183 RTI_Service_Admin_CommandRequest& native,
184 const RTI::Service::Admin::CommandRequest& request)
185 {
186 native.action = static_cast<RTI_Service_Admin_CommandActionKind>(
187 request.action());
188 if (request.application_name().is_set()) {
189 if (DDS_String_replace(
190 &native.application_name,
191 request.application_name().get().c_str()) == NULL) {
192 throw dds::core::Error("failed to set application name");
193 }
194 }
195 native.instance_id = request.instance_id();
196 if (DDS_String_replace(
197 &native.resource_identifier,
198 request.resource_identifier().c_str()) == NULL) {
199 throw dds::core::Error("failed to set resource identifier");
200 }
201 if (DDS_String_replace(
202 &native.string_body,
203 request.string_body().c_str()) == NULL) {
204 throw dds::core::Error("failed to set string body");
205 }
206 if (!DDS_OctetSeq_from_array(
207 &native.octet_body,
208 (DDS_Octet *) request.octet_body().data(),
209 (DDS_Long) request.octet_body().size())) {
210 throw dds::core::Error("failed to set octet body");
211 }
212 return native;
213 }
214
215 static RTI::Service::Admin::CommandReply& from_native(
216 RTI::Service::Admin::CommandReply& reply,
217 const RTI_Service_Admin_CommandReply& native)
218 {
219 using namespace RTI::Service::Admin;
220
221 reply.retcode(static_cast<CommandReplyRetcode>(native.retcode));
222 reply.native_retcode(native.native_retcode);
223 DDS_Octet *native_octet_body = DDS_OctetSeq_get_contiguous_buffer(
224 (DDS_OctetSeq *) &native.octet_body);
225 if (DDS_OctetSeq_get_length(&native.octet_body) < 0) {
226 throw dds::core::Error("invalid octet_body length");
227 }
228 reply.octet_body().resize(
229 (size_t) DDS_OctetSeq_get_length(&native.octet_body));
230 std::copy(
231 native_octet_body,
232 native_octet_body + DDS_OctetSeq_get_length(&native.octet_body),
233 reply.octet_body().begin());
234 reply.string_body(native.string_body);
235
236 return reply;
237 }
238
239};
240
241}}
242
243#endif // RTI_ROUTINGv_ROUTING_SERVICE_IMPL_HPP_
RTI Routing Service C++ Adapter API.
RTI Routing Service C++ Processor API.
RTI Routing Service Transformation API.
The top-level plug-in class.
Definition: AdapterPlugin.hpp:34
The top-level plug-in class.
Definition: ProcessorPlugin.hpp:202
The top-level plug-in class.
Definition: TransformationPlugin.hpp:44