aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-07 17:24:58 +0100
committerFxQnLr <[email protected]>2023-11-07 17:24:58 +0100
commit3df22a00e3dccd7beb0d60b644d7c38c7d0b594f (patch)
tree56363099c5e513e76ac40acdd48dd2d5124c8791 /src
parent32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff)
downloadwebol-3df22a00e3dccd7beb0d60b644d7c38c7d0b594f.tar
webol-3df22a00e3dccd7beb0d60b644d7c38c7d0b594f.tar.gz
webol-3df22a00e3dccd7beb0d60b644d7c38c7d0b594f.zip
better while loop
Diffstat (limited to 'src')
-rw-r--r--src/services/ping.rs56
1 files changed, 31 insertions, 25 deletions
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 @@
1use std::str::FromStr;
2use std::net::IpAddr;
1use std::sync::Arc; 3use std::sync::Arc;
2 4
3use axum::extract::{ws::WebSocket}; 5use 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
51async 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)]
63pub enum BroadcastCommands { 63pub enum BroadcastCommands {
64 PingSuccess(String), 64 Success(String),
65 PingTimeout(String) 65 Timeout(String),
66 Error(String),
66} 67}
67 68
68pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { 69pub 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 }