PDP.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // Copyright 2019 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 PDP.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_PDP_H_
  19. #define _FASTDDS_RTPS_PDP_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <mutex>
  22. #include <functional>
  23. #include <fastdds/rtps/common/Guid.h>
  24. #include <fastdds/rtps/attributes/RTPSParticipantAttributes.h>
  25. #include <fastdds/rtps/builtin/data/ReaderProxyData.h>
  26. #include <fastdds/rtps/builtin/data/WriterProxyData.h>
  27. #include <fastrtps/qos/QosPolicies.h>
  28. #include <fastrtps/utils/collections/ResourceLimitedVector.hpp>
  29. #include <fastdds/rtps/participant/ParticipantDiscoveryInfo.h>
  30. namespace eprosima {
  31. namespace fastrtps {
  32. namespace rtps {
  33. class RTPSWriter;
  34. class RTPSReader;
  35. class WriterHistory;
  36. class ReaderHistory;
  37. class RTPSParticipantImpl;
  38. class RTPSParticipantListener;
  39. class BuiltinProtocols;
  40. class EDP;
  41. class TimedEvent;
  42. class ReaderProxyData;
  43. class WriterProxyData;
  44. class ParticipantProxyData;
  45. class ReaderListener;
  46. class PDPListener;
  47. class PDPServerListener;
  48. /**
  49. * Abstract class PDP that implements the basic interfaces for all Participant Discovery implementations
  50. * It also keeps the Participant Discovery Data and provides interfaces to access it
  51. *@ingroup DISCOVERY_MODULE
  52. */
  53. class PDP
  54. {
  55. friend class PDPListener;
  56. friend class PDPServerListener;
  57. public:
  58. /**
  59. * Constructor
  60. * @param builtin Pointer to the BuiltinProcols object.
  61. * @param allocation Participant allocation parameters.
  62. */
  63. PDP(
  64. BuiltinProtocols* builtin,
  65. const RTPSParticipantAllocationAttributes& allocation);
  66. virtual ~PDP();
  67. virtual void initializeParticipantProxyData(ParticipantProxyData* participant_data);
  68. /**
  69. * Initialize the PDP.
  70. * @param part Pointer to the RTPSParticipant.
  71. * @return True on success
  72. */
  73. bool initPDP(
  74. RTPSParticipantImpl* part);
  75. bool enable();
  76. virtual bool init(RTPSParticipantImpl* part) = 0;
  77. /**
  78. * Creates an initializes a new participant proxy from a DATA(p) raw info
  79. * @param p from DATA msg deserialization
  80. * @param writer_guid GUID of originating writer
  81. * @return new ParticipantProxyData * or nullptr on failure
  82. */
  83. virtual ParticipantProxyData* createParticipantProxyData(
  84. const ParticipantProxyData& p,
  85. const GUID_t& writer_guid) = 0;
  86. /**
  87. * Force the sending of our local DPD to all remote RTPSParticipants and multicast Locators.
  88. * @param new_change If true a new change (with new seqNum) is created and sent;If false the last change is re-sent
  89. * @param dispose sets change kind to NOT_ALIVE_DISPOSED_UNREGISTERED
  90. * @param wparams allows to identify the change
  91. */
  92. virtual void announceParticipantState(
  93. bool new_change,
  94. bool dispose = false,
  95. WriteParams& wparams = WriteParams::WRITE_PARAM_DEFAULT);
  96. //!Stop the RTPSParticipantAnnouncement (only used in tests).
  97. virtual void stopParticipantAnnouncement();
  98. //!Reset the RTPSParticipantAnnouncement (only used in tests).
  99. virtual void resetParticipantAnnouncement();
  100. /**
  101. * Add a ReaderProxyData to the correct ParticipantProxyData.
  102. * @param [in] reader_guid GUID of the reader to add.
  103. * @param [out] participant_guid GUID of the ParticipantProxyData where the reader was added.
  104. * @param [in] initializer_func Function to be called in order to set the data of the ReaderProxyData.
  105. *
  106. * @return A pointer to the added ReaderProxyData (nullptr if it could not be added).
  107. */
  108. ReaderProxyData* addReaderProxyData(
  109. const GUID_t& reader_guid,
  110. GUID_t& participant_guid,
  111. std::function<bool(ReaderProxyData*, bool, const ParticipantProxyData&)> initializer_func);
  112. /**
  113. * Add a WriterProxyData to the correct ParticipantProxyData.
  114. * @param [in] writer_guid GUID of the writer to add.
  115. * @param [out] participant_guid GUID of the ParticipantProxyData where the writer was added.
  116. * @param [in] initializer_func Function to be called in order to set the data of the WriterProxyData.
  117. *
  118. * @return A pointer to the added WriterProxyData (nullptr if it could not be added).
  119. */
  120. WriterProxyData* addWriterProxyData(
  121. const GUID_t& writer_guid,
  122. GUID_t& participant_guid,
  123. std::function<bool(WriterProxyData*, bool, const ParticipantProxyData&)> initializer_func);
  124. /**
  125. * This method returns whether a ReaderProxyDataObject exists among the registered RTPSParticipants
  126. * (including the local RTPSParticipant).
  127. * @param [in] reader GUID_t of the reader we are looking for.
  128. * @return True if found.
  129. */
  130. bool has_reader_proxy_data(const GUID_t& reader);
  131. /**
  132. * This method gets a copy of a ReaderProxyData object if it is found among the registered RTPSParticipants
  133. * (including the local RTPSParticipant).
  134. * @param [in] reader GUID_t of the reader we are looking for.
  135. * @param [out] rdata Reference to the ReaderProxyData object where data is to be returned.
  136. * @return True if found.
  137. */
  138. bool lookupReaderProxyData(
  139. const GUID_t& reader,
  140. ReaderProxyData& rdata);
  141. /**
  142. * This method returns whether a WriterProxyData exists among the registered RTPSParticipants
  143. * (including the local RTPSParticipant).
  144. * @param [in] writer GUID_t of the writer we are looking for.
  145. * @return True if found.
  146. */
  147. bool has_writer_proxy_data(const GUID_t& writer);
  148. /**
  149. * This method gets a copy of a WriterProxyData object if it is found among the registered RTPSParticipants
  150. * (including the local RTPSParticipant).
  151. * @param [in] writer GUID_t of the writer we are looking for.
  152. * @param [out] wdata Reference to the WriterProxyData object where data is to be returned.
  153. * @return True if found.
  154. */
  155. bool lookupWriterProxyData(
  156. const GUID_t& writer,
  157. WriterProxyData& wdata);
  158. /**
  159. * This method returns the name of a participant if it is found among the registered RTPSParticipants.
  160. * @param [in] guid GUID_t of the RTPSParticipant we are looking for.
  161. * @param [out] name Copy of name on ParticipantProxyData object.
  162. * @return True if found.
  163. */
  164. bool lookup_participant_name(
  165. const GUID_t& guid,
  166. string_255& name);
  167. /**
  168. * This method removes and deletes a ReaderProxyData object from its corresponding RTPSParticipant.
  169. * @param reader_guid GUID_t of the reader to remove.
  170. * @return true if found and deleted.
  171. */
  172. bool removeReaderProxyData(const GUID_t& reader_guid);
  173. /**
  174. * This method removes and deletes a WriterProxyData object from its corresponding RTPSParticipant.
  175. * @param writer_guid GUID_t of the wtiter to remove.
  176. * @return true if found and deleted.
  177. */
  178. bool removeWriterProxyData(const GUID_t& writer_guid);
  179. /**
  180. * Create the SPDP Writer and Reader
  181. * @return True if correct.
  182. */
  183. virtual bool createPDPEndpoints() = 0;
  184. /**
  185. * This method assigns remote endpoints to the builtin endpoints defined in this protocol. It also calls the corresponding methods in EDP and WLP.
  186. * @param pdata Pointer to the RTPSParticipantProxyData object.
  187. */
  188. virtual void assignRemoteEndpoints(ParticipantProxyData* pdata) = 0;
  189. /**
  190. * Override to match additional endpoints to PDP. Like EDP or WLP.
  191. * @param pdata Pointer to the ParticipantProxyData object.
  192. */
  193. virtual void notifyAboveRemoteEndpoints(const ParticipantProxyData& pdata) = 0;
  194. /**
  195. * Some PDP classes require EDP matching with update PDP DATAs like EDPStatic
  196. * @return true if EDP endpoinst must be match
  197. */
  198. virtual bool updateInfoMatchesEDP() { return false; }
  199. /**
  200. * Remove remote endpoints from the participant discovery protocol
  201. * @param pdata Pointer to the ParticipantProxyData to remove
  202. */
  203. virtual void removeRemoteEndpoints(ParticipantProxyData* pdata) = 0;
  204. /**
  205. * This method removes a remote RTPSParticipant and all its writers and readers.
  206. * @param participant_guid GUID_t of the remote RTPSParticipant.
  207. * @param reason Why the participant is being removed (dropped vs removed)
  208. * @return true if correct.
  209. */
  210. virtual bool remove_remote_participant(
  211. const GUID_t& participant_guid,
  212. ParticipantDiscoveryInfo::DISCOVERY_STATUS reason);
  213. /**
  214. * This method returns the BuiltinAttributes of the local participant.
  215. * @return const reference to the BuiltinAttributes of the local participant.
  216. */
  217. const BuiltinAttributes& builtin_attributes() const;
  218. /**
  219. * Get a pointer to the local RTPSParticipant ParticipantProxyData object.
  220. * @return Pointer to the local RTPSParticipant ParticipantProxyData object.
  221. */
  222. ParticipantProxyData* getLocalParticipantProxyData()
  223. {
  224. return participant_proxies_.front();
  225. }
  226. /**
  227. * Get a pointer to the EDP object.
  228. * @return pointer to the EDP object.
  229. */
  230. inline EDP* getEDP(){return mp_EDP;}
  231. /**
  232. * Get a const_iterator to the beginning of the RTPSParticipant Proxies.
  233. * @return const_iterator.
  234. */
  235. ResourceLimitedVector<ParticipantProxyData*>::const_iterator ParticipantProxiesBegin()
  236. {
  237. return participant_proxies_.begin();
  238. }
  239. /**
  240. * Get a const_iterator to the end of the RTPSParticipant Proxies.
  241. * @return const_iterator.
  242. */
  243. ResourceLimitedVector<ParticipantProxyData*>::const_iterator ParticipantProxiesEnd()
  244. {
  245. return participant_proxies_.end();
  246. }
  247. /**
  248. * Assert the liveliness of a Remote Participant.
  249. * @param remote_guid GuidPrefix_t of the participant whose liveliness is being asserted.
  250. */
  251. void assert_remote_participant_liveliness(
  252. const GuidPrefix_t& remote_guid);
  253. /**
  254. * Get the RTPS participant
  255. * @return RTPS participant
  256. */
  257. inline RTPSParticipantImpl* getRTPSParticipant() const {return mp_RTPSParticipant;};
  258. /**
  259. * Get the mutex.
  260. * @return Pointer to the Mutex
  261. */
  262. inline std::recursive_mutex* getMutex() const {return mp_mutex;}
  263. CDRMessage_t get_participant_proxy_data_serialized(Endianness_t endian);
  264. protected:
  265. //!Pointer to the builtin protocols object.
  266. BuiltinProtocols* mp_builtin;
  267. //!Pointer to the local RTPSParticipant.
  268. RTPSParticipantImpl* mp_RTPSParticipant;
  269. //!Discovery attributes.
  270. BuiltinAttributes m_discovery;
  271. //!Pointer to the PDPWriter.
  272. RTPSWriter* mp_PDPWriter;
  273. //!Pointer to the PDPReader.
  274. RTPSReader* mp_PDPReader;
  275. //!Pointer to the EDP object.
  276. EDP* mp_EDP;
  277. //!Number of participant proxy data objects created
  278. size_t participant_proxies_number_;
  279. //!Registered RTPSParticipants (including the local one, that is the first one.)
  280. ResourceLimitedVector<ParticipantProxyData*> participant_proxies_;
  281. //!Pool of participant proxy data objects ready for reuse
  282. ResourceLimitedVector<ParticipantProxyData*> participant_proxies_pool_;
  283. //!Number of reader proxy data objects created
  284. size_t reader_proxies_number_;
  285. //!Pool of reader proxy data objects ready for reuse
  286. ResourceLimitedVector<ReaderProxyData*> reader_proxies_pool_;
  287. //!Number of writer proxy data objects created
  288. size_t writer_proxies_number_;
  289. //!Pool of writer proxy data objects ready for reuse
  290. ResourceLimitedVector<WriterProxyData*> writer_proxies_pool_;
  291. //!Variable to indicate if any parameter has changed.
  292. std::atomic_bool m_hasChangedLocalPDP;
  293. //!Listener for the SPDP messages.
  294. ReaderListener* mp_listener;
  295. //!WriterHistory
  296. WriterHistory* mp_PDPWriterHistory;
  297. //!Reader History
  298. ReaderHistory* mp_PDPReaderHistory;
  299. //!ReaderProxyData to allow preallocation of remote locators
  300. ReaderProxyData temp_reader_data_;
  301. //!WriterProxyData to allow preallocation of remote locators
  302. WriterProxyData temp_writer_data_;
  303. //!To protect temp_writer_data_ and temp_reader_data_
  304. std::mutex temp_data_lock_;
  305. //!Participant data atomic access assurance
  306. std::recursive_mutex* mp_mutex;
  307. //!To protect callbacks (ParticipantProxyData&)
  308. std::mutex callback_mtx_;
  309. /**
  310. * Adds an entry to the collection of participant proxy information.
  311. * May use one of the entries present in the pool.
  312. *
  313. * @param participant_guid GUID of the participant for which to create the proxy object.
  314. * @param with_lease_duration indicates whether lease duration event should be created.
  315. *
  316. * @return pointer to the currently inserted entry, nullptr if allocation limits were reached.
  317. */
  318. ParticipantProxyData* add_participant_proxy_data(
  319. const GUID_t& participant_guid,
  320. bool with_lease_duration);
  321. /**
  322. * Gets the key of a participant proxy data.
  323. *
  324. * @param [in] participant_guid GUID of the participant to look for.
  325. * @param [out] key of the corresponding proxy object.
  326. *
  327. * @return true when input GUID is found.
  328. */
  329. bool lookup_participant_key(
  330. const GUID_t& participant_guid,
  331. InstanceHandle_t& key);
  332. private:
  333. //!TimedEvent to periodically resend the local RTPSParticipant information.
  334. TimedEvent* resend_participant_info_event_;
  335. //!Participant's initial announcements config
  336. InitialAnnouncementConfig initial_announcements_;
  337. void check_remote_participant_liveliness(
  338. ParticipantProxyData* remote_participant);
  339. void check_and_notify_type_discovery(
  340. RTPSParticipantListener* listener,
  341. const WriterProxyData& wdata) const;
  342. void check_and_notify_type_discovery(
  343. RTPSParticipantListener* listener,
  344. const ReaderProxyData& rdata) const;
  345. void check_and_notify_type_discovery(
  346. RTPSParticipantListener* listener,
  347. const string_255& topic_name,
  348. const string_255& type_name,
  349. const types::TypeIdentifier* type_id,
  350. const types::TypeObject* type_obj,
  351. const xtypes::TypeInformation* type_info) const;
  352. /**
  353. * Calculates the next announcement interval
  354. */
  355. void set_next_announcement_interval();
  356. /**
  357. * Calculates the initial announcement interval
  358. */
  359. void set_initial_announcement_interval();
  360. };
  361. // configuration values for PDP reliable entities.
  362. extern const Duration_t pdp_heartbeat_period;
  363. extern const Duration_t pdp_nack_response_delay;
  364. extern const Duration_t pdp_nack_supression_duration;
  365. extern const Duration_t pdp_heartbeat_response_delay;
  366. extern const int32_t pdp_initial_reserved_caches;
  367. } /* namespace rtps */
  368. } /* namespace fastrtps */
  369. } /* namespace eprosima */
  370. #endif
  371. #endif /* _FASTDDS_RTPS_PDP_H_ */