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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
use crate::config::Config;
use crate::storage::Device;
use dashmap::DashMap;
use ipnetwork::IpNetwork;
use std::fmt::Display;
use time::{Duration, Instant};
use tokio::sync::broadcast::Sender;
use tracing::{debug, error, trace};
pub type StatusMap = DashMap<String, Value>;
#[derive(Debug, Clone)]
pub struct Value {
pub ip: IpNetwork,
pub eta: i64,
pub online: bool,
}
pub async fn spawn(
tx: Sender<BroadcastCommand>,
config: &Config,
device: Device,
uuid: String,
ping_map: &StatusMap,
) {
let timer = Instant::now();
let payload = [0; 8];
let mut msg: Option<BroadcastCommand> = None;
while msg.is_none() {
let ping = surge_ping::ping(device.ip.ip(), &payload).await;
if let Err(ping) = ping {
let ping_timeout = matches!(ping, surge_ping::SurgeError::Timeout { .. });
if !ping_timeout {
error!("{}", ping.to_string());
msg = Some(BroadcastCommand::error(uuid.clone()));
}
if timer.elapsed() >= Duration::minutes(config.pingtimeout) {
msg = Some(BroadcastCommand::timeout(uuid.clone()));
}
} else {
let (_, duration) = ping
.map_err(|err| error!("{}", err.to_string()))
.expect("fatal error");
debug!("ping took {:?}", duration);
msg = Some(BroadcastCommand::success(uuid.clone()));
};
}
trace!(?msg);
let msg = msg.expect("fatal error");
let _ = tx.send(msg.clone());
if msg.command == BroadcastCommands::Success {
if timer.elapsed().whole_seconds() > config.pingthreshold {
let newtimes = if let Some(mut oldtimes) = device.times {
oldtimes.push(timer.elapsed().whole_seconds());
oldtimes
} else {
vec![timer.elapsed().whole_seconds()]
};
let updatedev = Device {
id: device.id,
mac: device.mac,
broadcast_addr: device.broadcast_addr,
ip: device.ip,
times: Some(newtimes),
};
updatedev.write().unwrap();
}
ping_map.alter(&uuid, |_, v| Value {
ip: v.ip,
eta: v.eta,
online: true,
});
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
}
trace!("remove {} from ping_map", uuid);
ping_map.remove(&uuid);
}
#[derive(Clone, Debug, PartialEq)]
pub enum BroadcastCommands {
Success,
Timeout,
Error,
}
#[derive(Clone, Debug, PartialEq)]
pub struct BroadcastCommand {
pub uuid: String,
pub command: BroadcastCommands,
}
impl Display for BroadcastCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let prefix = match self.command {
BroadcastCommands::Success => "start",
BroadcastCommands::Timeout => "timeout",
BroadcastCommands::Error => "error",
};
f.write_str(format!("{prefix}_{}", self.uuid).as_str())
}
}
impl BroadcastCommand {
pub fn success(uuid: String) -> Self {
Self {
uuid,
command: BroadcastCommands::Success,
}
}
pub fn timeout(uuid: String) -> Self {
Self {
uuid,
command: BroadcastCommands::Timeout,
}
}
pub fn error(uuid: String) -> Self {
Self {
uuid,
command: BroadcastCommands::Error,
}
}
}
|