Subscriber.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 Subscriber.h
  16. */
  17. #ifndef SUBSCRIBER_H_
  18. #define SUBSCRIBER_H_
  19. #include <fastdds/rtps/common/Guid.h>
  20. #include <fastdds/rtps/common/Time_t.h>
  21. #include <fastrtps/attributes/SubscriberAttributes.h>
  22. #include <fastrtps/qos/DeadlineMissedStatus.h>
  23. #include <fastrtps/qos/LivelinessChangedStatus.h>
  24. namespace eprosima {
  25. namespace fastrtps {
  26. class SubscriberImpl;
  27. class SampleInfo_t;
  28. /**
  29. * Class Subscriber, contains the public API that allows the user to control the reception of messages.
  30. * This class should not be instantiated directly.
  31. * DomainRTPSParticipant class should be used to correctly create this element.
  32. * @ingroup FASTRTPS_MODULE
  33. * @snippet fastrtps_example.cpp ex_Subscriber
  34. */
  35. class RTPS_DllAPI Subscriber
  36. {
  37. friend class SubscriberImpl;
  38. virtual ~Subscriber()
  39. {
  40. }
  41. public:
  42. /**
  43. * Constructor from a SubscriberImpl pointer
  44. * @param pimpl Actual implementation of the subscriber
  45. */
  46. Subscriber(
  47. SubscriberImpl* pimpl)
  48. : mp_impl(pimpl)
  49. {
  50. }
  51. /**
  52. * Get the associated GUID
  53. * @return Associated GUID
  54. */
  55. const rtps::GUID_t& getGuid();
  56. /**
  57. * Method to block the current thread until an unread message is available
  58. */
  59. inline void waitForUnreadMessage()
  60. {
  61. const Duration_t one_day{ 24 * 3600, 0 };
  62. while (!wait_for_unread_samples(one_day))
  63. {
  64. }
  65. }
  66. /*!
  67. * @brief Blocks the current thread until an unread sample is available.
  68. * @param timeout Maximum time the function will be blocked if any sample is received.
  69. * @return true in case unread samples are available.
  70. * In other case, false.
  71. */
  72. bool wait_for_unread_samples(
  73. const Duration_t& timeout);
  74. /**
  75. * @brief Reads next unread sample from the Subscriber.
  76. * @param sample Pointer to the object where you want the sample stored.
  77. * @param info Pointer to a SampleInfo_t structure that informs you about your sample.
  78. * @return True if a sample was read.
  79. * @note This method is blocked for a period of time.
  80. * ReliabilityQosPolicy.max_blocking_time on SubscriberAttributes defines this period of time.
  81. */
  82. bool readNextData(
  83. void* sample,
  84. SampleInfo_t* info);
  85. /**
  86. * @brief Takes next sample from the Subscriber. The sample is removed from the subscriber.
  87. * @param sample Pointer to the object where you want the sample stored.
  88. * @param info Pointer to a SampleInfo_t structure that informs you about your sample.
  89. * @return True if a sample was taken.
  90. * @note This method is blocked for a period of time.
  91. * ReliabilityQosPolicy.max_blocking_time on SubscriberAttributes defines this period of time.
  92. */
  93. bool takeNextData(
  94. void* sample,
  95. SampleInfo_t* info);
  96. /**
  97. * @brief Returns information about the first untaken sample.
  98. * @param [out] info Pointer to a SampleInfo_t structure to store first untaken sample information.
  99. * @return true if sample info was returned. false if there is no sample to take.
  100. */
  101. bool get_first_untaken_info(
  102. SampleInfo_t* info);
  103. /**
  104. * Update the Attributes of the subscriber;
  105. * @param att Reference to a SubscriberAttributes object to update the parameters;
  106. * @return True if correctly updated, false if ANY of the updated parameters cannot be updated
  107. */
  108. bool updateAttributes(
  109. const SubscriberAttributes& att);
  110. /**
  111. * Get the Attributes of the Subscriber.
  112. * @return Attributes of the subscriber
  113. */
  114. const SubscriberAttributes& getAttributes() const;
  115. /*!
  116. * @brief Returns there is a clean state with all Publishers.
  117. * It occurs when the Subscriber received all samples sent by Publishers. In other words,
  118. * its WriterProxies are up to date.
  119. * @return There is a clean state with all Publishers.
  120. */
  121. bool isInCleanState() const;
  122. /**
  123. * Get the unread count.
  124. * @return Unread count
  125. */
  126. inline uint64_t getUnreadCount() const
  127. {
  128. return get_unread_count();
  129. }
  130. /**
  131. * Get the unread count.
  132. * @return Unread count
  133. */
  134. uint64_t get_unread_count() const;
  135. /**
  136. * @brief Get the requested deadline missed status
  137. * @param status The deadline missed status
  138. */
  139. void get_requested_deadline_missed_status(
  140. RequestedDeadlineMissedStatus& status);
  141. /**
  142. * @brief Returns the liveliness changed status
  143. * @param status Liveliness changed status
  144. */
  145. void get_liveliness_changed_status(
  146. LivelinessChangedStatus& status);
  147. private:
  148. SubscriberImpl* mp_impl;
  149. };
  150. } /* namespace pubsub */
  151. } /* namespace eprosima */
  152. #endif /* SUBSCRIBER_H_ */