oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
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
515
516
//! Reliable stateful RTPS reader with HEARTBEAT / ACKNACK cycle.

use std::net::SocketAddr;
use std::time::Duration;

use crate::protocol::dds::byte_cursor::Endianness;
use crate::protocol::dds::message::submessage::AckNack;
use crate::protocol::dds::message::{Message, MessageHeader, Submessage};
use crate::protocol::dds::transport::error::TransportError;
use crate::protocol::dds::transport::UdpTransport;
use crate::protocol::dds::types::guid::{Guid, PROTOCOL_VERSION_2_3, VENDOR_ID_OXICTL};
use crate::protocol::dds::types::locator::Locator;
use crate::protocol::dds::types::sequence::SequenceNumber;

use super::error::StatefulError;
use super::writer_proxy::WriterProxy;

/// Configuration for a [`StatefulReader`].
pub struct ReaderConfig {
    /// The GUID of this reader.
    pub guid: Guid,
    /// If `true`, send ACKNACK even for final (F-bit=1) HEARTBEATs.
    pub always_ack: bool,
}

impl ReaderConfig {
    /// Create a reader config with `always_ack = false`.
    pub fn new(guid: Guid) -> Self {
        Self {
            guid,
            always_ack: false,
        }
    }
}

/// A DATA sample delivered by a [`StatefulReader`].
pub struct ReceivedSample {
    /// The reconstructed GUID of the remote writer.
    pub writer_guid: Guid,
    /// The RTPS sequence number of this sample.
    pub sequence_number: SequenceNumber,
    /// The serialized payload bytes.
    pub data: Vec<u8>,
    /// `true` if the DATA submessage carried inline QoS.
    pub has_inline_qos: bool,
}

/// Reliable stateful RTPS reader.
///
/// Maintains a matched set of [`WriterProxy`] entries, records received SNs,
/// and sends ACKNACK submessages in response to HEARTBEAT submessages.
pub struct StatefulReader {
    config: ReaderConfig,
    transport: UdpTransport,
    writers: Vec<WriterProxy>,
}

impl StatefulReader {
    /// Create a new reader from `config`, using `transport` for network I/O.
    pub fn new(config: ReaderConfig, transport: UdpTransport) -> Self {
        Self {
            config,
            transport,
            writers: Vec::new(),
        }
    }

    // ── Matched-writer management ─────────────────────────────────────────────

    /// Add a matched writer (for tracking & ACKNACK sending).
    pub fn add_matched_writer(&mut self, guid: Guid, unicast_locators: Vec<Locator>) {
        if let Some(existing) = self
            .writers
            .iter_mut()
            .find(|w| w.remote_writer_guid == guid)
        {
            existing.unicast_locators = unicast_locators;
        } else {
            self.writers.push(WriterProxy::new(guid, unicast_locators));
        }
    }

    /// Remove a matched writer by GUID.
    pub fn remove_matched_writer(&mut self, guid: &Guid) {
        self.writers.retain(|w| &w.remote_writer_guid != guid);
    }

    // ── Receiving ─────────────────────────────────────────────────────────────

