From c41b9eb69c6edf7faf5c66203326c34aac51d6e1 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Wed, 11 Sep 2024 03:33:49 +0000 Subject: [PATCH] got webrtc working with controller-side connection --- src/config.rs | 2 +- src/coordinator/mod.rs | 18 +++++++++++++++ src/coordinator/satellite_connection.rs | 29 ++++++++++++++++++------- src/main.rs | 4 +++- src/tauri_functions.rs | 5 ++--- 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/config.rs b/src/config.rs index ca9c356..d8283f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,7 +30,7 @@ impl Default for AppConfig { fn default() -> Self { AppConfig { cameras: vec![ConnectionString { - ip: "10.0.0.29".to_owned(), + ip: "127.0.0.1".to_owned(), port: 8765, }], diff --git a/src/coordinator/mod.rs b/src/coordinator/mod.rs index dfc3308..0651d4a 100644 --- a/src/coordinator/mod.rs +++ b/src/coordinator/mod.rs @@ -133,11 +133,29 @@ pub async fn run_main_event_loop( }); } ApplicationEvent::WebRTCMessage((name, msg)) => { + info!( + "Valid camera names are: {:?}", + state + .camera_satellites + .iter() + .map(|x| x.name.clone()) + .collect::>() + ); + info!("Reqested name is: {}", name); + info!( + "Count of is_connected: {}", + state + .camera_satellites + .iter() + .filter(|x| x.is_connected()) + .count() + ); for conn in state .camera_satellites .iter_mut() .filter(|x| x.name == name && x.is_connected()) { + info!("Sending message"); if let Err(_) = conn.send(msg.clone()).await { error!("The websocket gave an error when I tried to send a message! I hope your logging is good enough"); } diff --git a/src/coordinator/satellite_connection.rs b/src/coordinator/satellite_connection.rs index 5b63cae..93ee3f5 100644 --- a/src/coordinator/satellite_connection.rs +++ b/src/coordinator/satellite_connection.rs @@ -2,7 +2,7 @@ use std::sync::{atomic::AtomicBool, Arc}; use async_channel::TryRecvError; use tokio::runtime::Handle; -use tracing::{error, instrument, warn}; +use tracing::{error, instrument, warn, info}; use crate::config::ConnectionString; @@ -19,9 +19,11 @@ pub struct SatelliteConnection { pub try_connecting: bool, connection: ConnectionString, + currently_connecting: bool, + to_socket: Option, from_socket: Option, - socket_is_alive: Arc, + socket_is_dead: Arc, } impl SatelliteConnection { @@ -34,29 +36,39 @@ impl SatelliteConnection { retry_attempts: 0, try_connecting: true, + currently_connecting: false, + to_socket: None, from_socket: None, - socket_is_alive: Arc::new(AtomicBool::new(false)), + socket_is_dead: Arc::new(AtomicBool::new(true)), } } #[instrument(skip(self))] pub async fn close(&mut self) { - self.socket_is_alive - .store(false, std::sync::atomic::Ordering::SeqCst); + self.socket_is_dead + .store(true, std::sync::atomic::Ordering::SeqCst); self.to_socket.take(); // closing all senders will dispose of the self.from_socket.take(); // channel } #[instrument(skip(self))] pub fn is_connected(&self) -> bool { - return self - .socket_is_alive + return !self + .socket_is_dead .load(std::sync::atomic::Ordering::SeqCst); } #[instrument(skip(self, rt))] pub async fn connect(&mut self, rt: Handle) { + if self.currently_connecting + || !self + .socket_is_dead + .load(std::sync::atomic::Ordering::SeqCst) + { + return; + } + self.currently_connecting = true; match vcs_common::connect_to_server(self.connection.build_conn_string(), rt).await { Ok((sender, recvr, is_alive)) => { if let Err(e) = sender.send(ApplicationMessage::NameRequest(None)).await { @@ -68,7 +80,8 @@ impl SatelliteConnection { } self.to_socket = Some(sender); self.from_socket = Some(recvr); - self.socket_is_alive = is_alive; + self.socket_is_dead = is_alive; + self.currently_connecting = false; } Err(e) => { self.retry_attempts += 1; diff --git a/src/main.rs b/src/main.rs index 5da69e1..9d8ba48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,12 +106,14 @@ fn main() { if message.is_some() { if let Err(e) = to_mec.send_blocking(ApplicationEvent::WebRTCMessage(( - "first".to_owned(), + "CameraSatellite_1".to_owned(), message.unwrap(), ))) { error!("Could not send to mec! {e}"); } + } else { + error!("Could not deserialize ui webrtc message"); } } None => { diff --git a/src/tauri_functions.rs b/src/tauri_functions.rs index 1a11fb2..195a447 100644 --- a/src/tauri_functions.rs +++ b/src/tauri_functions.rs @@ -13,8 +13,7 @@ pub struct TauriState { #[tauri::command] pub fn connect_to_camera(state: State<'_, TauriState>) { let mec = state.to_mec.clone(); - state.rt.spawn(async move{ - let _ = mec - .send_blocking(ApplicationEvent::RetryDisconnectedSatellites); + state.rt.spawn(async move { + let _ = mec.send_blocking(ApplicationEvent::RetryDisconnectedSatellites); }); }