From a2649af8a0295f4bc2350907adb93715a58edaa3 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Sun, 8 Jun 2025 13:11:31 -0500 Subject: [PATCH] adding client suicide & dev mode --- client/src/app.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/client/src/app.rs b/client/src/app.rs index 9ba2f21..d54b15d 100644 --- a/client/src/app.rs +++ b/client/src/app.rs @@ -1,22 +1,29 @@ use anyhow::{Context, Result}; -use navka_common::navka::{relay_client::RelayClient, HidReport}; use tokio::sync::mpsc; +use tokio::time::{timeout, Duration}; use tokio_stream::wrappers::ReceiverStream; use tonic::Request; +use tracing::info; +use navka_common::navka::{relay_client::RelayClient, HidReport}; use crate::input::keyboard::KeyboardAggregator; pub struct NavkaClientApp { server_addr: String, + dev_mode: bool, } impl NavkaClientApp { pub fn new() -> Result { + let dev_mode = std::env::var("NAVKA_DEV_MODE").is_ok(); let addr = std::env::args() .nth(1) .or_else(|| std::env::var("NAVKA_SERVER_ADDR").ok()) .unwrap_or_else(|| "http://127.0.0.1:50051".to_owned()); - Ok(Self { server_addr: addr }) + Ok(Self { + server_addr: addr, + dev_mode, + }) } pub async fn run(&mut self) -> Result<()> { @@ -40,12 +47,29 @@ impl NavkaClientApp { } }); - // 4) Inbound loop: we do something with reports from the server, e.g. logging: + // 4) Add 30 second suicide for dev mode + if self.dev_mode { + // dev mode: we do a 30-second kill + info!("NAVKA_DEV_MODE: aggregator will time out in 30 seconds"); + match timeout(Duration::from_secs(30), aggregator_task).await { + Ok(_) => { + info!("aggregator finished within 30s"); + } + Err(_) => { + tracing::warn!("dev mode: aggregator didn’t finish in 30s -> kill navka-client"); + } + } + std::process::exit(0); + } else { + // normal mode: read aggregator forever + } + + // 5) Inbound loop: we do something with reports from the server, e.g. logging: while let Some(report) = inbound.message().await? { tracing::info!(?report.data, "echo from server"); } - // 5) If inbound stream ends, stop the input task + // 6) If inbound stream ends, stop the input task input_task.abort(); Ok(()) }