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
//! Bounded next-hop learning, separate from confirmed transaction ownership.
use std::collections::HashMap;
use bacnet_types::MacAddr;
/// A full cache leaves additional networks using routed local broadcast.
const MAX_LEARNED_ROUTERS: usize = 64;
/// Router MACs learned from admitted terminal responses for remote networks.
/// The notification owner decides admission before this cache is updated.
pub(super) struct LearnedRouterCache {
routers: HashMap<u16, MacAddr>,
}
impl LearnedRouterCache {
pub(super) fn new() -> Self {
Self {
routers: HashMap::new(),
}
}
/// Update an existing DNET even when the cache cannot admit new networks.
pub(super) fn learn_router(&mut self, network: u16, router: &MacAddr) {
if router.is_empty() {
return;
}
if self.routers.len() >= MAX_LEARNED_ROUTERS && !self.routers.contains_key(&network) {
return;
}
self.routers.insert(network, router.clone());
}
pub(super) fn cached_router(&self, network: u16) -> Option<MacAddr> {
self.routers.get(&network).cloned()
}
}