Skip to main content

rustorb_dds/
status.rs

1//! Communication statuses (DDS 1.4, 2.2.4.1): counts of events on a
2//! writer or a reader. Reading a status resets its `*_change` counts.
3
4use rustorb_rtps::Guid;
5use rustorb_rtps::qos::QosPolicyId;
6
7use crate::instance::InstanceHandle;
8
9/// How many times one policy made endpoints incompatible.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub struct QosPolicyCount {
12    /// The policy.
13    pub policy_id: QosPolicyId,
14    /// How many times.
15    pub count: u32,
16}
17
18/// OFFERED_INCOMPATIBLE_QOS of a writer or REQUESTED_INCOMPATIBLE_QOS of
19/// a reader (DDS 1.4, 2.2.4.1): endpoints of the same topic, type and
20/// partition that did not match because of their QoS.
21#[derive(Clone, Debug, Default, PartialEq, Eq)]
22pub struct IncompatibleQosStatus {
23    /// How many such endpoints were found.
24    pub total_count: u32,
25    /// How many since the status was last read.
26    pub total_count_change: u32,
27    /// A policy that was incompatible the last time.
28    pub last_policy_id: Option<QosPolicyId>,
29    /// How many times each policy was incompatible.
30    pub policies: Vec<QosPolicyCount>,
31}
32
33impl IncompatibleQosStatus {
34    /// Counts one incompatible endpoint, for the given policies.
35    pub(crate) fn add(&mut self, policies: &[QosPolicyId]) {
36        self.total_count += 1;
37        self.total_count_change += 1;
38        self.last_policy_id = policies.first().copied();
39        for id in policies {
40            match self.policies.iter_mut().find(|c| c.policy_id == *id) {
41                Some(c) => c.count += 1,
42                None => self.policies.push(QosPolicyCount {
43                    policy_id: *id,
44                    count: 1,
45                }),
46            }
47        }
48    }
49
50    /// Returns the status and resets the change count.
51    pub(crate) fn take(&mut self) -> Self {
52        let s = self.clone();
53        self.total_count_change = 0;
54        s
55    }
56}
57
58/// INCONSISTENT_TOPIC of a topic (DDS 1.4, 2.2.4.1): writers or readers
59/// of the topic found in other participants whose types are not
60/// consistent with the local ones (DDS-XTypes 1.3, 7.6.3.4.2).
61#[derive(Clone, Debug, Default, PartialEq, Eq)]
62pub struct InconsistentTopicStatus {
63    /// How many such endpoints were found.
64    pub total_count: u32,
65    /// How many since the status was last read.
66    pub total_count_change: u32,
67}
68
69impl InconsistentTopicStatus {
70    pub(crate) fn add(&mut self) {
71        self.total_count += 1;
72        self.total_count_change += 1;
73    }
74
75    /// Returns the status and resets the change count.
76    pub(crate) fn take(&mut self) -> Self {
77        let s = self.clone();
78        self.total_count_change = 0;
79        s
80    }
81}
82
83/// Why a reader rejected a sample.
84#[derive(Clone, Copy, Debug, PartialEq, Eq)]
85pub enum SampleRejectedReason {
86    /// `max_instances` was reached.
87    InstancesLimit,
88    /// `max_samples` was reached.
89    SamplesLimit,
90    /// `max_samples_per_instance` was reached.
91    SamplesPerInstanceLimit,
92}
93
94/// SAMPLE_REJECTED of a reader (DDS 1.4, 2.2.4.1): samples dropped
95/// because of its RESOURCE_LIMITS.
96#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
97pub struct SampleRejectedStatus {
98    /// How many samples were rejected.
99    pub total_count: u32,
100    /// How many since the status was last read.
101    pub total_count_change: u32,
102    /// Why the last one was.
103    pub last_reason: Option<SampleRejectedReason>,
104    /// The instance of the last one.
105    pub last_instance_handle: Option<InstanceHandle>,
106}
107
108/// LIVELINESS_LOST of a writer (DDS 1.4, 2.2.4.1): times it failed to
109/// assert its liveliness within its lease, so that readers took it as no
110/// longer alive.
111#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
112pub struct LivelinessLostStatus {
113    /// How many times it became not alive.
114    pub total_count: u32,
115    /// How many since the status was last read.
116    pub total_count_change: u32,
117}
118
119impl LivelinessLostStatus {
120    pub(crate) fn add(&mut self) {
121        self.total_count += 1;
122        self.total_count_change += 1;
123    }
124
125    pub(crate) fn take(&mut self) -> Self {
126        let s = *self;
127        self.total_count_change = 0;
128        s
129    }
130}
131
132/// LIVELINESS_CHANGED of a reader (DDS 1.4, 2.2.4.1): how many of its
133/// matched writers are alive, and how many are not.
134///
135/// A writer counts as alive once it is matched. It becomes not alive
136/// when its lease passes without it asserting its liveliness, and alive
137/// again when it asserts it. A writer that goes away is no longer
138/// counted.
139#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
140pub struct LivelinessChangedStatus {
141    /// The matched writers alive now.
142    pub alive_count: u32,
143    /// The matched writers not alive now.
144    pub not_alive_count: u32,
145    /// The change in `alive_count` since the status was last read.
146    pub alive_count_change: i32,
147    /// The change in `not_alive_count` since the status was last read.
148    pub not_alive_count_change: i32,
149    /// The last writer whose liveliness changed.
150    pub last_publication_handle: Option<Guid>,
151}
152
153impl LivelinessChangedStatus {
154    /// A writer is matched (`Some(true)`, alive), not alive any more
155    /// (`Some(false)`), or gone (`None`), having been alive or not.
156    pub(crate) fn update(&mut self, writer: Guid, was: Option<bool>, now: Option<bool>) {
157        let count = |s: &mut Self, alive: bool, d: i32| {
158            if alive {
159                s.alive_count = s.alive_count.saturating_add_signed(d);
160                s.alive_count_change += d;
161            } else {
162                s.not_alive_count = s.not_alive_count.saturating_add_signed(d);
163                s.not_alive_count_change += d;
164            }
165        };
166        if was == now {
167            return;
168        }
169        if let Some(a) = was {
170            count(self, a, -1);
171        }
172        if let Some(a) = now {
173            count(self, a, 1);
174        }
175        self.last_publication_handle = Some(writer);
176    }
177
178    pub(crate) fn take(&mut self) -> Self {
179        let s = *self;
180        self.alive_count_change = 0;
181        self.not_alive_count_change = 0;
182        s
183    }
184}
185
186/// OFFERED_DEADLINE_MISSED of a writer or REQUESTED_DEADLINE_MISSED of a
187/// reader (DDS 1.4, 2.2.4.1): DEADLINE periods that passed without a
188/// sample of an instance, one count for each period and instance.
189#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
190pub struct DeadlineMissedStatus {
191    /// How many deadlines were missed.
192    pub total_count: u32,
193    /// How many since the status was last read.
194    pub total_count_change: u32,
195    /// The instance of the last one.
196    pub last_instance_handle: Option<InstanceHandle>,
197}
198
199impl DeadlineMissedStatus {
200    pub(crate) fn take(&mut self) -> Self {
201        let s = *self;
202        self.total_count_change = 0;
203        s
204    }
205}
206
207/// Deadlines of instances: when each must next get a sample.
208#[derive(Debug, Default)]
209pub(crate) struct Deadlines {
210    period: Option<std::time::Duration>,
211    due: std::collections::BTreeMap<InstanceHandle, std::time::Instant>,
212    pub(crate) missed: DeadlineMissedStatus,
213}
214
215impl Deadlines {
216    pub(crate) fn new(period: Option<std::time::Duration>) -> Self {
217        Deadlines {
218            period,
219            ..Self::default()
220        }
221    }
222
223    /// The instance got a sample at `now`.
224    pub(crate) fn renew(&mut self, h: InstanceHandle, now: std::time::Instant) {
225        if let Some(p) = self.period {
226            self.due.insert(h, now + p);
227        }
228    }
229
230    /// The instance is no longer expected to get samples.
231    pub(crate) fn remove(&mut self, h: &InstanceHandle) {
232        self.due.remove(h);
233    }
234
235    /// Counts the periods that passed by `now` without a sample, for the
236    /// instances `keep` keeps; the others are dropped. Returns whether
237    /// one was counted.
238    pub(crate) fn check(
239        &mut self,
240        now: std::time::Instant,
241        mut keep: impl FnMut(&InstanceHandle) -> bool,
242    ) -> bool {
243        let Some(p) = self.period else {
244            return false;
245        };
246        let before = self.missed.total_count;
247        self.due.retain(|h, _| keep(h));
248        for (h, due) in self.due.iter_mut() {
249            while *due <= now {
250                self.missed.total_count += 1;
251                self.missed.total_count_change += 1;
252                self.missed.last_instance_handle = Some(*h);
253                *due += p.max(std::time::Duration::from_millis(1));
254            }
255        }
256        self.missed.total_count != before
257    }
258}
259
260/// A set of communication status kinds (DDS 1.4 `StatusMask`, 2.2.4.1),
261/// with the values of the IDL PSM (2.3.3).
262#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
263pub struct StatusMask(pub u32);
264
265impl StatusMask {
266    /// No status.
267    pub const NONE: StatusMask = StatusMask(0);
268    /// Every status.
269    pub const ALL: StatusMask = StatusMask(u32::MAX);
270    /// `INCONSISTENT_TOPIC_STATUS`.
271    pub const INCONSISTENT_TOPIC: StatusMask = StatusMask(1 << 0);
272    /// `OFFERED_DEADLINE_MISSED_STATUS`.
273    pub const OFFERED_DEADLINE_MISSED: StatusMask = StatusMask(1 << 1);
274    /// `REQUESTED_DEADLINE_MISSED_STATUS`.
275    pub const REQUESTED_DEADLINE_MISSED: StatusMask = StatusMask(1 << 2);
276    /// `OFFERED_INCOMPATIBLE_QOS_STATUS`.
277    pub const OFFERED_INCOMPATIBLE_QOS: StatusMask = StatusMask(1 << 5);
278    /// `REQUESTED_INCOMPATIBLE_QOS_STATUS`.
279    pub const REQUESTED_INCOMPATIBLE_QOS: StatusMask = StatusMask(1 << 6);
280    /// `SAMPLE_LOST_STATUS`.
281    pub const SAMPLE_LOST: StatusMask = StatusMask(1 << 7);
282    /// `SAMPLE_REJECTED_STATUS`.
283    pub const SAMPLE_REJECTED: StatusMask = StatusMask(1 << 8);
284    /// `DATA_ON_READERS_STATUS`.
285    pub const DATA_ON_READERS: StatusMask = StatusMask(1 << 9);
286    /// `DATA_AVAILABLE_STATUS`.
287    pub const DATA_AVAILABLE: StatusMask = StatusMask(1 << 10);
288    /// `LIVELINESS_LOST_STATUS`.
289    pub const LIVELINESS_LOST: StatusMask = StatusMask(1 << 11);
290    /// `LIVELINESS_CHANGED_STATUS`.
291    pub const LIVELINESS_CHANGED: StatusMask = StatusMask(1 << 12);
292    /// `PUBLICATION_MATCHED_STATUS`.
293    pub const PUBLICATION_MATCHED: StatusMask = StatusMask(1 << 13);
294    /// `SUBSCRIPTION_MATCHED_STATUS`.
295    pub const SUBSCRIPTION_MATCHED: StatusMask = StatusMask(1 << 14);
296
297    /// Whether every status of `other` is in this set.
298    pub const fn contains(self, other: StatusMask) -> bool {
299        self.0 & other.0 == other.0
300    }
301
302    /// Whether this set and `other` have a status in common.
303    pub const fn intersects(self, other: StatusMask) -> bool {
304        self.0 & other.0 != 0
305    }
306
307    /// Whether the set is empty.
308    pub const fn is_empty(self) -> bool {
309        self.0 == 0
310    }
311
312    /// The statuses of the set, one at a time.
313    pub(crate) fn bits(self) -> impl Iterator<Item = StatusMask> {
314        (0..32)
315            .map(|i| StatusMask(1 << i))
316            .filter(move |b| self.intersects(*b))
317    }
318}
319
320impl std::ops::BitOr for StatusMask {
321    type Output = StatusMask;
322    fn bitor(self, rhs: StatusMask) -> StatusMask {
323        StatusMask(self.0 | rhs.0)
324    }
325}
326
327impl std::ops::BitOrAssign for StatusMask {
328    fn bitor_assign(&mut self, rhs: StatusMask) {
329        self.0 |= rhs.0;
330    }
331}
332
333impl std::ops::BitAnd for StatusMask {
334    type Output = StatusMask;
335    fn bitand(self, rhs: StatusMask) -> StatusMask {
336        StatusMask(self.0 & rhs.0)
337    }
338}
339
340impl std::ops::Not for StatusMask {
341    type Output = StatusMask;
342    fn not(self) -> StatusMask {
343        StatusMask(!self.0)
344    }
345}
346
347/// PUBLICATION_MATCHED of a writer or SUBSCRIPTION_MATCHED of a reader
348/// (DDS 1.4, 2.2.4.1): the endpoints it matched.
349#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
350pub struct MatchedStatus {
351    /// How many times it matched an endpoint.
352    pub total_count: u32,
353    /// How many since the status was last read.
354    pub total_count_change: u32,
355    /// How many endpoints it matches now.
356    pub current_count: u32,
357    /// The change in `current_count` since the status was last read.
358    pub current_count_change: i32,
359    /// The last endpoint matched or unmatched.
360    pub last_handle: Option<Guid>,
361}
362
363impl MatchedStatus {
364    pub(crate) fn matched(&mut self, other: Guid) {
365        self.total_count += 1;
366        self.total_count_change += 1;
367        self.current_count += 1;
368        self.current_count_change += 1;
369        self.last_handle = Some(other);
370    }
371
372    pub(crate) fn unmatched(&mut self, other: Guid) {
373        self.current_count = self.current_count.saturating_sub(1);
374        self.current_count_change -= 1;
375        self.last_handle = Some(other);
376    }
377
378    pub(crate) fn take(&mut self) -> Self {
379        let s = *self;
380        self.total_count_change = 0;
381        self.current_count_change = 0;
382        s
383    }
384}
385
386/// SAMPLE_LOST of a reader (DDS 1.4, 2.2.4.1): samples it never got.
387///
388/// They are counted from the gaps in the sequence numbers of each
389/// writer: samples a best-effort reader missed, and samples a writer no
390/// longer had when a reliable reader asked for them (KEEP_LAST).
391#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
392pub struct SampleLostStatus {
393    /// How many samples were lost.
394    pub total_count: u32,
395    /// How many since the status was last read.
396    pub total_count_change: u32,
397}
398
399impl SampleLostStatus {
400    pub(crate) fn add(&mut self, n: u32) {
401        self.total_count = self.total_count.saturating_add(n);
402        self.total_count_change = self.total_count_change.saturating_add(n);
403    }
404
405    pub(crate) fn take(&mut self) -> Self {
406        let s = *self;
407        self.total_count_change = 0;
408        s
409    }
410}