From dd303dc41e4d500e48760f79351720cc2c1c9ffe Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 29 Oct 2023 21:09:46 +0100 Subject: add ip to database and use for ping, remove arc from pingmap --- src/services/ping.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/services') diff --git a/src/services/ping.rs b/src/services/ping.rs index ed848fc..04ad511 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -6,12 +6,13 @@ use axum::extract::ws::{CloseFrame, Message}; use dashmap::DashMap; use tokio::sync::broadcast::{Sender}; use tracing::{debug, trace, warn}; +use crate::AppState; use crate::error::WebolError; -pub type PingMap = Arc>; +pub type PingMap = DashMap; -pub async fn spawn(tx: Sender, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { +pub async fn spawn(tx: Sender, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { let payload = [0; 8]; // TODO: Better while @@ -31,14 +32,14 @@ pub async fn spawn(tx: Sender, ip: String, uuid: String, ping let (_, duration) = ping.unwrap(); debug!("Ping took {:?}", duration); cont = false; - handle_broadcast_send(&tx, ip.clone(), ping_map.clone(), uuid.clone()).await; + handle_broadcast_send(&tx, ip.clone(), &ping_map, uuid.clone()).await; }; } Ok(()) } -async fn handle_broadcast_send(tx: &Sender, ip: String, ping_map: PingMap, uuid: String) { +async fn handle_broadcast_send(tx: &Sender, ip: String, ping_map: &PingMap, uuid: String) { debug!("sending pingsuccess message"); ping_map.insert(uuid.clone(), (ip.clone(), true)); let _ = tx.send(BroadcastCommands::PingSuccess(ip)); @@ -52,8 +53,8 @@ pub enum BroadcastCommands { PingSuccess(String) } -pub async fn status_websocket(mut socket: WebSocket, tx: Sender, ping_map: PingMap) { - warn!("{:?}", ping_map); +pub async fn status_websocket(mut socket: WebSocket, state: Arc) { + warn!("{:?}", state.ping_map); trace!("wait for ws message (uuid)"); let msg = socket.recv().await; @@ -62,13 +63,14 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender { debug!("already started"); + // TODO: What's better? // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); // socket.close().await.unwrap(); socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); @@ -77,7 +79,7 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender