qualia_core_db/net/acoustic_ble_mesh/
routing.rs1use super::*;
5
6pub struct MeshRouter {
8 routing_table: RoutingTable,
9 forwarding_table: ForwardingTable,
10 route_discovery: RouteDiscovery,
11 congestion_control: CongestionControl,
12}
13
14#[derive(Debug, Clone)]
16pub struct RoutingTable {
17 pub entries: Vec<RouteEntry>,
18}
19
20#[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#[derive(Debug, Clone)]
31pub struct ForwardingTable {
32 pub entries: Vec<ForwardingEntry>,
33}
34
35#[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
45pub struct RouteDiscovery {
47 pub discovery_protocol: DiscoveryProtocol,
48 pub route_cache: RouteCache,
49 pub discovery_timeout: Duration,
50}
51
52#[derive(Debug, Clone, PartialEq)]
54pub enum DiscoveryProtocol {
55 Proactive,
56 Reactive,
57 Hybrid,
58}
59
60#[derive(Debug, Clone)]
62pub struct RouteCache {
63 pub entries: Vec<CachedRoute>,
64}
65
66#[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
76pub struct CongestionControl {
78 pub algorithm: CongestionAlgorithm,
79 pub queue_management: QueueManagement,
80 pub rate_control: RateControl,
81}
82
83#[derive(Debug, Clone, PartialEq)]
85pub enum CongestionAlgorithm {
86 DropTail,
87 RED,
88 ECN,
89 Custom,
90}
91
92#[derive(Debug, Clone)]
94pub struct QueueManagement {
95 pub queue_size: usize,
96 pub drop_policy: DropPolicy,
97}
98
99#[derive(Debug, Clone, PartialEq)]
101pub enum DropPolicy {
102 DropTail,
103 DropHead,
104 Random,
105 Priority,
106}
107
108#[derive(Debug, Clone)]
110pub struct RateControl {
111 pub token_bucket: TokenBucket,
112 pub leaky_bucket: LeakyBucket,
113}
114
115#[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#[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 self.forwarding_table
167 .entries
168 .retain(|e| e.ttl > 0 && !e.next_hop.is_empty());
169 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}