summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-01 18:06:03 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-01 18:06:03 +0800
commite4a1fbbd596b5466b266a077cddd104933b4fbcf (patch)
treef5baa9e21d9b08c2d401428858505e09c3d1c396 /src
init
Diffstat (limited to 'src')
-rw-r--r--src/client/connector.rs41
-rw-r--r--src/client/connector/connectors.rs5
-rw-r--r--src/client/connector/connectors/httpconnector.rs45
-rw-r--r--src/client/connector/manager.rs12
-rw-r--r--src/client/main.rs6
-rw-r--r--src/lib.rs0
6 files changed, 109 insertions, 0 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
new file mode 100644
index 0000000..a846939
--- /dev/null
+++ b/src/client/connector.rs
@@ -0,0 +1,41 @@
+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<()>,
+}
+
+impl<C: Connection> Channel<C> {
+ fn new() -> Self {
+ let (sender, _): (Sender<C::Success>, Receiver<C::Success>) = channel();
+ let (_, receiver): (Sender<()>, Receiver<()>) = channel();
+ Self { sender, receiver }
+ }
+}
+
+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)
+
+ async fn r#loop(&self) -> ! {
+ loop {
+ if let Some(Err(e)) = self.update().await {
+ eprintln!("Error: {}", e)
+ }
+
+ sleep(time::Duration::from_secs(1));
+ }
+ }
+}
diff --git a/src/client/connector/connectors.rs b/src/client/connector/connectors.rs
new file mode 100644
index 0000000..3810a4a
--- /dev/null
+++ b/src/client/connector/connectors.rs
@@ -0,0 +1,5 @@
+mod httpconnector;
+
+pub(crate) enum Connectors<'a> {
+ Http(httpconnector::HTTPConnector<'a>),
+}
diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs
new file mode 100644
index 0000000..97f79dd
--- /dev/null
+++ b/src/client/connector/connectors/httpconnector.rs
@@ -0,0 +1,45 @@
+use std::net::AddrParseError;
+
+use crate::connector::*;
+
+pub(crate) struct HTTPConnector<'a> {
+ prev: String,
+ url: &'a str,
+ channel: Channel<Self>,
+}
+
+impl<'a> HTTPConnector<'a> {
+ fn new(url: &'a str) -> Result<Self, AddrParseError> {
+ Ok(Self {
+ prev: String::new(),
+ url,
+ channel: Channel::new(),
+ })
+ }
+}
+
+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).await {
+ Ok(resp) => match resp.text().await {
+ Ok(text) => text,
+ Err(e) => return Some(Err(e)),
+ },
+ Err(e) => return Some(Err(e)),
+ };
+
+ if res == self.prev {
+ 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
new file mode 100644
index 0000000..9ca8a24
--- /dev/null
+++ b/src/client/connector/manager.rs
@@ -0,0 +1,12 @@
+use crate::connector::connectors;
+use std::thread;
+
+pub(crate) struct ConnectorManager<'a> {
+ connector: connectors::Connectors<'a>,
+}
+
+impl<'a> ConnectorManager<'a> {
+ pub(crate) fn new(connector: connectors::Connectors<'a>) -> Self {
+ Self { connector }
+ }
+}
diff --git a/src/client/main.rs b/src/client/main.rs
new file mode 100644
index 0000000..f514a74
--- /dev/null
+++ b/src/client/main.rs
@@ -0,0 +1,6 @@
+#![feature(associated_type_defaults)]
+mod connector;
+
+fn main() {
+ // connector::manager::ConnectorManager::new();
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/lib.rs