aboutsummaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
Diffstat (limited to 'src/services')
-rw-r--r--src/services/ping.rs28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/services/ping.rs b/src/services/ping.rs
index 6e710ec..ff328a5 100644
--- a/src/services/ping.rs
+++ b/src/services/ping.rs
@@ -1,8 +1,9 @@
1use std::borrow::Cow;
1use std::sync::Arc; 2use std::sync::Arc;
2 3
3use axum::{extract::{WebSocketUpgrade, ws::WebSocket, State}, response::Response}; 4use axum::{extract::{WebSocketUpgrade, ws::WebSocket, State}, response::Response};
4use tokio::sync::mpsc::Sender; 5use tokio::sync::broadcast::{Sender};
5use tracing::{debug, error}; 6use tracing::{debug, error, trace};
6 7
7use crate::{error::WebolError, AppState}; 8use crate::{error::WebolError, AppState};
8 9
@@ -12,7 +13,7 @@ pub async fn spawn(tx: Sender<String>) -> Result<(), WebolError> {
12 let mut cont = true; 13 let mut cont = true;
13 while cont { 14 while cont {
14 let ping = surge_ping::ping( 15 let ping = surge_ping::ping(
15 "192.168.178.28".parse().map_err(WebolError::IpParse)?, 16 "127.0.0.1".parse().map_err(WebolError::IpParse)?,
16 &payload 17 &payload
17 ).await; 18 ).await;
18 19
@@ -30,24 +31,29 @@ pub async fn spawn(tx: Sender<String>) -> Result<(), WebolError> {
30 debug!("Ping took {:?}", duration); 31 debug!("Ping took {:?}", duration);
31 cont = false; 32 cont = false;
32 // FIXME: remove unwrap 33 // FIXME: remove unwrap
33 tx.send("Got ping".to_string()).await.unwrap(); 34 tx.send("Got ping".to_string()).unwrap();
34 }; 35 };
35 } 36 }
36 37
37 Ok(()) 38 Ok(())
38} 39}
39 40
40pub async fn ws_ping(ws: WebSocketUpgrade, State(_state): State<Arc<AppState>>) -> Response { 41// TODO: Status to routes, websocket here
41 ws.on_upgrade(handle_socket) 42pub async fn ws_ping(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response {
43 ws.on_upgrade(move |socket| handle_socket(socket, state.ping_send.clone()))
42} 44}
43 45
44// FIXME: Handle commands through enum 46// FIXME: Handle commands through enum
45async fn handle_socket(mut socket: WebSocket) { 47async fn handle_socket(mut socket: WebSocket, tx: Sender<String>) {
46 // TODO: Understand Cow 48 // TODO: Understand Cow
47 49 while let message = tx.subscribe().recv().await.unwrap() {
48 // match socket.send(axum::extract::ws::Message::Close(Some(CloseFrame { code: 4000, reason: Cow::Owned("started".to_owned()) }))).await.map_err(WebolError::Axum) { 50 trace!("GOT = {}", message);
49 match socket.send(axum::extract::ws::Message::Text("started".to_string())).await.map_err(WebolError::Axum) { 51 if &message == "Got ping" {
52 break;
53 }
54 };
55 match socket.send(axum::extract::ws::Message::Close(Some(axum::extract::ws::CloseFrame { code: 4000, reason: Cow::Owned("started".to_owned()) }))).await.map_err(WebolError::Axum) {
50 Ok(..) => (), 56 Ok(..) => (),
51 Err(err) => { error!("Server Error: {:?}", err) } 57 Err(err) => { error!("Server Error: {:?}", err) }
52 }; 58 };
53} 59} \ No newline at end of file