summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-12 23:33:54 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-12 23:33:54 +0800
commit40099749a6b264593ad54032411f02df9967790c (patch)
tree6ead79fd2c8930db1a64b80e40aa611f440dca8b /src/client
parentbc6b38c1b627c6dcdd50896ded4148dd6e0734ba (diff)
refactor: don't use concurrency, single threaded for now
Diffstat (limited to 'src/client')
-rw-r--r--src/client/connector.rs36
-rw-r--r--src/client/connector/connectors/httpconnector.rs14
-rw-r--r--src/client/connector/manager.rs13
-rw-r--r--src/client/executor.rs15
-rw-r--r--src/client/executor/tasks.rs29
-rw-r--r--src/client/main.rs3
6 files changed, 65 insertions, 45 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
index e092718..709b91d 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -1,37 +1,27 @@
pub(crate) mod connectors;
pub(crate) mod manager;
-use std::{
- sync::mpsc::{channel, Receiver, Sender},
- thread::sleep,
- time,
-};
-
-struct Channel<C: Connection> {
- sender: Sender<C::Success>,
- receiver: Receiver<C::Success>,
-}
-
-impl<C: Connection> Channel<C> {
- fn new() -> Self {
- let (sender, receiver) = channel();
- Self { sender, receiver }
- }
-}
+use std::{thread::sleep, time};
pub(crate) trait Connection {
type Err: std::fmt::Display;
type Success: std::fmt::Display = String;
- fn test_connection(&self) -> Result<bool, Self::Err>;
-
// should use channel to notify update
- async fn update(&self) -> Option<Result<Self::Success, Self::Err>>; // None if no update(content is same)
+ fn update(&self) -> Option<Result<Self::Success, Self::Err>>; // None if no update(content is same)
- async fn r#loop(&self) -> ! {
+ fn r#loop(&self) -> ! {
loop {
- if let Some(Err(e)) = self.update().await {
- eprintln!("Error: {}", e)
+ // if let Some(Err(e)) = self.update() {
+ // eprintln!("Error: {}", e)
+ // }
+ match self.update() {
+ Some(Ok(s)) => {
+ // _ = self.channel.sender.send(res.clone()); // don't care if receiver is dropped
+ // _ = executor::new(res.as_str());
+ }
+ Some(Err(e)) => eprintln!("Error: {}", e),
+ _ => {}
}
sleep(time::Duration::from_secs(1));
diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs
index 6d501cc..84596c9 100644
--- a/src/client/connector/connectors/httpconnector.rs
+++ b/src/client/connector/connectors/httpconnector.rs
@@ -3,7 +3,6 @@ use crate::connector::*;
pub(crate) struct HTTPConnector {
prev: String,
url: reqwest::Url,
- channel: Channel<Self>,
}
impl HTTPConnector {
@@ -11,7 +10,6 @@ impl HTTPConnector {
Ok(Self {
prev: String::new(),
url: reqwest::Url::parse(url)?,
- channel: Channel::new(),
})
}
}
@@ -19,13 +17,9 @@ impl HTTPConnector {
impl Connection for HTTPConnector {
type Err = reqwest::Error;
- fn test_connection(&self) -> std::result::Result<bool, reqwest::Error> {
- Ok(true)
- }
-
- async fn update(&self) -> Option<Result<Self::Success, Self::Err>> {
- let res = match reqwest::get(self.url.clone()).await {
- Ok(resp) => match resp.text().await {
+ fn update(&self) -> Option<Result<Self::Success, Self::Err>> {
+ let res = match reqwest::blocking::get(self.url.as_str()) {
+ Ok(resp) => match resp.text() {
Ok(text) => text,
Err(e) => return Some(Err(e)),
},
@@ -36,8 +30,6 @@ impl Connection for HTTPConnector {
return None;
}
- _ = self.channel.sender.send(res.clone()); // don't care if receiver is dropped
-
Some(Ok(res))
}
}
diff --git a/src/client/connector/manager.rs b/src/client/connector/manager.rs
index 67cf0a9..a7ef293 100644
--- a/src/client/connector/manager.rs
+++ b/src/client/connector/manager.rs
@@ -1,22 +1,15 @@
use crate::connector::Connection;
-use std::thread;
pub(crate) struct ConnectorManager<C: Connection> {
connector: C,
- thread: Option<thread::JoinHandle<!>>,
}
impl<C: Connection> ConnectorManager<C> {
pub(crate) fn new(connector: C) -> Self {
- Self {
- connector,
- thread: None,
- }
+ Self { connector }
}
- pub(crate) fn start(&mut self) {
- self.thread = Some(thread::spawn(|| loop {
- println!("Hello, world!");
- }))
+ pub(crate) fn enter_loop(&mut self) {
+ self.connector.r#loop();
}
}
diff --git a/src/client/executor.rs b/src/client/executor.rs
new file mode 100644
index 0000000..eee1b36
--- /dev/null
+++ b/src/client/executor.rs
@@ -0,0 +1,15 @@
+use crate::executor::tasks::Task;
+
+mod tasks;
+
+struct Executor<T: Task> {
+ task: T,
+}
+
+impl<T: Task> Executor<T> {
+ // fn new(args: &str) -> Self {
+ // // exe(parse())
+ // }
+
+ // IntoTask like IntoUrl
+}
diff --git a/src/client/executor/tasks.rs b/src/client/executor/tasks.rs
new file mode 100644
index 0000000..90094de
--- /dev/null
+++ b/src/client/executor/tasks.rs
@@ -0,0 +1,29 @@
+use std::str::FromStr;
+
+pub(crate) trait Task: FromStr {
+ type Err: std::fmt::Display;
+ type Success: std::fmt::Display = String;
+
+ fn execute() -> Result<Self::Success, <Self as Task>::Err>;
+}
+
+pub(crate) trait IntoTask {
+ type Err: std::fmt::Display;
+
+ fn into_task(self) -> Result<impl Task, Self::Err>;
+}
+
+impl IntoTask for String {
+ type Err = <String as FromStr>::Err;
+
+ fn into_task(self) -> Result<impl Task, Self::Err> {}
+}
+//
+// impl Task for {
+// type Err = std::num::ParseIntError;
+// type Success = u32;
+//
+// fn execute() -> Result<Self::Success, Self::Err> {
+// Ok(42)
+// }
+// }
diff --git a/src/client/main.rs b/src/client/main.rs
index 4ef7a9b..cada2f8 100644
--- a/src/client/main.rs
+++ b/src/client/main.rs
@@ -4,9 +4,10 @@ use std::thread;
use crate::connector::{connectors::httpconnector::HTTPConnector, manager::ConnectorManager};
mod connector;
+mod executor;
fn main() {
- ConnectorManager::new(HTTPConnector::new("https://example.com").unwrap()).start();
+ ConnectorManager::new(HTTPConnector::new("https://example.com").unwrap()).enter_loop();
thread::park();
}