Subscriber.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 Subscriber.hpp
  16. */
  17. #ifndef _FASTDDS_SUBSCRIBER_HPP_
  18. #define _FASTDDS_SUBSCRIBER_HPP_
  19. #include <fastrtps/attributes/SubscriberAttributes.h>
  20. #include <fastdds/dds/subscriber/DataReaderListener.hpp>
  21. #include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
  22. #include <fastrtps/types/TypesBase.h>
  23. #include <fastdds/dds/core/Entity.hpp>
  24. using eprosima::fastrtps::types::ReturnCode_t;
  25. namespace dds {
  26. namespace sub {
  27. class Subscriber;
  28. } // namespace sub
  29. } // namespace dds
  30. namespace eprosima {
  31. namespace fastrtps {
  32. class TopicAttributes;
  33. } // namespace fastrtps
  34. namespace fastdds {
  35. namespace dds {
  36. class DomainParticipant;
  37. class SubscriberListener;
  38. class SubscriberImpl;
  39. class DataReader;
  40. class DataReaderListener;
  41. class DataReaderQos;
  42. class TopicDescription;
  43. /**
  44. * Class Subscriber, contains the public API that allows the user to control the reception of messages.
  45. * This class should not be instantiated directly.
  46. * DomainRTPSParticipant class should be used to correctly create this element.
  47. * @ingroup FASTDDS_MODULE
  48. */
  49. class Subscriber : public DomainEntity
  50. {
  51. friend class SubscriberImpl;
  52. friend class DomainParticipantImpl;
  53. /**
  54. * Constructor from a SubscriberImpl pointer
  55. * @param pimpl Actual implementation of the subscriber
  56. * @param mask StatusMask (default: all)
  57. */
  58. RTPS_DllAPI Subscriber(
  59. SubscriberImpl* pimpl,
  60. const StatusMask& mask = StatusMask::all());
  61. RTPS_DllAPI Subscriber(
  62. DomainParticipant* dp,
  63. const SubscriberQos& qos = SUBSCRIBER_QOS_DEFAULT,
  64. SubscriberListener* listener = nullptr,
  65. const StatusMask& mask = StatusMask::all());
  66. public:
  67. /**
  68. * @brief Destructor
  69. */
  70. RTPS_DllAPI virtual ~Subscriber()
  71. {
  72. }
  73. /**
  74. * @brief This operation enables the Subscriber
  75. * @return RETCODE_OK is successfully enabled. RETCODE_PRECONDITION_NOT_MET if the participant creating this
  76. * Subscriber is not enabled.
  77. */
  78. RTPS_DllAPI ReturnCode_t enable() override;
  79. /**
  80. * Allows accessing the Subscriber Qos.
  81. * @return SubscriberQos reference
  82. */
  83. RTPS_DllAPI const SubscriberQos& get_qos() const;
  84. /**
  85. * Retrieves the Subscriber Qos.
  86. * @param qos SubscriberQos where the qos is returned
  87. * @return RETCODE_OK
  88. */
  89. RTPS_DllAPI ReturnCode_t get_qos(
  90. SubscriberQos& qos) const;
  91. /**
  92. * Allows modifying the Subscriber Qos.
  93. * The given Qos must be supported by the SubscriberQos.
  94. * @param qos new value for SubscriberQos
  95. * @return RETCODE_IMMUTABLE_POLICY if any of the Qos cannot be changed, RETCODE_INCONSISTENT_POLICY if the Qos is not
  96. * self consistent and RETCODE_OK if the qos is changed correctly.
  97. */
  98. RTPS_DllAPI ReturnCode_t set_qos(
  99. const SubscriberQos& qos);
  100. /**
  101. * Retrieves the attached SubscriberListener.
  102. * @return Pointer to the SubscriberListener
  103. */
  104. RTPS_DllAPI const SubscriberListener* get_listener() const;
  105. /**
  106. * Modifies the SubscriberListener.
  107. * @param listener new value for SubscriberListener
  108. * @return RETCODE_OK
  109. */
  110. RTPS_DllAPI ReturnCode_t set_listener(
  111. SubscriberListener* listener);
  112. /**
  113. * This operation creates a DataReader. The returned DataReader will be attached and belong to the Subscriber.
  114. * @param topic Topic the DataReader will be listening.
  115. * @param reader_qos QoS of the DataReader.
  116. * @param listener Pointer to the listener (default: nullptr)
  117. * @param mask StatusMask (default: all)
  118. * @return Pointer to the created DataReader. nullptr if failed.
  119. */
  120. RTPS_DllAPI DataReader* create_datareader(
  121. TopicDescription* topic,
  122. const DataReaderQos& reader_qos,
  123. DataReaderListener* listener = nullptr,
  124. const StatusMask& mask = StatusMask::all());
  125. /**
  126. * This operation creates a DataReader. The returned DataReader will be attached and belongs to the Subscriber.
  127. * @param topic Topic the DataReader will be listening.
  128. * @param profile_name DataReader profile name.
  129. * @param listener Pointer to the listener (default: nullptr)
  130. * @param mask StatusMask (default: all)
  131. * @return Pointer to the created DataReader. nullptr if failed.
  132. */
  133. RTPS_DllAPI DataReader* create_datareader_with_profile(
  134. TopicDescription* topic,
  135. const std::string& profile_name,
  136. DataReaderListener* listener = nullptr,
  137. const StatusMask& mask = StatusMask::all());
  138. /**
  139. * This operation deletes a DataReader that belongs to the Subscriber.
  140. *
  141. * The delete_datareader operation must be called on the same Subscriber object used to create the DataReader.
  142. * If delete_datareader is called on a different Subscriber, the operation will have no effect and it will
  143. * return an error.
  144. * @param reader DataReader to delete
  145. * @return RETCODE_PRECONDITION_NOT_MET if the datareader does not belong to this subscriber, RETCODE_OK if it is correctly
  146. * deleted and RETCODE_ERROR otherwise.
  147. */
  148. RTPS_DllAPI ReturnCode_t delete_datareader(
  149. DataReader* reader);
  150. /**
  151. * This operation retrieves a previously-created DataReader belonging to the Subscriber that is attached to a
  152. * Topic with a matching topic_name. If no such DataReader exists, the operation will return nullptr.
  153. *
  154. * If multiple DataReaders attached to the Subscriber 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 associated to the DataReader
  157. * @return Pointer to a previously created DataReader created on a Topic with that topic_name
  158. */
  159. RTPS_DllAPI DataReader* lookup_datareader(
  160. const std::string& topic_name) const;
  161. /**
  162. * This operation allows the application to access the DataReader objects.
  163. * @param readers Vector of DataReader where the list of existing readers is returned
  164. * @return RETCODE_OK
  165. */
  166. RTPS_DllAPI ReturnCode_t get_datareaders(
  167. std::vector<DataReader*>& readers) const;
  168. /**
  169. * This operation checks if the subscriber has DataReaders
  170. * @return true if the subscriber has one or several DataReaders, false in other case
  171. */
  172. RTPS_DllAPI bool has_datareaders() const;
  173. /* TODO
  174. bool begin_access();
  175. */
  176. /* TODO
  177. bool end_access();
  178. */
  179. /**
  180. * This operation invokes the operation on_data_available on the DataReaderListener objects attached to
  181. * contained DataReader entities.
  182. *
  183. * This operation is typically invoked from the on_data_on_readers operation in the SubscriberListener.
  184. * That way the SubscriberListener can delegate to the DataReaderListener objects the handling of the data.
  185. * @return RETCODE_OK
  186. */
  187. RTPS_DllAPI ReturnCode_t notify_datareaders() const;
  188. /* TODO
  189. bool delete_contained_entities();
  190. */
  191. /**
  192. * This operation sets a default value of the DataReader QoS policies which will be used for newly created
  193. * DataReader entities in the case where the QoS policies are defaulted in the create_datareader operation.
  194. *
  195. * This operation will check that the resulting policies are self consistent; if they are not, the operation
  196. * will have no effect and return false.
  197. *
  198. * The special value DATAREADER_QOS_DEFAULT may be passed to this operation to indicate that the default QoS
  199. * should be reset back to the initial values the factory would use, that is the values that would be used
  200. * if the set_default_datareader_qos operation had never been called.
  201. * @param qos new value for DataReaderQos to set as default
  202. * @return RETCODE_INCONSISTENT_POLICY if the Qos is not self consistent and RETCODE_OK if the qos is changed correctly.
  203. */
  204. RTPS_DllAPI ReturnCode_t set_default_datareader_qos(
  205. const DataReaderQos& qos);
  206. /**
  207. * This operation returns the default value of the DataReader QoS, that is, the QoS policies which will be
  208. * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
  209. * create_datareader operation.
  210. *
  211. * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
  212. * call to get_default_datareader_qos, or else, if the call was never made, the default values.
  213. * @return Current default DataReaderQos.
  214. */
  215. RTPS_DllAPI const DataReaderQos& get_default_datareader_qos() const;
  216. /**
  217. * This operation returns the default value of the DataReader QoS, that is, the QoS policies which will be
  218. * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
  219. * create_datareader operation.
  220. *
  221. * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
  222. * call to get_default_datareader_qos, or else, if the call was never made, the default values.
  223. * @return Current default DataReaderQos.
  224. */
  225. RTPS_DllAPI DataReaderQos& get_default_datareader_qos();
  226. /**
  227. * This operation retrieves the default value of the DataReader QoS, that is, the QoS policies which will be
  228. * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
  229. * create_datareader operation.
  230. *
  231. * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
  232. * call to get_default_datareader_qos, or else, if the call was never made, the default values.
  233. * @param qos DataReaderQos where the default_qos is returned
  234. * @return RETCODE_OK
  235. */
  236. RTPS_DllAPI ReturnCode_t get_default_datareader_qos(
  237. DataReaderQos& qos) const;
  238. /* TODO
  239. bool copy_from_topic_qos(
  240. DataReaderQos& reader_qos,
  241. const fastrtps::TopicAttributes& topic_qos) const;
  242. */
  243. /**
  244. * This operation returns the DomainParticipant to which the Subscriber belongs.
  245. * @return DomainParticipant Pointer
  246. */
  247. RTPS_DllAPI const DomainParticipant* get_participant() const;
  248. /**
  249. * Returns the Subscriber's handle.
  250. * @return InstanceHandle of this Subscriber.
  251. */
  252. RTPS_DllAPI const fastrtps::rtps::InstanceHandle_t& get_instance_handle() const;
  253. private:
  254. SubscriberImpl* impl_;
  255. friend class ::dds::sub::Subscriber;
  256. };
  257. } /* namespace dds */
  258. } /* namespace fastdds */
  259. } /* namespace eprosima */
  260. #endif /* _FASTDDS_SUBSCRIBER_HPP_ */