XMLProfileManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright 2017 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. #ifndef XML_PROFILE_MANAGER_H_
  16. #define XML_PROFILE_MANAGER_H_
  17. #include <fastrtps/attributes/ParticipantAttributes.h>
  18. #include <fastrtps/attributes/PublisherAttributes.h>
  19. #include <fastrtps/attributes/SubscriberAttributes.h>
  20. #include <fastrtps/xmlparser/XMLParserCommon.h>
  21. #include <fastrtps/xmlparser/XMLParser.h>
  22. #include <fastrtps/types/DynamicTypeBuilderFactory.h>
  23. #include <fastrtps/types/DynamicTypeBuilder.h>
  24. #include <fastrtps/types/DynamicPubSubType.h>
  25. #include <stdio.h>
  26. #include <string>
  27. #include <map>
  28. namespace eprosima {
  29. namespace fastrtps {
  30. namespace xmlparser {
  31. using participant_map_t = std::map<std::string, up_participant_t>;
  32. using part_map_iterator_t = participant_map_t::iterator;
  33. using publisher_map_t = std::map<std::string, up_publisher_t>;
  34. using publ_map_iterator_t = publisher_map_t::iterator;
  35. using subscriber_map_t = std::map<std::string, up_subscriber_t>;
  36. using subs_map_iterator_t = subscriber_map_t::iterator;
  37. using topic_map_t = std::map<std::string, up_topic_t>;
  38. using topic_map_iterator_t = topic_map_t::iterator;
  39. using requester_map_t = std::map<std::string, up_requester_t>;
  40. using requester_map_iterator_t = requester_map_t::iterator;
  41. using replier_map_t = std::map<std::string, up_replier_t>;
  42. using replier_map_iterator_t = replier_map_t::iterator;
  43. using xmlfiles_map_t = std::map<std::string, XMLP_ret>;
  44. using xmlfile_map_iterator_t = xmlfiles_map_t::iterator;
  45. /**
  46. * Class XMLProfileManager, used to make available profiles from XML file.
  47. * @ingroup XMLPROFILEMANAGER_MODULE
  48. */
  49. class XMLProfileManager
  50. {
  51. public:
  52. /**
  53. * Load the default profiles XML file.
  54. */
  55. RTPS_DllAPI static void loadDefaultXMLFile();
  56. /**
  57. * Load a profiles XML file.
  58. * @param filename Name for the file to be loaded.
  59. * @return XMLP_ret::XML_OK if all profiles are correct, XMLP_ret::XML_NOK if some are and some are not,
  60. * XMLP_ret::XML_ERROR in other case.
  61. */
  62. RTPS_DllAPI static XMLP_ret loadXMLFile(
  63. const std::string& filename);
  64. /**
  65. * Load a profiles XML node.
  66. * @param doc Node to be loaded.
  67. * @return XMLP_ret::XML_OK if all profiles are correct, XMLP_ret::XML_NOK if some are and some are not,
  68. * XMLP_ret::XML_ERROR in other case.
  69. */
  70. RTPS_DllAPI static XMLP_ret loadXMLNode(
  71. tinyxml2::XMLDocument& doc);
  72. /**
  73. * Load a profiles XML node.
  74. * @param profiles Node to be loaded.
  75. * @return XMLP_ret::XML_OK if all profiles are correct, XMLP_ret::XML_NOK if some are and some are not,
  76. * XMLP_ret::XML_ERROR in other case.
  77. */
  78. RTPS_DllAPI static XMLP_ret loadXMLProfiles(
  79. tinyxml2::XMLElement& profiles);
  80. /**
  81. * Load a dynamic types XML node.
  82. * @param types Node to be loaded.
  83. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case.
  84. */
  85. RTPS_DllAPI static XMLP_ret loadXMLDynamicTypes(
  86. tinyxml2::XMLElement& types);
  87. /**
  88. * Library settings setter.
  89. * @param library_settings New value for library settings.
  90. */
  91. RTPS_DllAPI static void library_settings(
  92. const LibrarySettingsAttributes& library_settings);
  93. /**
  94. * Library settings getter.
  95. * @return const ref to current library settings.
  96. */
  97. RTPS_DllAPI static const LibrarySettingsAttributes& library_settings();
  98. /**
  99. * Search for the profile specified and fill the structure.
  100. * @param profile_name Name for the profile to be used to fill the structure.
  101. * @param atts Structure to be filled.
  102. * @param log_error Flag to log an error if the profile_name is not found.
  103. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case. Defaults true.
  104. */
  105. RTPS_DllAPI static XMLP_ret fillParticipantAttributes(
  106. const std::string& profile_name,
  107. ParticipantAttributes& atts,
  108. bool log_error = true);
  109. //!Fills participant_attributes with the default values.
  110. RTPS_DllAPI static void getDefaultParticipantAttributes(
  111. ParticipantAttributes& participant_attributes);
  112. /**
  113. * Search for the profile specified and fill the structure.
  114. * @param profile_name Name for the profile to be used to fill the structure.
  115. * @param atts Structure to be filled.
  116. * @param log_error Flag to log an error if the profile_name is not found.
  117. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case. Defaults true.
  118. */
  119. RTPS_DllAPI static XMLP_ret fillPublisherAttributes(
  120. const std::string& profile_name,
  121. PublisherAttributes& atts,
  122. bool log_error = true);
  123. //!Fills publisher_attributes with the default values.
  124. RTPS_DllAPI static void getDefaultPublisherAttributes(
  125. PublisherAttributes& publisher_attributes);
  126. /**
  127. * Search for the profile specified and fill the structure.
  128. * @param profile_name Name for the profile to be used to fill the structure.
  129. * @param atts Structure to be filled.
  130. * @param log_error Flag to log an error if the profile_name is not found.
  131. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case. Defaults true.
  132. */
  133. RTPS_DllAPI static XMLP_ret fillSubscriberAttributes(
  134. const std::string& profile_name,
  135. SubscriberAttributes& atts,
  136. bool log_error = true);
  137. //!Fills subscriber_attributes with the default values.
  138. RTPS_DllAPI static void getDefaultSubscriberAttributes(
  139. SubscriberAttributes& subscriber_attributes);
  140. //!Add a new transport instance along with its id.
  141. RTPS_DllAPI static bool insertTransportById(
  142. const std::string& transport_id,
  143. sp_transport_t transport);
  144. //!Retrieves a transport instance by its id.
  145. RTPS_DllAPI static sp_transport_t getTransportById(
  146. const std::string& transport_id);
  147. /**
  148. * Search for the profile specified and fill the structure.
  149. * @param profile_name Name for the profile to be used to fill the structure.
  150. * @param atts Structure to be filled.
  151. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case.
  152. */
  153. RTPS_DllAPI static XMLP_ret fillTopicAttributes(
  154. const std::string& profile_name,
  155. TopicAttributes& atts);
  156. //!Fills topic_attributes with the default values.
  157. RTPS_DllAPI static void getDefaultTopicAttributes(
  158. TopicAttributes& topic_attributes);
  159. //!Add a new dynamic type instance along with its name.
  160. RTPS_DllAPI static bool insertDynamicTypeByName(
  161. const std::string& type_name,
  162. p_dynamictypebuilder_t type);
  163. //!Retrieves a transport instance by its name.
  164. RTPS_DllAPI static p_dynamictypebuilder_t getDynamicTypeByName(
  165. const std::string& type_name);
  166. /**
  167. * Search for the profile specified and fill the structure.
  168. * @param profile_name Name for the profile to be used to fill the structure.
  169. * @param atts Structure to be filled.
  170. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case.
  171. */
  172. RTPS_DllAPI static XMLP_ret fillRequesterAttributes(
  173. const std::string& profile_name,
  174. RequesterAttributes& atts);
  175. /**
  176. * Search for the profile specified and fill the structure.
  177. * @param profile_name Name for the profile to be used to fill the structure.
  178. * @param atts Structure to be filled.
  179. * @return XMLP_ret::XML_OK on success, XMLP_ret::XML_ERROR in other case.
  180. */
  181. RTPS_DllAPI static XMLP_ret fillReplierAttributes(
  182. const std::string& profile_name,
  183. ReplierAttributes& atts);
  184. /**
  185. * Deletes the XMLProsileManager instance.
  186. * FastRTPS's Domain calls this method automatically on its destructor, but
  187. * if using XMLProfileManager outside of FastRTPS, it should be called manually.
  188. */
  189. RTPS_DllAPI static void DeleteInstance()
  190. {
  191. participant_profiles_.clear();
  192. publisher_profiles_.clear();
  193. subscriber_profiles_.clear();
  194. xml_files_.clear();
  195. transport_profiles_.clear();
  196. }
  197. /**
  198. * Retrieves a DynamicPubSubType for the given dynamic type name.
  199. * Any instance retrieve by calling this method must be deleted calling the
  200. * XMLProfileManager::DeleteDynamicPubSubType method.
  201. */
  202. RTPS_DllAPI static types::DynamicPubSubType* CreateDynamicPubSubType(
  203. const std::string& type_name)
  204. {
  205. if (dynamic_types_.find(type_name) != dynamic_types_.end())
  206. {
  207. return new types::DynamicPubSubType(dynamic_types_[type_name]->build());
  208. }
  209. return nullptr;
  210. }
  211. /**
  212. * Deletes the given DynamicPubSubType previously created by calling
  213. * XMLProfileManager::CreateDynamicPubSubType method.
  214. */
  215. RTPS_DllAPI static void DeleteDynamicPubSubType(
  216. types::DynamicPubSubType* type)
  217. {
  218. delete type;
  219. }
  220. private:
  221. RTPS_DllAPI static XMLP_ret extractDynamicTypes(
  222. up_base_node_t properties,
  223. const std::string& filename);
  224. RTPS_DllAPI static XMLP_ret extractProfiles(
  225. up_base_node_t properties,
  226. const std::string& filename);
  227. RTPS_DllAPI static XMLP_ret extractParticipantProfile(
  228. up_base_node_t& profile,
  229. const std::string& filename);
  230. RTPS_DllAPI static XMLP_ret extractPublisherProfile(
  231. up_base_node_t& profile,
  232. const std::string& filename);
  233. RTPS_DllAPI static XMLP_ret extractSubscriberProfile(
  234. up_base_node_t& profile,
  235. const std::string& filename);
  236. RTPS_DllAPI static XMLP_ret extractTopicProfile(
  237. up_base_node_t& profile,
  238. const std::string& filename);
  239. RTPS_DllAPI static XMLP_ret extractRequesterProfile(
  240. up_base_node_t& profile,
  241. const std::string& filename);
  242. RTPS_DllAPI static XMLP_ret extractReplierProfile(
  243. up_base_node_t& profile,
  244. const std::string& filename);
  245. static BaseNode* root;
  246. static LibrarySettingsAttributes library_settings_;
  247. static participant_map_t participant_profiles_;
  248. static publisher_map_t publisher_profiles_;
  249. static subscriber_map_t subscriber_profiles_;
  250. static topic_map_t topic_profiles_;
  251. static requester_map_t requester_profiles_;
  252. static replier_map_t replier_profiles_;
  253. static xmlfiles_map_t xml_files_;
  254. static sp_transport_map_t transport_profiles_;
  255. static p_dynamictype_map_t dynamic_types_;
  256. };
  257. } /* xmlparser */
  258. } /* namespace */
  259. } /* namespace eprosima */
  260. #endif