summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-14 22:58:49 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-14 22:58:49 +0800
commit3d2874a218b0abe736dffa6d9a6b79c103ca5186 (patch)
treec73677067738f4e6c3d810f7cf70a331705b92e0
parent6fc21196b8283c397e7fe46e9a266b33166ca7cb (diff)
refactor: move event loop to client
-rw-r--r--src/client/client.rs10
-rw-r--r--src/client/connector.rs77
-rw-r--r--src/client/main.rs5
-rw-r--r--src/client/todo.md2
4 files changed, 49 insertions, 45 deletions
diff --git a/src/client/client.rs b/src/client/client.rs
index 2a5f595..cc4cea8 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -1,11 +1,12 @@
use gethostname::gethostname;
-use std::net::IpAddr;
+use std::{net::IpAddr, thread::sleep, time::Duration};
use whoami::{DesktopEnv, Language};
use crate::connector::Connection;
pub struct Client<C: Connection> {
pub connector: C,
+
ip: Option<IpAddr>,
hostname: String,
device_name: String,
@@ -71,4 +72,11 @@ impl<C: Connection> Client<C> {
c.connector.init(&c);
c
}
+
+ pub fn enter_loop(mut self) -> ! {
+ loop {
+ sleep(Duration::from_millis(500));
+ self.connector.tick();
+ }
+ }
}
diff --git a/src/client/connector.rs b/src/client/connector.rs
index 231b19a..7b5e1cd 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -1,6 +1,6 @@
pub mod httpconnector;
-use std::{net::IpAddr, thread::sleep, time::Duration};
+use std::net::IpAddr;
use crate::{client::Client, executor::Executor};
use remote_control_rs::tasks::IntoTask;
@@ -39,50 +39,47 @@ pub trait Connection {
}
fn reinit(&mut self) {
- sleep(Duration::from_millis(500));
self.force_network();
}
- fn r#loop(&mut self) -> ! {
- loop {
- // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO
- // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication
- // the cons of the current approch is that the first time we've entered, it would start sleeping
- self.reinit();
+ fn tick(&mut self) {
+ // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO
+ // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication
+ // the cons of the current approch is that the first time we've entered, it would start sleeping
+ self.reinit();
- match self.update() {
- Result::Ok(s) => {
- if self.is_prev(&s) {
- continue;
- }
-
- let mut it = s.as_ref().split_whitespace();
- let task = match it.next() {
- Some(t) => t,
- None => continue,
- };
- // FIXME: handle the errors... peak prod code
- self.ntfy(
- Executor::new(task, it.map(str::to_string))
- .unwrap()
- .run()
- .unwrap()
- .clone()
- .unwrap(),
- )
- .unwrap();
- }
- Result::Err(e) => {
- #[cfg(debug_assertions)]
- std::eprintln!(
- "[{}:{}:{}] {} = {:#?}",
- std::file!(),
- std::line!(),
- std::column!(),
- std::stringify!(e),
- &&e as &dyn std::fmt::Debug,
- );
+ match self.update() {
+ Result::Ok(s) => {
+ if self.is_prev(&s) {
+ return;
}
+
+ let mut it = s.as_ref().split_whitespace();
+ let task = match it.next() {
+ Some(t) => t,
+ None => return,
+ };
+ // FIXME: handle the errors... peak prod code
+ self.ntfy(
+ Executor::new(task, it.map(str::to_string))
+ .unwrap()
+ .run()
+ .unwrap()
+ .clone()
+ .unwrap(),
+ )
+ .unwrap();
+ }
+ Result::Err(e) => {
+ #[cfg(debug_assertions)]
+ std::eprintln!(
+ "[{}:{}:{}] {} = {:#?}",
+ std::file!(),
+ std::line!(),
+ std::column!(),
+ std::stringify!(e),
+ &&e as &dyn std::fmt::Debug,
+ );
}
}
}
diff --git a/src/client/main.rs b/src/client/main.rs
index 8ba230b..24c8a8b 100644
--- a/src/client/main.rs
+++ b/src/client/main.rs
@@ -2,7 +2,6 @@
#![feature(associated_type_defaults, never_type, lazy_type_alias)]
use crate::client::Client;
-use crate::connector::Connection;
use crate::connector::httpconnector::HTTPConnector;
use signal_hook::consts::signal::*;
use signal_hook::iterator::Signals;
@@ -39,6 +38,6 @@ fn main() {
]); // discard err
let ip = "http://127.0.0.1:8080";
- let mut client = Client::new(HTTPConnector::new(ip, ip, "/").unwrap());
- client.connector.r#loop();
+ let client = Client::new(HTTPConnector::new(ip, ip, "/").unwrap());
+ client.enter_loop();
}
diff --git a/src/client/todo.md b/src/client/todo.md
index d4c6166..5f316b8 100644
--- a/src/client/todo.md
+++ b/src/client/todo.md
@@ -2,4 +2,4 @@
- [ ] http json option
- [x] collect basic stuff
- [ ] better typed task
-- [ ] websocket http server option
+- [ ] websocket/SSE(streaming response) http server option