dust_dds 0.15.0

Data Distribution Service (DDS) implementation
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::{
    dcps::infrastructure::error::{DdsError, DdsResult},
    infrastructure::time::Duration,
};

use super::{
    qos_policy::{
        DataRepresentationQosPolicy, DeadlineQosPolicy, DestinationOrderQosPolicy,
        DurabilityQosPolicy, EntityFactoryQosPolicy, GroupDataQosPolicy, HistoryQosPolicy,
        HistoryQosPolicyKind, LatencyBudgetQosPolicy, LifespanQosPolicy, LivelinessQosPolicy,
        OwnershipQosPolicy, OwnershipStrengthQosPolicy, PartitionQosPolicy, PresentationQosPolicy,
        ReaderDataLifecycleQosPolicy, ReliabilityQosPolicy, ReliabilityQosPolicyKind,
        ResourceLimitsQosPolicy, TimeBasedFilterQosPolicy, TopicDataQosPolicy,
        TransportPriorityQosPolicy, UserDataQosPolicy, WriterDataLifecycleQosPolicy,
    },
    time::DurationKind,
};

/// QoS policies applicable to the [`DomainParticipantFactory`](crate::domain::domain_participant_factory::DomainParticipantFactory)
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct DomainParticipantFactoryQos {
    /// Value of the entity factory QoS policy.
    pub entity_factory: EntityFactoryQosPolicy,
}

/// Enumeration representing the kind of Qos to be used
#[derive(Debug)]
pub enum QosKind<T> {
    /// Default QoS variant
    Default,
    /// Specific QoS variant
    Specific(T),
}

/// QoS policies applicable to the [`DomainParticipant`](crate::domain::domain_participant::DomainParticipant)
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct DomainParticipantQos {
    /// Value of the user data QoS policy.
    pub user_data: UserDataQosPolicy,
    /// Value of the entity factory QoS policy.
    pub entity_factory: EntityFactoryQosPolicy,
}

/// QoS policies applicable to the [`Publisher`](crate::publication::publisher::Publisher)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PublisherQos {
    /// Value of the presentation QoS policy.
    pub presentation: PresentationQosPolicy,
    /// Value of the partition QoS policy.
    pub partition: PartitionQosPolicy,
    /// Value of the group data QoS policy.
    pub group_data: GroupDataQosPolicy,
    /// Value of the entity factory QoS policy.
    pub entity_factory: EntityFactoryQosPolicy,
}

impl PublisherQos {
    pub const fn const_default() -> Self {
        Self {
            presentation: PresentationQosPolicy::const_default(),
            partition: PartitionQosPolicy::const_default(),
            group_data: GroupDataQosPolicy::const_default(),
            entity_factory: EntityFactoryQosPolicy::const_default(),
        }
    }
}

impl Default for PublisherQos {
    fn default() -> Self {
        Self::const_default()
    }
}

/// QoS policies applicable to the [`DataWriter`](crate::publication::data_writer::DataWriter)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DataWriterQos {
    /// Value of the durability QoS policy.
    pub durability: DurabilityQosPolicy,
    /// Value of the deadline QoS policy.
    pub deadline: DeadlineQosPolicy,
    /// Value of the latency budget QoS policy.
    pub latency_budget: LatencyBudgetQosPolicy,
    /// Value of the liveliness QoS policy.
    pub liveliness: LivelinessQosPolicy,
    /// Value of the reliability QoS policy.
    pub reliability: ReliabilityQosPolicy,
    /// Value of the destination order QoS policy.
    pub destination_order: DestinationOrderQosPolicy,
    /// Value of the history QoS policy.
    pub history: HistoryQosPolicy,
    /// Value of the resource limits QoS policy.
    pub resource_limits: ResourceLimitsQosPolicy,
    /// Value of the transport priority QoS policy.
    pub transport_priority: TransportPriorityQosPolicy,
    /// Value of the lifespan QoS policy.
    pub lifespan: LifespanQosPolicy,
    /// Value of the user_data QoS policy.
    pub user_data: UserDataQosPolicy,
    /// Value of the ownership QoS policy.
    pub ownership: OwnershipQosPolicy,
    /// Value of the ownership strength QoS policy.
    pub ownership_strength: OwnershipStrengthQosPolicy,
    /// Value of the writer data lifecycle QoS policy.
    pub writer_data_lifecycle: WriterDataLifecycleQosPolicy,
    /// Value of the data representation QoS policy.
    pub representation: DataRepresentationQosPolicy,
}

