SubscriberHistory.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 SubscriberHistory.h
  16. *
  17. */
  18. #ifndef SUBSCRIBERHISTORY_H_
  19. #define SUBSCRIBERHISTORY_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <fastdds/dds/topic/TopicDataType.hpp>
  22. #include <fastdds/rtps/resources/ResourceManagement.h>
  23. #include <fastrtps/qos/ReaderQos.h>
  24. #include <fastdds/rtps/history/ReaderHistory.h>
  25. #include <fastrtps/qos/QosPolicies.h>
  26. #include <fastrtps/common/KeyedChanges.h>
  27. #include <fastrtps/subscriber/SampleInfo.h>
  28. #include <fastrtps/attributes/TopicAttributes.h>
  29. #include <chrono>
  30. #include <functional>
  31. namespace eprosima {
  32. namespace fastrtps {
  33. /**
  34. * Class SubscriberHistory, container of the different CacheChanges of a subscriber
  35. * @ingroup FASTRTPS_MODULE
  36. */
  37. class SubscriberHistory : public rtps::ReaderHistory
  38. {
  39. public:
  40. /**
  41. * Constructor. Requires information about the subscriber.
  42. * @param topic_att TopicAttributes.
  43. * @param type TopicDataType.
  44. * @param qos ReaderQoS policy.
  45. * @param payloadMax Maximum payload size per change.
  46. * @param mempolicy Set wether the payloads ccan dynamically resized or not.
  47. */
  48. SubscriberHistory(
  49. const TopicAttributes& topic_att,
  50. fastdds::dds::TopicDataType* type,
  51. const fastrtps::ReaderQos& qos,
  52. uint32_t payloadMax,
  53. rtps::MemoryManagementPolicy_t mempolicy);
  54. virtual ~SubscriberHistory();
  55. /**
  56. * Called when a change is received by the Subscriber. Will add the change to the history.
  57. * @pre Change should not be already present in the history.
  58. * @param[in] change The received change
  59. * @param unknown_missing_changes_up_to Number of missing changes before this one
  60. * @return
  61. */
  62. bool received_change(
  63. rtps::CacheChange_t* change,
  64. size_t unknown_missing_changes_up_to);
  65. /** @name Read or take data methods.
  66. * Methods to read or take data from the History.
  67. * @param data Pointer to the object where you want to read or take the information.
  68. * @param info Pointer to a SampleInfo_t object where you want
  69. * @param max_blocking_time Maximum time the function can be blocked.
  70. * to store the information about the retrieved data
  71. */
  72. ///@{
  73. bool readNextData(
  74. void* data,
  75. SampleInfo_t* info,
  76. std::chrono::steady_clock::time_point& max_blocking_time);
  77. bool takeNextData(
  78. void* data,
  79. SampleInfo_t* info,
  80. std::chrono::steady_clock::time_point& max_blocking_time);
  81. ///@}
  82. /**
  83. * @brief Returns information about the first untaken sample.
  84. * @param [out] info Pointer to a SampleInfo_t structure to store first untaken sample information.
  85. * @return true if sample info was returned. false if there is no sample to take.
  86. */
  87. bool get_first_untaken_info(
  88. SampleInfo_t* info);
  89. /**
  90. * This method is called to remove a change from the SubscriberHistory.
  91. * @param change Pointer to the CacheChange_t.
  92. * @return True if removed.
  93. */
  94. bool remove_change_sub(
  95. rtps::CacheChange_t* change);
  96. /**
  97. * @brief A method to set the next deadline for the given instance
  98. * @param handle The handle to the instance
  99. * @param next_deadline_us The time point when the deadline will occur
  100. * @return True if the deadline was set correctly
  101. */
  102. bool set_next_deadline(
  103. const rtps::InstanceHandle_t& handle,
  104. const std::chrono::steady_clock::time_point& next_deadline_us);
  105. /**
  106. * @brief A method to get the next instance handle that will miss the deadline and the time when the deadline will occur
  107. * @param handle The handle to the instance
  108. * @param next_deadline_us The time point when the instance will miss the deadline
  109. * @return True if the deadline was retrieved successfully
  110. */
  111. bool get_next_deadline(
  112. rtps::InstanceHandle_t& handle,
  113. std::chrono::steady_clock::time_point& next_deadline_us);
  114. private:
  115. using t_m_Inst_Caches = std::map<rtps::InstanceHandle_t, KeyedChanges>;
  116. //!Map where keys are instance handles and values vectors of cache changes
  117. t_m_Inst_Caches keyed_changes_;
  118. //!Time point when the next deadline will occur (only used for topics with no key)
  119. std::chrono::steady_clock::time_point next_deadline_us_;
  120. //!HistoryQosPolicy values.
  121. HistoryQosPolicy history_qos_;
  122. //!ResourceLimitsQosPolicy values.
  123. ResourceLimitsQosPolicy resource_limited_qos_;
  124. //!Topic Attributes
  125. TopicAttributes topic_att_;
  126. //!TopicDataType
  127. fastdds::dds::TopicDataType* type_;
  128. //!ReaderQos
  129. fastrtps::ReaderQos qos_;
  130. //!Type object to deserialize Key
  131. void* get_key_object_;
  132. /// Function processing a received change
  133. std::function<bool(rtps::CacheChange_t*, size_t)> receive_fn_;
  134. /**
  135. * @brief Method that finds a key in m_keyedChanges or tries to add it if not found
  136. * @param a_change The change to get the key from
  137. * @param map_it A map iterator to the given key
  138. * @return True if it was found or could be added to the map
  139. */
  140. bool find_key(
  141. rtps::CacheChange_t* a_change,
  142. t_m_Inst_Caches::iterator* map_it);
  143. /**
  144. * @brief Method that finds a key in m_keyedChanges or tries to add it if not found
  145. * @param a_change The change to get the key from
  146. * @param map_it A map iterator to the given key
  147. * @return True if it was found or could be added to the map
  148. */
  149. bool find_key_for_change(
  150. rtps::CacheChange_t* a_change,
  151. t_m_Inst_Caches::iterator& map_it);
  152. /**
  153. * @name Variants of incoming change processing.
  154. * Will be called with the history mutex taken.
  155. * @param[in] change The received change
  156. * @param unknown_missing_changes_up_to Number of missing changes before this one
  157. * @return
  158. */
  159. ///@{
  160. bool received_change_keep_all_no_key(
  161. rtps::CacheChange_t* change,
  162. size_t unknown_missing_changes_up_to);
  163. bool received_change_keep_last_no_key(
  164. rtps::CacheChange_t* change,
  165. size_t unknown_missing_changes_up_to);
  166. bool received_change_keep_all_with_key(
  167. rtps::CacheChange_t* change,
  168. size_t unknown_missing_changes_up_to);
  169. bool received_change_keep_last_with_key(
  170. rtps::CacheChange_t* change,
  171. size_t unknown_missing_changes_up_to);
  172. ///@}
  173. bool add_received_change(
  174. rtps::CacheChange_t* a_change);
  175. bool add_received_change_with_key(
  176. rtps::CacheChange_t* a_change,
  177. std::vector<rtps::CacheChange_t*>& instance_changes);
  178. bool deserialize_change(
  179. rtps::CacheChange_t* change,
  180. uint32_t ownership_strength,
  181. void* data,
  182. SampleInfo_t* info);
  183. };
  184. } // namespace fastrtps
  185. } // namespace eprosima
  186. #endif
  187. #endif /* SUBSCRIBERHISTORY_H_ */