diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/ping.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/services/ping.rs b/src/services/ping.rs index 04ad511..f0cc4a3 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -10,7 +10,13 @@ use crate::AppState; | |||
10 | 10 | ||
11 | use crate::error::WebolError; | 11 | use crate::error::WebolError; |
12 | 12 | ||
13 | pub type PingMap = DashMap<String, (String, bool)>; | 13 | pub type PingMap = DashMap<String, PingValue>; |
14 | |||
15 | #[derive(Debug, Clone)] | ||
16 | pub struct PingValue { | ||
17 | pub ip: String, | ||
18 | pub online: bool | ||
19 | } | ||
14 | 20 | ||
15 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { | 21 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { |
16 | let payload = [0; 8]; | 22 | let payload = [0; 8]; |
@@ -32,7 +38,7 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
32 | let (_, duration) = ping.unwrap(); | 38 | let (_, duration) = ping.unwrap(); |
33 | debug!("Ping took {:?}", duration); | 39 | debug!("Ping took {:?}", duration); |
34 | cont = false; | 40 | cont = false; |
35 | handle_broadcast_send(&tx, ip.clone(), &ping_map, uuid.clone()).await; | 41 | handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; |
36 | }; | 42 | }; |
37 | } | 43 | } |
38 | 44 | ||
@@ -41,7 +47,7 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
41 | 47 | ||
42 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { | 48 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { |
43 | debug!("sending pingsuccess message"); | 49 | debug!("sending pingsuccess message"); |
44 | ping_map.insert(uuid.clone(), (ip.clone(), true)); | 50 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); |
45 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); | 51 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); |
46 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | 52 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; |
47 | trace!("remove {} from ping_map", uuid); | 53 | trace!("remove {} from ping_map", uuid); |
@@ -67,7 +73,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
67 | 73 | ||
68 | trace!("got device: {:?}", device); | 74 | trace!("got device: {:?}", device); |
69 | 75 | ||
70 | match device.1 { | 76 | match device.online { |
71 | true => { | 77 | true => { |
72 | debug!("already started"); | 78 | debug!("already started"); |
73 | // TODO: What's better? | 79 | // TODO: What's better? |
@@ -76,7 +82,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
76 | socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); | 82 | socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); |
77 | }, | 83 | }, |
78 | false => { | 84 | false => { |
79 | let ip = device.0.to_owned(); | 85 | let ip = device.ip.to_owned(); |
80 | loop{ | 86 | loop{ |
81 | trace!("wait for tx message"); | 87 | trace!("wait for tx message"); |
82 | let message = state.ping_send.subscribe().recv().await.unwrap(); | 88 | let message = state.ping_send.subscribe().recv().await.unwrap(); |