narrowlink_types/agent/
mod.rs

1mod event;
2use core::fmt::Display;
3use std::net::SocketAddr;
4
5pub use event::InBound as EventInBound;
6pub use event::OutBound as EventOutBound;
7pub use event::Peer2PeerInstruction;
8pub use event::Request as EventRequest;
9pub use event::Response as EventResponse;
10use serde::Deserialize;
11use serde::Serialize;
12
13use crate::generic::Connect;
14use crate::generic::Protocol;
15
16#[derive(Serialize, Deserialize, Clone, Debug)]
17pub struct SystemInfo {
18    pub dynamic: DynSystemInfo,
19    pub constant: ConstSystemInfo,
20}
21
22#[derive(Serialize, Deserialize, Clone, Debug)]
23pub struct DynSystemInfo {
24    pub loadavg: f64,
25}
26
27impl Default for DynSystemInfo {
28    fn default() -> Self {
29        Self { loadavg: 0.0 }
30    }
31}
32
33#[derive(Serialize, Deserialize, Clone, Debug)]
34pub struct ConstSystemInfo {
35    pub cpus: u8,
36    pub local_addr: SocketAddr,
37}
38
39#[derive(Serialize, Deserialize, Clone)]
40pub struct AgentPublishInfo {
41    src_host: String,
42    src_port: u16,
43    dst_host: String,
44    dst_port: u16,
45    protocol: Protocol,
46}
47
48impl Display for AgentPublishInfo {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(
51            f,
52            "{}:{}->{}://{}:{}",
53            self.src_host,
54            if self.src_port == 0 {
55                "any".to_owned()
56            } else {
57                self.src_port.to_string()
58            },
59            self.protocol,
60            self.dst_host,
61            self.dst_port
62        )
63    }
64}
65
66impl AgentPublishInfo {
67    pub fn from_connect(host: String, src_port: u16, connect: &Connect) -> Self {
68        Self {
69            src_host: host,
70            src_port,
71            dst_host: connect.host.clone(),
72            dst_port: connect.port,
73            protocol: connect.protocol.clone(),
74        }
75    }
76}