diff --git a/src/app/mod.rs b/src/app/mod.rs index eb7b070..2cdd270 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -139,6 +139,11 @@ impl App { KeyCode::Char('l') => self.logs(), KeyCode::Char('m') => self.main(), _ => match mode { + CurrentScreen::Main => { + if code == KeyCode::Char('d') { + self.service.refresh_peers().await; + } + } CurrentScreen::Logging => match code { KeyCode::Left => change_log_level(-1), KeyCode::Right => change_log_level(1), @@ -168,7 +173,6 @@ impl App { }, SendingScreen::Text => unreachable!(), }, - CurrentScreen::Main => {} CurrentScreen::Stopping => unreachable!(), }, }, diff --git a/src/http_server.rs b/src/http_server.rs index 20954ab..ab7aa5a 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -29,6 +29,7 @@ impl JocalService { let handle = Handle::new(); self.http_handle.get_or_init(|| handle.clone()); + log::info!("starting http server"); axum_server::bind_rustls(addr, ssl_config) diff --git a/src/lib.rs b/src/lib.rs index dbec578..38aaf7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,8 +32,6 @@ pub const MULTICAST_IP: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 167); pub const LISTENING_SOCKET_ADDR: SocketAddrV4 = SocketAddrV4::new(Ipv4Addr::from_bits(0), DEFAULT_PORT); -pub type ShutdownSender = mpsc::Sender<()>; - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Listeners { Udp, @@ -79,7 +77,7 @@ pub struct JocalService { pub socket: Arc, pub client: reqwest::Client, pub config: Config, - http_handle: Arc>, + http_handle: OnceLock, // the receiving end will be held by the application so it can update the UI based on backend // events transfer_event_tx: UnboundedSender, diff --git a/src/main.rs b/src/main.rs index 81b28e4..72b3ea9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,9 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result let mut handles = JoinSet::new(); app.service.start(&mut handles).await; - let mut alarm = tokio::time::interval(Duration::from_millis(200)); + let mut tick = tokio::time::interval(Duration::from_millis(200)); + let shutdown = shutdown(&mut handles); + let mut shutdown = std::pin::pin!(shutdown); loop { terminal.draw(|frame| app.draw(frame))?; @@ -63,11 +65,18 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result res = app.handle_events() => { res?; } - _ = alarm.tick() => {} + _ = tick.tick() => {} } if app.screen() == CurrentScreen::Stopping { - break; + tokio::select! { + _ = shutdown.as_mut() => { + break; + } + _ = tick.tick() => { + log::info!("shutting down"); + } + } } let peers = app.service.peers.lock().await; @@ -94,14 +103,12 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result } } - shutdown(&mut handles).await; - Ok(()) } async fn shutdown(handles: &mut JoinSet) { - let mut alarm = tokio::time::interval(Duration::from_secs(5)); - alarm.tick().await; + let mut timeout = tokio::time::interval(Duration::from_secs(5)); + timeout.tick().await; loop { tokio::select! { join_result = handles.join_next() => { @@ -113,7 +120,7 @@ async fn shutdown(handles: &mut JoinSet) { None => break, } } - _ = alarm.tick() => { + _ = timeout.tick() => { info!("Exit timeout reached, aborting all unjoined tasks"); handles.abort_all(); break;