NetworkFactory.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #ifndef _FASTDDS_RTPS_NETWORK_FACTORY_HPP
  15. #define _FASTDDS_RTPS_NETWORK_FACTORY_HPP
  16. #include <fastdds/rtps/transport/TransportInterface.h>
  17. #include <fastdds/rtps/common/LocatorSelector.hpp>
  18. #include <fastdds/rtps/network/ReceiverResource.h>
  19. #include <fastdds/rtps/network/SenderResource.h>
  20. #include <fastdds/rtps/messages/MessageReceiver.h>
  21. #include <vector>
  22. #include <memory>
  23. namespace eprosima {
  24. namespace fastrtps {
  25. namespace rtps {
  26. class RTPSParticipantAttributes;
  27. /**
  28. * Provides the FastRTPS library with abstract resources, which
  29. * in turn manage the SEND and RECEIVE operations over some transport.
  30. * Once a transport is registered, it becomes invisible to the library
  31. * and is abstracted away for good.
  32. * @ingroup NETWORK_MODULE
  33. */
  34. class NetworkFactory
  35. {
  36. public:
  37. NetworkFactory();
  38. /**
  39. * Allows registration of a transport statically, by specifying the transport type and
  40. * its associated descriptor type. This is particularly useful for user-defined transports.
  41. */
  42. template<class T, class D>
  43. void RegisterTransport(
  44. const D& descriptor)
  45. {
  46. std::unique_ptr<T> transport(new T(descriptor));
  47. if (transport->init())
  48. {
  49. mRegisteredTransports.emplace_back(std::move(transport));
  50. }
  51. }
  52. /**
  53. * Allows registration of a transport dynamically. Only the transports built into FastRTPS
  54. * are supported here (although it can be easily extended at NetworkFactory.cpp)
  55. * @param descriptor Structure that defines all initial configuration for a given transport.
  56. */
  57. bool RegisterTransport(
  58. const fastdds::rtps::TransportDescriptorInterface* descriptor);
  59. /**
  60. * Walks over the list of transports, opening every possible channel that can send through
  61. * the given locator and returning a vector of Sender Resources associated with it.
  62. * @param locator Locator through which to send.
  63. */
  64. bool build_send_resources(
  65. fastdds::rtps::SendResourceList&,
  66. const Locator_t& locator);
  67. /**
  68. * Walks over the list of transports, opening every possible channel that we can listen to
  69. * from the given locator, and returns a vector of Receiver Resources for this goal.
  70. * @param local Locator from which to listen.
  71. * @param returned_resources_list List that will be filled with the created ReceiverResources.
  72. * @param receiver_max_message_size Max message size allowed by the message receiver.
  73. */
  74. bool BuildReceiverResources(
  75. Locator_t& local,
  76. std::vector<std::shared_ptr<ReceiverResource> >& returned_resources_list,
  77. uint32_t receiver_max_message_size);
  78. void NormalizeLocators(
  79. LocatorList_t& locators);
  80. /**
  81. * Transforms a remote locator into a locator optimized for local communications.
  82. *
  83. * If the remote locator corresponds to one of the local interfaces, it is converted
  84. * to the corresponding local address.
  85. *
  86. * @param [in] remote_locator Locator to be converted.
  87. * @param [out] result_locator Converted locator.
  88. *
  89. * @return false if the input locator is not supported/allowed by any of the registered transports,
  90. * true otherwise.
  91. */
  92. bool transform_remote_locator(
  93. const Locator_t& remote_locator,
  94. Locator_t& result_locator) const;
  95. /**
  96. * Performs the locator selection algorithm.
  97. *
  98. * It basically consists of the following steps
  99. * - selector.selection_start is called
  100. * - the transport selection algorithm is called for each registered transport
  101. *
  102. * @param [in, out] selector Locator selector.
  103. */
  104. void select_locators(
  105. LocatorSelector& selector) const;
  106. bool is_local_locator(
  107. const Locator_t& locator) const;
  108. size_t numberOfRegisteredTransports() const;
  109. uint32_t get_max_message_size_between_transports() const
  110. {
  111. return maxMessageSizeBetweenTransports_;
  112. }
  113. uint32_t get_min_send_buffer_size()
  114. {
  115. return minSendBufferSize_;
  116. }
  117. /**
  118. * Fills ret_locators with the list of all possible locators in the local machine at the given
  119. * physical_port of the locator_kind.
  120. * Return if found any.
  121. * */
  122. bool generate_locators(
  123. uint16_t physical_port,
  124. int locator_kind,
  125. LocatorList_t& ret_locators);
  126. /**
  127. * For each transport, ask for their default output locators.
  128. * */
  129. void GetDefaultOutputLocators(
  130. LocatorList_t& defaultLocators);
  131. /**
  132. * Adds locators to the metatraffic multicast list.
  133. * */
  134. bool getDefaultMetatrafficMulticastLocators(
  135. LocatorList_t& locators,
  136. uint32_t metatraffic_multicast_port) const;
  137. /**
  138. * Adds locators to the metatraffic unicast list.
  139. * */
  140. bool getDefaultMetatrafficUnicastLocators(
  141. LocatorList_t& locators,
  142. uint32_t metatraffic_unicast_port) const;
  143. /**
  144. * Fills the locator with the metatraffic multicast configuration.
  145. * */
  146. bool fillMetatrafficMulticastLocator(
  147. Locator_t& locator,
  148. uint32_t metatraffic_multicast_port) const;
  149. /**
  150. * Fills the locator with the metatraffic unicast configuration.
  151. * */
  152. bool fillMetatrafficUnicastLocator(
  153. Locator_t& locator,
  154. uint32_t metatraffic_unicast_port) const;
  155. /**
  156. * Configures the locator with the initial peer configuration.
  157. * */
  158. bool configureInitialPeerLocator(
  159. uint32_t domain_id,
  160. Locator_t& locator,
  161. RTPSParticipantAttributes& m_att) const;
  162. /**
  163. * Adds locators to the default unicast configuration.
  164. * */
  165. bool getDefaultUnicastLocators(
  166. uint32_t domain_id,
  167. LocatorList_t& locators,
  168. const RTPSParticipantAttributes& m_att) const;
  169. /**
  170. * Fills the locator with the default unicast configuration.
  171. * */
  172. bool fillDefaultUnicastLocator(
  173. uint32_t domain_id,
  174. Locator_t& locator,
  175. const RTPSParticipantAttributes& m_att) const;
  176. /**
  177. * Shutdown method to close the connections of the transports.
  178. */
  179. void Shutdown();
  180. private:
  181. std::vector<std::unique_ptr<fastdds::rtps::TransportInterface> > mRegisteredTransports;
  182. uint32_t maxMessageSizeBetweenTransports_;
  183. uint32_t minSendBufferSize_;
  184. /**
  185. * Calculates well-known ports.
  186. */
  187. uint16_t calculateWellKnownPort(
  188. uint32_t domain_id,
  189. const RTPSParticipantAttributes& att) const;
  190. };
  191. } // namespace rtps
  192. } // namespace fastrtps
  193. } // namespace eprosima
  194. #endif /* _FASTDDS_RTPS_NETWORK_FACTORY_HPP */