From 32561060a8dc6fc6118498da06bdd8f5b4c3f0fd Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 2 Nov 2023 20:59:36 +0100 Subject: fixed broadcast and cleanup --- src/services/ping.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/services') diff --git a/src/services/ping.rs b/src/services/ping.rs index a26dacc..d900acb 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -41,7 +41,7 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping } } else { let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); - debug!("Ping took {:?}", duration); + debug!("ping took {:?}", duration); cont = false; handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; }; @@ -50,10 +50,12 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping async fn handle_broadcast_send(tx: &Sender, ip: String, ping_map: &PingMap, uuid: String) { debug!("send pingsuccess message"); - ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); + trace!("sent message"); + ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); + trace!("updated ping_map"); tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; - trace!("remove {} from ping_map after success", uuid); + debug!("remove {} from ping_map after success", uuid); ping_map.remove(&uuid); } @@ -70,12 +72,12 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { trace!("Search for uuid: {:?}", uuid); - match state.ping_map.get(&uuid) { - Some(device) => { - debug!("got device: {} (online: {})", device.ip, device.online); - let _ = socket.send(process_device(state.clone(), uuid, device.to_owned()).await).await; + let device_exists = state.ping_map.contains_key(&uuid); + match device_exists { + true => { + let _ = socket.send(process_device(state.clone(), uuid).await).await; }, - None => { + false => { debug!("didn't find any device"); let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; }, @@ -84,7 +86,10 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { let _ = socket.close().await; } -async fn process_device(state: Arc, uuid: String, device: PingValue) -> Message { +async fn process_device(state: Arc, uuid: String) -> Message { + let pm = state.ping_map.clone().into_read_only(); + let device = pm.get(&uuid).expect("fatal error"); + debug!("got device: {} (online: {})", device.ip, device.online); match device.online { true => { debug!("already started"); -- cgit v1.2.3