shutdown more reliably

This commit is contained in:
Joe Ardent 2025-07-15 15:46:13 -07:00
parent b5a950d49e
commit cac0e4f6e3
4 changed files with 27 additions and 35 deletions

View file

@ -1,9 +1,4 @@
use std::{ use std::{collections::BTreeMap, io, net::SocketAddr, time::Duration};
collections::{BTreeMap, VecDeque},
io,
net::SocketAddr,
time::Duration,
};
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind}; use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind};
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
@ -74,7 +69,6 @@ impl App {
} }
async fn handle_events(&mut self) -> io::Result<()> { async fn handle_events(&mut self) -> io::Result<()> {
let mut tick = tokio::time::interval(Duration::from_millis(100));
tokio::select! { tokio::select! {
event = self.events.next().fuse() => { event = self.events.next().fuse() => {
if let Some(Ok(evt)) = event { if let Some(Ok(evt)) = event {
@ -88,7 +82,7 @@ impl App {
} }
} }
} }
_ = tick.tick() => {} _ = tokio::time::sleep(Duration::from_millis(200)) => {}
} }
Ok(()) Ok(())
@ -109,18 +103,25 @@ impl App {
} }
fn send(&mut self) { fn send(&mut self) {
self.screen.push(CurrentScreen::Sending); let last = self.screen.last();
match last {
Some(CurrentScreen::Sending) => {}
_ => self.screen.push(CurrentScreen::Sending),
}
} }
fn recv(&mut self) { fn recv(&mut self) {
self.screen.push(CurrentScreen::Receiving); let last = self.screen.last();
match last {
Some(CurrentScreen::Receiving) => {}
_ => self.screen.push(CurrentScreen::Receiving),
}
} }
fn pop(&mut self) { fn pop(&mut self) {
self.screen.pop();
if self.screen.last().is_none() { if self.screen.last().is_none() {
self.screen.push(CurrentScreen::Main); self.screen.push(CurrentScreen::Main);
} else {
self.screen.pop();
} }
} }
} }

View file

@ -1,6 +1,6 @@
use ratatui::{ use ratatui::{
Frame, Frame,
layout::{Constraint, Direction, Layout, Rect}, layout::{Constraint, Direction, Layout, Margin, Rect},
style::Stylize, style::Stylize,
text::Line, text::Line,
widgets::{Block, Borders, List, ListItem, Padding}, widgets::{Block, Borders, List, ListItem, Padding},
@ -34,8 +34,8 @@ impl App {
.cloned() .cloned()
.unwrap(); .unwrap();
network_info(frame, footer_left); network_info(frame, footer_left.inner(Margin::new(1, 1)));
peers(&self.peers, frame, footer_right); peers(&self.peers, frame, footer_right.inner(Margin::new(1, 1)));
// draw the main frame last // draw the main frame last
frame.render_widget(self, frame.area()); frame.render_widget(self, frame.area());
} }
@ -70,7 +70,7 @@ fn network_info(frame: &mut Frame, area: Rect) {
) )
.yellow() .yellow()
.into(); .into();
let http: Line = format!(" HTTP address:\t\t{:?}", joecalsend::LISTENING_SOCKET_ADDR) let http: Line = format!(" HTTP address:\t{:?}", joecalsend::LISTENING_SOCKET_ADDR)
.yellow() .yellow()
.into(); .into();
let items = [ let items = [

View file

@ -8,7 +8,6 @@ use std::{
collections::HashMap, collections::HashMap,
net::{Ipv4Addr, SocketAddr, SocketAddrV4}, net::{Ipv4Addr, SocketAddr, SocketAddrV4},
sync::{Arc, OnceLock}, sync::{Arc, OnceLock},
time::Duration,
}; };
use models::Device; use models::Device;
@ -108,22 +107,14 @@ impl JoecalState {
} }
pub async fn stop(&self) { pub async fn stop(&self) {
loop { let mut rstate = self.running_state.lock().await;
let mut rstate = self.running_state.lock().await; *rstate = RunningState::Stopping;
*rstate = RunningState::Stopping; let _ = self
if self .stop_tx
.stop_tx .get()
.get() .expect("Could not get stop signal transmitter")
.expect("Could not get stop signal transmitter") .send(())
.send(()) .await;
.await
.is_ok()
{
break;
} else {
tokio::time::sleep(Duration::from_millis(777)).await;
}
}
} }
pub async fn refresh_peers(&self) { pub async fn refresh_peers(&self) {

View file

@ -18,7 +18,7 @@ async fn main() -> error::Result<()> {
}; };
// for enumerating subnet peers when multicast fails (https://github.com/localsend/protocol?tab=readme-ov-file#32-http-legacy-mode) // for enumerating subnet peers when multicast fails (https://github.com/localsend/protocol?tab=readme-ov-file#32-http-legacy-mode)
let mut network_ip = ip; let mut _network_ip = ip;
let nifs = NetworkInterface::show().unwrap(); let nifs = NetworkInterface::show().unwrap();
for addr in nifs.into_iter().flat_map(|i| i.addr) { for addr in nifs.into_iter().flat_map(|i| i.addr) {
if let Addr::V4(V4IfAddr { if let Addr::V4(V4IfAddr {
@ -28,7 +28,7 @@ async fn main() -> error::Result<()> {
}) = addr }) = addr
&& ip == ifip && ip == ifip
{ {
network_ip = ip & netmask; _network_ip = ip & netmask;
break; break;
} }
} }