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

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