summaryrefslogtreecommitdiff
path: root/src/client/connector.rs
blob: e49d08ecdba956cd0c9a6bd20bca95e1b0f46804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<()>,
}

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));
        }
    }
}