    /// Non-blocking receive.
    ///
    /// Processes all submessages in one incoming RTPS packet:
    /// - `DATA` — records the SN and returns a [`ReceivedSample`].
    /// - `HEARTBEAT` — sends an ACKNACK if `!final_flag || always_ack`.
    ///
    /// Returns `Ok(None)` on timeout / WouldBlock.
    pub fn recv(&mut self) -> Result<Option<ReceivedSample>, StatefulError> {
        let mut buf = vec![0u8; 65536];

        // ── Phase 1: parse the incoming packet ────────────────────────────────
        // We need to own everything that comes out of the parsed message before
        // `buf` is dropped, since Message<'_> borrows from buf.
        enum Action {
            Data {
                writer_guid: Guid,
                sn: SequenceNumber,
                payload: Vec<u8>,
                has_inline_qos: bool,
            },
            Heartbeat {
                writer_guid: Guid,
                writer_id: crate::protocol::dds::types::guid::EntityId,
                last_sn: SequenceNumber,
                send_ack: bool,
            },
        }

        let actions: Vec<Action> = match self.transport.recv_into(&mut buf) {
            Ok((msg, _from)) => {
                let sender_prefix = msg.header.guid_prefix;
                let mut acts = Vec::new();
                for sub in msg.submessages.iter() {
                    match sub {
                        Submessage::Data(data) => {
                            let writer_guid = Guid {
                                prefix: sender_prefix,
                                entity_id: data.writer_id,
                            };
                            acts.push(Action::Data {
                                writer_guid,
                                sn: data.writer_sn,
                                payload: data.serialized_payload.to_vec(),
                                has_inline_qos: data.inline_qos_flag,
                            });
                        }
                        Submessage::Heartbeat(hb) => {
                            let writer_guid = Guid {
                                prefix: sender_prefix,
                                entity_id: hb.writer_id,
                            };
                            let send_ack = !hb.final_flag || self.config.always_ack;
                            acts.push(Action::Heartbeat {
                                writer_guid,
                                writer_id: hb.writer_id,
                                last_sn: hb.last_sn,
                                send_ack,
                            });
                        }
                        _ => {}
                    }
                }
                acts
            }
            Err(e) => {
                if let TransportError::Io(ref io_err) = e {
                    if matches!(
                        io_err.kind(),
                        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
                    ) {
                        return Ok(None);
                    }
                }
                return Err(StatefulError::Transport(e));
            }
        };

        // buf lifetime ends here; all data is now owned in `actions`.

        // ── Phase 2: update state and optionally send ACKNACK ─────────────────
        let mut found_sample: Option<ReceivedSample> = None;

        for action in actions {
            match action {
                Action::Data {
                    writer_guid,
                    sn,
                    payload,
                    has_inline_qos,
                } => {
                    // Find or create a WriterProxy for this writer.
                    if !self
                        .writers
                        .iter()
                        .any(|w| w.remote_writer_guid == writer_guid)
                    {
                        self.writers.push(WriterProxy::new(writer_guid, Vec::new()));
                    }
                    if let Some(proxy) = self
                        .writers
                        .iter_mut()
                        .find(|w| w.remote_writer_guid == writer_guid)
                    {
                        proxy.received(sn);
                    }
                    // Return the first DATA sample found.
                    if found_sample.is_none() {
                        found_sample = Some(ReceivedSample {
                            writer_guid,
                            sequence_number: sn,
                            data: payload,
                            has_inline_qos,
                        });
                    }
                }
                Action::Heartbeat {
                    writer_guid,
                    writer_id,
                    last_sn,
                    send_ack,
                } => {
                    if !send_ack {
                        continue;
                    }

                    // Find or create a WriterProxy.
                    if !self
                        .writers
                        .iter()
                        .any(|w| w.remote_writer_guid == writer_guid)
                    {
                        self.writers.push(WriterProxy::new(writer_guid, Vec::new()));
                    }

                    // Build and send ACKNACK (collect data before sending).
                    let (sn_set, count, locators) = {
                        if let Some(proxy) = self
                            .writers
                            .iter_mut()
                            .find(|w| w.remote_writer_guid == writer_guid)
                        {
                            let sn_set = proxy.build_missing_sn_set(last_sn);
                            let count = proxy.next_acknack_count();
                            let locators = proxy.unicast_locators.clone();
                            (sn_set, count, locators)
                        } else {
                            continue;
                        }
                    };

                    let ack = AckNack {
                        endianness: Endianness::Little,
                        final_flag: true,
                        reader_id: self.config.guid.entity_id,
                        writer_id,
                        reader_sn_state: sn_set,
                        count,
                    };

                    let mut subs: heapless::Vec<Submessage<'_>, 64> = heapless::Vec::new();
                    subs.push(Submessage::AckNack(ack))
                        .map_err(|_| StatefulError::BufferTooSmall)?;

                    let msg = Message {
                        header: MessageHeader {
                            version: PROTOCOL_VERSION_2_3,
                            vendor_id: VENDOR_ID_OXICTL,
                            guid_prefix: self.config.guid.prefix,
                        },
                        submessages: subs,
                    };

                    for locator in &locators {
                        self.transport
                            .send_to(&msg, locator)
                            .map_err(StatefulError::Transport)?;
                    }
                    // When the proxy has no locators, send to ENTITYID_UNKNOWN as a
                    // best-effort multicast (noop here if locators is empty — the
                    // test that exercises ACKNACK path uses explicit locators).
                    if locators.is_empty() {
                        // Nowhere to send; silently skip.
                    }
                }
            }
        }

        Ok(found_sample)
    }

    // ── Accessors ─────────────────────────────────────────────────────────────

