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

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