WLP.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2016 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. * @file WLP.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_WLP_H_
  19. #define _FASTDDS_RTPS_WLP_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <vector>
  22. #include <mutex>
  23. #include <fastdds/rtps/common/Time_t.h>
  24. #include <fastdds/rtps/common/Locator.h>
  25. #include <fastdds/rtps/common/Guid.h>
  26. #include <fastrtps/qos/QosPolicies.h>
  27. #include <fastdds/rtps/builtin/data/WriterProxyData.h>
  28. #include <fastdds/rtps/builtin/data/ReaderProxyData.h>
  29. namespace eprosima {
  30. namespace fastrtps {
  31. namespace rtps {
  32. class BuiltinProtocols;
  33. class LivelinessManager;
  34. class ReaderHistory;
  35. class ReaderProxyData;
  36. class RTPSParticipantImpl;
  37. class RTPSReader;
  38. class RTPSWriter;
  39. class StatefulReader;
  40. class StatefulWriter;
  41. class ParticipantProxyData;
  42. class TimedEvent;
  43. class WLPListener;
  44. class WriterHistory;
  45. class WriterProxyData;
  46. /**
  47. * Class WLP that implements the Writer Liveliness Protocol described in the RTPS specification.
  48. * @ingroup LIVELINESS_MODULE
  49. */
  50. class WLP
  51. {
  52. friend class WLPListener;
  53. friend class StatefulReader;
  54. friend class StatelessReader;
  55. public:
  56. /**
  57. * Constructor
  58. * @param prot Pointer to the BuiltinProtocols object.
  59. */
  60. WLP(
  61. BuiltinProtocols* prot);
  62. virtual ~WLP();
  63. /**
  64. * Initialize the WLP protocol.
  65. * @param p Pointer to the RTPS participant implementation.
  66. * @return true if the initialziacion was succesful.
  67. */
  68. bool initWL(
  69. RTPSParticipantImpl* p);
  70. /**
  71. * Assign the remote endpoints for a newly discovered RTPSParticipant.
  72. * @param pdata Pointer to the RTPSParticipantProxyData object.
  73. * @return True if correct.
  74. */
  75. bool assignRemoteEndpoints(
  76. const ParticipantProxyData& pdata);
  77. /**
  78. * Remove remote endpoints from the liveliness protocol.
  79. * @param pdata Pointer to the ParticipantProxyData to remove
  80. */
  81. void removeRemoteEndpoints(
  82. ParticipantProxyData* pdata);
  83. /**
  84. * Add a local writer to the liveliness protocol.
  85. * @param W Pointer to the RTPSWriter.
  86. * @param wqos Quality of service policies for the writer.
  87. * @return True if correct.
  88. */
  89. bool add_local_writer(
  90. RTPSWriter* W,
  91. const WriterQos& wqos);
  92. /**
  93. * Remove a local writer from the liveliness protocol.
  94. * @param W Pointer to the RTPSWriter.
  95. * @return True if removed.
  96. */
  97. bool remove_local_writer(
  98. RTPSWriter* W);
  99. /**
  100. * @brief Adds a local reader to the liveliness protocol
  101. * @param reader Pointer to the RTPS reader
  102. * @param rqos Quality of service policies for the reader
  103. * @return True if added successfully
  104. */
  105. bool add_local_reader(
  106. RTPSReader* reader,
  107. const ReaderQos& rqos);
  108. /**
  109. * @brief Removes a local reader from the livliness protocol
  110. * @param reader Pointer to the reader to remove
  111. * @return True if removed successfully
  112. */
  113. bool remove_local_reader(
  114. RTPSReader* reader);
  115. /**
  116. * @brief A method to assert liveliness of a given writer
  117. * @param writer The writer, specified via its id
  118. * @param kind The writer liveliness kind
  119. * @param lease_duration The writer lease duration
  120. * @return True if liveliness was asserted
  121. */
  122. bool assert_liveliness(
  123. GUID_t writer,
  124. LivelinessQosPolicyKind kind,
  125. Duration_t lease_duration);
  126. /**
  127. * @brief A method to assert liveliness of MANUAL_BY_PARTICIPANT writers
  128. * @return True if there were any MANUAL_BY_PARTICIPANT writers
  129. */
  130. bool assert_liveliness_manual_by_participant();
  131. /**
  132. * Get the livelines builtin writer
  133. * @return stateful writer
  134. */
  135. StatefulWriter* builtin_writer();
  136. /**
  137. * Get the livelines builtin writer's history
  138. * @return writer history
  139. */
  140. WriterHistory* builtin_writer_history();
  141. #if HAVE_SECURITY
  142. bool pairing_remote_reader_with_local_writer_after_security(
  143. const GUID_t& local_writer,
  144. const ReaderProxyData& remote_reader_data);
  145. bool pairing_remote_writer_with_local_reader_after_security(
  146. const GUID_t& local_reader,
  147. const WriterProxyData& remote_writer_data);
  148. #endif
  149. private:
  150. /**
  151. * Create the endpoints used in the WLP.
  152. * @return true if correct.
  153. */
  154. bool createEndpoints();
  155. //! Minimum time among liveliness periods of automatic writers, in milliseconds
  156. double min_automatic_ms_;
  157. //! Minimum time among liveliness periods of manual by participant writers, in milliseconds
  158. double min_manual_by_participant_ms_;
  159. //!Pointer to the local RTPSParticipant.
  160. RTPSParticipantImpl* mp_participant;
  161. //!Pointer to the builtinprotocol class.
  162. BuiltinProtocols* mp_builtinProtocols;
  163. //!Pointer to the builtinRTPSParticipantMEssageWriter.
  164. StatefulWriter* mp_builtinWriter;
  165. //!Pointer to the builtinRTPSParticipantMEssageReader.
  166. StatefulReader* mp_builtinReader;
  167. //!Writer History
  168. WriterHistory* mp_builtinWriterHistory;
  169. //!Reader History
  170. ReaderHistory* mp_builtinReaderHistory;
  171. //!Listener object.
  172. WLPListener* mp_listener;
  173. //!Pointer to the periodic assertion timer object for automatic liveliness writers
  174. TimedEvent* automatic_liveliness_assertion_;
  175. //!Pointer to the periodic assertion timer object for manual by participant liveliness writers
  176. TimedEvent* manual_liveliness_assertion_;
  177. //! List of the writers using automatic liveliness.
  178. std::vector<RTPSWriter*> automatic_writers_;
  179. //! List of the writers using manual by participant liveliness.
  180. std::vector<RTPSWriter*> manual_by_participant_writers_;
  181. //! List of writers using manual by topic liveliness
  182. std::vector<RTPSWriter*> manual_by_topic_writers_;
  183. //! List of readers
  184. std::vector<RTPSReader*> readers_;
  185. //! A boolean indicating that there is at least one reader requesting automatic liveliness
  186. bool automatic_readers_;
  187. //! A class used by writers in this participant to keep track of their liveliness
  188. LivelinessManager* pub_liveliness_manager_;
  189. //! A class used by readers in this participant to keep track of liveliness of matched writers
  190. LivelinessManager* sub_liveliness_manager_;
  191. InstanceHandle_t automatic_instance_handle_;
  192. InstanceHandle_t manual_by_participant_instance_handle_;
  193. /**
  194. * @brief A method invoked by pub_liveliness_manager_ to inform that a writer changed its liveliness
  195. * @param writer The writer losing liveliness
  196. * @param kind The liveliness kind
  197. * @param lease_duration The liveliness lease duration
  198. * @param alive_change The change in the alive count
  199. * @param not_alive_change The change in the not alive count
  200. */
  201. void pub_liveliness_changed(
  202. const GUID_t& writer,
  203. const LivelinessQosPolicyKind& kind,
  204. const Duration_t& lease_duration,
  205. int32_t alive_change,
  206. int32_t not_alive_change);
  207. /**
  208. * @brief A method invoked by sub_liveliness_manager_ to inform that a writer changed its liveliness
  209. * @param writer The writer losing liveliness
  210. * @param kind The liveliness kind of the writer losing liveliness
  211. * @param lease_duration The liveliness lease duration of the writer losing liveliness
  212. * @param alive_change The change in the alive count
  213. * @param not_alive_change The change in the not alive count
  214. */
  215. void sub_liveliness_changed(
  216. const GUID_t& writer,
  217. const LivelinessQosPolicyKind& kind,
  218. const Duration_t& lease_duration,
  219. int32_t alive_change,
  220. int32_t not_alive_change);
  221. /**
  222. * @brief A method to update the liveliness changed status of a given reader
  223. * @param writer The writer changing liveliness, specified by its guid
  224. * @param reader The reader whose liveliness needs to be updated
  225. * @param alive_change The change requested for alive count. Should be -1, 0 or +1
  226. * @param not_alive_change The change requested for not alive count. Should be -1, 0 or +1
  227. */
  228. void update_liveliness_changed_status(
  229. GUID_t writer,
  230. RTPSReader* reader,
  231. int32_t alive_change,
  232. int32_t not_alive_change);
  233. /**
  234. * Implements the automatic liveliness timed event
  235. */
  236. bool automatic_liveliness_assertion();
  237. /**
  238. * Implements the manual by participant liveliness timed event
  239. */
  240. bool participant_liveliness_assertion();
  241. /**
  242. * Adds a cache change to the WLP writer
  243. * @param instance key of the change to add
  244. * @return true if change is correctly added
  245. */
  246. bool send_liveliness_message(
  247. const InstanceHandle_t& instance);
  248. #if HAVE_SECURITY
  249. //!Pointer to the builtinRTPSParticipantMEssageWriter.
  250. StatefulWriter* mp_builtinWriterSecure;
  251. //!Pointer to the builtinRTPSParticipantMEssageReader.
  252. StatefulReader* mp_builtinReaderSecure;
  253. //!Writer History
  254. WriterHistory* mp_builtinWriterSecureHistory;
  255. //!Reader History
  256. ReaderHistory* mp_builtinReaderSecureHistory;
  257. /**
  258. * Create the secure endpoitns used in the WLP.
  259. * @return true if correct.
  260. */
  261. bool createSecureEndpoints();
  262. #endif
  263. std::mutex temp_data_lock_;
  264. ReaderProxyData temp_reader_proxy_data_;
  265. WriterProxyData temp_writer_proxy_data_;
  266. };
  267. } /* namespace rtps */
  268. } /* namespace fastrtps */
  269. } /* namespace eprosima */
  270. #endif
  271. #endif /* _FASTDDS_RTPS_WLP_H_ */