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 {
136 Self {
137 routing_table: RoutingTable::new(),
138 forwarding_table: ForwardingTable::new(),
139 route_discovery: RouteDiscovery::new(),
140 congestion_control: CongestionControl::new(),
141 }
142 }
143
144 pub fn initialize(&mut self) -> Result<(), MeshError> {
146 self.congestion_control.initialize()?;
147 Ok(())
148 }
149
150 pub fn get_route_count(&self) -> u32 {
152 self.routing_table.entries.len() as u32
153 }
154
155 pub fn forwarding_table(&self) -> &ForwardingTable {
157 &self.forwarding_table
158 }
159
160 pub fn route_discovery(&self) -> &RouteDiscovery {
162 &self.route_discovery
163 }
164
165 pub fn congestion_control(&self) -> &CongestionControl {
167 &self.congestion_control
168 }
169
170 pub fn optimize_routes(&mut self) -> Result<(), MeshError> {
172 self.forwarding_table
174 .entries
175 .retain(|e| e.ttl > 0 && !e.next_hop.is_empty());
176 for e in &mut self.forwarding_table.entries {
178 e.ttl = e.ttl.saturating_sub(1);
179 }
180 Ok(())
181 }
182}
183
184impl RoutingTable {
185 pub fn new() -> Self {
187 Self {
188 entries: Vec::new(),
189 }
190 }
191}
192
193impl ForwardingTable {
194 pub fn new() -> Self {
196 Self {
197 entries: Vec::new(),
198 }
199 }
200}
201
202impl RouteDiscovery {
203 pub fn new() -> Self {
205 Self {
206 discovery_protocol: DiscoveryProtocol::Hybrid,
207 route_cache: RouteCache::new(),
208 discovery_timeout: Duration::from_secs(30),
209 }
210 }
211}
212
213impl RouteCache {
214 pub fn new() -> Self {
216 Self {
217 entries: Vec::new(),
218 }
219 }
220}
221
222impl CongestionControl {
223 pub fn new() -> Self {
225 Self {
226 algorithm: CongestionAlgorithm::RED,
227 queue_management: QueueManagement::new(),
228 rate_control: RateControl::new(),
229 }
230 }
231
232 pub fn initialize(&mut self) -> Result<(), MeshError> {
234 Ok(())
235 }
236
237 pub fn algorithm(&self) -> &CongestionAlgorithm {
239 &self.algorithm
240 }
241
242 pub fn queue_management(&self) -> &QueueManagement {
244 &self.queue_management
245 }
246
247 pub fn rate_control(&self) -> &RateControl {
249 &self.rate_control
250 }
251}
252
253impl QueueManagement {
254 pub fn new() -> Self {
256 Self {
257 queue_size: 1000,
258 drop_policy: DropPolicy::DropTail,
259 }
260 }
261}
262
263impl RateControl {
264 pub fn new() -> Self {
266 Self {
267 token_bucket: TokenBucket::new(),
268 leaky_bucket: LeakyBucket::new(),
269 }
270 }
271}
272
273impl TokenBucket {
274 pub fn new() -> Self {
276 Self {
277 capacity: 1000,
278 rate: 100,
279 tokens: 1000,
280 last_update: Instant::now(),
281 }
282 }
283}
284
285impl LeakyBucket {
286 pub fn new() -> Self {
288 Self {
289 capacity: 1000,
290 rate: 100,
291 level: 0,
292 last_update: Instant::now(),
293 }
294 }
295}