freenet_test_network/
peer.rs

1use crate::{process::PeerProcess, remote::PeerLocation, Result};
2use std::net::UdpSocket;
3use std::path::{Path, PathBuf};
4
5/// Represents a single peer in the test network
6pub struct TestPeer {
7    pub(crate) id: String,
8    pub(crate) is_gateway: bool,
9    pub(crate) ws_port: u16,
10    pub(crate) network_port: u16,
11    pub(crate) network_address: String,
12    pub(crate) data_dir: PathBuf,
13    pub(crate) process: Box<dyn PeerProcess + Send>,
14    pub(crate) public_key_path: Option<PathBuf>,
15    pub(crate) location: PeerLocation,
16}
17
18impl TestPeer {
19    /// Get the WebSocket URL for connecting to this peer
20    pub fn ws_url(&self) -> String {
21        format!("ws://127.0.0.1:{}/v1/contract/command", self.ws_port)
22    }
23
24    /// Get the peer's identifier
25    pub fn id(&self) -> &str {
26        &self.id
27    }
28
29    /// Check if this is a gateway peer
30    pub fn is_gateway(&self) -> bool {
31        self.is_gateway
32    }
33
34    /// Get the peer's network address
35    pub fn network_address(&self) -> &str {
36        &self.network_address
37    }
38
39    /// Get the peer's network port
40    pub fn network_port(&self) -> u16 {
41        self.network_port
42    }
43
44    /// Get the peer's full socket address (address:port)
45    pub fn socket_address(&self) -> std::net::SocketAddr {
46        format!("{}:{}", self.network_address, self.network_port)
47            .parse()
48            .expect("valid socket address")
49    }
50
51    /// Get the root data directory for this peer
52    pub fn data_dir_path(&self) -> &Path {
53        &self.data_dir
54    }
55
56    /// Get the path to this peer's log file
57    pub fn log_path(&self) -> PathBuf {
58        self.process.log_path()
59    }
60
61    /// Read logs from this peer (fetches from Docker if needed)
62    pub fn read_logs(&self) -> Result<Vec<crate::logs::LogEntry>> {
63        self.process.read_logs()
64    }
65
66    /// Check if the peer process is still running
67    pub fn is_running(&self) -> bool {
68        self.process.is_running()
69    }
70
71    /// Kill the peer process
72    pub fn kill(&mut self) -> Result<()> {
73        self.process.kill()
74    }
75
76    /// Get the peer's location (local or remote)
77    pub fn location(&self) -> &PeerLocation {
78        &self.location
79    }
80}
81
82/// Get a free port by binding to port 0 and letting the OS assign one
83pub(crate) fn get_free_port() -> Result<u16> {
84    // Use UDP to mirror the freenet transport listener and avoid conflicts with UDP-only bindings.
85    let socket = UdpSocket::bind("127.0.0.1:0")?;
86    Ok(socket.local_addr()?.port())
87}