Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/include/fastdds/dds/subscriber/Subscriber.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2019 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 Subscriber.hpp
17
 */
18
19
#ifndef FASTDDS_DDS_SUBSCRIBER__SUBSCRIBER_HPP
20
#define FASTDDS_DDS_SUBSCRIBER__SUBSCRIBER_HPP
21
22
#include <fastdds/dds/core/Entity.hpp>
23
#include <fastdds/dds/core/ReturnCode.hpp>
24
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
25
#include <fastdds/dds/subscriber/InstanceState.hpp>
26
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
27
#include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
28
#include <fastdds/dds/subscriber/SampleState.hpp>
29
#include <fastdds/dds/subscriber/ViewState.hpp>
30
#include <fastdds/dds/topic/qos/TopicQos.hpp>
31
#include <fastdds/dds/topic/TypeSupport.hpp>
32
33
namespace dds {
34
namespace sub {
35
36
class Subscriber;
37
38
} // namespace sub
39
} // namespace dds
40
41
namespace eprosima {
42
namespace fastdds {
43
namespace rtps {
44
45
class IPayloadPool;
46
47
} // namespace rtps
48
49
namespace dds {
50
51
class DomainParticipant;
52
class SubscriberListener;
53
class SubscriberImpl;
54
class DataReader;
55
class DataReaderListener;
56
class DataReaderQos;
57
class TopicDescription;
58
/**
59
 * Class Subscriber, contains the public API that allows the user to control the reception of messages.
60
 * This class should not be instantiated directly.
61
 * DomainRTPSParticipant class should be used to correctly create this element.
62
 *
63
 * @ingroup FASTDDS_MODULE
64
 */
65
class Subscriber : public DomainEntity
66
{
67
protected:
68
69
    friend class SubscriberImpl;
70
    friend class DomainParticipantImpl;
71
72
    /**
73
     * Create a subscriber, assigning its pointer to the associated implementation.
74
     * Don't use directly, create Subscriber using create_subscriber from DomainParticipant.
75
     */
76
    Subscriber(
77
            SubscriberImpl* pimpl,
78
            const StatusMask& mask = StatusMask::all());
79
80
    Subscriber(
81
            DomainParticipant* dp,
82
            const SubscriberQos& qos = SUBSCRIBER_QOS_DEFAULT,
83
            SubscriberListener* listener = nullptr,
84
            const StatusMask& mask = StatusMask::all());
85
86
public:
87
88
    /**
89
     * @brief Destructor
90
     */
91
    virtual ~Subscriber()
92
0
    {
93
0
    }
94
95
    /**
96
     * @brief This operation enables the Subscriber
97
     *
98
     * @return RETCODE_OK is successfully enabled. RETCODE_PRECONDITION_NOT_MET if the participant creating this
99
     *         Subscriber is not enabled.
100
     */
101
    FASTDDS_EXPORTED_API ReturnCode_t enable() override;
102
103
    /**
104
     * Allows accessing the Subscriber Qos.
105
     *
106
     * @return SubscriberQos reference
107
     */
108
    FASTDDS_EXPORTED_API const SubscriberQos& get_qos() const;
109
110
    /**
111
     * Retrieves the Subscriber Qos.
112
     *
113
     * @param qos SubscriberQos where the qos is returned
114
     * @return RETCODE_OK
115
     */
116
    FASTDDS_EXPORTED_API ReturnCode_t get_qos(
117
            SubscriberQos& qos) const;
118
119
    /**
120
     * Allows modifying the Subscriber Qos.
121
     * The given Qos must be supported by the SubscriberQos.
122
     *
123
     * @param qos new value for SubscriberQos
124
     * @return RETCODE_IMMUTABLE_POLICY if any of the Qos cannot be changed, RETCODE_INCONSISTENT_POLICY if the Qos is not
125
     * self consistent and RETCODE_OK if the qos is changed correctly.
126
     */
127
    FASTDDS_EXPORTED_API ReturnCode_t set_qos(
128
            const SubscriberQos& qos);
129
130
    /**
131
     * Retrieves the attached SubscriberListener.
132
     *
133
     * @return Pointer to the SubscriberListener
134
     */
135
    FASTDDS_EXPORTED_API const SubscriberListener* get_listener() const;
136
137
    /**
138
     * Modifies the SubscriberListener, sets the mask to StatusMask::all()
139
     *
140
     * @param listener new value for SubscriberListener
141
     * @return RETCODE_OK
142
     */
143
    FASTDDS_EXPORTED_API ReturnCode_t set_listener(
144
            SubscriberListener* listener);
145
146
    /**
147
     * Modifies the SubscriberListener.
148
     *
149
     * @param listener new value for the SubscriberListener
150
     * @param mask StatusMask that holds statuses the listener responds to.
151
     * @return RETCODE_OK
152
     */
153
    FASTDDS_EXPORTED_API ReturnCode_t set_listener(
154
            SubscriberListener* listener,
155
            const StatusMask& mask);
156
    /**
157
     * This operation creates a DataReader. The returned DataReader will be attached and belong to the Subscriber.
158
     *
159
     * @param topic Topic the DataReader will be listening.
160
     * @param reader_qos QoS of the DataReader.
161
     * @param listener Pointer to the listener (default: nullptr)
162
     * @param mask StatusMask that holds statuses the listener responds to (default: all).
163
     * @param payload_pool IPayloadPool shared pointer that defines reader payload (default: nullptr).
164
     * @return Pointer to the created DataReader. nullptr if failed.
165
     */
166
    FASTDDS_EXPORTED_API DataReader* create_datareader(
167
            TopicDescription* topic,
168
            const DataReaderQos& reader_qos,
169
            DataReaderListener* listener = nullptr,
170
            const StatusMask& mask = StatusMask::all(),
171
            std::shared_ptr<fastdds::rtps::IPayloadPool> payload_pool = nullptr);
172
173
    /**
174
     * This operation creates a DataReader. The returned DataReader will be attached and belongs to the Subscriber.
175
     *
176
     * @param topic Topic the DataReader will be listening.
177
     * @param profile_name DataReader profile name.
178
     * @param listener Pointer to the listener (default: nullptr)
179
     * @param mask StatusMask that holds statuses the listener responds to (default: all).
180
     * @param payload_pool IPayloadPool shared pointer that defines reader payload (default: nullptr).
181
     * @return Pointer to the created DataReader. nullptr if failed.
182
     */
183
    FASTDDS_EXPORTED_API DataReader* create_datareader_with_profile(
184
            TopicDescription* topic,
185
            const std::string& profile_name,
186
            DataReaderListener* listener = nullptr,
187
            const StatusMask& mask = StatusMask::all(),
188
            std::shared_ptr<fastdds::rtps::IPayloadPool> payload_pool = nullptr);
189
190
    /**
191
     * This operation deletes a DataReader that belongs to the Subscriber.
192
     *
193
     * The delete_datareader operation must be called on the same Subscriber object used to create the DataReader.
194
     * If delete_datareader is called on a different Subscriber, the operation will have no effect and it will
195
     * return an error.
196
     *
197
     * @param reader DataReader to delete
198
     * @return RETCODE_PRECONDITION_NOT_MET if the datareader does not belong to this subscriber, RETCODE_OK if it is correctly
199
     * deleted and RETCODE_ERROR otherwise.
200
     */
201
    FASTDDS_EXPORTED_API ReturnCode_t delete_datareader(
202
            const DataReader* reader);
203
204
    /**
205
     * This operation retrieves a previously-created DataReader belonging to the Subscriber that is attached to a
206
     * Topic with a matching topic_name. If no such DataReader exists, the operation will return nullptr.
207
     *
208
     * If multiple DataReaders attached to the Subscriber satisfy this condition, then the operation will return
209
     * one of them. It is not specified which one.
210
     *
211
     * @param topic_name Name of the topic associated to the DataReader
212
     * @return Pointer to a previously created DataReader created on a Topic with that topic_name
213
     */
214
    FASTDDS_EXPORTED_API DataReader* lookup_datareader(
215
            const std::string& topic_name) const;
216
217
    /**
218
     * This operation allows the application to access the DataReader objects.
219
     *
220
     * @param readers Vector of DataReader where the list of existing readers is returned
221
     * @return RETCODE_OK
222
     */
223
    FASTDDS_EXPORTED_API ReturnCode_t get_datareaders(
224
            std::vector<DataReader*>& readers) const;
225
226
    /**
227
     * @brief This operation allows the application to access the DataReader objects that contain samples with the
228
     * specified sample_states, view_states, and instance_states.
229
     *
230
     * @param [out] readers Vector of DataReader where the list of existing readers is returned
231
     * @param sample_states Vector of SampleStateKind
232
     * @param view_states Vector of ViewStateKind
233
     * @param instance_states Vector of InstanceStateKind
234
     * @return RETCODE_OK
235
     *
236
     * @warning Not supported yet. Currently returns RETCODE_UNSUPPORTED
237
     */
238
    FASTDDS_EXPORTED_API ReturnCode_t get_datareaders(
239
            std::vector<DataReader*>& readers,
240
            const std::vector<SampleStateKind>& sample_states,
241
            const std::vector<ViewStateKind>& view_states,
242
            const std::vector<InstanceStateKind>& instance_states) const;
243
244
    /**
245
     * This operation checks if the subscriber has DataReaders
246
     *
247
     * @return true if the subscriber has one or several DataReaders, false in other case
248
     */
249
    FASTDDS_EXPORTED_API bool has_datareaders() const;
250
251
    /**
252
     * @brief Indicates that the application is about to access the data samples in any of the DataReader objects
253
     * attached to the Subscriber.
254
     *
255
     * @return RETCODE_OK
256
     *
257
     * @warning Not supported yet. Currently returns RETCODE_UNSUPPORTED
258
     */
259
    FASTDDS_EXPORTED_API ReturnCode_t begin_access();
260
261
    /**
262
     * @brief Indicates that the application has finished accessing the data samples in DataReader objects managed by
263
     * the Subscriber.
264
     *
265
     * @return RETCODE_OK
266
     *
267
     * @warning Not supported yet. Currently returns RETCODE_UNSUPPORTED
268
     */
269
    FASTDDS_EXPORTED_API ReturnCode_t end_access();
270
271
272
    /**
273
     * This operation invokes the operation on_data_available on the DataReaderListener objects attached to
274
     * contained DataReader entities.
275
     *
276
     * This operation is typically invoked from the on_data_on_readers operation in the SubscriberListener.
277
     * That way the SubscriberListener can delegate to the DataReaderListener objects the handling of the data.
278
     *
279
     * @return RETCODE_OK
280
     */
281
    FASTDDS_EXPORTED_API ReturnCode_t notify_datareaders() const;
282
283
    /**
284
     * @brief Deletes all contained DataReaders. If the DataReaders have any QueryCondition or ReadCondition, they are
285
     * deleted before the DataReader itself.
286
     *
287
     * @return RETCODE_OK if successful, an error code otherwise
288
     */
289
    FASTDDS_EXPORTED_API ReturnCode_t delete_contained_entities();
290
291
    /**
292
     * This operation sets a default value of the DataReader QoS policies which will be used for newly created
293
     * DataReader entities in the case where the QoS policies are defaulted in the create_datareader operation.
294
     *
295
     * This operation will check that the resulting policies are self consistent; if they are not, the operation
296
     * will have no effect and return false.
297
     *
298
     * The special value DATAREADER_QOS_DEFAULT may be passed to this operation to indicate that the default QoS
299
     * should be reset back to the initial values the factory would use, that is the values that would be used
300
     * if the set_default_datareader_qos operation had never been called.
301
     *
302
     * @param qos new value for DataReaderQos to set as default
303
     * @return RETCODE_INCONSISTENT_POLICY if the Qos is not self consistent and RETCODE_OK if the qos is changed correctly.
304
     */
305
    FASTDDS_EXPORTED_API ReturnCode_t set_default_datareader_qos(
306
            const DataReaderQos& qos);
307
308
    /**
309
     * This operation returns the default value of the DataReader QoS, that is, the QoS policies which will be
310
     * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
311
     * create_datareader operation.
312
     *
313
     * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
314
     * call to get_default_datareader_qos, or else, if the call was never made, the default values.
315
     *
316
     * @return Current default DataReaderQos.
317
     */
318
    FASTDDS_EXPORTED_API const DataReaderQos& get_default_datareader_qos() const;
319
320
321
    /**
322
     * This operation returns the default value of the DataReader QoS, that is, the QoS policies which will be
323
     * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
324
     * create_datareader operation.
325
     *
326
     * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
327
     * call to get_default_datareader_qos, or else, if the call was never made, the default values.
328
     *
329
     * @return Current default DataReaderQos.
330
     */
331
    FASTDDS_EXPORTED_API DataReaderQos& get_default_datareader_qos();
332
333
    /**
334
     * This operation retrieves the default value of the DataReader QoS, that is, the QoS policies which will be
335
     * used for newly created DataReader entities in the case where the QoS policies are defaulted in the
336
     * create_datareader operation.
337
     *
338
     * The values retrieved get_default_datareader_qos will match the set of values specified on the last successful
339
     * call to get_default_datareader_qos, or else, if the call was never made, the default values.
340
     *
341
     * @param qos DataReaderQos where the default_qos is returned
342
     * @return RETCODE_OK
343
     */
344
    FASTDDS_EXPORTED_API ReturnCode_t get_default_datareader_qos(
345
            DataReaderQos& qos) const;
346
347
    /**
348
     * Fills the @ref DataReaderQos with the values of the XML profile.
349
     *
350
     * @param profile_name DataReader profile name.
351
     * @param qos @ref DataReaderQos object where the qos is returned.
352
     * @return @ref RETCODE_OK if the profile exists. @ref RETCODE_BAD_PARAMETER otherwise.
353
     */
354
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_profile(
355
            const std::string& profile_name,
356
            DataReaderQos& qos) const;
357
358
    /**
359
     * Fills the @ref DataReaderQos with the values of the XML profile, and also its corresponding topic name (if specified).
360
     *
361
     * @param profile_name DataReader profile name.
362
     * @param qos @ref DataReaderQos object where the qos is returned.
363
     * @param topic_name String where the name of the topic associated to this profile is returned (if specified).
364
     * @return @ref RETCODE_OK if the profile exists. @ref RETCODE_BAD_PARAMETER otherwise.
365
     */
366
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_profile(
367
            const std::string& profile_name,
368
            DataReaderQos& qos,
369
            std::string& topic_name) const;
370
371
    /**
372
     * Fills the @ref DataReaderQos with the first DataReader profile found in the provided XML.
373
     *
374
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
375
     * @param qos @ref DataReaderQos object where the qos is returned.
376
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
377
     */
378
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_xml(
379
            const std::string& xml,
380
            DataReaderQos& qos) const;
381
382
    /**
383
     * Fills the @ref DataReaderQos with the first DataReader profile found in the provided XML, and also its corresponding topic name (if specified).
384
     *
385
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
386
     * @param qos @ref DataReaderQos object where the qos is returned.
387
     * @param topic_name String where the name of the topic associated to this profile is returned (if specified).
388
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
389
     */
390
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_xml(
391
            const std::string& xml,
392
            DataReaderQos& qos,
393
            std::string& topic_name) const;
394
395
    /**
396
     * Fills the @ref DataReaderQos with the DataReader profile with \c profile_name to be found in the provided XML.
397
     *
398
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
399
     * @param qos @ref DataReaderQos object where the qos is returned.
400
     * @param profile_name DataReader profile name.
401
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
402
     */
403
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_xml(
404
            const std::string& xml,
405
            DataReaderQos& qos,
406
            const std::string& profile_name) const;
407
408
    /**
409
     * Fills the @ref DataReaderQos with the DataReader profile with \c profile_name to be found in the provided XML, and also its corresponding topic name (if specified).
410
     *
411
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
412
     * @param qos @ref DataReaderQos object where the qos is returned.
413
     * @param topic_name String where the name of the topic associated to this profile is returned (if specified).
414
     * @param profile_name DataReader profile name.
415
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
416
     */
417
    FASTDDS_EXPORTED_API ReturnCode_t get_datareader_qos_from_xml(
418
            const std::string& xml,
419
            DataReaderQos& qos,
420
            std::string& topic_name,
421
            const std::string& profile_name) const;
422
423
    /**
424
     * Fills the @ref DataReaderQos with the default DataReader profile found in the provided XML (if there is).
425
     *
426
     * @note This method does not update the default datareader qos (returned by \c get_default_datareader_qos).
427
     *
428
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
429
     * @param qos @ref DataReaderQos object where the qos is returned.
430
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
431
     */
432
    FASTDDS_EXPORTED_API ReturnCode_t get_default_datareader_qos_from_xml(
433
            const std::string& xml,
434
            DataReaderQos& qos) const;
435
436
    /**
437
     * Fills the @ref DataReaderQos with the default DataReader profile found in the provided XML (if there is), and also its corresponding topic name (if specified).
438
     *
439
     * @note This method does not update the default datareader qos (returned by \c get_default_datareader_qos).
440
     *
441
     * @param xml Raw XML string containing the profile to be used to fill the \c qos structure.
442
     * @param qos @ref DataReaderQos object where the qos is returned.
443
     * @param topic_name String where the name of the topic associated to this profile is returned (if specified).
444
     * @return @ref RETCODE_OK on success. @ref RETCODE_BAD_PARAMETER otherwise.
445
     */
446
    FASTDDS_EXPORTED_API ReturnCode_t get_default_datareader_qos_from_xml(
447
            const std::string& xml,
448
            DataReaderQos& qos,
449
            std::string& topic_name) const;
450
451
    /**
452
     * @brief Copies TopicQos into the corresponding DataReaderQos
453
     *
454
     * @param [in, out] reader_qos
455
     * @param [in] topic_qos
456
     * @return RETCODE_OK if successful, an error code otherwise
457
     *
458
     * @warning Not supported yet. Currently returns RETCODE_UNSUPPORTED
459
     */
460
    FASTDDS_EXPORTED_API static ReturnCode_t copy_from_topic_qos(
461
            DataReaderQos& reader_qos,
462
            const TopicQos& topic_qos);
463
464
    /**
465
     * This operation returns the DomainParticipant to which the Subscriber belongs.
466
     *
467
     * @return DomainParticipant Pointer
468
     */
469
    FASTDDS_EXPORTED_API const DomainParticipant* get_participant() const;
470
471
    /**
472
     * Returns the Subscriber's handle.
473
     *
474
     * @return InstanceHandle of this Subscriber.
475
     */
476
    FASTDDS_EXPORTED_API const InstanceHandle_t& get_instance_handle() const;
477
478
protected:
479
480
    SubscriberImpl* impl_;
481
482
    friend class ::dds::sub::Subscriber;
483
};
484
485
} // namespace dds
486
} // namespace fastdds
487
} // namespace eprosima
488
489
#endif // FASTDDS_DDS_SUBSCRIBER__SUBSCRIBER_HPP