adding client suicide & dev mode
This commit is contained in:
parent
a638d98373
commit
a2649af8a0
@ -1,22 +1,29 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use navka_common::navka::{relay_client::RelayClient, HidReport};
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
use tokio::time::{timeout, Duration};
|
||||||
use tokio_stream::wrappers::ReceiverStream;
|
use tokio_stream::wrappers::ReceiverStream;
|
||||||
use tonic::Request;
|
use tonic::Request;
|
||||||
|
use tracing::info;
|
||||||
|
use navka_common::navka::{relay_client::RelayClient, HidReport};
|
||||||
use crate::input::keyboard::KeyboardAggregator;
|
use crate::input::keyboard::KeyboardAggregator;
|
||||||
|
|
||||||
pub struct NavkaClientApp {
|
pub struct NavkaClientApp {
|
||||||
server_addr: String,
|
server_addr: String,
|
||||||
|
dev_mode: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NavkaClientApp {
|
impl NavkaClientApp {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
|
let dev_mode = std::env::var("NAVKA_DEV_MODE").is_ok();
|
||||||
let addr = std::env::args()
|
let addr = std::env::args()
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.or_else(|| std::env::var("NAVKA_SERVER_ADDR").ok())
|
.or_else(|| std::env::var("NAVKA_SERVER_ADDR").ok())
|
||||||
.unwrap_or_else(|| "http://127.0.0.1:50051".to_owned());
|
.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<()> {
|
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? {
|
while let Some(report) = inbound.message().await? {
|
||||||
tracing::info!(?report.data, "echo from server");
|
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();
|
input_task.abort();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user