rmw_fastrtps_shared_cpp  master
Code shared on static and dynamic type support of rmw_fastrtps_cpp.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
custom_service_info.hpp
Go to the documentation of this file.
1 // Copyright 2016-2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RMW_FASTRTPS_SHARED_CPP__CUSTOM_SERVICE_INFO_HPP_
16 #define RMW_FASTRTPS_SHARED_CPP__CUSTOM_SERVICE_INFO_HPP_
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <list>
21 #include <mutex>
22 #include <unordered_set>
23 #include <unordered_map>
24 
25 #include "fastcdr/FastBuffer.h"
26 
27 #include "fastdds/dds/core/status/PublicationMatchedStatus.hpp"
28 #include "fastdds/dds/core/status/SubscriptionMatchedStatus.hpp"
29 #include "fastdds/dds/publisher/DataWriter.hpp"
30 #include "fastdds/dds/publisher/DataWriterListener.hpp"
31 #include "fastdds/dds/subscriber/DataReader.hpp"
32 #include "fastdds/dds/subscriber/DataReaderListener.hpp"
33 #include "fastdds/dds/subscriber/SampleInfo.hpp"
34 #include "fastdds/dds/topic/TypeSupport.hpp"
35 
36 #include "fastdds/rtps/common/Guid.h"
37 #include "fastdds/rtps/common/InstanceHandle.h"
38 #include "fastdds/rtps/common/SampleIdentity.h"
39 
41 
44 
45 class ServiceListener;
46 class ServicePubListener;
47 
48 enum class client_present_t
49 {
50  FAILURE, // an error occurred when checking
51  MAYBE, // reader not matched, writer still present
52  YES, // reader matched
53  GONE // neither reader nor writer
54 };
55 
56 typedef struct CustomServiceInfo
57 {
58  eprosima::fastdds::dds::TypeSupport request_type_support_{nullptr};
59  const void * request_type_support_impl_{nullptr};
60  eprosima::fastdds::dds::TypeSupport response_type_support_{nullptr};
61  const void * response_type_support_impl_{nullptr};
62  eprosima::fastdds::dds::DataReader * request_reader_{nullptr};
63  eprosima::fastdds::dds::DataWriter * response_writer_{nullptr};
64 
67 
68  const char * typesupport_identifier_{nullptr};
70 
71 typedef struct CustomServiceRequest
72 {
73  eprosima::fastrtps::rtps::SampleIdentity sample_identity_;
74  eprosima::fastcdr::FastBuffer * buffer_;
75  eprosima::fastdds::dds::SampleInfo sample_info_ {};
76 
78  : buffer_(nullptr) {}
80 
81 class ServicePubListener : public eprosima::fastdds::dds::DataWriterListener
82 {
83  using subscriptions_set_t =
84  std::unordered_set<eprosima::fastrtps::rtps::GUID_t,
87  std::unordered_map<eprosima::fastrtps::rtps::GUID_t,
88  eprosima::fastrtps::rtps::GUID_t,
90 
91 public:
93  {
94  (void) info;
95  }
96 
97  void
99  eprosima::fastdds::dds::DataWriter * /* writer */,
100  const eprosima::fastdds::dds::PublicationMatchedStatus & info) final
101  {
102  std::lock_guard<std::mutex> lock(mutex_);
103  if (info.current_count_change == 1) {
104  subscriptions_.insert(eprosima::fastrtps::rtps::iHandle2GUID(info.last_subscription_handle));
105  } else if (info.current_count_change == -1) {
106  eprosima::fastrtps::rtps::GUID_t erase_endpoint_guid =
107  eprosima::fastrtps::rtps::iHandle2GUID(info.last_subscription_handle);
108  subscriptions_.erase(erase_endpoint_guid);
109  auto endpoint = clients_endpoints_.find(erase_endpoint_guid);
110  if (endpoint != clients_endpoints_.end()) {
111  clients_endpoints_.erase(endpoint->second);
112  clients_endpoints_.erase(erase_endpoint_guid);
113  }
114  } else {
115  return;
116  }
117  cv_.notify_all();
118  }
119 
120  template<class Rep, class Period>
121  bool
123  const eprosima::fastrtps::rtps::GUID_t & guid,
124  const std::chrono::duration<Rep, Period> & rel_time)
125  {
126  auto guid_is_present = [this, guid]() RCPPUTILS_TSA_REQUIRES(mutex_)->bool
127  {
128  return subscriptions_.find(guid) != subscriptions_.end();
129  };
130 
131  std::unique_lock<std::mutex> lock(mutex_);
132  return cv_.wait_for(lock, rel_time, guid_is_present);
133  }
134 
137  const eprosima::fastrtps::rtps::GUID_t & guid)
138  {
139  {
140  std::lock_guard<std::mutex> lock(mutex_);
141  // Check if the guid is still in the map
142  if (clients_endpoints_.find(guid) == clients_endpoints_.end()) {
143  // Client is gone
144  return client_present_t::GONE;
145  }
146  }
147  // Wait for subscription
150  }
151  return client_present_t::YES;
152  }
153 
154  void endpoint_erase_if_exists(const eprosima::fastrtps::rtps::GUID_t & endpointGuid)
155  {
156  std::lock_guard<std::mutex> lock(mutex_);
157  auto endpoint = clients_endpoints_.find(endpointGuid);
158  if (endpoint != clients_endpoints_.end()) {
159  clients_endpoints_.erase(endpoint->second);
160  clients_endpoints_.erase(endpointGuid);
161  }
162  }
163 
165  const eprosima::fastrtps::rtps::GUID_t & readerGuid,
166  const eprosima::fastrtps::rtps::GUID_t & writerGuid)
167  {
168  std::lock_guard<std::mutex> lock(mutex_);
169  clients_endpoints_.emplace(readerGuid, writerGuid);
170  clients_endpoints_.emplace(writerGuid, readerGuid);
171  }
172 
173 private:
174  std::mutex mutex_;
175  subscriptions_set_t subscriptions_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
176  clients_endpoints_map_t clients_endpoints_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
178 };
179 
180 class ServiceListener : public eprosima::fastdds::dds::DataReaderListener
181 {
182 public:
184  : info_(info), list_has_data_(false),
185  conditionMutex_(nullptr), conditionVariable_(nullptr)
186  {
187  }
188 
189  void
191  eprosima::fastdds::dds::DataReader * /* reader */,
192  const eprosima::fastdds::dds::SubscriptionMatchedStatus & info) final
193  {
194  if (info.current_count_change == -1) {
196  eprosima::fastrtps::rtps::iHandle2GUID(info.last_publication_handle));
197  }
198  }
199 
200  void
201  on_data_available(eprosima::fastdds::dds::DataReader * reader) final
202  {
203  assert(reader);
204 
205  CustomServiceRequest request;
206  request.buffer_ = new eprosima::fastcdr::FastBuffer();
207 
209  data.is_cdr_buffer = true;
210  data.data = request.buffer_;
211  data.impl = nullptr; // not used when is_cdr_buffer is true
212  if (reader->take_next_sample(&data, &request.sample_info_) == ReturnCode_t::RETCODE_OK) {
213  if (request.sample_info_.valid_data) {
214  request.sample_identity_ = request.sample_info_.sample_identity;
215  // Use response subscriber guid (on related_sample_identity) when present.
216  const eprosima::fastrtps::rtps::GUID_t & reader_guid =
217  request.sample_info_.related_sample_identity.writer_guid();
218  if (reader_guid != eprosima::fastrtps::rtps::GUID_t::unknown() ) {
219  request.sample_identity_.writer_guid() = reader_guid;
220  }
221 
222  // Save both guids in the clients_endpoints map
223  const eprosima::fastrtps::rtps::GUID_t & writer_guid =
224  request.sample_info_.sample_identity.writer_guid();
225  info_->pub_listener_->endpoint_add_reader_and_writer(reader_guid, writer_guid);
226 
227  std::lock_guard<std::mutex> lock(internalMutex_);
228 
229  if (conditionMutex_ != nullptr) {
230  std::unique_lock<std::mutex> clock(*conditionMutex_);
231  list.push_back(request);
232  // the change to list_has_data_ needs to be mutually exclusive with
233  // rmw_wait() which checks hasData() and decides if wait() needs to
234  // be called
235  list_has_data_.store(true);
236  clock.unlock();
237  conditionVariable_->notify_one();
238  } else {
239  list.push_back(request);
240  list_has_data_.store(true);
241  }
242  }
243  }
244  }
245 
248  {
249  std::lock_guard<std::mutex> lock(internalMutex_);
250  CustomServiceRequest request;
251 
252  if (conditionMutex_ != nullptr) {
253  std::unique_lock<std::mutex> clock(*conditionMutex_);
254  if (!list.empty()) {
255  request = list.front();
256  list.pop_front();
257  list_has_data_.store(!list.empty());
258  }
259  } else {
260  if (!list.empty()) {
261  request = list.front();
262  list.pop_front();
263  list_has_data_.store(!list.empty());
264  }
265  }
266 
267  return request;
268  }
269 
270  void
271  attachCondition(std::mutex * conditionMutex, std::condition_variable * conditionVariable)
272  {
273  std::lock_guard<std::mutex> lock(internalMutex_);
274  conditionMutex_ = conditionMutex;
275  conditionVariable_ = conditionVariable;
276  }
277 
278  void
280  {
281  std::lock_guard<std::mutex> lock(internalMutex_);
282  conditionMutex_ = nullptr;
283  conditionVariable_ = nullptr;
284  }
285 
286  bool
288  {
289  return list_has_data_.load();
290  }
291 
292 private:
293  CustomServiceInfo * info_;
294  std::mutex internalMutex_;
295  std::list<CustomServiceRequest> list RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
296  std::atomic_bool list_has_data_;
297  std::mutex * conditionMutex_ RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
298  std::condition_variable * conditionVariable_ RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
299 };
300 
301 #endif // RMW_FASTRTPS_SHARED_CPP__CUSTOM_SERVICE_INFO_HPP_
CustomServiceRequest
Definition: custom_service_info.hpp:71
CustomServiceRequest::sample_info_
eprosima::fastdds::dds::SampleInfo sample_info_
Definition: custom_service_info.hpp:75
rmw_fastrtps_shared_cpp::SerializedData::impl
const void * impl
Definition: TypeSupport.hpp:41
std::list
CustomServiceInfo::request_reader_
eprosima::fastdds::dds::DataReader * request_reader_
Definition: custom_service_info.hpp:62
std::unordered_set
ServiceListener::on_subscription_matched
void on_subscription_matched(eprosima::fastdds::dds::DataReader *, const eprosima::fastdds::dds::SubscriptionMatchedStatus &info) final
Definition: custom_service_info.hpp:190
rmw_fastrtps_shared_cpp::SerializedData::is_cdr_buffer
bool is_cdr_buffer
Definition: TypeSupport.hpp:39
std::chrono::duration
CustomServiceInfo::typesupport_identifier_
const char * typesupport_identifier_
Definition: custom_service_info.hpp:68
std::lock_guard
client_present_t::GONE
@ GONE
TypeSupport.hpp
ServicePubListener::wait_for_subscription
bool wait_for_subscription(const eprosima::fastrtps::rtps::GUID_t &guid, const std::chrono::duration< Rep, Period > &rel_time)
Definition: custom_service_info.hpp:122
CustomServiceRequest::CustomServiceRequest
CustomServiceRequest()
Definition: custom_service_info.hpp:77
RCPPUTILS_TSA_REQUIRES
#define RCPPUTILS_TSA_REQUIRES(...)
client_present_t
client_present_t
Definition: custom_service_info.hpp:48
client_present_t::YES
@ YES
ServicePubListener::ServicePubListener
ServicePubListener(CustomServiceInfo *info)
Definition: custom_service_info.hpp:92
CustomServiceRequest::sample_identity_
eprosima::fastrtps::rtps::SampleIdentity sample_identity_
Definition: custom_service_info.hpp:73
CustomServiceInfo::response_type_support_
eprosima::fastdds::dds::TypeSupport response_type_support_
Definition: custom_service_info.hpp:60
ServiceListener
Definition: custom_service_info.hpp:180
client_present_t::FAILURE
@ FAILURE
rmw_fastrtps_shared_cpp::hash_fastrtps_guid
Definition: guid_utils.hpp:59
ServiceListener::hasData
bool hasData()
Definition: custom_service_info.hpp:287
ServiceListener::on_data_available
void on_data_available(eprosima::fastdds::dds::DataReader *reader) final
Definition: custom_service_info.hpp:201
CustomServiceInfo::response_type_support_impl_
const void * response_type_support_impl_
Definition: custom_service_info.hpp:61
CustomServiceInfo
Definition: custom_service_info.hpp:56
rmw_fastrtps_shared_cpp::SerializedData
Definition: TypeSupport.hpp:37
rmw_fastrtps_shared_cpp::SerializedData::data
void * data
Definition: TypeSupport.hpp:40
std::unique_lock
CustomServiceInfo::request_type_support_impl_
const void * request_type_support_impl_
Definition: custom_service_info.hpp:59
guid_utils.hpp
CustomServiceInfo::listener_
ServiceListener * listener_
Definition: custom_service_info.hpp:65
CustomServiceRequest
struct CustomServiceRequest CustomServiceRequest
CustomServiceInfo::response_writer_
eprosima::fastdds::dds::DataWriter * response_writer_
Definition: custom_service_info.hpp:63
std::condition_variable::wait_for
T wait_for(T... args)
ServicePubListener::check_for_subscription
client_present_t check_for_subscription(const eprosima::fastrtps::rtps::GUID_t &guid)
Definition: custom_service_info.hpp:136
CustomServiceRequest::buffer_
eprosima::fastcdr::FastBuffer * buffer_
Definition: custom_service_info.hpp:74
ServiceListener::attachCondition
void attachCondition(std::mutex *conditionMutex, std::condition_variable *conditionVariable)
Definition: custom_service_info.hpp:271
thread_safety_annotations.hpp
std::condition_variable
ServiceListener::detachCondition
void detachCondition()
Definition: custom_service_info.hpp:279
ServicePubListener::endpoint_erase_if_exists
void endpoint_erase_if_exists(const eprosima::fastrtps::rtps::GUID_t &endpointGuid)
Definition: custom_service_info.hpp:154
CustomServiceInfo::pub_listener_
ServicePubListener * pub_listener_
Definition: custom_service_info.hpp:66
CustomServiceInfo
struct CustomServiceInfo CustomServiceInfo
ServiceListener::ServiceListener
ServiceListener(CustomServiceInfo *info)
Definition: custom_service_info.hpp:183
std::mutex
ServicePubListener::endpoint_add_reader_and_writer
void endpoint_add_reader_and_writer(const eprosima::fastrtps::rtps::GUID_t &readerGuid, const eprosima::fastrtps::rtps::GUID_t &writerGuid)
Definition: custom_service_info.hpp:164
ServicePubListener
Definition: custom_service_info.hpp:81
CustomServiceInfo::request_type_support_
eprosima::fastdds::dds::TypeSupport request_type_support_
Definition: custom_service_info.hpp:58
std::unordered_map
std::condition_variable::notify_all
T notify_all(T... args)
ServicePubListener::on_publication_matched
void on_publication_matched(eprosima::fastdds::dds::DataWriter *, const eprosima::fastdds::dds::PublicationMatchedStatus &info) final
Definition: custom_service_info.hpp:98
client_present_t::MAYBE
@ MAYBE
ServiceListener::getRequest
CustomServiceRequest getRequest()
Definition: custom_service_info.hpp:247