Coverage Report

Created: 2022-08-24 06:19

/src/Fast-DDS/include/fastdds/statistics/rtps/StatisticsCommon.hpp
Line
Count
Source (jump to first uncovered line)
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 StatisticsCommon.hpp
17
 */
18
19
#ifndef _FASTDDS_STATISTICS_RTPS_STATISTICSCOMMON_HPP_
20
#define _FASTDDS_STATISTICS_RTPS_STATISTICSCOMMON_HPP_
21
22
#include <memory>
23
#include <type_traits>
24
25
#include <fastdds/rtps/common/Guid.h>
26
#include <fastdds/rtps/common/SampleIdentity.h>
27
#include <fastdds/rtps/common/Time_t.h>
28
#include <fastdds/statistics/IListeners.hpp>
29
#include <fastrtps/utils/TimedMutex.hpp>
30
31
32
namespace eprosima {
33
34
namespace fastrtps {
35
namespace rtps {
36
37
class RTPSMessageGroup;
38
39
} // rtps
40
} // fastrtps
41
42
namespace fastdds {
43
namespace statistics {
44
45
#ifdef FASTDDS_STATISTICS
46
47
// Members are private details
48
struct StatisticsAncillary;
49
50
class StatisticsListenersImpl
51
{
52
    std::unique_ptr<StatisticsAncillary> members_;
53
54
protected:
55
56
    /**
57
     * Create a class A auxiliary structure
58
     * @return true if successfully created
59
     */
60
    template<class A>
61
    void init_statistics()
62
    {
63
        static_assert(
64
            std::is_base_of<StatisticsAncillary, A>::value,
65
            "Auxiliary structure must derive from StatisticsAncillary");
66
67
        if (!members_)
68
        {
69
            members_.reset(new A);
70
        }
71
    }
72
73
    /**
74
     * Returns the auxiliary members
75
     * @return The specialized auxiliary structure for each class
76
     */
77
    StatisticsAncillary* get_aux_members() const;
78
79
    /**
80
     * Add a listener to receive statistics backend callbacks
81
     * @param listener
82
     * @return true if successfully added
83
     */
84
    bool add_statistics_listener_impl(
85
            std::shared_ptr<fastdds::statistics::IListener> listener);
86
87
    /**
88
     * Remove a listener from receiving statistics backend callbacks
89
     * @param listener
90
     * @return true if successfully removed
91
     */
92
    bool remove_statistics_listener_impl(
93
            std::shared_ptr<fastdds::statistics::IListener> listener);
94
95
    /**
96
     * Lambda function to traverse the listener collection
97
     * @param f function object to apply to each listener
98
     * @return function object after being applied to each listener
99
     */
100
    template<class Function>
101
    Function for_each_listener(
102
            Function f);
103
104
    /**
105
     * Retrieve endpoint mutexes from derived class
106
     * @return defaults to the endpoint mutex
107
     */
108
    virtual fastrtps::RecursiveTimedMutex& get_statistics_mutex() = 0;
109
110
    /**
111
     * Retrieve the GUID_t from derived class
112
     * @return endpoint GUID_t
113
     */
114
    virtual const fastrtps::rtps::GUID_t& get_guid() const = 0;
115
};
116
117
// Members are private details
118
struct StatisticsWriterAncillary;
119
120
class StatisticsWriterImpl
121
    : protected StatisticsListenersImpl
122
{
123
124
    /**
125
     * Create the auxiliary structure
126
     * @return nullptr on failure
127
     */
128
    StatisticsWriterAncillary* get_members() const;
129
130
    /**
131
     * Retrieve endpoint mutexes from derived class
132
     * @return defaults to the endpoint mutex
133
     */
134
    fastrtps::RecursiveTimedMutex& get_statistics_mutex() final;
135
136
    /**
137
     * Retrieve the GUID_t from derived class
138
     * @return endpoint GUID_t
139
     */
140
    const fastrtps::rtps::GUID_t& get_guid() const final;
141
142
protected:
143
144
    /**
145
     * Constructor. Mandatory member initialization.
146
     */
147
    StatisticsWriterImpl();
148
149
    // TODO: methods for listeners callbacks
150
151
    /**
152
     * @brief Report a change on the number of DATA / DATAFRAG submessages sent for a specific sample.
153
     * @param sample_identity SampleIdentity of the affected sample.
154
     * @param num_sent_submessages Current total number of submessages sent for the affected sample.
155
     */
156
    void on_sample_datas(
157
            const fastrtps::rtps::SampleIdentity& sample_identity,
158
            size_t num_sent_submessages);
159
160
    /**
161
     * @brief Report that a HEARTBEAT message is sent
162
     * @param current count of heartbeats
163
     */
164
    void on_heartbeat(
165
            uint32_t count);
166
167
    /**
168
     * @brief Report that a DATA / DATA_FRAG message is generated
169
     * @param num_destinations number of locators to which the message will be sent
170
     */
171
    void on_data_generated(
172
            size_t num_destinations);
173
174
    /// Notify listeners of DATA / DATA_FRAG counts
175
    void on_data_sent();
176
177
    /**
178
     * @brief Reports publication throughtput based on last added sample to writer's history
179
     * @param payload size of the message sent
180
     */
181
    void on_publish_throughput(
182
            uint32_t payload);
183
184
    /// Report that a GAP message is sent
185
    void on_gap();
186
187
    /*
188
     * @brief Report that several changes are marked for redelivery
189
     * @param number of changes to redeliver
190
     */
191
    void on_resent_data(
192
            uint32_t to_send);
193
};
194
195
// Members are private details
196
struct StatisticsReaderAncillary;
197
198
class StatisticsReaderImpl
199
    : protected StatisticsListenersImpl
200
{
201
    friend class fastrtps::rtps::RTPSMessageGroup;
202
203
    /**
204
     * Create the auxiliary structure
205
     * @return nullptr on failure
206
     */
207
    StatisticsReaderAncillary* get_members() const;
208
209
    /**
210
     * Retrieve endpoint mutexes from derived class
211
     * @return defaults to the endpoint mutex
212
     */
213
    fastrtps::RecursiveTimedMutex& get_statistics_mutex() final;
214
215
    /**
216
     * Retrieve the GUID_t from derived class
217
     * @return endpoint GUID_t
218
     */
219
    const fastrtps::rtps::GUID_t& get_guid() const final;
220
221
protected:
222
223
    /**
224
     * Constructor. Mandatory member initialization.
225
     */
226
    StatisticsReaderImpl();
227
228
    // TODO: methods for listeners callbacks
229
230
    /**
231
     * @brief Report that a sample has been notified to the user.
232
     * @param writer_guid GUID of the writer from where the sample was received.
233
     * @param source_timestamp Source timestamp received from the writer for the sample being notified.
234
     */
235
    void on_data_notify(
236
            const fastrtps::rtps::GUID_t& writer_guid,
237
            const fastrtps::rtps::Time_t& source_timestamp);
238
239
    /**
240
     * @brief Report that an ACKNACK message is sent
241
     * @param count current count of ACKNACKs
242
     */
243
    void on_acknack(
244
            int32_t count);
245
246
    /**
247
     * @brief Report that a NACKFRAG message is sent
248
     * @param count current count of NACKFRAGs
249
     */
250
    void on_nackfrag(
251
            int32_t count);
252
253
    /**
254
     * @brief Reports subscription throughtput based on last added sample to reader's history
255
     * @param payload size of the message received
256
     */
257
    void on_subscribe_throughput(
258
            uint32_t payload);
259
};
260
261
#else // when FASTDDS_STATISTICS is not defined a dummy implementation is used
262
263
class StatisticsWriterImpl
264
{
265
protected:
266
267
    // TODO: methods for listeners callbacks
268
269
    /**
270
     * @brief Report a change on the number of DATA / DATAFRAG submessages sent for a specific sample.
271
     * Parameter: SampleIdentity of the affected sample.
272
     * Parameter: Current total number of submessages sent for the affected sample.
273
     */
274
    inline void on_sample_datas(
275
            const fastrtps::rtps::SampleIdentity&,
276
            size_t)
277
0
    {
278
0
    }
279
280
    /**
281
     * @brief Report that a HEARTBEAT message is sent
282
     * Parameter: current count of heartbeats
283
     */
284
    inline void on_heartbeat(
285
            uint32_t)
286
0
    {
287
0
    }
288
289
    /**
290
     * @brief Report that a DATA / DATA_FRAG message is generated
291
     * Parameter: number of locators to which the message will be sent
292
     */
293
    inline void on_data_generated(
294
            size_t)
295
0
    {
296
0
    }
297
298
    /// Notify listeners of DATA / DATA_FRAG counts
299
    inline void on_data_sent()
300
0
    {
301
0
    }
302
303
    /**
304
     * @brief Reports publication throughtput based on last added sample to writer's history
305
     * Parameter: size of the message sent
306
     */
307
    inline void on_publish_throughput(
308
            uint32_t)
309
0
    {
310
0
    }
311
312
    /// Report that a GAP message is sent
313
    inline void on_gap()
314
0
    {
315
0
    }
316
317
    /*
318
     * @brief Report that several changes are marked for redelivery
319
     * Parameter: number of changes to redeliver
320
     */
321
    inline void on_resent_data(
322
            uint32_t)
323
0
    {
324
0
    }
325
326
};
327
328
class StatisticsReaderImpl
329
{
330
    friend class fastrtps::rtps::RTPSMessageGroup;
331
332
protected:
333
334
    // TODO: methods for listeners callbacks
335
336
    /**
337
     * @brief Report that a sample has been notified to the user.
338
     * Parameter: GUID of the writer from where the sample was received.
339
     * Parameter: Source timestamp received from the writer for the sample being notified.
340
     */
341
    inline void on_data_notify(
342
            const fastrtps::rtps::GUID_t&,
343
            const fastrtps::rtps::Time_t&)
344
0
    {
345
0
    }
346
347
    /**
348
     * @brief Report that an ACKNACK message is sent
349
     * Parameter: current count of ACKNACKs
350
     */
351
    inline void on_acknack(
352
            int32_t)
353
0
    {
354
0
    }
355
356
    /**
357
     * @brief Report that a NACKFRAG message is sent
358
     * Parameter: current count of NACKFRAGs
359
     */
360
    inline void on_nackfrag(
361
            int32_t)
362
0
    {
363
0
    }
364
365
    /**
366
     * @brief Reports subscription throughtput based on last added sample to reader's history
367
     * Parameter: size of the message received
368
     */
369
    inline void on_subscribe_throughput(
370
            uint32_t)
371
0
    {
372
0
    }
373
374
};
375
376
#endif // FASTDDS_STATISTICS
377
378
} // namespace statistics
379
} // namespace fastdds
380
} // namespace eprosima
381
382
#endif // _FASTDDS_STATISTICS_RTPS_STATISTICSCOMMON_HPP_