aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-07 17:27:03 +0100
committerGitHub <[email protected]>2023-11-07 17:27:03 +0100
commit4f124e6ba636e6191c2960d96d0057f3061988fc (patch)
tree56363099c5e513e76ac40acdd48dd2d5124c8791
parent1cd2a8e4aecfaad2a8385a6bea61580209b86398 (diff)
parent3df22a00e3dccd7beb0d60b644d7c38c7d0b594f (diff)
downloadwebol-4f124e6ba636e6191c2960d96d0057f3061988fc.tar
webol-4f124e6ba636e6191c2960d96d0057f3061988fc.tar.gz
webol-4f124e6ba636e6191c2960d96d0057f3061988fc.zip
Merge pull request #7 from FxQnLr/ping
better while loop
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/services/ping.rs56
3 files changed, 33 insertions, 27 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 350c8e9..9873a4c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2187,7 +2187,7 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
2187 2187
2188[[package]] 2188[[package]]
2189name = "webol" 2189name = "webol"
2190version = "0.2.0" 2190version = "0.2.1"
2191dependencies = [ 2191dependencies = [
2192 "axum", 2192 "axum",
2193 "axum-macros", 2193 "axum-macros",
diff --git a/Cargo.toml b/Cargo.toml
index 1bf823f..5279abf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "webol" 2name = "webol"
3version = "0.2.0" 3version = "0.2.1"
4edition = "2021" 4edition = "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 @@
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 }