sn_testnet_deploy/
rpc_client.rs

1// Copyright (c) 2023, MaidSafe.
2// All rights reserved.
3//
4// This SAFE Network Software is licensed under the BSD-3-Clause license.
5// Please see the LICENSE file for more details.
6
7use crate::{error::Result, run_external_command};
8use std::{net::SocketAddr, path::PathBuf};
9
10pub struct NodeInfo {
11    pub endpoint: String,
12    pub peer_id: String,
13    pub logs_dir: PathBuf,
14    pub pid: u16,
15    pub safenode_version: String,
16    pub last_restart: u32,
17}
18
19#[derive(Clone)]
20pub struct RpcClient {
21    pub binary_path: PathBuf,
22    pub working_directory_path: PathBuf,
23}
24
25impl RpcClient {
26    pub fn new(binary_path: PathBuf, working_directory_path: PathBuf) -> RpcClient {
27        RpcClient {
28            binary_path,
29            working_directory_path,
30        }
31    }
32
33    pub fn get_info(&self, rpc_address: SocketAddr) -> Result<NodeInfo> {
34        let output = run_external_command(
35            self.binary_path.clone(),
36            self.working_directory_path.clone(),
37            vec![rpc_address.to_string(), "info".to_string()],
38            false,
39            false,
40        )?;
41
42        parse_output(output)
43    }
44}
45
46pub fn parse_output(output: Vec<String>) -> Result<NodeInfo> {
47    let endpoint = output
48        .iter()
49        .find(|line| line.starts_with("RPC endpoint:"))
50        .map(|line| line.split(": ").nth(1).unwrap_or("").to_string())
51        .unwrap_or_default();
52    let peer_id = output
53        .iter()
54        .find(|line| line.starts_with("Peer Id:"))
55        .map(|line| line.split(": ").nth(1).unwrap_or("").to_string())
56        .unwrap_or_default();
57    let logs_dir = output
58        .iter()
59        .find(|line| line.starts_with("Logs dir:"))
60        .map(|line| line.split(": ").nth(1).unwrap_or("").to_string())
61        .unwrap_or_default();
62    let pid = output
63        .iter()
64        .find(|line| line.starts_with("PID:"))
65        .map(|line| {
66            line.split(": ")
67                .nth(1)
68                .unwrap_or("")
69                .parse::<u16>()
70                .unwrap_or(0)
71        })
72        .unwrap_or_default();
73    let safenode_version = output
74        .iter()
75        .find(|line| line.starts_with("Binary version:"))
76        .map(|line| line.split(": ").nth(1).unwrap_or("").to_string())
77        .unwrap_or_default();
78    let last_restart = output
79        .iter()
80        .find(|line| line.starts_with("Time since last restart:"))
81        .map(|line| {
82            line.split(": ")
83                .nth(1)
84                .unwrap_or("")
85                .trim_end_matches('s')
86                .parse::<u32>()
87                .unwrap_or(0)
88        })
89        .unwrap_or_default();
90
91    Ok(NodeInfo {
92        endpoint,
93        peer_id,
94        logs_dir: PathBuf::from(logs_dir),
95        pid,
96        safenode_version,
97        last_restart,
98    })
99}