rustorb_dds/qos.rs
1//! The QoS of publishers, subscribers, data writers and data readers
2//! that can be set for now.
3
4use std::time::Duration;
5
6use rustorb_rtps::qos::ReliabilityKind;
7pub use rustorb_rtps::qos::{DurabilityKind, LivelinessKind, QosPolicyId};
8pub use rustorb_rtps::sedp::data_representation;
9pub use rustorb_xtypes::assignability::{TypeConsistency, TypeConsistencyKind};
10
11/// The HISTORY of a writer or a reader (DDS 1.4, 2.2.3.18): how many
12/// samples it keeps.
13///
14/// The depth applies to each instance.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum History {
17 /// Keep the latest `n` samples (at least 1).
18 KeepLast(usize),
19 /// Keep every sample: a reader until it is taken, a reliable writer
20 /// until every reader has acknowledged it.
21 KeepAll,
22}
23
24impl Default for History {
25 /// `KEEP_LAST` with depth 1, the DDS default.
26 fn default() -> Self {
27 History::KeepLast(1)
28 }
29}
30
31/// The RELIABILITY of a writer or a reader (DDS 1.4, 2.2.3.14).
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub struct Reliability {
34 /// BEST_EFFORT or RELIABLE.
35 pub kind: ReliabilityKind,
36 /// How long a reliable writer's `write` may wait for room in its
37 /// history (KEEP_ALL with `max_samples` reached) before it fails with
38 /// [`crate::Error::Timeout`].
39 pub max_blocking_time: Duration,
40}
41
42impl Reliability {
43 /// The default `max_blocking_time`: 100 ms (DDS 1.4, 2.2.3).
44 pub const DEFAULT_MAX_BLOCKING_TIME: Duration = Duration::from_millis(100);
45
46 /// RELIABLE, with the default blocking time.
47 pub const fn reliable() -> Self {
48 Reliability {
49 kind: ReliabilityKind::Reliable,
50 max_blocking_time: Self::DEFAULT_MAX_BLOCKING_TIME,
51 }
52 }
53
54 /// BEST_EFFORT.
55 pub const fn best_effort() -> Self {
56 Reliability {
57 kind: ReliabilityKind::BestEffort,
58 max_blocking_time: Self::DEFAULT_MAX_BLOCKING_TIME,
59 }
60 }
61
62 pub(crate) fn to_rtps(self) -> rustorb_rtps::qos::Reliability {
63 rustorb_rtps::qos::Reliability {
64 kind: self.kind,
65 max_blocking_time: rustorb_rtps::Duration::from_std(self.max_blocking_time),
66 }
67 }
68}
69
70/// The RESOURCE_LIMITS of a writer or a reader (DDS 1.4, 2.2.3.19).
71/// `None` is `LENGTH_UNLIMITED`, the default.
72///
73/// A RELIABLE, KEEP_ALL writer waits when it has `max_samples` samples,
74/// or `max_samples_per_instance` of the instance it writes; registering
75/// an instance beyond `max_instances` fails.
76///
77/// A reader rejects a sample that would go beyond them (SAMPLE_REJECTED,
78/// [`crate::DataReader::sample_rejected_status`]); under KEEP_LAST, a
79/// sample that replaces the oldest of its instance is not rejected. A
80/// reliable reader acknowledges a rejected sample all the same: it is not
81/// sent again.
82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
83pub struct ResourceLimits {
84 /// The most samples kept.
85 pub max_samples: Option<usize>,
86 /// The most instances kept.
87 pub max_instances: Option<usize>,
88 /// The most samples kept for one instance.
89 pub max_samples_per_instance: Option<usize>,
90}
91
92/// The DURABILITY_SERVICE of a TRANSIENT or PERSISTENT writer (DDS 1.4,
93/// 2.2.3.5): what the participant keeps of the writer's samples once the
94/// writer is deleted, and for how long it keeps a disposed instance.
95#[derive(Clone, Copy, Debug, PartialEq, Eq)]
96pub struct DurabilityService {
97 /// `service_cleanup_delay`: how long after the writer is deleted the
98 /// samples of the instances it disposed are dropped. Zero, the
99 /// default, drops them at once.
100 pub service_cleanup_delay: Duration,
101 /// `history_kind` and `history_depth`; KEEP_LAST(1) by default.
102 pub history: History,
103 /// `max_samples`, `max_instances` and `max_samples_per_instance`;
104 /// none by default.
105 pub resource_limits: ResourceLimits,
106}
107
108impl Default for DurabilityService {
109 fn default() -> Self {
110 DurabilityService {
111 service_cleanup_delay: Duration::ZERO,
112 history: History::default(),
113 resource_limits: ResourceLimits::default(),
114 }
115 }
116}
117
118impl DurabilityService {
119 pub(crate) fn to_rtps(self) -> rustorb_rtps::qos::DurabilityService {
120 let n = |v: Option<usize>| v.map_or(-1, |n| n.min(i32::MAX as usize) as i32);
121 let (history_kind, history_depth) = match self.history {
122 History::KeepLast(d) => (0, d.min(i32::MAX as usize) as i32),
123 History::KeepAll => (1, 1),
124 };
125 rustorb_rtps::qos::DurabilityService {
126 service_cleanup_delay: rustorb_rtps::Duration::from_std(self.service_cleanup_delay),
127 history_kind,
128 history_depth,
129 max_samples: n(self.resource_limits.max_samples),
130 max_instances: n(self.resource_limits.max_instances),
131 max_samples_per_instance: n(self.resource_limits.max_samples_per_instance),
132 }
133 }
134}
135
136/// The WRITER_DATA_LIFECYCLE of a writer (DDS 1.4, 2.2.3.21).
137#[derive(Clone, Copy, Debug, PartialEq, Eq)]
138pub struct WriterDataLifecycle {
139 /// Whether unregistering an instance, which deleting the writer does
140 /// for all of them, disposes it too. TRUE by default.
141 pub autodispose_unregistered_instances: bool,
142}
143
144impl Default for WriterDataLifecycle {
145 fn default() -> Self {
146 WriterDataLifecycle {
147 autodispose_unregistered_instances: true,
148 }
149 }
150}
151
152/// The LIVELINESS of a writer or a reader (DDS 1.4, 2.2.3.11).
153#[derive(Clone, Copy, Debug, PartialEq, Eq)]
154pub struct Liveliness {
155 /// AUTOMATIC, MANUAL_BY_PARTICIPANT or MANUAL_BY_TOPIC.
156 pub kind: LivelinessKind,
157 /// How long a writer is alive without asserting it; `None` is
158 /// infinite, the default.
159 pub lease_duration: Option<Duration>,
160}
161
162impl Default for Liveliness {
163 /// AUTOMATIC with an infinite lease.
164 fn default() -> Self {
165 Liveliness {
166 kind: LivelinessKind::Automatic,
167 lease_duration: None,
168 }
169 }
170}
171
172impl Liveliness {
173 pub(crate) fn to_rtps(self) -> rustorb_rtps::qos::Liveliness {
174 rustorb_rtps::qos::Liveliness {
175 kind: self.kind,
176 lease_duration: duration_to_rtps(self.lease_duration),
177 }
178 }
179}
180
181/// An optional duration on the wire: `None` is infinite.
182pub(crate) fn duration_to_rtps(d: Option<Duration>) -> rustorb_rtps::Duration {
183 d.map_or(
184 rustorb_rtps::Duration::INFINITE,
185 rustorb_rtps::Duration::from_std,
186 )
187}
188
189/// The QoS of a [`crate::Publisher`].
190#[derive(Clone, Debug, Default, PartialEq, Eq)]
191pub struct PublisherQos {
192 /// PARTITION (DDS 1.4, 2.2.3.13): the partitions its writers are in.
193 /// Empty is the default partition. A name may hold the fnmatch
194 /// wildcards `*`, `?` and `[...]`, which match the names of readers'
195 /// partitions (but not other wildcard names).
196 pub partition: Vec<String>,
197}
198
199/// The QoS of a [`crate::Subscriber`].
200#[derive(Clone, Debug, Default, PartialEq, Eq)]
201pub struct SubscriberQos {
202 /// PARTITION: the partitions its readers are in, as for
203 /// [`PublisherQos::partition`].
204 pub partition: Vec<String>,
205}
206
207/// The QoS of a [`crate::DataWriter`] that can be set for now.
208#[derive(Clone, Debug, PartialEq, Eq)]
209pub struct DataWriterQos {
210 /// RELIABILITY; RELIABLE by default.
211 pub reliability: Reliability,
212 /// HISTORY; KEEP_LAST(1) by default. The depth is for each instance.
213 pub history: History,
214 /// RESOURCE_LIMITS.
215 pub resource_limits: ResourceLimits,
216 /// WRITER_DATA_LIFECYCLE.
217 pub writer_data_lifecycle: WriterDataLifecycle,
218 /// DURABILITY; VOLATILE by default. A TRANSIENT_LOCAL writer keeps
219 /// the samples its HISTORY and RESOURCE_LIMITS allow, also once they
220 /// are acknowledged, and sends them to the TRANSIENT_LOCAL readers
221 /// matched later; for now it keeps them also for instances it has
222 /// unregistered.
223 ///
224 /// A TRANSIENT writer does the same, and its samples outlive it:
225 /// deleting it leaves them with the participant, which keeps what
226 /// [`Self::durability_service`] allows and goes on sending them to
227 /// late readers until it is deleted itself. A PERSISTENT writer also
228 /// keeps them in a file, and a PERSISTENT writer of the same topic
229 /// and type created later, by this program or another, takes them up
230 /// again, also after a restart (see
231 /// [`crate::ParticipantConfig::persistence_directory`]).
232 pub durability: DurabilityKind,
233 /// DURABILITY_SERVICE, for a TRANSIENT or PERSISTENT writer.
234 pub durability_service: DurabilityService,
235 /// DEADLINE period (DDS 1.4, 2.2.3.7); `None`, infinite, by default.
236 /// Each period in which a registered instance is not written counts
237 /// in [`crate::DataWriter::offered_deadline_missed_status`]; a
238 /// disposed instance is not counted until it is written again.
239 pub deadline: Option<Duration>,
240 /// LIVELINESS (DDS 1.4, 2.2.3.11). The participant asserts AUTOMATIC
241 /// writers; a MANUAL_BY_PARTICIPANT or MANUAL_BY_TOPIC writer is
242 /// asserted by writing or by `assert_liveliness`, and is lost
243 /// ([`crate::DataWriter::liveliness_lost_status`]) when its lease
244 /// passes without that.
245 pub liveliness: Liveliness,
246 /// USER_DATA (DDS 1.4, 2.2.3.1): octets the writer announces to the
247 /// other participants, for the application. Empty by default. ROS 2
248 /// reads `typehash=...;` here.
249 pub user_data: Vec<u8>,
250 /// DATA_REPRESENTATION (DDS-XTypes 1.3, 7.6.3.1): the writer
251 /// serializes its samples in the first
252 /// ([`data_representation::XCDR`] or [`data_representation::XCDR2`])
253 /// and offers it to readers. Empty, the default, is XCDR, which every
254 /// reader accepts; it is then not announced.
255 pub data_representation: Vec<i16>,
256 /// DATA_TAGS (DataTagQosPolicy, DDS Security 1.2, 7.2.5 and 9.7):
257 /// (name, value) tags of every sample the writer writes, announced
258 /// with it. With DDS Security, the permissions must allow publishing
259 /// with them (10.4.1.5.3.4.1.5). Empty by default.
260 pub data_tags: Vec<(String, String)>,
261}
262
263impl Default for DataWriterQos {
264 /// The DDS defaults: RELIABLE, KEEP_LAST(1), no limits, VOLATILE.
265 fn default() -> Self {
266 DataWriterQos {
267 reliability: Reliability::reliable(),
268 history: History::default(),
269 resource_limits: ResourceLimits::default(),
270 writer_data_lifecycle: WriterDataLifecycle::default(),
271 durability: DurabilityKind::Volatile,
272 durability_service: DurabilityService::default(),
273 deadline: None,
274 liveliness: Liveliness::default(),
275 user_data: Vec::new(),
276 data_representation: Vec::new(),
277 data_tags: Vec::new(),
278 }
279 }
280}
281
282/// The QoS of a [`crate::DataReader`] that can be set for now.
283#[derive(Clone, Debug, PartialEq, Eq)]
284pub struct DataReaderQos {
285 /// RELIABILITY; BEST_EFFORT by default.
286 pub reliability: Reliability,
287 /// HISTORY; KEEP_LAST(1) by default. The depth is for each instance.
288 pub history: History,
289 /// RESOURCE_LIMITS; none by default.
290 pub resource_limits: ResourceLimits,
291 /// DURABILITY; VOLATILE by default. A TRANSIENT_LOCAL reader gets
292 /// what a TRANSIENT_LOCAL writer kept from before they matched.
293 pub durability: DurabilityKind,
294 /// DEADLINE period; `None`, infinite, by default. Each period in
295 /// which an alive instance gets no sample counts in
296 /// [`crate::DataReader::requested_deadline_missed_status`].
297 pub deadline: Option<Duration>,
298 /// LIVELINESS: the most the reader accepts from writers (the kind and
299 /// the lease are matched on). Matched writers that lose their
300 /// liveliness count in [`crate::DataReader::liveliness_changed_status`],
301 /// and the instances only they wrote become NOT_ALIVE_NO_WRITERS.
302 pub liveliness: Liveliness,
303 /// USER_DATA: octets the reader announces to the other
304 /// participants. Empty by default.
305 pub user_data: Vec<u8>,
306 /// DATA_REPRESENTATION (DDS-XTypes 1.3, 7.6.3.1): the representations
307 /// the reader accepts; XCDR and XCDR2 by default, which it can both
308 /// read. An empty list is XCDR only.
309 pub data_representation: Vec<i16>,
310 /// TYPE_CONSISTENCY_ENFORCEMENT (DDS-XTypes 1.3, 7.6.3.4): how far the
311 /// writer's type may differ from the reader's. By default, the
312 /// reader's type must be assignable from the writer's, ignoring
313 /// sequence and string bounds. It applies when both announce their
314 /// types ([`crate::DomainParticipant::register_type`]); otherwise the
315 /// type names must be the same.
316 pub type_consistency: TypeConsistency,
317 /// DATA_TAGS (DataTagQosPolicy, DDS Security 1.2, 7.2.5 and 9.7):
318 /// (name, value) tags announced with the reader. With DDS Security,
319 /// the permissions must allow subscribing with them. Empty by
320 /// default.
321 pub data_tags: Vec<(String, String)>,
322}
323
324impl Default for DataReaderQos {
325 /// The DDS defaults: BEST_EFFORT, KEEP_LAST(1), VOLATILE.
326 fn default() -> Self {
327 DataReaderQos {
328 reliability: Reliability::best_effort(),
329 history: History::default(),
330 resource_limits: ResourceLimits::default(),
331 durability: DurabilityKind::Volatile,
332 deadline: None,
333 liveliness: Liveliness::default(),
334 user_data: Vec::new(),
335 data_representation: vec![data_representation::XCDR, data_representation::XCDR2],
336 type_consistency: TypeConsistency::default(),
337 data_tags: Vec::new(),
338 }
339 }
340}