aboutsummaryrefslogtreecommitdiff
path: root/src/services/ping.rs
blob: 9191f86aabad6c40782a504d40b6ab575e319315 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::config::Config;
use crate::db::Device;
use dashmap::DashMap;
use ipnetwork::IpNetwork;
use sqlx::PgPool;
use std::fmt::Display;
use time::{Duration, Instant};
use tokio::sync::broadcast::Sender;
use tracing::{debug, error, trace};

pub type StatusMap = DashMap<String, Value>;

#[derive(Debug, Clone)]
pub struct Value {
    pub ip: IpNetwork,
    pub online: bool,
}

pub async fn spawn(
    tx: Sender<BroadcastCommand>,
    config: &Config,
    device: Device,
    uuid: String,
    ping_map: &StatusMap,
    db: &PgPool,
) {
    let timer = Instant::now();
    let payload = [0; 8];

    let mut msg: Option<BroadcastCommand> = None;
    while msg.is_none() {
        let ping = surge_ping::ping(device.ip.ip(), &payload).await;

        if let Err(ping) = ping {
            let ping_timeout = matches!(ping, surge_ping::SurgeError::Timeout { .. });
            if !ping_timeout {
                error!("{}", ping.to_string());
                msg = Some(BroadcastCommand::error(uuid.clone()));
            }
            if timer.elapsed() >= Duration::minutes(config.pingtimeout) {
                msg = Some(BroadcastCommand::timeout(uuid.clone()));
            }
        } else {
            let (_, duration) = ping
                .map_err(|err| error!("{}", err.to_string()))
                .expect("fatal error");
            debug!("ping took {:?}", duration);
            msg = Some(BroadcastCommand::success(uuid.clone()));
        };
    }

    let msg = msg.expect("fatal error");

    let _ = tx.send(msg.clone());
    if let BroadcastCommands::Success = msg.command {
        sqlx::query!(
            r#"
            UPDATE devices
            SET times = array_append(times, $1)
            WHERE id = $2;
            "#,
            timer.elapsed().whole_seconds(),
            device.id
        )
        .execute(db)
        .await
        .unwrap();
        ping_map.insert(
            uuid.clone(),
            Value {
                ip: device.ip,
                online: true,
            },
        );
        tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
    }
    trace!("remove {} from ping_map", uuid);
    ping_map.remove(&uuid);
}

#[derive(Clone, Debug, PartialEq)]
pub enum BroadcastCommands {
    Success,
    Timeout,
    Error,
}

#[derive(Clone, Debug, PartialEq)]
pub struct BroadcastCommand {
    pub uuid: String,
    pub command: BroadcastCommands,
}

impl Display for BroadcastCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let prefix = match self.command {
            BroadcastCommands::Success => "start",
            BroadcastCommands::Timeout => "timeout",
            BroadcastCommands::Error => "error",
        };

        f.write_str(format!("{prefix}_{}", self.uuid).as_str())
    }
}

impl BroadcastCommand {
    pub fn success(uuid: String) -> Self {
        Self {
            uuid,
            command: BroadcastCommands::Success,
        }
    }

    pub fn timeout(uuid: String) -> Self {
        Self {
            uuid,
            command: BroadcastCommands::Timeout,
        }
    }

    pub fn error(uuid: String) -> Self {
        Self {
            uuid,
            command: BroadcastCommands::Error,
        }
    }
}