Skip to main content

isothrum_client/
discovery.rs

1//! Where a home's service is: the files it keeps in the home, and `service.json`, which says
2//! where it listens. A client finds the endpoint only there, on every platform.
3
4use std::path::{Path, PathBuf};
5
6use serde::{Deserialize, Serialize};
7
8/// The files of a home's service.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ServicePaths {
11    home: PathBuf,
12}
13
14impl ServicePaths {
15    /// The service files of `home`.
16    #[must_use]
17    pub fn new(home: impl Into<PathBuf>) -> ServicePaths {
18        ServicePaths { home: home.into() }
19    }
20
21    /// The home.
22    #[must_use]
23    pub fn home(&self) -> &Path {
24        &self.home
25    }
26
27    /// The file the service holds an OS lock on while it runs: one service per home.
28    #[must_use]
29    pub fn lock(&self) -> PathBuf {
30        self.home.join("service.lock")
31    }
32
33    /// The file a client holds an OS lock on while it replaces the home's service
34    /// ([`Client::connect_exact`](crate::Client::connect_exact)): two replace it in turn.
35    #[must_use]
36    pub fn replace_lock(&self) -> PathBuf {
37        self.home.join("service.replace.lock")
38    }
39
40    /// Where the running service says who and where it is ([`ServiceInfo`]).
41    #[must_use]
42    pub fn info(&self) -> PathBuf {
43        self.home.join("service.json")
44    }
45
46    /// The Unix domain socket a service on Unix listens on.
47    #[must_use]
48    pub fn socket(&self) -> PathBuf {
49        self.home.join("service.sock")
50    }
51
52    /// The log a started service writes (its standard output and error).
53    #[must_use]
54    pub fn log(&self) -> PathBuf {
55        self.home.join("service.log")
56    }
57
58    /// The running service's [`ServiceInfo`], when `service.json` is there and reads as one (it
59    /// may be a dead service's: connecting tells).
60    #[must_use]
61    pub fn read_info(&self) -> Option<ServiceInfo> {
62        let text = std::fs::read_to_string(self.info()).ok()?;
63        serde_json::from_str(&text).ok()
64    }
65}
66
67/// What a running service writes to `<home>/service.json` once it listens.
68#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
69pub struct ServiceInfo {
70    /// Its process id.
71    pub pid: u32,
72    /// When it started (RFC 3339, UTC).
73    pub started: String,
74    /// Its version.
75    pub version: String,
76    /// The protocol it speaks.
77    pub protocol: u32,
78    /// Where it listens: the socket's path on Unix, the named pipe's name on Windows.
79    pub endpoint: String,
80    /// Its life: a random number each start draws.
81    pub epoch: u64,
82    /// The build it runs ([`build_of`]): of the binary the client that started it started (the
83    /// [`BUILD_ENV`] it was given), else of its own executable. A service of another build is
84    /// replaced as one of another release is ([`Client::connect_exact`](crate::Client::connect_exact));
85    /// none from a service of before builds were written.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub build: Option<String>,
88}
89
90/// The variable a client gives the service it starts: the build of the binary it started
91/// ([`build_of`]), which the service writes as its [`ServiceInfo::build`].
92pub const BUILD_ENV: &str = "ISOTHRUM_SERVICE_BUILD";
93
94/// The build of the executable at `path`: its size and modification time (`<bytes>-<nanoseconds
95/// since the epoch>`), which an update of the program changes; none when it can't be read. Two
96/// versions of an app may carry the same release of Isothrum: this tells them apart.
97#[must_use]
98pub fn build_of(path: &Path) -> Option<String> {
99    let meta = std::fs::metadata(path).ok().filter(std::fs::Metadata::is_file)?;
100    let modified = meta.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?;
101    Some(format!("{}-{}", meta.len(), modified.as_nanos()))
102}