diff options
author | FxQnLr <[email protected]> | 2023-10-29 21:09:46 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-10-29 21:09:46 +0100 |
commit | dd303dc41e4d500e48760f79351720cc2c1c9ffe (patch) | |
tree | b730a1da1e0e58e12494bbab88076d403f4ccd97 /src/services | |
parent | 84c32953ae5f52be44af4b48381747f55cb04f4a (diff) | |
download | webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.tar webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.tar.gz webol-dd303dc41e4d500e48760f79351720cc2c1c9ffe.zip |
add ip to database and use for ping, remove arc from pingmap
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/ping.rs | 20 |
1 files changed, 11 insertions, 9 deletions
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}; | |||
6 | use dashmap::DashMap; | 6 | use dashmap::DashMap; |
7 | use tokio::sync::broadcast::{Sender}; | 7 | use tokio::sync::broadcast::{Sender}; |
8 | use tracing::{debug, trace, warn}; | 8 | use tracing::{debug, trace, warn}; |
9 | use crate::AppState; | ||
9 | 10 | ||
10 | use crate::error::WebolError; | 11 | use crate::error::WebolError; |
11 | 12 | ||
12 | pub type PingMap = Arc<DashMap<String, (String, bool)>>; | 13 | pub type PingMap = DashMap<String, (String, bool)>; |
13 | 14 | ||
14 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { | 15 | pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: &PingMap) -> Result<(), WebolError> { |
15 | let payload = [0; 8]; | 16 | let payload = [0; 8]; |
16 | 17 | ||
17 | // TODO: Better while | 18 | // TODO: Better while |
@@ -31,14 +32,14 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
31 | let (_, duration) = ping.unwrap(); | 32 | let (_, duration) = ping.unwrap(); |
32 | debug!("Ping took {:?}", duration); | 33 | debug!("Ping took {:?}", duration); |
33 | cont = false; | 34 | cont = false; |
34 | handle_broadcast_send(&tx, ip.clone(), ping_map.clone(), uuid.clone()).await; | 35 | handle_broadcast_send(&tx, ip.clone(), &ping_map, uuid.clone()).await; |
35 | }; | 36 | }; |
36 | } | 37 | } |
37 | 38 | ||
38 | Ok(()) | 39 | Ok(()) |
39 | } | 40 | } |
40 | 41 | ||
41 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) { | 42 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { |
42 | debug!("sending pingsuccess message"); | 43 | debug!("sending pingsuccess message"); |
43 | ping_map.insert(uuid.clone(), (ip.clone(), true)); | 44 | ping_map.insert(uuid.clone(), (ip.clone(), true)); |
44 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); | 45 | let _ = tx.send(BroadcastCommands::PingSuccess(ip)); |
@@ -52,8 +53,8 @@ pub enum BroadcastCommands { | |||
52 | PingSuccess(String) | 53 | PingSuccess(String) |
53 | } | 54 | } |
54 | 55 | ||
55 | pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommands>, ping_map: PingMap) { | 56 | pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { |
56 | warn!("{:?}", ping_map); | 57 | warn!("{:?}", state.ping_map); |
57 | 58 | ||
58 | trace!("wait for ws message (uuid)"); | 59 | trace!("wait for ws message (uuid)"); |
59 | let msg = socket.recv().await; | 60 | let msg = socket.recv().await; |
@@ -62,13 +63,14 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommand | |||
62 | trace!("Search for uuid: {:?}", uuid); | 63 | trace!("Search for uuid: {:?}", uuid); |
63 | 64 | ||
64 | // TODO: Handle Error | 65 | // TODO: Handle Error |
65 | let device = ping_map.get(&uuid).unwrap().to_owned(); | 66 | let device = state.ping_map.get(&uuid).unwrap().to_owned(); |
66 | 67 | ||
67 | trace!("got device: {:?}", device); | 68 | trace!("got device: {:?}", device); |
68 | 69 | ||
69 | match device.1 { | 70 | match device.1 { |
70 | true => { | 71 | true => { |
71 | debug!("already started"); | 72 | debug!("already started"); |
73 | // TODO: What's better? | ||
72 | // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); | 74 | // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); |
73 | // socket.close().await.unwrap(); | 75 | // socket.close().await.unwrap(); |
74 | socket.send(Message::Close(Some(CloseFrame { code: 4001, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); | 76 | 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<BroadcastCommand | |||
77 | let ip = device.0.to_owned(); | 79 | let ip = device.0.to_owned(); |
78 | loop{ | 80 | loop{ |
79 | trace!("wait for tx message"); | 81 | trace!("wait for tx message"); |
80 | let message = tx.subscribe().recv().await.unwrap(); | 82 | let message = state.ping_send.subscribe().recv().await.unwrap(); |
81 | trace!("GOT = {:?}", message); | 83 | trace!("GOT = {:?}", message); |
82 | // if let BroadcastCommands::PingSuccess(msg_ip) = message { | 84 | // if let BroadcastCommands::PingSuccess(msg_ip) = message { |
83 | // if msg_ip == ip { | 85 | // if msg_ip == ip { |
@@ -95,7 +97,7 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommand | |||
95 | socket.send(Message::Close(Some(CloseFrame { code: 4000, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); | 97 | socket.send(Message::Close(Some(CloseFrame { code: 4000, reason: Cow::from(format!("start_{}", uuid)) }))).await.unwrap(); |
96 | // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); | 98 | // socket.send(Message::Text(format!("start_{}", uuid))).await.unwrap(); |
97 | // socket.close().await.unwrap(); | 99 | // socket.close().await.unwrap(); |
98 | warn!("{:?}", ping_map); | 100 | warn!("{:?}", state.ping_map); |
99 | } | 101 | } |
100 | } | 102 | } |
101 | } \ No newline at end of file | 103 | } \ No newline at end of file |