Coverage Report

Created: 2026-07-25 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Fast-DDS/src/cpp/statistics/fastdds/domain/DomainParticipantImpl.hpp
Line
Count
Source
1
// Copyright 2021 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
/**
16
 * @file DomainParticipantImpl.hpp
17
 */
18
19
#ifndef _FASTDDS_STADISTICS_FASTDDS_DOMAIN_DOMAINPARTICIPANTIMPL_HPP_
20
#define _FASTDDS_STADISTICS_FASTDDS_DOMAIN_DOMAINPARTICIPANTIMPL_HPP_
21
22
#include <fastdds/config.hpp>
23
24
#ifdef FASTDDS_STATISTICS
25
26
#include <string>
27
28
#include <fastdds/dds/core/ReturnCode.hpp>
29
#include <fastdds/dds/domain/DomainParticipant.hpp>
30
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
31
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
32
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
33
#include <fastdds/dds/publisher/Publisher.hpp>
34
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
35
#include <fastdds/dds/topic/Topic.hpp>
36
#include <fastdds/dds/topic/TopicDescription.hpp>
37
#include <fastdds/dds/topic/TypeSupport.hpp>
38
39
#include <fastdds/domain/DomainParticipantImpl.hpp>
40
41
#include "DomainParticipantStatisticsListener.hpp"
42
#include <statistics/rtps/monitor-service/Interfaces.hpp>
43
44
namespace efd = eprosima::fastdds::dds;
45
46
namespace eprosima {
47
namespace fastdds {
48
namespace statistics {
49
50
class MonitorServiceStatusData;
51
52
namespace dds {
53
54
class PublisherImpl;
55
56
class DomainParticipantImpl : public efd::DomainParticipantImpl,
57
    public rtps::IStatusQueryable
58
{
59
public:
60
61
    /**
62
     * @brief This operation enables a Statistics DataWriter
63
     *
64
     * @param [in] topic_name Name of the topic associated to the Statistics DataWriter
65
     * @param [in] dwqos DataWriterQos to be set
66
     * @return RETCODE_BAD_PARAMETER if the topic name provided does not correspond to any Statistics DataWriter,
67
     * RETCODE_INCONSISTENT_POLICY if the DataWriterQos provided is inconsistent,
68
     * RETCODE_OK if the DataWriter has been created or if it has been created previously,
69
     * and RETCODE_ERROR otherwise
70
     */
71
    efd::ReturnCode_t enable_statistics_datawriter(
72
            const std::string& topic_name,
73
            const efd::DataWriterQos& dwqos);
74
75
    /**
76
     * @brief This operation enables a Statistics DataWriter from a provided XML defined profile
77
     *
78
     * @param [in] profile_name Name for the profile to be used to fill the QoS structure.
79
     * @param [in] topic_name Name of the statistics topic to be enabled.
80
     * @return RETCODE_BAD_PARAMETER if the topic name provided does not correspond to any Statistics DataWriter,
81
     * RETCODE_INCONSISTENT_POLICY if the DataWriterQos provided is inconsistent,
82
     * RETCODE_OK if the DataWriter has been created or if it has been created previously,
83
     * and RETCODE_ERROR otherwise
84
     */
85
    efd::ReturnCode_t enable_statistics_datawriter_with_profile(
86
            const std::string& profile_name,
87
            const std::string& topic_name);
88
89
    /**
90
     * @brief This operation disables a Statistics DataWriter
91
     *
92
     * @param [in] topic_name Name of the topic associated to the Statistics DataWriter
93
     * @return RETCODE_UNSUPPORTED if the FASTDDS_STATISTICS CMake option has not been set,
94
     * RETCODE_BAD_PARAMETER if the topic name provided does not correspond to any Statistics DataWriter,
95
     * RETCODE_OK if the DataWriter has been correctly deleted or does not exist,
96
     * and RETCODE_ERROR otherwise
97
     */
98
    efd::ReturnCode_t disable_statistics_datawriter(
99
            const std::string& topic_name);
100
101
    /**
102
     * @brief This operation enables the DomainParticipantImpl
103
     *
104
     * @return RETCODE_OK if successful
105
     */
106
    efd::ReturnCode_t enable() override;
107
108
    void disable() override;
109
110
    /**
111
     * Auxiliary function that checks if a topic name corresponds to a statistics builtin topic name.
112
     * @param [in]   topic_name string with the topic name to check.
113
     * @return whether the input string corresponds to a builtin statistics topic name.
114
     */
115
    static bool is_statistics_topic_name(
116
            const std::string& topic_name) noexcept;
117
118
    /**
119
     * @brief This override calls the parent method and returns builtin publishers to nullptr
120
     *
121
     * @return RETCODE_OK if successful
122
     * @note This method is meant to be used followed by a deletion of the participant as it implies
123
     * the deletion of the builtin statistics publishers.
124
     */
125
    efd::ReturnCode_t delete_contained_entities() override;
126
127
    /**
128
     * Enables the monitor service in this DomainParticipant.
129
     *
130
     * @return RETCODE_OK if the monitor service could be correctly enabled.
131
     * @return RETCODE_ERROR if the monitor service could not be enabled properly.
132
     * @return RETCODE_UNSUPPORTED if FASTDDS_STATISTICS is not enabled.
133
     *
134
     */
135
    efd::ReturnCode_t enable_monitor_service();
136
137
    /**
138
     * Disables the monitor service in this DomainParticipant. Does nothing if the service was not enabled before.
139
     *
140
     * @return RETCODE_OK if the monitor service could be correctly disabled.
141
     * @return RETCODE_NOT_ENABLED if the monitor service was not previously enabled.
142
     * @return RETCODE_ERROR if the service could not be properly disabled.
143
     * @return RETCODE_UNSUPPORTED if FASTDDS_STATISTICS is not enabled.
144
     *
145
     */
146
    efd::ReturnCode_t disable_monitor_service();
147
148
    /**
149
     * fills in the ParticipantBuiltinTopicData from a MonitorService Message
150
     *
151
     * @param [out] data Proxy to fill
152
     * @param [in] msg MonitorService Message to get the proxy information from.
153
     *
154
     * @return RETCODE_OK if the operation succeeds.
155
     * @return RETCODE_ERROR if the  operation fails.
156
     */
157
    efd::ReturnCode_t fill_discovery_data_from_cdr_message(
158
            fastdds::rtps::ParticipantBuiltinTopicData& data,
159
            const fastdds::statistics::MonitorServiceStatusData& msg);
160
161
    /**
162
     * fills in the WriterProxyData from a MonitorService Message
163
     *
164
     * @param [out] data Proxy to fill.
165
     * @param [in] msg MonitorService Message to get the proxy information from.
166
     *
167
     * @return RETCODE_OK if the operation succeeds.
168
     * @return RETCODE_ERROR if the  operation fails.
169
     */
170
    efd::ReturnCode_t fill_discovery_data_from_cdr_message(
171
            fastdds::dds::PublicationBuiltinTopicData& data,
172
            const fastdds::statistics::MonitorServiceStatusData& msg);
173
174
    /**
175
     * fills in the SubscriptionBuiltinTopicData from a MonitorService Message
176
     *
177
     * @param [out] data Proxy to fill.
178
     * @param [in] msg MonitorService Message to get the proxy information from.
179
     *
180
     * @return RETCODE_OK if the operation succeeds.
181
     * @return RETCODE_ERROR if the  operation fails.
182
     */
183
    efd::ReturnCode_t fill_discovery_data_from_cdr_message(
184
            fastdds::dds::SubscriptionBuiltinTopicData& data,
185
            const fastdds::statistics::MonitorServiceStatusData& msg);
186
187
    /**
188
     * Gets the status observer for that entity
189
     *
190
     * @return status observer
191
     */
192
193
    const rtps::IStatusObserver* get_status_observer()
194
0
    {
195
0
        return status_observer_.load();
196
0
    }
197
198
protected:
199
200
    /**
201
     * Constructor
202
     */
203
    DomainParticipantImpl(
204
            efd::DomainParticipant* dp,
205
            efd::DomainId_t did,
206
            const efd::DomainParticipantQos& qos,
207
            efd::DomainParticipantListener* listen = nullptr)
208
0
        : efd::DomainParticipantImpl(
209
0
                dp,
210
0
                did,
211
0
                qos,
212
0
                listen)
213
0
        , builtin_publisher_(nullptr)
214
0
        , statistics_listener_(std::make_shared<DomainParticipantStatisticsListener>())
215
0
    {
216
0
    }
217
218
    efd::PublisherImpl* create_publisher_impl(
219
            const efd::PublisherQos& qos,
220
            efd::PublisherListener* listener) override;
221
222
    efd::SubscriberImpl* create_subscriber_impl(
223
            const efd::SubscriberQos& qos,
224
            efd::SubscriberListener* listener) override;
225
226
    /**
227
     * Auxiliary function to create the statistics builtin entities.
228
     */
229
    void create_statistics_builtin_entities();
230
231
    /**
232
     * Auxiliary function to enable statistics builtin datawriters.
233
     * @param topic_list string with the semicolon separated list of statistics topics.
234
     */
235
    void enable_statistics_builtin_datawriters(
236
            const std::string& topic_list);
237
238
    /**
239
     * Auxiliary function to delete the statistics builtin entities.
240
     */
241
    void delete_statistics_builtin_entities();
242
243
    /**
244
     * Auxiliary function that transforms the topic alias to the topic name.
245
     * @param [in]   topic_name_or_alias string with the statistic topic name or alias.
246
     * @param [out]  topic_name          string with the corresponding topic name to use.
247
     * @param [out]  event_kind          statistics event kind corresponding to the topic name.
248
     * @return whether the input string corresponds to a valid topic name or alias.
249
     */
250
    bool transform_and_check_topic_name(
251
            const std::string& topic_name_or_alias,
252
            std::string& topic_name,
253
            uint32_t& event_kind) noexcept;
254
255
    /**
256
     * Auxiliary function to register the statistics type depending on the statistics topic name.
257
     * It also creates the topic (or finds it if already created) and returns the pointer.
258
     * @param [out] topic pointer to the created topic pointer.
259
     * If the method returns false the parameter is not modified.
260
     * @param [in] topic_name string with the statistics topic name.
261
     * @return true if successful.
262
     * false in case there is incompatibility between the type associated to the Topic and the expected type.
263
     */
264
    bool register_statistics_type_and_topic(
265
            efd::Topic** topic,
266
            const std::string& topic_name) noexcept;
267
268
    /**
269
     * Auxiliary function that checks if the topic is already created within the domain participant.
270
     * If it is not, it creates the topic and registers the type. It also checks if the type can be registered.
271
     * If succesfull, it returns the pointer to the found or created topic.
272
     * @param [out] topic pointer to the found or created topic pointer.
273
     * @param [in] topic_name string with the topic name to find or create.
274
     * @param [in] type TypeSupport to register.
275
     * @return false if the topic is found but uses another type different from the expected one or if register_type
276
     * fails because there is another TypeSupport using the same name. true otherwise.
277
     */
278
    bool find_or_create_topic_and_type(
279
            efd::Topic** topic,
280
            const std::string& topic_name,
281
            const efd::TypeSupport& type) noexcept;
282
283
    /**
284
     * Auxiliary method to delete a topic and deregister a type after checking that they are consistent.
285
     * @param topic_name string with the topic name.
286
     * @param type_name string with the type name.
287
     * @return false if the type and topic do not match or if delete_topic fails. Otherwise true.
288
     */
289
    bool delete_topic_and_type(
290
            const std::string& topic_name) noexcept;
291
292
    /**
293
     * @brief Implementation of the IStatusQueryable interface.
294
     */
295
    bool get_monitoring_status(
296
            const fastdds::rtps::GUID_t& entity_guid,
297
            eprosima::fastdds::statistics::MonitorServiceData&) override;
298
299
    efd::Publisher* builtin_publisher_ = nullptr;
300
    PublisherImpl* builtin_publisher_impl_ = nullptr;
301
    std::shared_ptr<DomainParticipantStatisticsListener> statistics_listener_;
302
    std::atomic<const rtps::IStatusObserver*> status_observer_{nullptr};
303
304
    friend class efd::DomainParticipantFactory;
305
};
306
307
/* Environment variable to specify a semicolon-separated list of topic names that define the statistics DataWriters that
308
 * the DomainParticipant will enable when enabled itself.
309
 * The topic names must conform to the topic names aliases defined in @ref topic_names.hpp.
310
 * For the variable to take any effect the CMake option FASTDDS_STATISTICS must be enabled.
311
 */
312
constexpr const char* FASTDDS_STATISTICS_ENVIRONMENT_VARIABLE = "FASTDDS_STATISTICS";
313
314
} // dds
315
} // statistics
316
} // fastdds
317
} // eprosima
318
319
#endif // FASTDDS_STATISTICS
320
321
#endif // _FASTDDS_STADISTICS_FASTDDS_DOMAIN_DOMAINPARTICIPANTIMPL_HPP_