RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
StreamWriterForwarder.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_STREAM_WRITER_FORWARDER_HPP_
12 #define RTI_ROUTING_ADAPTER_DETAIL_STREAM_WRITER_FORWARDER_HPP_
13 
14 #include <rti/core/Exception.hpp>
15 
16 #include <rti/routing/adapter/StreamWriter.hpp>
17 #include <rti/routing/detail/UpdatableEntityForwarder.hpp>
18 #include <rti/routing/detail/ForwarderUtils.hpp>
19 
20 namespace rti { namespace routing { namespace adapter { namespace detail {
21 
22 
23 class StreamWriterForwarder {
24 public:
25 
26  static RTI_RoutingServiceStreamWriterExt* create_native(
27  Connection *connection,
28  Session *session,
29  const struct RTI_RoutingServiceStreamInfo *native_stream_info,
30  const struct RTI_RoutingServiceProperties *native_properties,
31  RTI_RoutingServiceEnvironment *environment)
32  {
33  try {
34  using rti::routing::detail::ScopedForwarder;
35 
36  StreamInfo stream_info(*native_stream_info);
37 
38  std::map<std::string, std::string> properties;
39  rti::routing::PropertyAdapter::add_properties_from_native(
40  properties,
41  native_properties);
42 
43  StreamWriterForwarder *forwarder = new StreamWriterForwarder(
44  NULL);
45  ScopedForwarder<Connection, StreamWriterForwarder> scoped(
46  connection,
47  forwarder,
48  environment);
49  forwarder->stream_writer_ = connection->create_stream_writer(
50  session,
51  stream_info,
52  properties);
53  RTI_ROUTING_THROW_ON_NULL(forwarder->stream_writer_);
54 
55  scoped.release();
56  return forwarder->native();
57 
58  } catch(const std::exception& ex) {
59  RTI_RoutingServiceEnvironment_set_error(
60  environment,
61  "%s",
62  ex.what());
63  return NULL;
64  } catch (...) {
65  RTI_RoutingServiceEnvironment_set_error(
66  environment,
67  "unexpected exception");
68  return NULL;
69  }
70 
71  }
72 
73  static void delete_native(
74  Connection *connection,
75  RTI_RoutingServiceStreamWriterExt *native_stream_writer,
76  RTI_RoutingServiceEnvironment *environment)
77  {
78  StreamWriterForwarder *stream_writer_forwarder =
79  static_cast<StreamWriterForwarder*>(
80  native_stream_writer->stream_writer_data);
81  try {
82  if (stream_writer_forwarder->stream_writer_ != NULL) {
83  connection->delete_stream_writer(
84  stream_writer_forwarder->stream_writer_);
85  stream_writer_forwarder->stream_writer_ = NULL;
86  }
87  } catch(const std::exception& ex) {
88  RTI_RoutingServiceEnvironment_set_error(
89  environment,
90  "%s",
91  ex.what());
92  } catch (...) {
93  }
94 
95  delete stream_writer_forwarder;
96  }
97 
98 
99  static int write(
100  void *native_stream_writer_data,
101  const RTI_RoutingServiceSample *sample_array,
102  const RTI_RoutingServiceSampleInfo *sample_info_array,
103  int array_length,
104  RTI_RoutingServiceEnvironment *environment)
105  {
106 
107  StreamWriterForwarder *forwarder =
108  static_cast<StreamWriterForwarder*>(native_stream_writer_data);
109 
110  try {
111  RTI_ROUTING_SAMPLE_VECTOR_COPY_FROM_NATIVE(
112  forwarder->sample_seq_,
113  sample_array,
114  array_length);
115  if (sample_info_array != NULL) {
116  RTI_ROUTING_SAMPLE_VECTOR_COPY_FROM_NATIVE(
117  forwarder->info_seq_,
118  sample_info_array,
119  array_length);
120  } else {
121  forwarder->info_seq_.clear();
122  }
123  return forwarder->stream_writer_->write(
124  forwarder->sample_seq_,
125  forwarder->info_seq_);
126  } catch (const std::exception& ex) {
127  RTI_RoutingServiceEnvironment_set_error(
128  environment,
129  "%s",
130  ex.what());
131  } catch (...) {
132  RTI_RoutingServiceEnvironment_set_error(
133  environment,
134  "unexpected exception");
135  }
136 
137  return 0;
138  }
139 
140  static void update(
141  void *native_stream_writer_data,
142  const struct RTI_RoutingServiceProperties * native_properties,
143  RTI_RoutingServiceEnvironment * environment)
144  {
145 
146  StreamWriterForwarder *stream_writer_forwarder =
147  static_cast<StreamWriterForwarder*> (native_stream_writer_data);
148 
149  rti::routing::detail::UpdatableEntityForwarder::update(
150  stream_writer_forwarder->stream_writer_,
151  native_properties,
152  environment);
153  }
154 
155  RTI_RoutingServiceStreamWriterExt* native()
156  {
157  return &this->native_;
158  }
159 
160 private:
161 
162  StreamWriterForwarder(
163  StreamWriter *stream_writer) :
164  stream_writer_(stream_writer)
165  {
166  RTIOsapiMemory_zero(&native_, sizeof(native_));
167  native_.stream_writer_data =
168  static_cast<void*>(this);
169  native_.write =
170  StreamWriterForwarder::write;
171  native_.update =
172  StreamWriterForwarder::update;
173 
174  }
175 
176  ~StreamWriterForwarder()
177  {
178  }
179 
180 private:
181  RTI_RoutingServiceStreamWriterExt native_;
182  StreamWriter *stream_writer_;
183  std::vector<StreamWriter::SamplePtr> sample_seq_;
184  std::vector<StreamWriter::InfoPtr> info_seq_;
185 
186 };
187 
188 }}}}
189 
190 #endif // RTI_ROUTING_ADAPTER_DETAIL_STREAM_WRITER_FORWARDER_HPP_

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