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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use super::SocketHandle;
use crate::socket::PollAt;
use crate::time::{Duration, Instant};
use crate::wire::IpAddress;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
enum NeighborState {
Active,
Waiting {
neighbor: IpAddress,
silent_until: Instant,
},
}
impl Default for NeighborState {
fn default() -> Self {
NeighborState::Active
}
}
#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub(crate) struct Meta {
pub(crate) handle: SocketHandle,
neighbor_state: NeighborState,
}
impl Meta {
pub(crate) const DISCOVERY_SILENT_TIME: Duration = Duration::from_millis(3_000);
pub(crate) fn poll_at<F>(&self, socket_poll_at: PollAt, has_neighbor: F) -> PollAt
where
F: Fn(IpAddress) -> bool,
{
match self.neighbor_state {
NeighborState::Active => socket_poll_at,
NeighborState::Waiting { neighbor, .. } if has_neighbor(neighbor) => socket_poll_at,
NeighborState::Waiting { silent_until, .. } => PollAt::Time(silent_until),
}
}
pub(crate) fn egress_permitted<F>(&mut self, timestamp: Instant, has_neighbor: F) -> bool
where
F: Fn(IpAddress) -> bool,
{
match self.neighbor_state {
NeighborState::Active => true,
NeighborState::Waiting {
neighbor,
silent_until,
} => {
if has_neighbor(neighbor) {
net_trace!(
"{}: neighbor {} discovered, unsilencing",
self.handle,
neighbor
);
self.neighbor_state = NeighborState::Active;
true
} else if timestamp >= silent_until {
net_trace!(
"{}: neighbor {} silence timer expired, rediscovering",
self.handle,
neighbor
);
true
} else {
false
}
}
}
}
pub(crate) fn neighbor_missing(&mut self, timestamp: Instant, neighbor: IpAddress) {
net_trace!(
"{}: neighbor {} missing, silencing until t+{}",
self.handle,
neighbor,
Self::DISCOVERY_SILENT_TIME
);
self.neighbor_state = NeighborState::Waiting {
neighbor,
silent_until: timestamp + Self::DISCOVERY_SILENT_TIME,
};
}
}