diff options
author | FxQnLr <[email protected]> | 2023-11-02 20:59:36 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-11-02 20:59:36 +0100 |
commit | 32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (patch) | |
tree | c357bcaca0681caf9a6742c857bb494dc4315900 /src | |
parent | 9e3afcfee276af982a1e1d11f24c9711defc124e (diff) | |
download | webol-32561060a8dc6fc6118498da06bdd8f5b4c3f0fd.tar webol-32561060a8dc6fc6118498da06bdd8f5b4c3f0fd.tar.gz webol-32561060a8dc6fc6118498da06bdd8f5b4c3f0fd.zip |
fixed broadcast and cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/routes/start.rs | 5 | ||||
-rw-r--r-- | src/services/ping.rs | 23 |
2 files changed, 16 insertions, 12 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 9cd358b..271f924 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; | |||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | use axum::extract::State; | 5 | use axum::extract::State; |
6 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
7 | use tracing::{debug, info, warn}; | 7 | use tracing::{debug, info}; |
8 | use uuid::Uuid; | 8 | use uuid::Uuid; |
9 | use crate::auth::auth; | 9 | use crate::auth::auth; |
10 | use crate::config::SETTINGS; | 10 | use crate::config::SETTINGS; |
@@ -16,7 +16,6 @@ use crate::services::ping::PingValue; | |||
16 | #[axum_macros::debug_handler] | 16 | #[axum_macros::debug_handler] |
17 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { | 17 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { |
18 | info!("POST request"); | 18 | info!("POST request"); |
19 | warn!("{:?}", state.ping_map); | ||
20 | let secret = headers.get("authorization"); | 19 | let secret = headers.get("authorization"); |
21 | let authorized = auth(secret).map_err(WebolError::Auth)?; | 20 | let authorized = auth(secret).map_err(WebolError::Auth)?; |
22 | if authorized { | 21 | if authorized { |
@@ -46,7 +45,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
46 | let uuid_gen = Uuid::new_v4().to_string(); | 45 | let uuid_gen = Uuid::new_v4().to_string(); |
47 | let uuid_genc = uuid_gen.clone(); | 46 | let uuid_genc = uuid_gen.clone(); |
48 | tokio::spawn(async move { | 47 | tokio::spawn(async move { |
49 | debug!("Init ping service"); | 48 | debug!("init ping service"); |
50 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); | 49 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); |
51 | 50 | ||
52 | crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await | 51 | 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 a26dacc..d900acb 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -41,7 +41,7 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
41 | } | 41 | } |
42 | } else { | 42 | } else { |
43 | let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); | 43 | let (_, duration) = ping.map_err(|err| error!("{}", err.to_string())).expect("fatal error"); |
44 | debug!("Ping took {:?}", duration); | 44 | debug!("ping took {:?}", duration); |
45 | cont = false; | 45 | cont = false; |
46 | handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; | 46 | handle_broadcast_send(&tx, ip.clone(), ping_map, uuid.clone()).await; |
47 | }; | 47 | }; |
@@ -50,10 +50,12 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping | |||
50 | 50 | ||
51 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { | 51 | async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: &PingMap, uuid: String) { |
52 | debug!("send pingsuccess message"); | 52 | debug!("send pingsuccess message"); |
53 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); | ||
54 | let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); | 53 | let _ = tx.send(BroadcastCommands::PingSuccess(uuid.clone())); |
54 | trace!("sent message"); | ||
55 | ping_map.insert(uuid.clone(), PingValue { ip: ip.clone(), online: true }); | ||
56 | trace!("updated ping_map"); | ||
55 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; | 57 | tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; |
56 | trace!("remove {} from ping_map after success", uuid); | 58 | debug!("remove {} from ping_map after success", uuid); |
57 | ping_map.remove(&uuid); | 59 | ping_map.remove(&uuid); |
58 | } | 60 | } |
59 | 61 | ||
@@ -70,12 +72,12 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
70 | 72 | ||
71 | trace!("Search for uuid: {:?}", uuid); | 73 | trace!("Search for uuid: {:?}", uuid); |
72 | 74 | ||
73 | match state.ping_map.get(&uuid) { | 75 | let device_exists = state.ping_map.contains_key(&uuid); |
74 | Some(device) => { | 76 | match device_exists { |
75 | debug!("got device: {} (online: {})", device.ip, device.online); | 77 | true => { |
76 | let _ = socket.send(process_device(state.clone(), uuid, device.to_owned()).await).await; | 78 | let _ = socket.send(process_device(state.clone(), uuid).await).await; |
77 | }, | 79 | }, |
78 | None => { | 80 | false => { |
79 | debug!("didn't find any device"); | 81 | debug!("didn't find any device"); |
80 | let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; | 82 | let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; |
81 | }, | 83 | }, |
@@ -84,7 +86,10 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
84 | let _ = socket.close().await; | 86 | let _ = socket.close().await; |
85 | } | 87 | } |
86 | 88 | ||
87 | async fn process_device(state: Arc<AppState>, uuid: String, device: PingValue) -> Message { | 89 | async fn process_device(state: Arc<AppState>, uuid: String) -> Message { |
90 | let pm = state.ping_map.clone().into_read_only(); | ||
91 | let device = pm.get(&uuid).expect("fatal error"); | ||
92 | debug!("got device: {} (online: {})", device.ip, device.online); | ||
88 | match device.online { | 93 | match device.online { |
89 | true => { | 94 | true => { |
90 | debug!("already started"); | 95 | debug!("already started"); |