impl DataWriterQos {
    pub const fn const_default() -> Self {
        Self {
            reliability: ReliabilityQosPolicy {
                kind: ReliabilityQosPolicyKind::Reliable,
                max_blocking_time: DurationKind::Finite(Duration::new(
                    0,
                    100_000_000, /*100ms*/
                )),
            },
            durability: DurabilityQosPolicy::const_default(),
            deadline: DeadlineQosPolicy::const_default(),
            latency_budget: LatencyBudgetQosPolicy::const_default(),
            liveliness: LivelinessQosPolicy::const_default(),
            destination_order: DestinationOrderQosPolicy::const_default(),
            history: HistoryQosPolicy::const_default(),
            resource_limits: ResourceLimitsQosPolicy::const_default(),
            user_data: UserDataQosPolicy::const_default(),
            ownership: OwnershipQosPolicy::const_default(),
            ownership_strength: OwnershipStrengthQosPolicy::const_default(),
            lifespan: LifespanQosPolicy::const_default(),
            transport_priority: TransportPriorityQosPolicy::const_default(),
            writer_data_lifecycle: WriterDataLifecycleQosPolicy::const_default(),
            representation: DataRepresentationQosPolicy::const_default(),
        }
    }
}

impl Default for DataWriterQos {
    fn default() -> Self {
        Self::const_default()
    }
}

impl DataWriterQos {
    pub(crate) fn is_consistent(&self) -> DdsResult<()> {
        // On the writer there can be no more than one value on the representation
        if self.representation.value.len() > 1 {
            return Err(DdsError::InconsistentPolicy);
        }

        // The setting of RESOURCE_LIMITS max_samples must be consistent with the max_samples_per_instance. For these two
        // values to be consistent they must verify that *max_samples >= max_samples_per_instanc
        if self.resource_limits.max_samples < self.resource_limits.max_samples_per_instance {
            return Err(DdsError::InconsistentPolicy);
        }

        // The setting of RESOURCE_LIMITS max_samples_per_instance must be consistent with the HISTORY depth. For these two
        // QoS to be consistent, they must verify that *depth <= max_samples_per_instance.*
        match self.history.kind {
            HistoryQosPolicyKind::KeepLast(depth) => {
                if depth as usize > self.resource_limits.max_samples_per_instance {
                    Err(DdsError::InconsistentPolicy)
                } else {
                    Ok(())
                }
            }
            HistoryQosPolicyKind::KeepAll => Ok(()),
        }
    }

    pub(crate) fn check_immutability(&self, other: &Self) -> DdsResult<()> {
        if self.durability != other.durability
            || self.liveliness != other.liveliness
            || self.reliability != other.reliability
            || self.destination_order != other.destination_order
            || self.history != other.history
            || self.resource_limits != other.resource_limits
            || self.ownership != other.ownership
        {
            Err(DdsError::ImmutablePolicy)
        } else {
            Ok(())
        }
    }
}

/// QoS policies applicable to the [`Subscriber`](crate::subscription::subscriber::Subscriber)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SubscriberQos {
    /// Value of the presentation QoS policy.
    pub presentation: PresentationQosPolicy,
    /// Value of the partition QoS policy.
    pub partition: PartitionQosPolicy,
    /// Value of the group data QoS policy.
    pub group_data: GroupDataQosPolicy,
    /// Value of the entity factory QoS policy.
    pub entity_factory: EntityFactoryQosPolicy,
}

impl SubscriberQos {
    pub const fn const_default() -> Self {
        Self {
            presentation: PresentationQosPolicy::const_default(),
            partition: PartitionQosPolicy::const_default(),
            group_data: GroupDataQosPolicy::const_default(),
            entity_factory: EntityFactoryQosPolicy::const_default(),
        }
    }

    pub(crate) fn check_immutability(&self, other: &Self) -> DdsResult<()> {
        if self.presentation != other.presentation {
            Err(DdsError::ImmutablePolicy)
        } else {
            Ok(())
        }
    }
}

