From 9e3afcfee276af982a1e1d11f24c9711defc124e Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 2 Nov 2023 20:58:29 +0100 Subject: Revert "update dependencies" This reverts commit 94104b621e2eec44dd90eb22fae2db2ce4938b87. --- src/routes/start.rs | 5 +++-- src/services/ping.rs | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/routes/start.rs b/src/routes/start.rs index 271f924..9cd358b 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use std::sync::Arc; use axum::extract::State; use serde_json::{json, Value}; -use tracing::{debug, info}; +use tracing::{debug, info, warn}; use uuid::Uuid; use crate::auth::auth; use crate::config::SETTINGS; @@ -16,6 +16,7 @@ use crate::services::ping::PingValue; #[axum_macros::debug_handler] pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { info!("POST request"); + warn!("{:?}", state.ping_map); let secret = headers.get("authorization"); let authorized = auth(secret).map_err(WebolError::Auth)?; if authorized { @@ -45,7 +46,7 @@ pub async fn start(State(state): State>, headers: HeaderMap let uuid_gen = Uuid::new_v4().to_string(); let uuid_genc = uuid_gen.clone(); tokio::spawn(async move { - debug!("init ping service"); + debug!("Init ping service"); state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await diff --git a/src/services/ping.rs b/src/services/ping.rs index d900acb..a26dacc 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,12 +50,10 @@ 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"); - 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"); + let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; - debug!("remove {} from ping_map after success", uuid); + trace!("remove {} from ping_map after success", uuid); ping_map.remove(&uuid); } @@ -72,12 +70,12 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { trace!("Search for uuid: {:?}", uuid); - let device_exists = state.ping_map.contains_key(&uuid); - match device_exists { - true => { - let _ = socket.send(process_device(state.clone(), uuid).await).await; + 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; }, - false => { + None => { debug!("didn't find any device"); let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; }, @@ -86,10 +84,7 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { let _ = socket.close().await; } -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); +async fn process_device(state: Arc, uuid: String, device: PingValue) -> Message { match device.online { true => { debug!("already started"); -- cgit v1.2.3