Skip to main content

qualia_core_db/net/acoustic_ble_mesh/
routing.rs

1//! Mesh routing: the inter-network router, routing/forwarding tables, route
2//! discovery and caching, and congestion control (queue management + rate control).
3
4use super::*;
5
6/// Mesh router for inter-network routing
7pub struct MeshRouter {
8    routing_table: RoutingTable,
9    forwarding_table: ForwardingTable,
10    route_discovery: RouteDiscovery,
11    congestion_control: CongestionControl,
12}
13
14/// Routing table
15#[derive(Debug, Clone)]
16pub struct RoutingTable {
17    pub entries: Vec<RouteEntry>,
18}
19
20/// Route entry
21#[derive(Debug, Clone)]
22pub struct RouteEntry {
23    pub destination: u16,
24    pub next_hop: u16,
25    pub metric: u8,
26    pub sequence_number: u16,
27}
28
29/// Forwarding table
30#[derive(Debug, Clone)]
31pub struct ForwardingTable {
32    pub entries: Vec<ForwardingEntry>,
33}
34
35/// Forwarding entry
36#[derive(Debug, Clone)]
37pub struct ForwardingEntry {
38    pub destination: String,
39    pub next_hop: String,
40    pub interface: NetworkInterface,
41    pub metric: u16,
42    pub ttl: u8,
43}
44
45/// Route discovery
46pub struct RouteDiscovery {
47    pub discovery_protocol: DiscoveryProtocol,
48    pub route_cache: RouteCache,
49    pub discovery_timeout: Duration,
50}
51
52/// Discovery protocols
53#[derive(Debug, Clone, PartialEq)]
54pub enum DiscoveryProtocol {
55    Proactive,
56    Reactive,
57    Hybrid,
58}
59
60/// Route cache
61#[derive(Debug, Clone)]
62pub struct RouteCache {
63    pub entries: Vec<CachedRoute>,
64}
65
66/// Cached route
67#[derive(Debug, Clone)]
68pub struct CachedRoute {
69    pub destination: String,
70    pub route: Vec<String>,
71    pub metric: u16,
72    pub timestamp: Instant,
73    pub ttl: Duration,
74}
75
76/// Congestion control
77pub struct CongestionControl {
78    pub algorithm: CongestionAlgorithm,
79    pub queue_management: QueueManagement,
80    pub rate_control: RateControl,
81}
82
83/// Congestion algorithms
84#[derive(Debug, Clone, PartialEq)]
85pub enum CongestionAlgorithm {
86    DropTail,
87    RED,
88    ECN,
89    Custom,
90}
91
92/// Queue management
93#[derive(Debug, Clone)]
94pub struct QueueManagement {
95    pub queue_size: usize,
96    pub drop_policy: DropPolicy,
97}
98
99/// Drop policies
100#[derive(Debug, Clone, PartialEq)]
101pub enum DropPolicy {
102    DropTail,
103    DropHead,
104    Random,
105    Priority,
106}
107
108/// Rate control
109#[derive(Debug, Clone)]
110pub struct RateControl {
111    pub token_bucket: TokenBucket,
112    pub leaky_bucket: LeakyBucket,
113}
114
115/// Token bucket
116#[derive(Debug, Clone)]
117pub struct TokenBucket {
118    pub capacity: u32,
119    pub rate: u32,
120    pub tokens: u32,
121    pub last_update: Instant,
122}
123
124/// Leaky bucket
125#[derive(Debug, Clone)]
126pub struct LeakyBucket {
127    pub capacity: u32,
128    pub rate: u32,
129    pub level: u32,
130    pub last_update: Instant,
131}
132
133impl MeshRouter {
134    pub fn new() -> Self {
135        Self {
136            routing_table: RoutingTable::new(),
137            forwarding_table: ForwardingTable::new(),
138            route_discovery: RouteDiscovery::new(),
139            congestion_control: CongestionControl::new(),
140        }
141    }
142
143    pub fn initialize(&mut self) -> Result<(), MeshError> {
144        self.congestion_control.initialize()?;
145        Ok(())
146    }
147
148    pub fn get_route_count(&self) -> u32 {
149        self.routing_table.entries.len() as u32
150    }
151
152    pub fn forwarding_table(&self) -> &ForwardingTable {
153        &self.forwarding_table
154    }
155
156    pub fn route_discovery(&self) -> &RouteDiscovery {
157        &self.route_discovery
158    }
159
160    pub fn congestion_control(&self) -> &CongestionControl {
161        &self.congestion_control
162    }
163
164    pub fn optimize_routes(&mut self) -> Result<(), MeshError> {
165        // Prune stale forwarding entries (expired TTL or empty next hop).
166        self.forwarding_table
167            .entries
168            .retain(|e| e.ttl > 0 && !e.next_hop.is_empty());
169        // Decrement TTL on remaining entries.
170        for e in &mut self.forwarding_table.entries {
171            e.ttl = e.ttl.saturating_sub(1);
172        }
173        Ok(())
174    }
175}
176
177impl RoutingTable {
178    pub fn new() -> Self {
179        Self {
180            entries: Vec::new(),
181        }
182    }
183}
184
185impl ForwardingTable {
186    pub fn new() -> Self {
187        Self {
188            entries: Vec::new(),
189        }
190    }
191}
192
193impl RouteDiscovery {
194    pub fn new() -> Self {
195        Self {
196            discovery_protocol: DiscoveryProtocol::Hybrid,
197            route_cache: RouteCache::new(),
198            discovery_timeout: Duration::from_secs(30),
199        }
200    }
201}
202
203impl RouteCache {
204    pub fn new() -> Self {
205        Self {
206            entries: Vec::new(),
207        }
208    }
209}
210
211impl CongestionControl {
212    pub fn new() -> Self {
213        Self {
214            algorithm: CongestionAlgorithm::RED,
215            queue_management: QueueManagement::new(),
216            rate_control: RateControl::new(),
217        }
218    }
219
220    pub fn initialize(&mut self) -> Result<(), MeshError> {
221        Ok(())
222    }
223
224    pub fn algorithm(&self) -> &CongestionAlgorithm {
225        &self.algorithm
226    }
227
228    pub fn queue_management(&self) -> &QueueManagement {
229        &self.queue_management
230    }
231
232    pub fn rate_control(&self) -> &RateControl {
233        &self.rate_control
234    }
235}
236
237impl QueueManagement {
238    pub fn new() -> Self {
239        Self {
240            queue_size: 1000,
241            drop_policy: DropPolicy::DropTail,
242        }
243    }
244}
245
246impl RateControl {
247    pub fn new() -> Self {
248        Self {
249            token_bucket: TokenBucket::new(),
250            leaky_bucket: LeakyBucket::new(),
251        }
252    }
253}
254
255impl TokenBucket {
256    pub fn new() -> Self {
257        Self {
258            capacity: 1000,
259            rate: 100,
260            tokens: 1000,
261            last_update: Instant::now(),
262        }
263    }
264}
265
266impl LeakyBucket {
267    pub fn new() -> Self {
268        Self {
269            capacity: 1000,
270            rate: 100,
271            level: 0,
272            last_update: Instant::now(),
273        }
274    }
275}