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