LivelinessManager.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2016-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 LivelinessManager.h
  16. */
  17. #ifndef _FASTDDS_RTPS_LIVELINESS_MANAGER_H_
  18. #define _FASTDDS_RTPS_LIVELINESS_MANAGER_H_
  19. #include <fastdds/rtps/writer/LivelinessData.h>
  20. #include <fastrtps/utils/collections/ResourceLimitedVector.hpp>
  21. #include <fastdds/rtps/resources/TimedEvent.h>
  22. #include <mutex>
  23. namespace eprosima {
  24. namespace fastrtps {
  25. namespace rtps {
  26. using LivelinessCallback = std::function<void(
  27. const GUID_t&,
  28. const LivelinessQosPolicyKind&,
  29. const Duration_t&,
  30. int32_t alive_change,
  31. int32_t not_alive_change)>;
  32. /**
  33. * @brief A class managing the liveliness of a set of writers. Writers are represented by their LivelinessData
  34. * @details Uses a shared timed event and informs outside classes on liveliness changes
  35. * @ingroup WRITER_MODULE
  36. */
  37. class LivelinessManager
  38. {
  39. public:
  40. /**
  41. * @brief Constructor
  42. * @param callback A callback that will be invoked when a writer changes its liveliness status
  43. * @param service ResourceEvent object that will operate with the events.
  44. * @param manage_automatic True to manage writers with automatic liveliness, false otherwise
  45. */
  46. LivelinessManager(
  47. const LivelinessCallback& callback,
  48. ResourceEvent& service,
  49. bool manage_automatic = true);
  50. /**
  51. * @brief Constructor
  52. */
  53. ~LivelinessManager();
  54. /**
  55. * @brief LivelinessManager
  56. * @param other
  57. */
  58. LivelinessManager(const LivelinessManager& other) = delete;
  59. /**
  60. * @brief Adds a writer to the set
  61. * @param guid GUID of the writer
  62. * @param kind Liveliness kind
  63. * @param lease_duration Liveliness lease duration
  64. * @return True if the writer was successfully added
  65. */
  66. bool add_writer(
  67. GUID_t guid,
  68. LivelinessQosPolicyKind kind,
  69. Duration_t lease_duration);
  70. /**
  71. * @brief Removes a writer
  72. * @param guid GUID of the writer
  73. * @param kind Liveliness kind
  74. * @param lease_duration Liveliness lease duration
  75. * @return True if the writer was successfully removed
  76. */
  77. bool remove_writer(
  78. GUID_t guid,
  79. LivelinessQosPolicyKind kind,
  80. Duration_t lease_duration);
  81. /**
  82. * @brief Asserts liveliness of a writer in the set
  83. * @param guid The writer to assert liveliness of
  84. * @param kind The kind of the writer
  85. * @param lease_duration The lease duration
  86. * @return True if liveliness was successfully asserted
  87. */
  88. bool assert_liveliness(
  89. GUID_t guid,
  90. LivelinessQosPolicyKind kind,
  91. Duration_t lease_duration);
  92. /**
  93. * @brief Asserts liveliness of writers with given liveliness kind
  94. * @param kind Liveliness kind
  95. * @return True if liveliness was successfully asserted
  96. */
  97. bool assert_liveliness(LivelinessQosPolicyKind kind);
  98. /**
  99. * @brief A method to check any writer of the given kind is alive
  100. * @param kind The liveliness kind to check for
  101. * @return True if at least one writer of this kind is alive. False otherwise
  102. */
  103. bool is_any_alive(LivelinessQosPolicyKind kind);
  104. /**
  105. * @brief A method to return liveliness data
  106. * @details Should only be used for testing purposes
  107. * @return Vector of liveliness data
  108. */
  109. const ResourceLimitedVector<LivelinessData> &get_liveliness_data() const;
  110. private:
  111. //! @brief A method responsible for invoking the callback when liveliness is asserted
  112. //! @param writer The liveliness data of the writer asserting liveliness
  113. //!
  114. void assert_writer_liveliness(LivelinessData& writer);
  115. /**
  116. * @brief A method to calculate the time when the next writer is going to lose liveliness
  117. * @details This method is public for testing purposes but it should not be used from outside this class
  118. * @return True if at least one writer is alive
  119. */
  120. bool calculate_next();
  121. //! @brief A method to find a writer from a guid, liveliness kind and lease duration
  122. //! @param guid The guid of the writer
  123. //! @param kind The liveliness kind
  124. //! @param lease_duration The lease duration
  125. //! @param wit_out Returns an iterator to the writer liveliness data
  126. //! @return Returns true if writer was found, false otherwise
  127. bool find_writer(
  128. const GUID_t &guid,
  129. const LivelinessQosPolicyKind &kind,
  130. const Duration_t &lease_duration,
  131. ResourceLimitedVector<LivelinessData>::iterator* wit_out);
  132. //! @brief A method called if the timer expires
  133. //! @return True if the timer should be restarted
  134. bool timer_expired();
  135. //! A callback to inform outside classes that a writer changed its liveliness status
  136. LivelinessCallback callback_;
  137. //! A boolean indicating whether we are managing writers with automatic liveliness
  138. bool manage_automatic_;
  139. //! A vector of liveliness data
  140. ResourceLimitedVector<LivelinessData> writers_;
  141. //! A mutex to protect the liveliness data
  142. std::mutex mutex_;
  143. //! The timer owner, i.e. the writer which is next due to lose its liveliness
  144. LivelinessData* timer_owner_;
  145. //! A timed callback expiring when a writer (the timer owner) loses its liveliness
  146. TimedEvent timer_;
  147. };
  148. } /* namespace rtps */
  149. } /* namespace fastrtps */
  150. } /* namespace eprosima */
  151. #endif /* _FASTDDS_RTPS_LIVELINESS_MANAGER_H_ */