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

View file

@ -1,6 +1,6 @@
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout, Rect},
layout::{Constraint, Direction, Layout, Margin, Rect},
style::Stylize,
text::Line,
widgets::{Block, Borders, List, ListItem, Padding},
@ -34,8 +34,8 @@ impl App {
.cloned()
.unwrap();
network_info(frame, footer_left);
peers(&self.peers, frame, footer_right);
network_info(frame, footer_left.inner(Margin::new(1, 1)));
peers(&self.peers, frame, footer_right.inner(Margin::new(1, 1)));
// draw the main frame last
frame.render_widget(self, frame.area());
}
@ -70,7 +70,7 @@ fn network_info(frame: &mut Frame, area: Rect) {
)
.yellow()
.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()
.into();
let items = [

View file

@ -8,7 +8,6 @@ use std::{
collections::HashMap,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
sync::{Arc, OnceLock},
time::Duration,
};
use models::Device;
@ -108,22 +107,14 @@ impl JoecalState {
}
pub async fn stop(&self) {
loop {
let mut rstate = self.running_state.lock().await;
*rstate = RunningState::Stopping;
if self
.stop_tx
.get()
.expect("Could not get stop signal transmitter")
.send(())
.await
.is_ok()
{
break;
} else {
tokio::time::sleep(Duration::from_millis(777)).await;
}
}
let mut rstate = self.running_state.lock().await;
*rstate = RunningState::Stopping;
let _ = self
.stop_tx
.get()
.expect("Could not get stop signal transmitter")
.send(())
.await;
}
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)
let mut network_ip = ip;
let mut _network_ip = ip;
let nifs = NetworkInterface::show().unwrap();
for addr in nifs.into_iter().flat_map(|i| i.addr) {
if let Addr::V4(V4IfAddr {
@ -28,7 +28,7 @@ async fn main() -> error::Result<()> {
}) = addr
&& ip == ifip
{
network_ip = ip & netmask;
_network_ip = ip & netmask;
break;
}
}