Skip to main content

ace_sim/
tcp_runner.rs

1//! TcpSimRunner - drives SimNodeErased slices over a TcpSimBus.
2//!
3//! Extends the SimRunner concept for TCP-aware scenarios. Alongside message delivery it:
4//!     - Delivers TcpEvents to nodes that implement TcpEventHandler
5//!     - Handles connection state transitions visible to nodes
6//!     - Drains node outboxes back onto the TCP bus (rejected if disconnected)
7
8// region: Imports
9
10use crate::{
11    clock::{Duration, Instant},
12    io::NodeAddress,
13    node::SimNodeErased,
14    tcp_bus::{TcpEvent, TcpSimBus},
15};
16
17// endregion: Imports
18
19// region: TcpEventHandler
20
21/// Optional trait for nodes that need to observe TCP connection events.
22///
23/// Nodes on TCP bus may implement this to react to connection establishent, reset, and closure.
24/// The `TcpSimRunner` calls this after deliverying messages on each tick.
25///
26/// Not all nodes need TCP event awareness - only the `DoipTester` and gateway face nodes need it.
27/// Nodes that do not implement this trait simply ignore connection events.
28pub trait TcpEventHandler {
29    fn on_tcp_event(&mut self, event: &TcpEvent, now: Instant);
30}
31
32// endregion: TcpEventHandler
33
34// region: TcpSimRunner
35
36/// Drives `SimNodeErased` slices over a `TcpSimBus`.
37///
38/// `N` - max message payload bytes
39/// `Q` - max messages in-flight on the bus
40pub struct TcpSimRunner<
41    const MAX_DATA: usize,
42    const MAX_QUEUED: usize,
43    const TCP_MAX_EVENTS: usize,
44    const MAX_OUTBOX: usize,
45> {
46    bus: TcpSimBus<MAX_DATA, MAX_QUEUED, TCP_MAX_EVENTS>,
47}
48
49impl<
50        const MAX_DATA: usize,
51        const MAX_QUEUED: usize,
52        const TCP_MAX_EVENTS: usize,
53        const MAX_OUTBOX: usize,
54    > TcpSimRunner<MAX_DATA, MAX_QUEUED, TCP_MAX_EVENTS, MAX_OUTBOX>
55{
56    pub fn new(bus: TcpSimBus<MAX_DATA, MAX_QUEUED, TCP_MAX_EVENTS>) -> Self {
57        Self { bus }
58    }
59
60    pub fn bus(&mut self) -> &mut TcpSimBus<MAX_DATA, MAX_QUEUED, TCP_MAX_EVENTS> {
61        &mut self.bus
62    }
63
64    pub fn now(&self) -> Instant {
65        self.bus.now()
66    }
67
68    /// Ticks the simulation by `duration`.
69    ///
70    /// Order of operations per tick:
71    ///     1. Advance bus clock, apply TCP fault injection, deliver due messages
72    ///     2. Deliver TCP connection events to nodes implementing `TcpEventHandler`
73    ///     3. Tick all nodes for time-based transitions
74    ///     4. Drain node outboxes and enqueue onto the bus
75    ///
76    /// Returns the number of messages delivered.
77    pub fn tick(
78        &mut self,
79        nodes: &mut [&mut dyn SimNodeErased<MAX_DATA, MAX_OUTBOX>],
80        tcp_event_nodes: &mut [&mut dyn TcpEventHandler],
81        duration: Duration,
82    ) -> usize {
83        let delivered = self.bus.tick(duration);
84        let now = self.bus.now();
85        let mut count = 0;
86
87        for envelope in &delivered {
88            for node in nodes.iter_mut() {
89                if *node.address() == envelope.dst {
90                    node.handle(&envelope.src, &envelope.data, now);
91                    count += 1;
92                }
93            }
94        }
95
96        let tcp_events: heapless::Vec<TcpEvent, TCP_MAX_EVENTS> = self.bus.drain_events().collect();
97        for event in &tcp_events {
98            for handler in tcp_event_nodes.iter_mut() {
99                handler.on_tcp_event(event, now);
100            }
101        }
102
103        for node in nodes.iter_mut() {
104            node.tick(now);
105        }
106
107        let mut outbox: heapless::Vec<(NodeAddress, heapless::Vec<u8, MAX_DATA>), MAX_OUTBOX> =
108            heapless::Vec::new();
109        for node in nodes.iter_mut() {
110            outbox.clear();
111            node.drain_outbox(&mut outbox);
112
113            let src = node.address().clone();
114
115            for (dst, data) in outbox.iter() {
116                self.bus.send(src.clone(), dst.clone(), data);
117            }
118        }
119
120        count
121    }
122}
123
124// endregion: TcpSimRunner