Coverage Report

Created: 2022-08-24 06:19

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