1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::internal::ffi;
use crate::internal::traits::AsFfi;
use crate::{Participant, Result};
/// A `Subscriber` groups [`Readers`](crate::Reader) and controls their shared
/// [`QoS`](crate::QoS). Readers created under a subscriber inherit its
/// [`QoS`](crate::QoS) where applicable.
///
/// Use [`Subscriber::new`] for simple construction or [`Subscriber::builder`]
/// for [`QoS`](crate::QoS) and
/// [`listener`](crate::listener::SubscriberListener) configuration.
///
/// In most applications a subscriber is created implicitly when constructing a
/// [`Reader`](crate::Reader) directly. Use an explicit subscriber when you need
/// coordinated reads across multiple readers.
#[derive(Debug)]
pub struct Subscriber<'domain, 'participant> {
pub(crate) inner: cyclonedds_sys::dds_entity_t,
phantom: std::marker::PhantomData<&'participant Participant<'domain>>,
}
/// Builder for [`Subscriber`] (accessible via [`Subscriber::builder`]).
#[derive(Debug)]
pub struct SubscriberBuilder<'domain, 'participant, 'qos> {
participant: &'participant Participant<'domain>,
qos: Option<&'qos crate::QoS>,
listener: Option<crate::SubscriberListener>,
}
impl<'d, 'p, 'q> SubscriberBuilder<'d, 'p, 'q> {
/// Creates a new [`SubscriberBuilder`] for the given [`Participant`].
///
/// # Examples
///
/// ```
/// use cyclonedds::builder::SubscriberBuilder;
/// use cyclonedds::{Domain, Participant};
///
/// let domain = Domain::default();
/// let participant = Participant::new(&domain)?;
/// let subscriber_builder = SubscriberBuilder::new(&participant);
/// # Ok::<_, cyclonedds::Error>(())
/// ```
#[must_use]
pub const fn new(participant: &'p Participant<'d>) -> Self {
Self {
participant,
qos: None,
listener: None,
}
}
/// Sets the [`QoS`](crate::QoS) for this subscriber builder.
///
/// # Examples
///
/// ```
/// use cyclonedds::builder::SubscriberBuilder;
/// use cyclonedds::qos::policy;
/// use cyclonedds::{Duration, QoS};
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let qos = QoS::new().with_reliability(policy::Reliability::Reliable {
/// max_blocking_time: Duration::from_millis(100),
/// });
/// let subscriber_builder = SubscriberBuilder::new(&participant).with_qos(&qos);
/// # Ok::<_, cyclonedds::Error>(())
/// ```
#[must_use]
pub const fn with_qos(mut self, qos: &'q crate::QoS) -> Self {
self.qos = Some(qos);
self
}
///
/// Sets the [`Listener`](crate::Listener) on this subscriber builder.
///
/// # Examples
///
/// ```
/// use cyclonedds::Listener;
/// use cyclonedds::builder::SubscriberBuilder;
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let subscriber_builder = SubscriberBuilder::new(&participant).with_listener(Listener::new());
/// # Ok::<_, cyclonedds::Error>(())
/// ```
#[must_use]
pub fn with_listener<L>(mut self, listener: L) -> Self
where
L: AsRef<crate::SubscriberListener>,
{
self.listener = Some(*listener.as_ref());
self
}
/// Builds the [`Subscriber`].
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber failed to create.
///
/// # Examples
///
/// ```
/// use cyclonedds::QoS;
/// use cyclonedds::builder::SubscriberBuilder;
/// use cyclonedds::qos::policy;
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let qos = QoS::new().with_durability(policy::Durability::TransientLocal);
/// let subscriber = SubscriberBuilder::new(&participant)
/// .with_qos(&qos)
/// .build()?;
/// # Ok::<_, cyclonedds::Error>(())
/// ```
pub fn build(self) -> Result<Subscriber<'d, 'p>> {
// NOTE: using `and_then` to avoid ? branch on the listener for coverage
// since the C lib currently panics on OOM rather than returning null.
self.listener
.map(|listener| listener.as_ffi())
.transpose()
.and_then(|listener| {
Ok(Subscriber {
inner: ffi::dds_create_subscriber(
self.participant.inner,
self.qos.map(|qos| &qos.inner),
listener.as_ref(),
)?,
phantom: std::marker::PhantomData,
})
})
}
}
impl<'d, 'p> Subscriber<'d, 'p> {
/// Creates a new `Subscriber` under `participant` with default
/// [`QoS`](crate::QoS) and no
/// [`listener`](crate::listener::SubscriberListener).
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber fails to create.
///
/// # Examples
///
/// ```
/// use cyclonedds::Subscriber;
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let subscriber = Subscriber::new(&participant)?;
/// Ok::<_, cyclonedds::Error>(())
/// ```
pub fn new(participant: &'p Participant<'d>) -> Result<Self> {
Self::builder(participant).build()
}
/// Returns a [`SubscriberBuilder`](crate::builder::SubscriberBuilder) for
/// constructing a subscriber with custom [`QoS`](crate::QoS) or a
/// [`listener`](crate::listener::SubscriberListener).
///
/// # Examples
///
/// ```
/// use cyclonedds::{
/// QoS, Subscriber,
/// qos::policy::{Durability, Presentation},
/// };
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let qos = QoS::new().with_presentation(Presentation::Topic {
/// coherent_access: true,
/// ordered_access: true,
/// });
/// let subscriber = Subscriber::builder(&participant).with_qos(&qos).build()?;
/// Ok::<_, cyclonedds::Error>(())
/// ```
#[must_use]
pub const fn builder<'q>(participant: &'p Participant<'d>) -> SubscriberBuilder<'d, 'p, 'q> {
SubscriberBuilder::new(participant)
}
/// (WARN: unimplemented in C lib): Notifies all readers belonging to this
/// subscriber that data is available.
///
/// <div class="warning">
///
/// This function is currently not implemented by the underlying C library
/// and will thus always return an unsupported error.
///
/// </div>
///
/// Triggers the
/// [`DataOnReaders`](crate::listener::SubscriberListener::with_data_on_readers)
/// callback on the subscriber's listener and the
/// [`DataAvailable`](crate::listener::ReaderListener::with_data_available)
/// callback on each reader's listener.
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber fails to notify the
/// readers.
///
/// # Examples
///
/// ```no_run
/// use cyclonedds::Subscriber;
/// # use cyclonedds::{Domain, Participant};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let subscriber = Subscriber::new(&participant)?;
/// subscriber.notify_readers()?;
/// # Ok::<_, cyclonedds::Error>(())
/// ```
pub fn notify_readers(&self) -> Result<()> {
ffi::dds_notify_readers(self.inner)
}
pub(crate) const fn from_existing(
inner: cyclonedds_sys::dds_entity_t,
) -> std::mem::ManuallyDrop<Self> {
std::mem::ManuallyDrop::new(Self {
inner,
phantom: std::marker::PhantomData,
})
}
/// Sets the [`SubscriberListener`](crate::SubscriberListener) on this
/// subscriber, replacing any previously set listener.
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber fails to set the
/// listener.
///
/// # Examples
///
/// ```
/// use cyclonedds::SubscriberListener;
/// # use cyclonedds::{Domain, Participant, Subscriber};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let mut subscriber = Subscriber::new(&participant)?;
/// subscriber.set_listener(SubscriberListener::new())?;
/// # Ok::<_, cyclonedds::Error>(())
/// ```
pub fn set_listener<L>(&mut self, listener: L) -> Result<()>
where
L: AsRef<crate::SubscriberListener>,
{
listener
.as_ref()
.as_ffi()
.and_then(|listener| ffi::dds_set_listener(self.inner, Some(listener.inner)))
}
/// Removes the listener from this subscriber.
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber fails to unset the
/// listener.
///
/// # Examples
///
/// ```
/// # use cyclonedds::{Domain, Participant, Subscriber};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
/// let mut subscriber = Subscriber::new(&participant)?;
/// subscriber.unset_listener()?;
/// # Ok::<_, cyclonedds::Error>(())
/// ```
pub fn unset_listener(&mut self) -> Result<()> {
ffi::dds_set_listener(self.inner, None)?;
Ok(())
}
/// Sets the [`SubscriberListener`](crate::SubscriberListener) on this
/// subscriber, consuming and returning `self`.
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) if the subscriber fails to set the
/// listener.
///
/// # Examples
///
/// ```
/// use cyclonedds::SubscriberListener;
/// # use cyclonedds::{Domain, Participant, Subscriber};
/// # let domain = Domain::default();
/// # let participant = Participant::new(&domain)?;
///
/// let subscriber = Subscriber::new(&participant)?.with_listener(SubscriberListener::new())?;
/// # Ok::<_, cyclonedds::Error>(())
/// ```
pub fn with_listener<L>(mut self, listener: L) -> Result<Self>
where
L: AsRef<crate::SubscriberListener>,
{
self.set_listener(listener).map(|_err| self)
}
}
impl Drop for Subscriber<'_, '_> {
fn drop(&mut self) {
let result = ffi::dds_delete(self.inner);
debug_assert!(
result.is_ok(),
"unable to delete {self:?}: failed with {result:?}"
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_subscriber_create() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let qos = crate::QoS::new();
let participant = Participant::new(&domain).unwrap();
let _ = Subscriber::new(&participant).unwrap();
let _ = Subscriber::builder(&participant)
.with_qos(&qos)
.build()
.unwrap();
}
#[test]
fn test_subscriber_create_with_invalid_participant() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let qos = crate::QoS::new();
let mut participant = Participant::new(&domain).unwrap();
let participant_id = participant.inner;
participant.inner = 0;
let result = Subscriber::new(&participant).unwrap_err();
assert_eq!(result, crate::Error::BadParameter);
let result = Subscriber::builder(&participant)
.with_qos(&qos)
.build()
.unwrap_err();
assert_eq!(result, crate::Error::BadParameter);
participant.inner = participant_id;
}
#[test]
fn test_subscriber_from_existing_subscriber() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let participant = crate::Participant::new(&domain).unwrap();
let subscriber = Subscriber::new(&participant).unwrap();
let new_subscriber = Subscriber::from_existing(subscriber.inner);
assert_eq!(new_subscriber.inner, subscriber.inner);
}
#[test]
fn test_subscriber_notify_readers_not_yet_supported_by_c_lib() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let participant = crate::Participant::new(&domain).unwrap();
let subscriber = Subscriber::new(&participant).unwrap();
let result = subscriber.notify_readers();
assert_eq!(
result,
Err(crate::Error::Unsupported),
"result was not unsupported (might be implemented now?)"
);
}
#[test]
fn test_subscriber_with_listener() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let participant = crate::Participant::new(&domain).unwrap();
let listener = crate::SubscriberListener::new().with_data_on_readers(|_| ());
let _ = Subscriber::new(&participant)
.unwrap()
.with_listener(listener)
.unwrap();
let _ = Subscriber::builder(&participant)
.with_listener(listener)
.build()
.unwrap();
let mut subscriber = Subscriber::new(&participant).unwrap();
subscriber.set_listener(listener).unwrap();
subscriber.unset_listener().unwrap();
}
#[test]
fn test_subscriber_with_listener_on_invalid_subscriber() {
let domain_id = crate::tests::domain::unique_id();
let domain = crate::Domain::new(domain_id).unwrap();
let participant = crate::Participant::new(&domain).unwrap();
let listener = crate::SubscriberListener::new().with_data_on_readers(|_| ());
let mut subscriber = Subscriber::new(&participant).unwrap();
let subscriber_id = subscriber.inner;
subscriber.inner = 0;
let result = subscriber.set_listener(listener).unwrap_err();
assert_eq!(result, crate::Error::BadParameter);
let result = subscriber.unset_listener().unwrap_err();
assert_eq!(result, crate::Error::BadParameter);
subscriber.inner = subscriber_id;
}
}