summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-10-29 20:30:01 +0100
committerFxQnLr <[email protected]>2023-10-29 20:30:01 +0100
commit84c32953ae5f52be44af4b48381747f55cb04f4a (patch)
tree6b7ffd4d7ac7b89bb9bf169be7ba6d266f79d650 /src
parent0cca10290d089aabac8f2e4356cfaf80f06ae194 (diff)
downloadwebol-84c32953ae5f52be44af4b48381747f55cb04f4a.tar
webol-84c32953ae5f52be44af4b48381747f55cb04f4a.tar.gz
webol-84c32953ae5f52be44af4b48381747f55cb04f4a.zip
impl dashmap
Diffstat (limited to 'src')
-rw-r--r--src/main.rs7
-rw-r--r--src/routes/start.rs2
-rw-r--r--src/services/ping.rs11
3 files changed, 9 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 545d8fe..762a817 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,11 @@
1use std::collections::HashMap;
2use std::env; 1use std::env;
3use std::sync::Arc; 2use std::sync::Arc;
4use axum::{Router, routing::post}; 3use axum::{Router, routing::post};
5use axum::routing::{get, put}; 4use axum::routing::{get, put};
5use dashmap::DashMap;
6use sqlx::PgPool; 6use sqlx::PgPool;
7use time::util::local_offset; 7use time::util::local_offset;
8use tokio::sync::broadcast::{channel, Sender}; 8use tokio::sync::broadcast::{channel, Sender};
9use tokio::sync::Mutex;
10use tracing::{info, level_filters::LevelFilter}; 9use tracing::{info, level_filters::LevelFilter};
11use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; 10use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
12use crate::config::SETTINGS; 11use crate::config::SETTINGS;
@@ -51,9 +50,9 @@ async fn main() {
51 50
52 let (tx, _) = channel(32); 51 let (tx, _) = channel(32);
53 52
54 let ping_map: HashMap<String, (String, bool)> = HashMap::new(); 53 let ping_map: DashMap<String, (String, bool)> = DashMap::new();
55 54
56 let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(Mutex::new(ping_map)) }); 55 let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map: Arc::new(ping_map) });
57 56
58 let app = Router::new() 57 let app = Router::new()
59 .route("/start", post(start)) 58 .route("/start", post(start))
diff --git a/src/routes/start.rs b/src/routes/start.rs
index b1c8a73..5b73281 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -47,7 +47,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap
47 let uuid_gencc = uuid_gen.clone(); 47 let uuid_gencc = uuid_gen.clone();
48 tokio::spawn(async move{ 48 tokio::spawn(async move{
49 debug!("Init ping service"); 49 debug!("Init ping service");
50 state.ping_map.lock().await.insert(uuid_gen, ("192.168.178.94".to_string(), false)); 50 state.ping_map.insert(uuid_gen, ("192.168.178.94".to_string(), false));
51 51
52 warn!("{:?}", state.ping_map); 52 warn!("{:?}", state.ping_map);
53 53
diff --git a/src/services/ping.rs b/src/services/ping.rs
index 6835fc0..ed848fc 100644
--- a/src/services/ping.rs
+++ b/src/services/ping.rs
@@ -1,16 +1,15 @@
1use std::borrow::Cow; 1use std::borrow::Cow;
2use std::collections::HashMap;
3use std::sync::Arc; 2use std::sync::Arc;
4 3
5use axum::extract::{ws::WebSocket}; 4use axum::extract::{ws::WebSocket};
6use axum::extract::ws::{CloseFrame, Message}; 5use axum::extract::ws::{CloseFrame, Message};
6use dashmap::DashMap;
7use tokio::sync::broadcast::{Sender}; 7use tokio::sync::broadcast::{Sender};
8use tokio::sync::Mutex;
9use tracing::{debug, trace, warn}; 8use tracing::{debug, trace, warn};
10 9
11use crate::error::WebolError; 10use crate::error::WebolError;
12 11
13pub type PingMap = Arc<Mutex<HashMap<String, (String, bool)>>>; 12pub type PingMap = Arc<DashMap<String, (String, bool)>>;
14 13
15pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> { 14pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping_map: PingMap) -> Result<(), WebolError> {
16 let payload = [0; 8]; 15 let payload = [0; 8];
@@ -41,11 +40,11 @@ pub async fn spawn(tx: Sender<BroadcastCommands>, ip: String, uuid: String, ping
41 40
42async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) { 41async fn handle_broadcast_send(tx: &Sender<BroadcastCommands>, ip: String, ping_map: PingMap, uuid: String) {
43 debug!("sending pingsuccess message"); 42 debug!("sending pingsuccess message");
44 ping_map.lock().await.insert(uuid.clone(), (ip.clone(), true)); 43 ping_map.insert(uuid.clone(), (ip.clone(), true));
45 let _ = tx.send(BroadcastCommands::PingSuccess(ip)); 44 let _ = tx.send(BroadcastCommands::PingSuccess(ip));
46 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; 45 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
47 trace!("remove {} from ping_map", uuid); 46 trace!("remove {} from ping_map", uuid);
48 ping_map.lock().await.remove(&uuid); 47 ping_map.remove(&uuid);
49} 48}
50 49
51#[derive(Clone, Debug)] 50#[derive(Clone, Debug)]
@@ -63,7 +62,7 @@ pub async fn status_websocket(mut socket: WebSocket, tx: Sender<BroadcastCommand
63 trace!("Search for uuid: {:?}", uuid); 62 trace!("Search for uuid: {:?}", uuid);
64 63
65 // TODO: Handle Error 64 // TODO: Handle Error
66 let device = ping_map.lock().await.get(&uuid).unwrap().to_owned(); 65 let device = ping_map.get(&uuid).unwrap().to_owned();
67 66
68 trace!("got device: {:?}", device); 67 trace!("got device: {:?}", device);
69 68