Skip to main content

monitord/varlink/
endpoint.rs

1//! Where a varlink collector connects to.
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::varlink::machine_connector::{MachineConnector, MachineSocket};
7
8/// A varlink socket to connect to: either reachable by path from monitord's
9/// own namespaces (the host), or inside a machine, connected from within the
10/// machine's PID namespace by its [`MachineConnector`].
11#[derive(Clone, Debug)]
12pub enum VarlinkEndpoint {
13    Path(String),
14    Machine {
15        connector: Arc<MachineConnector>,
16        socket: MachineSocket,
17    },
18}
19
20impl VarlinkEndpoint {
21    pub async fn connect(&self) -> anyhow::Result<zlink::tokio::unix::Connection> {
22        match self {
23            Self::Path(path) => Ok(zlink::tokio::unix::connect(path).await?),
24            Self::Machine { connector, socket } => connector.connect(*socket).await,
25        }
26    }
27}
28
29impl From<&str> for VarlinkEndpoint {
30    fn from(path: &str) -> Self {
31        Self::Path(path.to_string())
32    }
33}
34
35impl From<String> for VarlinkEndpoint {
36    fn from(path: String) -> Self {
37        Self::Path(path)
38    }
39}
40
41impl fmt::Display for VarlinkEndpoint {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::Path(path) => f.write_str(path),
45            Self::Machine { connector, socket } => write!(
46                f,
47                "{} in machine with leader {}",
48                socket.path(),
49                connector.leader_pid()
50            ),
51        }
52    }
53}