aboutsummaryrefslogtreecommitdiff
path: root/src/services/ping.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-10-29 19:55:26 +0100
committerFxQnLr <[email protected]>2023-10-29 19:55:26 +0100
commit0cca10290d089aabac8f2e4356cfaf80f06ae194 (patch)
tree708d44f2c439bb23b664114e16d92af63c693f3b /src/services/ping.rs
parent00dd8a9abee6b9f0cfc37c6f20f30f0d99dfe91a (diff)
downloadwebol-0cca10290d089aabac8f2e4356cfaf80f06ae194.tar
webol-0cca10290d089aabac8f2e4356cfaf80f06ae194.tar.gz
webol-0cca10290d089aabac8f2e4356cfaf80f06ae194.zip
does what is expected, but badly
Diffstat (limited to 'src/services/ping.rs')
-rw-r--r--src/services/ping.rs68
1 files changed, 39 insertions, 29 deletions
diff --git a/src/services/ping.rs b/src/services/ping.rs
index e3d465d..6835fc0 100644
--- a/src/services/ping.rs
+++ b/src/services/ping.rs
@@ -3,16 +3,19 @@ use std::collections::HashMap;
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use axum::extract::{ws::WebSocket}; 5use axum::extract::{ws::WebSocket};
6use axum::extract::ws::Message; 6use axum::extract::ws::{CloseFrame, Message};
7use tokio::sync::broadcast::{Sender}; 7use tokio::sync::broadcast::{Sender};
8use tokio::sync::Mutex; 8use tokio::sync::Mutex;
9use tracing::{debug, error, trace, warn}; 9use tracing::{debug, trace, warn};
10 10
11use crate::error::WebolError; 11use crate::error::WebolError;
12 12
13pub async fn spawn(tx: Sender<String>, ip: String) -> Result<(), WebolError> { 13pub type PingMap = Arc<Mutex<HashMap<String, (String, bool)>>>;
14
15pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> {
14 let payload = [0; 8]; 16 let payload = [0; 8];
15 17
18 // TODO: Better while
16 let mut cont = true; 19 let mut cont = true;
17 while cont { 20 while cont {
18 let ping = surge_ping::ping( 21 let ping = surge_ping::ping(
@@ -22,40 +25,44 @@ pub async fn spawn(tx: Sender<String>, ip: String) -> Result<(), WebolError> {
22 25
23 if let Err(ping) = ping { 26 if let Err(ping) = ping {
24 cont = matches!(ping, surge_ping::SurgeError::Timeout { .. }); 27 cont = matches!(ping, surge_ping::SurgeError::Timeout { .. });
25
26 // debug!("{}", cont);
27
28 if !cont { 28 if !cont {
29 return Err(ping).map_err(WebolError::Ping) 29 return Err(ping).map_err(WebolError::Ping)
30 } 30 }
31
32 } else { 31 } else {
33 let (_, duration) = ping.unwrap(); 32 let (_, duration) = ping.unwrap();
34 debug!("Ping took {:?}", duration); 33 debug!("Ping took {:?}", duration);
35 cont = false; 34 cont = false;
36 // FIXME: remove unwrap 35 handle_broadcast_send(&tx, ip.clone(), ping_map.clone(), uuid.clone()).await;
37 // FIXME: if error: SendError because no listener, then handle the entry directly
38 tx.send(ip.clone());
39 }; 36 };
40 } 37 }
41 38
42 Ok(()) 39 Ok(())
43} 40}
44 41
45// FIXME: Handle commands through enum 42async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) {
46pub async fn status_websocket(mut socket: WebSocket, tx: Sender<String>, ping_map: Arc<Mutex<HashMap<String, (String, bool)>>>) { 43 debug!("sending pingsuccess message");
47 warn!("{:?}", ping_map); 44 ping_map.lock().await.insert(uuid.clone(), (ip.clone(), true));
45 let _ = tx.send(BroadcastCommands::PingSuccess(ip));
46 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
47 trace!("remove {} from ping_map", uuid);
48 ping_map.lock().await.remove(&uuid);
49}
48 50
49 let mut uuid: Option<String> = None; 51#[derive(Clone, Debug)]
52pub enum BroadcastCommands {
53 PingSuccess(String)
54}
55
56pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommands>, ping_map: PingMap) {
57 warn!("{:?}", ping_map);
50 58
51 trace!("wait for ws message (uuid)"); 59 trace!("wait for ws message (uuid)");
52 let msg = socket.recv().await; 60 let msg = socket.recv().await;
53 uuid = Some(msg.unwrap().unwrap().into_text().unwrap()); 61 let uuid = msg.unwrap().unwrap().into_text().unwrap();
54
55 let uuid = uuid.unwrap();
56 62
57 trace!("Search for uuid: {:?}", uuid); 63 trace!("Search for uuid: {:?}", uuid);
58 64
65 // TODO: Handle Error
59 let device = ping_map.lock().await.get(&uuid).unwrap().to_owned(); 66 let device = ping_map.lock().await.get(&uuid).unwrap().to_owned();
60 67
61 trace!("got device: {:?}", device); 68 trace!("got device: {:?}", device);
@@ -63,29 +70,32 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender<String>, ping_ma
63 match device.1 { 70 match device.1 {
64 true => { 71 true => {
65 debug!("already started"); 72 debug!("already started");
66 socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); 73 // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap();
67 socket.close().await.unwrap(); 74 // socket.close().await.unwrap();
75 socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap();
68 }, 76 },
69 false => { 77 false => {
70 let ip = device.0.to_owned(); 78 let ip = device.0.to_owned();
71 let mut i = 0;
72 loop{ 79 loop{
73 trace!("{}", i);
74 // TODO: Check if older than 10 minutes, close if true
75 trace!("wait for tx message"); 80 trace!("wait for tx message");
76 let message = tx.subscribe().recv().await.unwrap(); 81 let message = tx.subscribe().recv().await.unwrap();
77 trace!("GOT = {}", message); 82 trace!("GOT = {:?}", message);
78 if message == ip { 83 // if let BroadcastCommands::PingSuccess(msg_ip) = message {
84 // if msg_ip == ip {
85 // trace!("message == ip");
86 // break;
87 // }
88 // }
89 let BroadcastCommands::PingSuccess(msg_ip) = message;
90 if msg_ip == ip {
79 trace!("message == ip"); 91 trace!("message == ip");
80 break; 92 break;
81 } 93 }
82 i += 1;
83 }; 94 };
84 95
85 socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); 96 socket.send(Message::Close(Some(CloseFrame { code: 4000, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap();
86 socket.close().await.unwrap(); 97 // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap();
87 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; 98 // socket.close().await.unwrap();
88 ping_map.lock().await.remove(&uuid);
89 warn!("{:?}", ping_map); 99 warn!("{:?}", ping_map);
90 } 100 }
91 } 101 }