veilid_core/veilid_api/types/
veilid_state.rs

1use super::*;
2
3/// Attachment abstraction for network 'signal strength'.
4#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, JsonSchema)]
5#[cfg_attr(
6    all(target_arch = "wasm32", target_os = "unknown"),
7    derive(Tsify),
8    tsify(namespace, from_wasm_abi, into_wasm_abi)
9)]
10#[must_use]
11pub enum AttachmentState {
12    Detached = 0,
13    Attaching = 1,
14    AttachedWeak = 2,
15    AttachedGood = 3,
16    AttachedStrong = 4,
17    FullyAttached = 5,
18    OverAttached = 6,
19    Detaching = 7,
20}
21impl AttachmentState {
22    #[must_use]
23    pub fn is_detached(&self) -> bool {
24        matches!(self, Self::Detached)
25    }
26    #[must_use]
27    pub fn is_attached(&self) -> bool {
28        matches!(
29            self,
30            Self::AttachedWeak
31                | Self::AttachedGood
32                | Self::AttachedStrong
33                | Self::FullyAttached
34                | Self::OverAttached
35        )
36    }
37}
38
39impl fmt::Display for AttachmentState {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
41        let out = match self {
42            AttachmentState::Attaching => "attaching",
43            AttachmentState::AttachedWeak => "attached_weak",
44            AttachmentState::AttachedGood => "attached_good",
45            AttachmentState::AttachedStrong => "attached_strong",
46            AttachmentState::FullyAttached => "fully_attached",
47            AttachmentState::OverAttached => "over_attached",
48            AttachmentState::Detaching => "detaching",
49            AttachmentState::Detached => "detached",
50        };
51        write!(f, "{}", out)
52    }
53}
54
55impl TryFrom<String> for AttachmentState {
56    type Error = ();
57
58    fn try_from(s: String) -> Result<Self, Self::Error> {
59        AttachmentState::try_from(s.as_ref())
60    }
61}
62
63impl TryFrom<&str> for AttachmentState {
64    type Error = ();
65
66    fn try_from(s: &str) -> Result<Self, Self::Error> {
67        Ok(match s {
68            "attaching" => AttachmentState::Attaching,
69            "attached_weak" => AttachmentState::AttachedWeak,
70            "attached_good" => AttachmentState::AttachedGood,
71            "attached_strong" => AttachmentState::AttachedStrong,
72            "fully_attached" => AttachmentState::FullyAttached,
73            "over_attached" => AttachmentState::OverAttached,
74            "detaching" => AttachmentState::Detaching,
75            "detached" => AttachmentState::Detached,
76            _ => return Err(()),
77        })
78    }
79}
80
81/// Describe the attachment state of the Veilid node
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
83#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
84#[must_use]
85pub struct VeilidStateAttachment {
86    /// The overall quality of the routing table if attached, or the current state the attachment state machine.
87    pub state: AttachmentState,
88    /// If attached and there are enough reachable nodes in the routing table to perform all the actions of the PublicInternet RoutingDomain,
89    /// including things like private/safety route allocation and DHT operations.
90    pub public_internet_ready: bool,
91    /// If attached and there are enough reachable nodes in the routing table to perform all the actions of the LocalNetwork RoutingDomain.
92    pub local_network_ready: bool,
93    /// Node uptime
94    pub uptime: TimestampDuration,
95    /// Uptime since last attach, empty if the node is currently detached
96    pub attached_uptime: Option<TimestampDuration>,
97}
98
99/// Describe a recently accessed peer
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
101#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
102#[must_use]
103pub struct PeerTableData {
104    /// The node ids used by this peer
105    #[schemars(with = "Vec<String>")]
106    #[cfg_attr(
107        all(target_arch = "wasm32", target_os = "unknown"),
108        tsify(type = "string[]")
109    )]
110    pub node_ids: Vec<TypedNodeId>,
111    /// The peer's human readable address.
112    pub peer_address: String,
113    /// Statistics we have collected on this peer.
114    pub peer_stats: PeerStats,
115}
116
117/// Describe the current network state of the Veilid node
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
119#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
120#[must_use]
121pub struct VeilidStateNetwork {
122    /// If the network has been started or not.
123    pub started: bool,
124    /// The total number of bytes per second used by Veilid currently in the download direction.
125    pub bps_down: ByteCount,
126    /// The total number of bytes per second used by Veilid currently in the upload direction.
127    pub bps_up: ByteCount,
128    /// The list of most recently accessed peers.
129    /// This is not an active connection table, nor is representative of the entire routing table.
130    pub peers: Vec<PeerTableData>,
131}
132
133/// Describe a private route change that has happened
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
135#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
136#[must_use]
137pub struct VeilidRouteChange {
138    /// If a private route that was allocated has died, it is listed here.
139    #[schemars(with = "Vec<String>")]
140    pub dead_routes: Vec<RouteId>,
141    /// If a private route that was imported has died, it is listed here.
142    #[schemars(with = "Vec<String>")]
143    pub dead_remote_routes: Vec<RouteId>,
144}
145
146/// Describe changes to the Veilid node configuration
147/// Currently this is only ever emitted once, however we reserve the right to
148/// add the ability to change the configuration or have it changed by the Veilid node
149/// itself during runtime.
150#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
151#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
152#[must_use]
153pub struct VeilidStateConfig {
154    /// If the Veilid node configuration has changed the full new config will be here.
155    pub config: VeilidConfig,
156}
157
158/// Describe when DHT records have subkey values changed
159#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
160#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
161#[must_use]
162pub struct VeilidValueChange {
163    /// The DHT Record key that changed
164    #[schemars(with = "String")]
165    pub key: TypedRecordKey,
166    /// The portion of the DHT Record's subkeys that have changed
167    /// If the subkey range is empty, any watch present on the value has died.
168    pub subkeys: ValueSubkeyRangeSet,
169    /// The count remaining on the watch that triggered this value change
170    /// If there is no watch and this is received, it will be set to u32::MAX
171    /// If this value is zero, any watch present on the value has died.
172    pub count: u32,
173    /// The (optional) value data for the first subkey in the subkeys range
174    /// If 'subkeys' is not a single value, other values than the first value
175    /// must be retrieved with RoutingContext::get_dht_value().
176    pub value: Option<ValueData>,
177}
178
179/// An update from the veilid-core to the host application describing a change
180/// to the internal state of the Veilid node.
181#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
182#[cfg_attr(
183    all(target_arch = "wasm32", target_os = "unknown"),
184    derive(Tsify),
185    tsify(into_wasm_abi)
186)]
187#[serde(tag = "kind")]
188#[must_use]
189pub enum VeilidUpdate {
190    Log(Box<VeilidLog>),
191    AppMessage(Box<VeilidAppMessage>),
192    AppCall(Box<VeilidAppCall>),
193    Attachment(Box<VeilidStateAttachment>),
194    Network(Box<VeilidStateNetwork>),
195    Config(Box<VeilidStateConfig>),
196    RouteChange(Box<VeilidRouteChange>),
197    ValueChange(Box<VeilidValueChange>),
198    Shutdown,
199}
200
201/// A queriable state of the internals of veilid-core.
202#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
203#[cfg_attr(
204    all(target_arch = "wasm32", target_os = "unknown"),
205    derive(Tsify),
206    tsify(into_wasm_abi)
207)]
208#[must_use]
209pub struct VeilidState {
210    pub attachment: Box<VeilidStateAttachment>,
211    pub network: Box<VeilidStateNetwork>,
212    pub config: Box<VeilidStateConfig>,
213}