impl Default for SubscriberQos {
    fn default() -> Self {
        Self::const_default()
    }
}
/// QoS policies applicable to the [`DataReader`](crate::subscription::data_reader::DataReader)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DataReaderQos {
    /// Value of the durability QoS policy.
    pub durability: DurabilityQosPolicy,
    /// Value of the deadline QoS policy.
    pub deadline: DeadlineQosPolicy,
    /// Value of the latency budget QoS policy.
    pub latency_budget: LatencyBudgetQosPolicy,
    /// Value of the liveliness QoS policy.
    pub liveliness: LivelinessQosPolicy,
    /// Value of the reliability QoS policy.
    pub reliability: ReliabilityQosPolicy,
    /// Value of the destination order QoS policy.
    pub destination_order: DestinationOrderQosPolicy,
    /// Value of the history QoS policy.
    pub history: HistoryQosPolicy,
    /// Value of the resource limits QoS policy.
    pub resource_limits: ResourceLimitsQosPolicy,
    /// Value of the user data QoS policy.
    pub user_data: UserDataQosPolicy,
    /// Value of the ownership QoS policy.
    pub ownership: OwnershipQosPolicy,
    /// Value of the time based filter QoS policy.
    pub time_based_filter: TimeBasedFilterQosPolicy,
    /// Value of the reader data lifecycle QoS policy.
    pub reader_data_lifecycle: ReaderDataLifecycleQosPolicy,
    /// Value of the data representation QoS policy.
    pub representation: DataRepresentationQosPolicy,
}

impl DataReaderQos {
    pub const fn const_default() -> Self {
        Self {
            reliability: ReliabilityQosPolicy {
                kind: ReliabilityQosPolicyKind::BestEffort,
                max_blocking_time: DurationKind::Finite(Duration::new(
                    0,
                    100_000_000, /*100ms*/
                )),
            },
            durability: DurabilityQosPolicy::const_default(),
            deadline: DeadlineQosPolicy::const_default(),
            latency_budget: LatencyBudgetQosPolicy::const_default(),
            liveliness: LivelinessQosPolicy::const_default(),
            destination_order: DestinationOrderQosPolicy::const_default(),
            history: HistoryQosPolicy::const_default(),
            resource_limits: ResourceLimitsQosPolicy::const_default(),
            user_data: UserDataQosPolicy::const_default(),
            ownership: OwnershipQosPolicy::const_default(),
            time_based_filter: TimeBasedFilterQosPolicy::const_default(),
            reader_data_lifecycle: ReaderDataLifecycleQosPolicy::const_default(),
            representation: DataRepresentationQosPolicy::const_default(),
        }
    }
}

impl Default for DataReaderQos {
    fn default() -> Self {
        Self::const_default()
    }
}

impl DataReaderQos {
    pub(crate) fn is_consistent(&self) -> DdsResult<()> {
        // The setting of RESOURCE_LIMITS max_samples must be consistent with the max_samples_per_instance. For these two
        // values to be consistent they must verify that *max_samples >= max_samples_per_instance.*
        if self.resource_limits.max_samples < self.resource_limits.max_samples_per_instance {
            return Err(DdsError::InconsistentPolicy);
        }

        // The setting of RESOURCE_LIMITS max_samples_per_instance must be consistent with the HISTORY depth. For these two
        // QoS to be consistent, they must verify that *depth <= max_samples_per_instance.*
        match self.history.kind {
            HistoryQosPolicyKind::KeepLast(depth) => {
                if depth as usize > self.resource_limits.max_samples_per_instance {
                    return Err(DdsError::InconsistentPolicy);
                }
            }
            HistoryQosPolicyKind::KeepAll => (),
        }

        // The setting of the DEADLINE policy must be set consistently with that of the TIME_BASED_FILTER. For these two policies
        // to be consistent the settings must be such that *deadline period>= minimum_separation.*
        if self.deadline.period < self.time_based_filter.minimum_separation {
            return Err(DdsError::InconsistentPolicy);
        }

        Ok(())
    }

    pub(crate) fn check_immutability(&self, other: &Self) -> DdsResult<()> {
        if self.durability != other.durability
            || self.liveliness != other.liveliness
            || self.reliability != other.reliability
            || self.destination_order != other.destination_order
            || self.history != other.history
            || self.resource_limits != other.resource_limits
            || self.ownership != other.ownership
        {
            Err(DdsError::ImmutablePolicy)
        } else {
            Ok(())
        }
    }
}

