iroh_node_util/rpc/client/
node.rs

1//! Client to interact with an iroh node.
2//!
3//! The main entry point is [`Client`].
4use std::collections::BTreeMap;
5
6use anyhow::Result;
7use quic_rpc::RpcClient;
8
9use super::net::NodeStatus;
10use crate::rpc::proto::{node::*, RpcService};
11
12/// Client to interact with an iroh node.
13#[derive(Debug, Clone)]
14#[repr(transparent)]
15pub struct Client {
16    pub(super) rpc: RpcClient<RpcService>,
17}
18
19impl Client {
20    /// Creates a new node client
21    pub fn new(rpc: RpcClient<RpcService>) -> Self {
22        Self { rpc }
23    }
24
25    /// Shuts down the node.
26    ///
27    /// If `force` is true, the node will be shut down instantly without
28    /// waiting for things to stop gracefully.
29    pub async fn shutdown(&self, force: bool) -> Result<()> {
30        self.rpc.rpc(ShutdownRequest { force }).await?;
31        Ok(())
32    }
33
34    /// Fetches statistics of the running node.
35    pub async fn stats(&self) -> Result<BTreeMap<String, CounterStats>> {
36        let res = self.rpc.rpc(StatsRequest {}).await??;
37        Ok(res.stats)
38    }
39
40    /// Fetches status information about this node.
41    pub async fn status(&self) -> Result<NodeStatus> {
42        let response = self.rpc.rpc(StatusRequest).await??;
43        Ok(response)
44    }
45}