Coverage Report

Created: 2022-08-24 06:18

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