diff options
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/services/ping.rs | 56 |
3 files changed, 33 insertions, 27 deletions
@@ -2187,7 +2187,7 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" | |||
2187 | 2187 | ||
2188 | [[package]] | 2188 | [[package]] |
2189 | name = "webol" | 2189 | name = "webol" |
2190 | version = "0.2.0" | 2190 | version = "0.2.1" |
2191 | dependencies = [ | 2191 | dependencies = [ |
2192 | "axum", | 2192 | "axum", |
2193 | "axum-macros", | 2193 | "axum-macros", |
@@ -1,6 +1,6 @@ | |||
1 | [package] | 1 | [package] |
2 | name = "webol" | 2 | name = "webol" |
3 | version = "0.2.0" | 3 | version = "0.2.1" |
4 | edition = "2021" | 4 | edition = "2021" |
5 | 5 | ||
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
diff --git a/src/services/ping.rs b/src/services/ping.rs index d900acb..67acc1c 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::str::FromStr; | ||
2 | use std::net::IpAddr; | ||
1 | use std::sync::Arc; | 3 | use std::sync::Arc; |
2 | 4 | ||
3 | use axum::extract::{ws::WebSocket}; | 5 | use axum::extract::{ws::WebSocket}; |
@@ -21,48 +23,47 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
21 | let timer = Instant::now(); | 23 | let timer = Instant::now(); |
22 | let payload = [0; 8]; | 24 | let payload = [0; 8]; |
23 | 25 | ||
24 | let mut cont = true; | 26 | let ping_ip = IpAddr::from_str(&ip).expect("bad ip"); |
25 | while cont { | 27 | |
28 | let mut msg: Option<BroadcastCommands> = None; | ||
29 | while msg.is_none() { | ||
26 | let ping = surge_ping::ping( | 30 | let ping = surge_ping::ping( |
27 | ip.parse().expect("bad ip"), | 31 | ping_ip, |
28 | &payload | 32 | &payload |
29 | ).await; | 33 | ).await; |
30 | 34 | ||
31 | if let Err(ping) = ping { | 35 | if let Err(ping) = ping { |
32 | cont = matches!(ping, surge_ping::SurgeError::Timeout { .. }); | 36 | let ping_timeout = matches!(ping, surge_ping::SurgeError::Timeout { .. }); |
33 | if !cont { | 37 | if !ping_timeout { |
34 | error!("{}", ping.to_string()); | 38 | error!("{}", ping.to_string()); |
39 | msg = Some(BroadcastCommands::Error(uuid.clone())); | ||
35 | } | 40 | } |
36 | if timer.elapsed() >= Duration::minutes(SETTINGS.get_int("pingtimeout").unwrap_or(10)) { | 41 | if timer.elapsed() >= Duration::minutes(SETTINGS.get_int("pingtimeout").unwrap_or(10)) { |
37 | let _ = tx.send(BroadcastCommands::PingTimeout(uuid.clone())); | 42 | msg = Some(BroadcastCommands::Timeout(uuid.clone())); |
38 | trace!("remove {} from ping_map after timeout", uuid); | ||
39 | ping_map.remove(&uuid); | ||
40 | cont = false; | ||
41 | } | 43 | } |
42 | } else { | 44 | } else { |
43 | let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); | 45 | let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); |
44 | debug!("ping took {:?}", duration); | 46 | debug!("ping took {:?}", duration); |
45 | cont = false; | 47 | msg = Some(BroadcastCommands::Success(uuid.clone())); |
46 | handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; | ||
47 | }; | 48 | }; |
48 | } | 49 | } |
49 | } | ||
50 | 50 | ||
51 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { | 51 | let msg = msg.expect("fatal error"); |
52 | debug!("send pingsuccess message"); | 52 | |
53 | let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); | 53 | let _ = tx.send(msg.clone()); |
54 | trace!("sent message"); | 54 | if let BroadcastCommands::Success(..) = msg { |
55 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); | 55 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); |
56 | trace!("updated ping_map"); | 56 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; |
57 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | 57 | } |
58 | debug!("remove {} from ping_map after success", uuid); | 58 | trace!("remove {} from ping_map", uuid); |
59 | ping_map.remove(&uuid); | 59 | ping_map.remove(&uuid); |
60 | } | 60 | } |
61 | 61 | ||
62 | #[derive(Clone, Debug)] | 62 | #[derive(Clone, Debug, PartialEq)] |
63 | pub enum BroadcastCommands { | 63 | pub enum BroadcastCommands { |
64 | PingSuccess(String), | 64 | Success(String), |
65 | PingTimeout(String) | 65 | Timeout(String), |
66 | Error(String), | ||
66 | } | 67 | } |
67 | 68 | ||
68 | pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | 69 | pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { |
@@ -101,15 +102,20 @@ async fn process_device(state: Arc<AppState>, uuid: String) -> Message { | |||
101 | let message = state.ping_send.subscribe().recv().await.expect("fatal error"); | 102 | let message = state.ping_send.subscribe().recv().await.expect("fatal error"); |
102 | trace!("got message {:?}", message); | 103 | trace!("got message {:?}", message); |
103 | return match message { | 104 | return match message { |
104 | BroadcastCommands::PingSuccess(msg_uuid) => { | 105 | BroadcastCommands::Success(msg_uuid) => { |
105 | if msg_uuid != uuid { continue; } | 106 | if msg_uuid != uuid { continue; } |
106 | trace!("message == uuid success"); | 107 | trace!("message == uuid success"); |
107 | Message::Text(format!("start_{}", uuid)) | 108 | Message::Text(format!("start_{}", uuid)) |
108 | }, | 109 | }, |
109 | BroadcastCommands::PingTimeout(msg_uuid) => { | 110 | BroadcastCommands::Timeout(msg_uuid) => { |
110 | if msg_uuid != uuid { continue; } | 111 | if msg_uuid != uuid { continue; } |
111 | trace!("message == uuid timeout"); | 112 | trace!("message == uuid timeout"); |
112 | Message::Text(format!("timeout_{}", uuid)) | 113 | Message::Text(format!("timeout_{}", uuid)) |
114 | }, | ||
115 | BroadcastCommands::Error(msg_uuid) => { | ||
116 | if msg_uuid != uuid { continue; } | ||
117 | trace!("message == uuid error"); | ||
118 | Message::Text(format!("error_{}", uuid)) | ||
113 | } | 119 | } |
114 | } | 120 | } |
115 | } | 121 | } |