diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index 2a50548..47c6059 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -308,7 +308,8 @@ pub async fn run_server ( config_file: ConfigFile, shutdown_oneshot: oneshot::Receiver <()>, hidden_path: Option , - asset_root: Option + asset_root: Option , + hit_counter: Option >, ) -> Result <(), ServerError> { @@ -340,8 +341,13 @@ pub async fn run_server ( let mut spawn_handler = || { let file_server = Arc::clone (&file_server); + let hit_counter = hit_counter.clone (); |req: http_serde::RequestParts| async move { + if let Some (hit_tx) = &hit_counter { + eprintln! ("hit_tx.send"); + hit_tx.send (()).await; + } Ok (file_server.serve_all (req.method, &req.uri, &req.headers).await?) } }; @@ -560,7 +566,8 @@ pub mod executable { config_file, ptth_core::graceful_shutdown::init (), Some (path), - asset_root + asset_root, + None, ).await?; Ok (()) diff --git a/crates/ptth_server_gui/src/main.rs b/crates/ptth_server_gui/src/main.rs index 84e387a..a8565ab 100644 --- a/crates/ptth_server_gui/src/main.rs +++ b/crates/ptth_server_gui/src/main.rs @@ -14,7 +14,10 @@ use fltk::{ }; use tokio::{ - sync::oneshot, + sync::{ + mpsc, + oneshot, + }, }; struct ServerInstance { @@ -24,6 +27,7 @@ struct ServerInstance { #[derive (Clone, Copy)] enum Message { + Hit, RunServer, StopServer, } @@ -45,6 +49,19 @@ fn main () let config_file_opt = ptth_server::load_toml::load:: ("./config/ptth_server.toml").ok (); + let (hit_tx, mut hit_rx) = mpsc::channel (1); + { + let fltk_tx = fltk_tx.clone (); + + rt.spawn (async move { + eprintln! ("Entering channel task"); + while let Some (_) = hit_rx.recv ().await { + eprintln! ("fltk_tx"); + fltk_tx.send (Message::Hit); + } + }); + } + let mut gui = Gui::new (fltk_tx, config_file_opt.as_ref ()); gui.set_server_running (false); @@ -53,6 +70,10 @@ fn main () while app.wait () { match fltk_rx.recv () { + Some (Message::Hit) => { + gui.hits += 1; + gui.refresh_label (); + }, Some (Message::RunServer) => { let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel (); @@ -75,12 +96,14 @@ fn main () allow_any_client: true, }; + let hit_tx = hit_tx.clone (); let task = rt.spawn (async { if let Err (e) = ptth_server::run_server ( config_file, shutdown_rx, None, - None + None, + Some (hit_tx), ).await { tracing::error! ("ptth_server crashed: {}", e); @@ -114,6 +137,7 @@ struct Gui { input_api_key: SecretInput, server_is_running: bool, + hits: u64, } #[derive (Default, serde::Deserialize)] @@ -180,6 +204,7 @@ impl Gui { input_api_key, server_is_running: false, + hits: 0, } } @@ -200,12 +225,15 @@ impl Gui { } fn refresh_label (&mut self) { - self.frame.set_label (if self.server_is_running { - "Running" + let s = + if self.server_is_running { + format! ("Running. Requests: {}", self.hits) } else { - "Stopped" - }); + "Stopped".to_string () + }; + + self.frame.set_label (&s); } }