RTPSDomain.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 RTPSDomain.h
  16. */
  17. #ifndef _FASTDDS_RTPS_DOMAIN_H_
  18. #define _FASTDDS_RTPS_DOMAIN_H_
  19. #include <fastdds/rtps/common/Types.h>
  20. #include <fastdds/rtps/attributes/RTPSParticipantAttributes.h>
  21. #include <atomic>
  22. #include <mutex>
  23. #include <set>
  24. namespace eprosima {
  25. namespace fastrtps {
  26. namespace rtps {
  27. class RTPSParticipantImpl;
  28. class RTPSParticipant;
  29. class RTPSParticipantListener;
  30. class RTPSWriter;
  31. class WriterAttributes;
  32. class WriterHistory;
  33. class WriterListener;
  34. class RTPSReader;
  35. class ReaderAttributes;
  36. class ReaderHistory;
  37. class ReaderListener;
  38. class RTPSDomainImpl;
  39. /**
  40. * Class RTPSDomain,it manages the creation and destruction of RTPSParticipant RTPSWriter and RTPSReader. It stores
  41. * a list of all created RTPSParticipant. Is has only static methods.
  42. * @ingroup RTPS_MODULE
  43. */
  44. class RTPSDomain
  45. {
  46. friend class RTPSDomainImpl;
  47. public:
  48. /**
  49. * Method to shut down all RTPSParticipants, readers, writers, etc.
  50. * It must be called at the end of the process to avoid memory leaks.
  51. * It also shut downs the DomainRTPSParticipant.
  52. */
  53. RTPS_DllAPI static void stopAll();
  54. /**
  55. * @brief Create a RTPSParticipant.
  56. * @param domain_id DomainId to be used by the RTPSParticipant (80 by default).
  57. * @param attrs RTPSParticipant Attributes.
  58. * @param plisten Pointer to the ParticipantListener.
  59. * @return Pointer to the RTPSParticipant.
  60. */
  61. RTPS_DllAPI static RTPSParticipant* createParticipant(
  62. uint32_t domain_id,
  63. const RTPSParticipantAttributes& attrs,
  64. RTPSParticipantListener* plisten = nullptr);
  65. /**
  66. * @brief Create a RTPSParticipant.
  67. * @param domain_id DomainId to be used by the RTPSParticipant (80 by default).
  68. * @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable()
  69. * @param attrs RTPSParticipant Attributes.
  70. * @param plisten Pointer to the ParticipantListener.
  71. * @return Pointer to the RTPSParticipant.
  72. */
  73. RTPS_DllAPI static RTPSParticipant* createParticipant(
  74. uint32_t domain_id,
  75. bool enabled,
  76. const RTPSParticipantAttributes& attrs,
  77. RTPSParticipantListener* plisten = nullptr);
  78. /**
  79. * Create a RTPSWriter in a participant.
  80. * @param p Pointer to the RTPSParticipant.
  81. * @param watt Writer Attributes.
  82. * @param hist Pointer to the WriterHistory.
  83. * @param listen Pointer to the WriterListener.
  84. * @return Pointer to the created RTPSWriter.
  85. */
  86. RTPS_DllAPI static RTPSWriter* createRTPSWriter(
  87. RTPSParticipant* p,
  88. WriterAttributes& watt,
  89. WriterHistory* hist,
  90. WriterListener* listen = nullptr);
  91. /**
  92. * Remove a RTPSWriter.
  93. * @param writer Pointer to the writer you want to remove.
  94. * @return True if correctly removed.
  95. */
  96. RTPS_DllAPI static bool removeRTPSWriter(
  97. RTPSWriter* writer);
  98. /**
  99. * Create a RTPSReader in a participant.
  100. * @param p Pointer to the RTPSParticipant.
  101. * @param ratt Reader Attributes.
  102. * @param hist Pointer to the ReaderHistory.
  103. * @param listen Pointer to the ReaderListener.
  104. * @return Pointer to the created RTPSReader.
  105. */
  106. RTPS_DllAPI static RTPSReader* createRTPSReader(
  107. RTPSParticipant* p,
  108. ReaderAttributes& ratt,
  109. ReaderHistory* hist,
  110. ReaderListener* listen = nullptr);
  111. /**
  112. * Remove a RTPSReader.
  113. * @param reader Pointer to the reader you want to remove.
  114. * @return True if correctly removed.
  115. */
  116. RTPS_DllAPI static bool removeRTPSReader(
  117. RTPSReader* reader);
  118. /**
  119. * Remove a RTPSParticipant and delete all its associated Writers, Readers, resources, etc.
  120. * @param[in] p Pointer to the RTPSParticipant;
  121. * @return True if correct.
  122. */
  123. RTPS_DllAPI static bool removeRTPSParticipant(
  124. RTPSParticipant* p);
  125. /**
  126. * Set the maximum RTPSParticipantID.
  127. * @param maxRTPSParticipantId ID.
  128. */
  129. static inline void setMaxRTPSParticipantId(
  130. uint32_t maxRTPSParticipantId)
  131. {
  132. m_maxRTPSParticipantID = maxRTPSParticipantId;
  133. }
  134. /**
  135. * Creates a RTPSParticipant as default server or client if ROS_MASTER_URI environment variable is set.
  136. * @param domain_id DDS domain associated
  137. * @param attrs RTPSParticipant Attributes.
  138. * @param listen Pointer to the ParticipantListener.
  139. * @return Pointer to the RTPSParticipant.
  140. */
  141. static RTPSParticipant* clientServerEnvironmentCreationOverride(
  142. uint32_t domain_id,
  143. const RTPSParticipantAttributes& attrs,
  144. RTPSParticipantListener* listen /*= nullptr*/);
  145. private:
  146. typedef std::pair<RTPSParticipant*, RTPSParticipantImpl*> t_p_RTPSParticipant;
  147. RTPSDomain() = delete;
  148. /**
  149. * DomainRTPSParticipant destructor
  150. */
  151. ~RTPSDomain() = delete;
  152. /**
  153. * @brief Get Id to create a RTPSParticipant.
  154. * @return Different ID for each call.
  155. */
  156. static inline uint32_t getNewId()
  157. {
  158. return m_maxRTPSParticipantID++;
  159. }
  160. static void removeRTPSParticipant_nts(
  161. t_p_RTPSParticipant&);
  162. static std::mutex m_mutex;
  163. static std::atomic<uint32_t> m_maxRTPSParticipantID;
  164. static std::vector<t_p_RTPSParticipant> m_RTPSParticipants;
  165. static std::set<uint32_t> m_RTPSParticipantIDs;
  166. };
  167. }
  168. } /* namespace */
  169. } /* namespace eprosima */
  170. #endif /* _FASTDDS_RTPS_DOMAIN_H_*/