diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/mod.rs | 1 | ||||
-rw-r--r-- | src/services/ping.rs | 118 |
2 files changed, 119 insertions, 0 deletions
diff --git a/src/services/mod.rs b/src/services/mod.rs new file mode 100644 index 0000000..a766209 --- /dev/null +++ b/src/services/mod.rs | |||
@@ -0,0 +1 @@ | |||
pub mod ping; | |||
diff --git a/src/services/ping.rs b/src/services/ping.rs new file mode 100644 index 0000000..d900acb --- /dev/null +++ b/src/services/ping.rs | |||
@@ -0,0 +1,118 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use axum::extract::{ws::WebSocket}; | ||
4 | use axum::extract::ws::Message; | ||
5 | use dashmap::DashMap; | ||
6 | use time::{Duration, Instant}; | ||
7 | use tokio::sync::broadcast::{Sender}; | ||
8 | use tracing::{debug, error, trace}; | ||
9 | use crate::AppState; | ||
10 | use crate::config::SETTINGS; | ||
11 | |||
12 | pub type PingMap = DashMap<String, PingValue>; | ||
13 | |||
14 | #[derive(Debug, Clone)] | ||
15 | pub struct PingValue { | ||
16 | pub ip: String, | ||
17 | pub online: bool | ||
18 | } | ||
19 | |||
20 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) { | ||
21 | let timer = Instant::now(); | ||
22 | let payload = [0; 8]; | ||
23 | |||
24 | let mut cont = true; | ||
25 | while cont { | ||
26 | let ping = surge_ping::ping( | ||
27 | ip.parse().expect("bad ip"), | ||
28 | &payload | ||
29 | ).await; | ||
30 | |||
31 | if let Err(ping) = ping { | ||
32 | cont = matches!(ping, surge_ping::SurgeError::Timeout { .. }); | ||
33 | if !cont { | ||
34 | error!("{}", ping.to_string()); | ||
35 | } | ||
36 | if timer.elapsed() >= Duration::minutes(SETTINGS.get_int("pingtimeout").unwrap_or(10)) { | ||
37 | let _ = tx.send(BroadcastCommands::PingTimeout(uuid.clone())); | ||
38 | trace!("remove {} from ping_map after timeout", uuid); | ||
39 | ping_map.remove(&uuid); | ||
40 | cont = false; | ||
41 | } | ||
42 | } else { | ||
43 | let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); | ||
44 | debug!("ping took {:?}", duration); | ||
45 | cont = false; | ||
46 | handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; | ||
47 | }; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { | ||
52 | debug!("send pingsuccess message"); | ||
53 | let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); | ||
54 | trace!("sent message"); | ||
55 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); | ||
56 | trace!("updated ping_map"); | ||
57 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | ||
58 | debug!("remove {} from ping_map after success", uuid); | ||
59 | ping_map.remove(&uuid); | ||
60 | } | ||
61 | |||
62 | #[derive(Clone, Debug)] | ||
63 | pub enum BroadcastCommands { | ||
64 | PingSuccess(String), | ||
65 | PingTimeout(String) | ||
66 | } | ||
67 | |||
68 | pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | ||
69 | trace!("wait for ws message (uuid)"); | ||
70 | let msg = socket.recv().await; | ||
71 | let uuid = msg.unwrap().unwrap().into_text().unwrap(); | ||
72 | |||
73 | trace!("Search for uuid: {:?}", uuid); | ||
74 | |||
75 | let device_exists = state.ping_map.contains_key(&uuid); | ||
76 | match device_exists { | ||
77 | true => { | ||
78 | let _ = socket.send(process_device(state.clone(), uuid).await).await; | ||
79 | }, | ||
80 | false => { | ||
81 | debug!("didn't find any device"); | ||
82 | let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; | ||
83 | }, | ||
84 | }; | ||
85 | |||
86 | let _ = socket.close().await; | ||
87 | } | ||
88 | |||
89 | async fn process_device(state: Arc<AppState>, uuid: String) -> Message { | ||
90 | let pm = state.ping_map.clone().into_read_only(); | ||
91 | let device = pm.get(&uuid).expect("fatal error"); | ||
92 | debug!("got device: {} (online: {})", device.ip, device.online); | ||
93 | match device.online { | ||
94 | true => { | ||
95 | debug!("already started"); | ||
96 | Message::Text(format!("start_{}", uuid)) | ||
97 | }, | ||
98 | false => { | ||
99 | loop{ | ||
100 | trace!("wait for tx message"); | ||
101 | let message = state.ping_send.subscribe().recv().await.expect("fatal error"); | ||
102 | trace!("got message {:?}", message); | ||
103 | return match message { | ||
104 | BroadcastCommands::PingSuccess(msg_uuid) => { | ||
105 | if msg_uuid != uuid { continue; } | ||
106 | trace!("message == uuid success"); | ||
107 | Message::Text(format!("start_{}", uuid)) | ||
108 | }, | ||
109 | BroadcastCommands::PingTimeout(msg_uuid) => { | ||
110 | if msg_uuid != uuid { continue; } | ||
111 | trace!("message == uuid timeout"); | ||
112 | Message::Text(format!("timeout_{}", uuid)) | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | } \ No newline at end of file | ||