RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
ConnectionForwarder.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_ADAPTER_DETAIL_CONNECTION_FORWARDER_HPP_
12 #define RTI_ROUTING_ADAPTER_DETAIL_CONNECTION_FORWARDER_HPP_
13 
14 #include <rti/core/Exception.hpp>
15 
16 #include <osapi/osapi_heap.h>
17 #include <routingservice/routingservice_adapter_new.h>
18 
20 #include <rti/routing/adapter/detail/SessionForwarder.hpp>
21 #include <rti/routing/adapter/detail/StreamReaderForwarder.hpp>
22 #include <rti/routing/adapter/detail/StreamWriterForwarder.hpp>
23 #include <rti/routing/adapter/detail/DiscoveryStreamReaderForwarder.hpp>
24 #include <rti/routing/detail/UpdatableEntityForwarder.hpp>
25 #include <rti/routing/detail/ForwarderUtils.hpp>
26 
27 namespace rti { namespace routing { namespace adapter { namespace detail {
28 
29 class ConnectionForwarder {
30 public:
31  static RTI_RoutingServiceConnectionExt * create_native(
32  AdapterPlugin *adapter,
33  const char *,
34  const char *,
35  const RTI_RoutingServiceStreamReaderListenerExt *native_output_stream_listener,
36  const RTI_RoutingServiceStreamReaderListenerExt *native_input_stream_listener,
37  const RTI_RoutingServiceTypeInfo **,
38  int,
39  const RTI_RoutingServiceProperties *native_properties,
40  RTI_RoutingServiceEnvironment *environment)
41  {
42  try {
43  using rti::routing::detail::ScopedForwarder;
44 
45  // Set properties
46  std::map<std::string, std::string> properties;
47  rti::routing::PropertyAdapter::add_properties_from_native(
48  properties,
49  native_properties);
50 
51  ConnectionForwarder *forwarder = new ConnectionForwarder(
52  native_output_stream_listener,
53  native_input_stream_listener);
54  ScopedForwarder<AdapterPlugin, ConnectionForwarder> scoped(
55  adapter,
56  forwarder,
57  environment);
58  // Create connection
59  forwarder->connection_ = adapter->create_connection(
60  &forwarder->input_discovery_listener_,
61  &forwarder->output_discovery_listener_,
62  properties);
63  RTI_ROUTING_THROW_ON_NULL(forwarder->connection_);
64 
65  // Create built-in SRs
66  forwarder->input_stream_discovery_forwarder_ = new DiscoveryStreamReaderForwarder(
67  forwarder->connection_->input_stream_discovery_reader());
68 
69  forwarder->output_stream_discovery_forwarder_ = new DiscoveryStreamReaderForwarder(
70  forwarder->connection_->output_stream_discovery_reader());
71 
72  scoped.release();
73  return forwarder->native();
74  } catch (const std::exception& ex) {
75  RTI_RoutingServiceEnvironment_set_error(
76  environment,
77  "%s",
78  ex.what());
79  return NULL;
80  } catch (...) {
81  RTI_RoutingServiceEnvironment_set_error(
82  environment,
83  "unexpected exception");
84  return NULL;
85  }
86  }
87 
88  static void delete_native(
89  AdapterPlugin *adapter,
90  RTI_RoutingServiceConnectionExt *native_connection,
91  RTI_RoutingServiceEnvironment *environment)
92  {
93  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
94  native_connection->connection_data);
95  try {
96  // delete built-in SRs
97  delete forwarder->input_stream_discovery_forwarder_;
98  delete forwarder->output_stream_discovery_forwarder_;
99 
100  // delete connection
101  if (forwarder->connection_ != NULL) {
102  adapter->delete_connection(forwarder->connection_);
103  forwarder->connection_ = NULL;
104  }
105  } catch(const std::exception& ex) {
106  RTI_RoutingServiceEnvironment_set_error(
107  environment,
108  "%s",
109  ex.what());
110  } catch (...) {
111  }
112 
113  delete forwarder;
114  }
115 
116 
117  static RTI_RoutingServiceStreamReaderExt *
118  get_input_stream_discovery_reader(
119  void *native_connection_data,
120  RTI_RoutingServiceEnvironment *)
121  {
122  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
123  native_connection_data);
124  return forwarder->input_stream_discovery_forwarder_->native();
125  }
126 
127  static RTI_RoutingServiceStreamReaderExt *
128  get_output_stream_discovery_reader(
129  void *native_connection_data,
130  RTI_RoutingServiceEnvironment *)
131  {
132  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
133  native_connection_data);
134  return forwarder->output_stream_discovery_forwarder_->native();
135  }
136 
137  static RTI_RoutingServiceSessionExt * create_session(
138  void *native_connection_data,
139  const struct RTI_RoutingServiceProperties * native_properties,
140  RTI_RoutingServiceEnvironment *environment)
141  {
142 
143  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
144  native_connection_data);
145 
146  return SessionForwarder::create_native(
147  forwarder->connection_,
148  native_properties,
149  environment);
150  }
151 
152  static void delete_session(
153  void *native_connection_data,
154  RTI_RoutingServiceSessionExt *native_session,
155  RTI_RoutingServiceEnvironment *environment)
156  {
157  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*> (
158  native_connection_data);
159 
160  SessionForwarder::delete_native(
161  forwarder->connection_,
162  native_session,
163  environment);
164  }
165 
166  static RTI_RoutingServiceStreamReaderExt* create_stream_reader(
167  void *native_connection_data,
168  void *native_session_data,
169  const struct RTI_RoutingServiceStreamInfo *native_stream_info,
170  const struct RTI_RoutingServiceProperties *native_properties,
171  const struct RTI_RoutingServiceStreamReaderListenerExt *native_listener,
172  RTI_RoutingServiceEnvironment *environment)
173  {
174  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
175  native_connection_data);
176  SessionForwarder *session_forwarder = static_cast<SessionForwarder*>(
177  native_session_data);
178 
179  return StreamReaderForwarder::create_native(
180  forwarder->connection_,
181  session_forwarder->session(),
182  native_stream_info,
183  native_properties,
184  native_listener,
185  environment);
186  }
187 
188  static void delete_stream_reader(
189  void *native_connection_data,
190  RTI_RoutingServiceStreamReaderExt *native_reader,
191  RTI_RoutingServiceEnvironment *environment)
192  {
193  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
194  native_connection_data);
195  StreamReaderForwarder::delete_native(
196  forwarder->connection_,
197  native_reader,
198  environment);
199  }
200 
201  static RTI_RoutingServiceStreamWriterExt* create_stream_writer(
202  void *native_connection_data,
203  void *native_session_data,
204  const struct RTI_RoutingServiceStreamInfo *native_stream_info,
205  const struct RTI_RoutingServiceProperties *native_properties,
206  const struct RTI_RoutingServiceStreamWriterListenerExt *,
207  RTI_RoutingServiceEnvironment *environment)
208  {
209  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
210  native_connection_data);
211  SessionForwarder *session_forwarder = static_cast<SessionForwarder*>(
212  native_session_data);
213 
214  return StreamWriterForwarder::create_native(
215  forwarder->connection_,
216  session_forwarder->session(),
217  native_stream_info,
218  native_properties,
219  environment);
220  }
221 
222  static void delete_stream_writer(
223  void *native_connection_data,
224  RTI_RoutingServiceStreamWriterExt *native_writer,
225  RTI_RoutingServiceEnvironment *environment)
226  {
227  ConnectionForwarder *forwarder = static_cast<ConnectionForwarder*>(
228  native_connection_data);
229  StreamWriterForwarder::delete_native(
230  forwarder->connection_,
231  native_writer,
232  environment);
233  }
234 
235  static void update(
236  void *native_connection_data,
237  const struct RTI_RoutingServiceProperties *native_properties,
238  RTI_RoutingServiceEnvironment *environment)
239  {
240  ConnectionForwarder *forwarder =
241  static_cast<ConnectionForwarder*> (native_connection_data);
242 
243  rti::routing::detail::UpdatableEntityForwarder::update(
244  forwarder->connection_,
245  native_properties,
246  environment);
247  }
248 
249 
250  Connection * connection()
251  {
252  return connection_;
253  }
254 
255  RTI_RoutingServiceConnectionExt* native()
256  {
257  return &this->native_;
258  }
259 
260  RTI_RoutingServiceStreamReaderExt* native_input_discovery_reader()
261  {
262  return input_stream_discovery_forwarder_->native();
263  }
264 
265  RTI_RoutingServiceStreamReaderExt* native_output_discovery_reader()
266  {
267  return output_stream_discovery_forwarder_->native();
268  }
269 
270 private:
271 
272  ConnectionForwarder(
273  const RTI_RoutingServiceStreamReaderListenerExt *native_output_stream_listener,
274  const RTI_RoutingServiceStreamReaderListenerExt *native_input_stream_listener) :
275  connection_(NULL),
276  output_stream_discovery_forwarder_(NULL),
277  output_discovery_listener_(native_output_stream_listener),
278  input_stream_discovery_forwarder_(NULL),
279  input_discovery_listener_(native_input_stream_listener)
280  {
281  RTIOsapiMemory_zero(&native_, sizeof(native_));
282  native_.connection_data =
283  static_cast<void*>(this);
284  native_.create_session =
285  ConnectionForwarder::create_session;
286  native_.delete_session =
287  ConnectionForwarder::delete_session;
288  native_.create_stream_reader =
289  ConnectionForwarder::create_stream_reader;
290  native_.delete_stream_reader =
291  ConnectionForwarder::delete_stream_reader;
292  native_.create_stream_writer =
293  ConnectionForwarder::create_stream_writer;
294  native_.delete_stream_writer =
295  ConnectionForwarder::delete_stream_writer;
296  native_.get_input_stream_discovery_reader =
297  ConnectionForwarder::get_input_stream_discovery_reader;
298  native_.get_output_stream_discovery_reader =
299  ConnectionForwarder::get_output_stream_discovery_reader;
300  native_.update =
301  ConnectionForwarder::update;
302  }
303 
304  ~ConnectionForwarder()
305  {
306  }
307 
308 private:
309  RTI_RoutingServiceConnectionExt native_;
310  Connection *connection_;
311  DiscoveryStreamReaderForwarder *output_stream_discovery_forwarder_;
312  StreamReaderListener output_discovery_listener_;
313  DiscoveryStreamReaderForwarder *input_stream_discovery_forwarder_;
314  StreamReaderListener input_discovery_listener_;
315 
316 };
317 
318 }}}}
319 
320 #endif // RTI_ROUTING_ADAPTER_DETAIL_CONNECTION_FORWARDER_HPP_

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