/// QoS policies applicable to the [`Topic`](crate::topic_definition::topic::Topic)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TopicQos {
    /// Value of the topic data QoS policy.
    pub topic_data: TopicDataQosPolicy,
    /// Value of the durability QoS policy.
    pub durability: DurabilityQosPolicy,
    /// Value of the deadline QoS policy.
    pub deadline: DeadlineQosPolicy,
    /// Value of the latency budget QoS policy.
    pub latency_budget: LatencyBudgetQosPolicy,
    /// Value of the liveliness QoS policy.
    pub liveliness: LivelinessQosPolicy,
    /// Value of the reliability QoS policy.
    pub reliability: ReliabilityQosPolicy,
    /// Value of the destination order QoS policy.
    pub destination_order: DestinationOrderQosPolicy,
    /// Value of the history QoS policy.
    pub history: HistoryQosPolicy,
    /// Value of the resource limits QoS policy.
    pub resource_limits: ResourceLimitsQosPolicy,
    /// Value of the transport priority QoS policy.
    pub transport_priority: TransportPriorityQosPolicy,
    /// Value of the lifespan QoS policy.
    pub lifespan: LifespanQosPolicy,
    /// Value of the ownership QoS policy.
    pub ownership: OwnershipQosPolicy,
    /// Value of the data representation QoS policy.
    pub representation: DataRepresentationQosPolicy,
}

impl TopicQos {
    pub const fn const_default() -> Self {
        Self {
            reliability: ReliabilityQosPolicy {
                kind: ReliabilityQosPolicyKind::BestEffort,
                max_blocking_time: DurationKind::Finite(Duration::new(
                    0,
                    100_000_000, /*100ms*/
                )),
            },
            topic_data: TopicDataQosPolicy::const_default(),
            durability: DurabilityQosPolicy::const_default(),
            deadline: DeadlineQosPolicy::const_default(),
            latency_budget: LatencyBudgetQosPolicy::const_default(),
            liveliness: LivelinessQosPolicy::const_default(),
            destination_order: DestinationOrderQosPolicy::const_default(),
            history: HistoryQosPolicy::const_default(),
            resource_limits: ResourceLimitsQosPolicy::const_default(),
            transport_priority: TransportPriorityQosPolicy::const_default(),
            lifespan: LifespanQosPolicy::const_default(),
            ownership: OwnershipQosPolicy::const_default(),
            representation: DataRepresentationQosPolicy::const_default(),
        }
    }
}

impl Default for TopicQos {
    fn default() -> Self {
        Self::const_default()
    }
}

impl TopicQos {
    pub(crate) fn is_consistent(&self) -> DdsResult<()> {
        // The setting of RESOURCE_LIMITS max_samples must be consistent with the max_samples_per_instance. For these two
        // values to be consistent they must verify that *max_samples >= max_samples_per_instance.*
        if self.resource_limits.max_samples < self.resource_limits.max_samples_per_instance {
            return Err(DdsError::InconsistentPolicy);
        }

        // The setting of RESOURCE_LIMITS max_samples_per_instance must be consistent with the HISTORY depth. For these two
        // QoS to be consistent, they must verify that *depth <= max_samples_per_instance.*
        match self.history.kind {
            HistoryQosPolicyKind::KeepLast(depth) => {
                if depth as usize > self.resource_limits.max_samples_per_instance {
                    Err(DdsError::InconsistentPolicy)
                } else {
                    Ok(())
                }
            }
            HistoryQosPolicyKind::KeepAll => Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::infrastructure::qos_policy::Length;

    use super::*;

    #[test]
    fn data_writer_qos_consistency() {
        assert_eq!(DataWriterQos::default().is_consistent(), Ok(()));
        assert_eq!(
            DataWriterQos {
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    max_samples: Length::Limited(1),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
        assert_eq!(
            DataWriterQos {
                history: HistoryQosPolicy {
                    kind: HistoryQosPolicyKind::KeepLast(3),
                },
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
    }

    #[test]
    fn data_reader_qos_consistency() {
        assert_eq!(DataReaderQos::default().is_consistent(), Ok(()));
        assert_eq!(
            DataReaderQos {
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    max_samples: Length::Limited(1),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
        assert_eq!(
            DataReaderQos {
                history: HistoryQosPolicy {
                    kind: HistoryQosPolicyKind::KeepLast(3),
                },
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
    }

    #[test]
    fn topic_qos_consistency() {
        assert_eq!(TopicQos::default().is_consistent(), Ok(()));
        assert_eq!(
            TopicQos {
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    max_samples: Length::Limited(1),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
        assert_eq!(
            TopicQos {
                history: HistoryQosPolicy {
                    kind: HistoryQosPolicyKind::KeepLast(3),
                },
                resource_limits: ResourceLimitsQosPolicy {
                    max_samples_per_instance: Length::Limited(2),
                    ..Default::default()
                },
                ..Default::default()
            }
            .is_consistent(),
            Err(DdsError::InconsistentPolicy)
        );
    }
}