Publisher.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Publisher.hpp
  16. *
  17. */
  18. #ifndef _FASTDDS_PUBLISHER_HPP_
  19. #define _FASTDDS_PUBLISHER_HPP_
  20. #include <fastrtps/fastrtps_dll.h>
  21. #include <fastdds/rtps/common/Time_t.h>
  22. #include <fastrtps/attributes/PublisherAttributes.h>
  23. #include <fastrtps/types/TypesBase.h>
  24. #include <fastdds/dds/core/Entity.hpp>
  25. #include <fastdds/dds/publisher/qos/PublisherQos.hpp>
  26. #include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
  27. using eprosima::fastrtps::types::ReturnCode_t;
  28. namespace dds {
  29. namespace pub {
  30. class Publisher;
  31. }
  32. }
  33. namespace eprosima {
  34. namespace fastrtps {
  35. class TopicAttributes;
  36. } // namespace fastrtps
  37. namespace fastdds {
  38. namespace dds {
  39. class DomainParticipant;
  40. class PublisherListener;
  41. class PublisherImpl;
  42. class DataWriter;
  43. class DataWriterListener;
  44. class Topic;
  45. /**
  46. * Class Publisher, used to send data to associated subscribers.
  47. * @ingroup FASTDDS_MODULE
  48. */
  49. class Publisher : public DomainEntity
  50. {
  51. friend class PublisherImpl;
  52. friend class DomainParticipantImpl;
  53. /**
  54. * Create a publisher, assigning its pointer to the associated writer.
  55. * Don't use directly, create Publisher using create_publisher from Participant.
  56. */
  57. RTPS_DllAPI Publisher(
  58. PublisherImpl* p,
  59. const StatusMask& mask = StatusMask::all());
  60. RTPS_DllAPI Publisher(
  61. DomainParticipant* dp,
  62. const PublisherQos& qos = PUBLISHER_QOS_DEFAULT,
  63. PublisherListener* listener = nullptr,
  64. const StatusMask& mask = StatusMask::all());
  65. public:
  66. /**
  67. * @brief Destructor
  68. */
  69. RTPS_DllAPI virtual ~Publisher();
  70. /**
  71. * @brief This operation enables the Publisher
  72. * @return RETCODE_OK is successfully enabled. RETCODE_PRECONDITION_NOT_MET if the participant creating this
  73. * Publisher is not enabled.
  74. */
  75. RTPS_DllAPI ReturnCode_t enable() override;
  76. /**
  77. * Allows accessing the Publisher Qos.
  78. * @return PublisherQos reference
  79. */
  80. RTPS_DllAPI const PublisherQos& get_qos() const;
  81. /**
  82. * Retrieves the Publisher Qos.
  83. * @return RETCODE_OK
  84. */
  85. RTPS_DllAPI ReturnCode_t get_qos(
  86. PublisherQos& qos) const;
  87. /**
  88. * Allows modifying the Publisher Qos.
  89. * The given Qos must be supported by the PublisherQos.
  90. * @param qos PublisherQos to be set
  91. * @return RETCODE_IMMUTABLE_POLICY if any of the Qos cannot be changed, RETCODE_INCONSISTENT_POLICY if the Qos is not
  92. * self consistent and RETCODE_OK if the qos is changed correctly.
  93. */
  94. RTPS_DllAPI ReturnCode_t set_qos(
  95. const PublisherQos& qos);
  96. /**
  97. * Retrieves the attached PublisherListener.
  98. * @return PublisherListener pointer
  99. */
  100. RTPS_DllAPI const PublisherListener* get_listener() const;
  101. /**
  102. * Modifies the PublisherListener.
  103. * @param listener PublisherListener pointer
  104. * @return RETCODE_OK
  105. */
  106. RTPS_DllAPI ReturnCode_t set_listener(
  107. PublisherListener* listener);
  108. /**
  109. * This operation creates a DataWriter. The returned DataWriter will be attached and belongs to the Publisher.
  110. * @param topic Topic the DataWriter will be listening
  111. * @param qos QoS of the DataWriter.
  112. * @param listener Pointer to the listener (default: nullptr).
  113. * @param mask StatusMask (default: all).
  114. * @return Pointer to the created DataWriter. nullptr if failed.
  115. */
  116. RTPS_DllAPI DataWriter* create_datawriter(
  117. Topic* topic,
  118. const DataWriterQos& qos,
  119. DataWriterListener* listener = nullptr,
  120. const StatusMask& mask = StatusMask::all());
  121. /**
  122. * This operation creates a DataWriter. The returned DataWriter will be attached and belongs to the Publisher.
  123. * @param topic Topic the DataWriter will be listening
  124. * @param profile_name DataWriter profile name.
  125. * @param listener Pointer to the listener (default: nullptr).
  126. * @param mask StatusMask (default: all).
  127. * @return Pointer to the created DataWriter. nullptr if failed.
  128. */
  129. RTPS_DllAPI DataWriter* create_datawriter_with_profile(
  130. Topic* topic,
  131. const std::string& profile_name,
  132. DataWriterListener* listener = nullptr,
  133. const StatusMask& mask = StatusMask::all());
  134. /**
  135. * This operation deletes a DataWriter that belongs to the Publisher.
  136. *
  137. * The delete_datawriter operation must be called on the same Publisher object used to create the DataWriter.
  138. * If delete_datawriter is called on a different Publisher, the operation will have no effect and it will
  139. * return false.
  140. *
  141. * The deletion of the DataWriter will automatically unregister all instances.
  142. * Depending on the settings of the WRITER_DATA_LIFECYCLE QosPolicy, the deletion of the DataWriter
  143. * may also dispose all instances.
  144. * @param writer DataWriter to delete
  145. * @return RETCODE_PRECONDITION_NOT_MET if it does not belong to this Publisher, RETCODE_OK if it is correctly deleted and
  146. * RETCODE_ERROR otherwise.
  147. */
  148. RTPS_DllAPI ReturnCode_t delete_datawriter(
  149. DataWriter* writer);
  150. /**
  151. * This operation retrieves a previously created DataWriter belonging to the Publisher that is attached to a
  152. * Topic with a matching topic_name. If no such DataWriter exists, the operation will return nullptr.
  153. *
  154. * If multiple DataWriter attached to the Publisher satisfy this condition, then the operation will return
  155. * one of them. It is not specified which one.
  156. * @param topic_name Name of the Topic
  157. * @return Pointer to a previously created DataWriter associated to a Topic with the requested topic_name
  158. */
  159. RTPS_DllAPI DataWriter* lookup_datawriter(
  160. const std::string& topic_name) const;
  161. /**
  162. * Fills the given vector with all the datawriters of this publisher.
  163. * @param writers Vector where the DataWriters are returned
  164. * @return true
  165. */
  166. RTPS_DllAPI bool get_datawriters(
  167. std::vector<DataWriter*>& writers) const;
  168. /**
  169. * This operation checks if the publisher has DataWriters
  170. * @return true if the publisher has one or several DataWriters, false otherwise
  171. */
  172. RTPS_DllAPI bool has_datawriters() const;
  173. /* TODO
  174. bool suspend_publications();
  175. */
  176. /* TODO
  177. bool resume_publications();
  178. */
  179. /* TODO
  180. bool begin_coherent_changes();
  181. */
  182. /* TODO
  183. bool end_coherent_changes();
  184. */
  185. /**
  186. * This operation blocks the calling thread until either all data written by the reliable DataWriter entities
  187. * is acknowledged by all matched reliable DataReader entities, or else the duration specified by the max_wait
  188. * parameter elapses, whichever happens first. A return value of true indicates that all the samples written
  189. * have been acknowledged by all reliable matched data readers; a return value of false indicates that max_wait
  190. * elapsed before all the data was acknowledged.
  191. * @param max_wait Maximum blocking time for this operation
  192. * @return RETCODE_TIMEOUT if the function takes more than the maximum blocking time established, RETCODE_OK if the
  193. * Publisher receives the acknowledgments and RETCODE_ERROR otherwise.
  194. */
  195. RTPS_DllAPI ReturnCode_t wait_for_acknowledgments(
  196. const fastrtps::Duration_t& max_wait);
  197. /**
  198. * This operation returns the DomainParticipant to which the Publisher belongs.
  199. * @return Pointer to the DomainParticipant
  200. */
  201. RTPS_DllAPI const DomainParticipant* get_participant() const;
  202. /* TODO
  203. bool delete_contained_entities();
  204. */
  205. /**
  206. * This operation sets a default value of the DataWriter QoS policies which will be used for newly created
  207. * DataWriter entities in the case where the QoS policies are defaulted in the create_datawriter operation.
  208. *
  209. * This operation will check that the resulting policies are self consistent; if they are not, the operation
  210. * will have no effect and return false.
  211. *
  212. * The special value DATAWRITER_QOS_DEFAULT may be passed to this operation to indicate that the default QoS
  213. * should be reset back to the initial values the factory would use, that is the values that would be used
  214. * if the set_default_datawriter_qos operation had never been called.
  215. * @param qos DataWriterQos to be set
  216. * @return RETCODE_INCONSISTENT_POLICY if the Qos is not self consistent and RETCODE_OK if the qos is changed correctly.
  217. */
  218. RTPS_DllAPI ReturnCode_t set_default_datawriter_qos(
  219. const DataWriterQos& qos);
  220. /**
  221. * This operation returns the default value of the DataWriter QoS, that is, the QoS policies which will be used
  222. * for newly created DataWriter entities in the case where the QoS policies are defaulted in the
  223. * create_datawriter operation.
  224. *
  225. * The values retrieved by get_default_datawriter_qos will match the set of values specified on the last
  226. * successful call to set_default_datawriter_qos, or else, if the call was never made, the default values.
  227. * @return Current default WriterQos
  228. */
  229. RTPS_DllAPI const DataWriterQos& get_default_datawriter_qos() const;
  230. /**
  231. * This operation retrieves the default value of the DataWriter QoS, that is, the QoS policies which will be used
  232. * for newly created DataWriter entities in the case where the QoS policies are defaulted in the
  233. * create_datawriter operation.
  234. *
  235. * The values retrieved by get_default_datawriter_qos will match the set of values specified on the last
  236. * successful call to set_default_datawriter_qos, or else, if the call was never made, the default values.
  237. * @param qos Reference to the current default WriterQos.
  238. * @return RETCODE_OK
  239. */
  240. RTPS_DllAPI ReturnCode_t get_default_datawriter_qos(
  241. DataWriterQos& qos) const;
  242. /* TODO
  243. bool copy_from_topic_qos(
  244. WriterQos& writer_qos,
  245. const fastrtps::TopicAttributes& topic_qos) const;
  246. */
  247. /**
  248. * Returns the Publisher's handle.
  249. * @return InstanceHandle of this Publisher.
  250. */
  251. RTPS_DllAPI const fastrtps::rtps::InstanceHandle_t& get_instance_handle() const;
  252. private:
  253. PublisherImpl* impl_;
  254. friend class ::dds::pub::Publisher;
  255. };
  256. } /* namespace dds */
  257. } /* namespace fastdds */
  258. } /* namespace eprosima */
  259. #endif /* _FASTDDS_PUBLISHER_HPP_ */