PDPServer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 PDPServer.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_PDPSERVER_H_
  19. #define _FASTDDS_RTPS_PDPSERVER_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <fastdds/rtps/builtin/discovery/participant/PDP.h>
  22. #include <fastdds/rtps/messages/RTPSMessageGroup.h>
  23. #include <fastdds/rtps/builtin/discovery/participant/timedevent/DServerEvent.h>
  24. // TODO: remove when the Writer API issue is resolved
  25. #include <fastdds/rtps/attributes/WriterAttributes.h>
  26. namespace eprosima {
  27. namespace fastrtps{
  28. namespace rtps {
  29. class StatefulWriter;
  30. class StatefulReader;
  31. class RemoteWriterAttributes;
  32. /**
  33. * Class PDPServer manages server side of the discovery server mechanism
  34. *@ingroup DISCOVERY_MODULE
  35. */
  36. class PDPServer : public PDP
  37. {
  38. friend class DServerEvent;
  39. friend class PDPServerListener;
  40. friend class InPDPCallback;
  41. typedef std::set<const ParticipantProxyData*> pending_matches_list;
  42. typedef std::set<InstanceHandle_t> key_list;
  43. //! EDP pending matches
  44. pending_matches_list _p2match;
  45. //! Keys to wipe out from WriterHistory because its related Participants have been removed
  46. key_list _demises;
  47. //! TRANSIENT or TRANSIENT_LOCAL durability;
  48. DurabilityKind_t _durability;
  49. //! Temporary locator list to solve new Writer API issue
  50. // TODO: remove when the Writer API issue is resolved
  51. std::map<GUID_t, ReaderProxyData> clients_;
  52. public:
  53. /**
  54. * Constructor
  55. * @param builtin Pointer to the BuiltinProcols object.
  56. * @param allocation participant's allocation settings
  57. * @param durability_kind the kind of persistence we want for the discovery data
  58. */
  59. PDPServer(
  60. BuiltinProtocols* builtin,
  61. const RTPSParticipantAllocationAttributes& allocation,
  62. DurabilityKind_t durability_kind = TRANSIENT_LOCAL);
  63. ~PDPServer();
  64. void initializeParticipantProxyData(ParticipantProxyData* participant_data) override;
  65. /**
  66. * Initialize the PDP.
  67. * @param part Pointer to the RTPSParticipant.
  68. * @return True on success
  69. */
  70. bool init(RTPSParticipantImpl* part) override;
  71. /**
  72. * Creates an initializes a new participant proxy from a DATA(p) raw info
  73. * @param p ParticipantProxyData from DATA msg deserialization
  74. * @param writer_guid GUID of originating writer
  75. * @return new ParticipantProxyData * or nullptr on failure
  76. */
  77. ParticipantProxyData* createParticipantProxyData(
  78. const ParticipantProxyData& p,
  79. const GUID_t& writer_guid) override;
  80. /**
  81. * Create the SPDP Writer and Reader
  82. * @return True if correct.
  83. */
  84. bool createPDPEndpoints() override;
  85. /**
  86. * This method removes a remote RTPSParticipant and all its writers and readers.
  87. * @param participant_guid GUID_t of the remote RTPSParticipant.
  88. * @param reason Why the participant is being removed (dropped vs removed)
  89. * @return true if correct.
  90. */
  91. bool remove_remote_participant(
  92. const GUID_t& participant_guid,
  93. ParticipantDiscoveryInfo::DISCOVERY_STATUS reason) override;
  94. /**
  95. * Methods to update WriterHistory with reader information
  96. */
  97. /**
  98. * Some History data is flag for defer removal till every client
  99. * acknowledges reception
  100. * @return True if trimming must be done
  101. */
  102. bool pendingHistoryCleaning();
  103. /**
  104. *! Callback to remove unnecesary WriterHistory info from PDP and EDP
  105. * @return True if trimming is completed
  106. */
  107. bool trimWriterHistory();
  108. /**
  109. * Add participant CacheChange_ts from reader to writer
  110. * @param c metatraffic CacheChange_t
  111. * @return True if successfully modified WriterHistory
  112. */
  113. bool addRelayedChangeToHistory(CacheChange_t& c);
  114. /**
  115. * Trigger the participant CacheChange_t removal system
  116. * @param h instanceHandle associated with participants CacheChange_ts
  117. * @return True if successfully modified WriterHistory
  118. */
  119. void removeParticipantFromHistory(const InstanceHandle_t& h);
  120. /**
  121. * Methods to synchronize EDP matching
  122. */
  123. /**
  124. * Add a participant to the queue of pending participants to EDP matching
  125. * @param p ParticipantProxyData associated with the new participant
  126. */
  127. void queueParticipantForEDPMatch(const ParticipantProxyData* p);
  128. /**
  129. * Remove a participant from the queue of pending participants to EDP matching
  130. * @param guid GUID associated with the new participant
  131. */
  132. void removeParticipantForEDPMatch(const GUID_t& guid);
  133. /**
  134. * Check if all client have acknowledge the server PDP data
  135. * @return True if all clients known each other
  136. */
  137. bool all_clients_acknowledge_PDP();
  138. /**
  139. * Check if there are pending matches.
  140. * @return True if all participants EDP endpoints are already matched
  141. */
  142. inline bool pendingEDPMatches()
  143. {
  144. std::lock_guard<std::recursive_mutex> guardPDP(*mp_mutex);
  145. return !_p2match.empty();
  146. }
  147. //! Matches all clients EDP endpoints
  148. void match_all_clients_EDP_endpoints();
  149. /**
  150. * Methods to synchronize with another servers
  151. */
  152. /**
  153. * Check if all servers have acknowledge this server PDP data
  154. * This method must be called from a mutex protected context.
  155. * @return True if all can reach the client
  156. */
  157. bool all_servers_acknowledge_PDP();
  158. /**
  159. * Check if we have our PDP received data updated
  160. * This method must be called from a mutex protected context.
  161. * @return True if we known all the participants the servers are aware of
  162. */
  163. bool is_all_servers_PDPdata_updated();
  164. /**
  165. * Matching server EDP endpoints
  166. * @return true if all servers have been discovered
  167. */
  168. bool match_servers_EDP_endpoints();
  169. /**
  170. * Force the sending of our local PDP to all servers
  171. * @param new_change If true a new change (with new seqNum) is created and sent; if false the last change is re-sent
  172. * @param dispose Sets change kind to NOT_ALIVE_DISPOSED_UNREGISTERED
  173. * @param wparams allows to identify the change
  174. */
  175. void announceParticipantState(
  176. bool new_change,
  177. bool dispose = false,
  178. WriteParams& wparams = WriteParams::WRITE_PARAM_DEFAULT) override;
  179. /**
  180. * These methods wouldn't be needed under perfect server operation (no need of dynamic endpoint allocation)
  181. * but must be implemented to solve server shutdown situations.
  182. * @param pdata Pointer to the RTPSParticipantProxyData object.
  183. */
  184. void assignRemoteEndpoints(ParticipantProxyData* pdata) override;
  185. void removeRemoteEndpoints(ParticipantProxyData * pdata) override;
  186. void notifyAboveRemoteEndpoints(const ParticipantProxyData& pdata) override;
  187. #if HAVE_SQLITE3
  188. //! Get filename for persistence database file
  189. std::string GetPersistenceFileName();
  190. #endif
  191. //! returns true if loading info from persistency database
  192. bool ongoingDeserialization();
  193. //! Process the info recorded in the persistence database
  194. void processPersistentData();
  195. //! Wakes up the DServerEvent for new matching or trimming
  196. void awakeServerThread() { mp_sync->restart_timer(); }
  197. // The following struct and two methods solve a callback synchronization issue
  198. class InPDPCallback
  199. {
  200. friend class PDPServer;
  201. PDPServer & server_;
  202. public:
  203. InPDPCallback(PDPServer & svr);
  204. ~InPDPCallback();
  205. };
  206. // ! returns a unique_ptr to an object that handles PDP_callback_ in a RAII fashion
  207. std::unique_ptr<InPDPCallback> signalCallback();
  208. // ! calls PDP Reader matched_writer_remove preventing deadlocks
  209. bool safe_PDP_matched_writer_remove(const GUID_t& wguid);
  210. private:
  211. /**
  212. * Callback to remove unnecesary WriterHistory info from PDP alone
  213. * @return True if trimming is completed
  214. */
  215. bool trimPDPWriterHistory();
  216. /**
  217. * TimedEvent for server synchronization:
  218. * first stage: periodically resend the local RTPSParticipant information until all servers have acknowledge reception
  219. * second stage: waiting PDP info is up to date before allowing EDP matching
  220. */
  221. DServerEvent* mp_sync;
  222. // ! on PDP DATA(p[UD]) callback. Only modified by transport threads which are
  223. // serialized for PDP reader
  224. volatile bool PDP_callback_;
  225. };
  226. }
  227. } /* namespace rtps */
  228. } /* namespace eprosima */
  229. #endif
  230. #endif /* _FASTDDS_RTPS_PDPSERVER_H_ */