    /// Set or clear the read timeout on the underlying socket.
    pub fn set_read_timeout(&mut self, dur: Option<Duration>) -> Result<(), StatefulError> {
        self.transport
            .set_read_timeout(dur)
            .map_err(StatefulError::Transport)
    }

    /// The GUID of this reader.
    pub fn own_guid(&self) -> &Guid {
        &self.config.guid
    }

    /// Returns the local socket address this reader is bound to.
    pub fn local_addr(&self) -> Result<SocketAddr, StatefulError> {
        self.transport
            .local_addr()
            .map_err(StatefulError::Transport)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::dds::message::submessage::Heartbeat;
    use crate::protocol::dds::message::{Message, MessageHeader, Submessage};
    use crate::protocol::dds::stateful::writer::{StatefulWriter, WriterConfig};
    use crate::protocol::dds::transport::TransportConfig;
    use crate::protocol::dds::types::guid::{EntityId, GuidPrefix, ENTITYID_UNKNOWN};
    use crate::protocol::dds::types::sequence::SequenceNumber;
    use std::net::{SocketAddr, UdpSocket};
    use std::time::Duration;

    fn sn(v: i64) -> SequenceNumber {
        SequenceNumber::new(v)
    }

    fn make_guid(prefix_byte: u8, key: u8) -> Guid {
        Guid {
            prefix: GuidPrefix([prefix_byte; 12]),
            entity_id: EntityId {
                entity_key: [0x00, 0x00, key],
                entity_kind: 0x02,
            },
        }
    }

    fn make_writer(guid: Guid) -> StatefulWriter {
        let cfg = TransportConfig::unicast(SocketAddr::from(([127, 0, 0, 1], 0)));
        let transport = UdpTransport::new(cfg).unwrap();
        StatefulWriter::new(WriterConfig::new(guid).with_history_capacity(8), transport)
    }

    fn make_reader(guid: Guid) -> StatefulReader {
        let cfg = TransportConfig {
            read_timeout: Some(Duration::from_millis(300)),
            ..TransportConfig::unicast(SocketAddr::from(([127, 0, 0, 1], 0)))
        };
        let transport = UdpTransport::new(cfg).unwrap();
        StatefulReader::new(ReaderConfig::new(guid), transport)
    }

    fn locator_of(addr: SocketAddr) -> Locator {
        Locator::udp_v4(addr.port() as u32, [127, 0, 0, 1])
    }

    // ── Test 1: writer + reader loopback — 3 samples ──────────────────────────
    #[test]
    fn reader_loopback_three_samples() {
        let writer_guid = make_guid(0x11, 0x01);
        let reader_guid = make_guid(0x22, 0x07);

        let mut writer = make_writer(writer_guid);
        let mut reader = make_reader(reader_guid);

        let reader_addr = reader.local_addr().unwrap();
        writer.add_matched_reader(reader_guid, vec![locator_of(reader_addr)]);

        let payloads: [&[u8]; 3] = [b"alpha", b"beta", b"gamma"];
        for p in &payloads {
            writer.write(p).unwrap();
        }

        let mut received: Vec<ReceivedSample> = Vec::new();
        for _ in 0..3 {
            if let Some(sample) = reader.recv().unwrap() {
                received.push(sample);
            }
        }

        assert_eq!(received.len(), 3);
        assert_eq!(received[0].data, b"alpha");
        assert_eq!(received[1].data, b"beta");
        assert_eq!(received[2].data, b"gamma");
        assert_eq!(received[0].sequence_number, sn(1));
        assert_eq!(received[1].sequence_number, sn(2));
        assert_eq!(received[2].sequence_number, sn(3));
    }

    // ── Test 2: reader handles HEARTBEAT and sends ACKNACK ────────────────────
    #[test]
    fn reader_handles_heartbeat_sends_acknack() {
        // Raw UDP socket to act as the writer.
        let writer_sock = UdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 0))).unwrap();
        writer_sock
            .set_read_timeout(Some(Duration::from_millis(500)))
            .unwrap();
        let writer_addr = writer_sock.local_addr().unwrap();

        let writer_guid = make_guid(0x33, 0x01);
        let reader_guid = make_guid(0x44, 0x07);

        // Create a reader that always responds to heartbeats.
        let cfg = TransportConfig {
            read_timeout: Some(Duration::from_millis(500)),
            ..TransportConfig::unicast(SocketAddr::from(([127, 0, 0, 1], 0)))
        };
        let mut reader = StatefulReader::new(
            ReaderConfig {
                guid: reader_guid,
                always_ack: true,
            },
            UdpTransport::new(cfg).unwrap(),
        );

        let reader_addr = reader.local_addr().unwrap();
        reader.add_matched_writer(writer_guid, vec![locator_of(writer_addr)]);

        // Craft and send a synthetic HEARTBEAT from writer → reader.
        let hb = Heartbeat {
            endianness: Endianness::Little,
            final_flag: true, // normally final means no ack needed, but always_ack=true
            liveliness_flag: false,
            group_info_flag: false,
            reader_id: ENTITYID_UNKNOWN,
            writer_id: writer_guid.entity_id,
            first_sn: sn(1),
            last_sn: sn(3),
            count: 1,
        };

        let mut subs: heapless::Vec<Submessage<'_>, 64> = heapless::Vec::new();
        subs.push(Submessage::Heartbeat(hb)).unwrap();
        let hb_msg = Message {
            header: MessageHeader {
                version: crate::protocol::dds::types::guid::PROTOCOL_VERSION_2_3,
                vendor_id: crate::protocol::dds::types::guid::VENDOR_ID_OXICTL,
                guid_prefix: writer_guid.prefix,
            },
            submessages: subs,
        };

        // Serialize and send the heartbeat directly via writer_sock.
        let mut raw_buf = [0u8; 65535];
        let n = crate::protocol::dds::serialize_message(&hb_msg, &mut raw_buf).unwrap();
        writer_sock.send_to(&raw_buf[..n], reader_addr).unwrap();

        // Reader should process the HEARTBEAT and send back an ACKNACK.
        let result = reader.recv().unwrap();
        // HEARTBEAT alone — no DATA, so result is None.
        assert!(result.is_none());

        // Now the writer_sock should have received an ACKNACK from the reader.
        let mut ack_buf = [0u8; 65535];
        let (m, _) = writer_sock.recv_from(&mut ack_buf).unwrap();
        let ack_msg = crate::protocol::dds::parse_message(&ack_buf[..m]).unwrap();

        let has_acknack = ack_msg
            .submessages
            .iter()
            .any(|s| matches!(s, Submessage::AckNack(a) if a.writer_id == writer_guid.entity_id));
        assert!(
            has_acknack,
            "expected ACKNACK targeting the writer entity ID"
        );
    }

    // ── Test 3: full HEARTBEAT → ACKNACK cycle ────────────────────────────────
    #[test]
    fn writer_reader_heartbeat_ack_cycle() {
        let writer_guid = make_guid(0x55, 0x01);
        let reader_guid = make_guid(0x66, 0x07);

        let mut writer = {
            let cfg = TransportConfig {
                read_timeout: Some(Duration::from_millis(300)),
                ..TransportConfig::unicast(SocketAddr::from(([127, 0, 0, 1], 0)))
            };
            let transport = UdpTransport::new(cfg).unwrap();
            StatefulWriter::new(
                WriterConfig::new(writer_guid).with_history_capacity(8),
                transport,
            )
        };

        let mut reader = {
            let cfg = TransportConfig {
                read_timeout: Some(Duration::from_millis(300)),
                ..TransportConfig::unicast(SocketAddr::from(([127, 0, 0, 1], 0)))
            };
            let transport = UdpTransport::new(cfg).unwrap();
            StatefulReader::new(
                ReaderConfig {
                    guid: reader_guid,
                    always_ack: false,
                },
                transport,
            )
        };

        let writer_addr = writer.local_addr().unwrap();
        let reader_addr = reader.local_addr().unwrap();

        writer.add_matched_reader(reader_guid, vec![locator_of(reader_addr)]);
        reader.add_matched_writer(writer_guid, vec![locator_of(writer_addr)]);

        // Step 1: writer sends DATA.
        writer.write(b"payload").unwrap();

        // Step 2: reader receives DATA.
        let sample = reader.recv().unwrap();
        assert!(sample.is_some());
        assert_eq!(sample.unwrap().data, b"payload");

        // Step 3: writer sends HEARTBEAT (final_flag=false → reader MUST ack).
        writer.send_heartbeat().unwrap();

        // Step 4: reader receives HEARTBEAT and replies with ACKNACK.
        let no_data = reader.recv().unwrap();
        assert!(no_data.is_none(), "HEARTBEAT should not yield a sample");

        // Step 5: writer processes the ACKNACK.
        writer.process_incoming().unwrap();
        // Reaching here without panic means the full cycle succeeded.
    }
}