diff --git a/crates/chia-client/examples/client.rs b/crates/chia-client/examples/client.rs index 8b9e4d348..6b746f484 100644 --- a/crates/chia-client/examples/client.rs +++ b/crates/chia-client/examples/client.rs @@ -29,7 +29,7 @@ async fn main() -> anyhow::Result<()> { tokio::spawn(async move { loop { sleep(Duration::from_secs(10)).await; - let count = client_clone.peer_count().await; + let count = client_clone.len().await; log::info!("Currently connected to {} peers", count); client.find_peers().await; } diff --git a/crates/chia-client/src/client.rs b/crates/chia-client/src/client.rs index 7644068e0..82620cd53 100644 --- a/crates/chia-client/src/client.rs +++ b/crates/chia-client/src/client.rs @@ -112,13 +112,37 @@ impl Client { (client, receiver) } - pub async fn peer_count(&self) -> usize { + pub async fn len(&self) -> usize { self.0.peers.read().await.len() } + pub async fn is_empty(&self) -> bool { + self.0.peers.read().await.is_empty() + } + + pub async fn peer_ids(&self) -> Vec { + self.0.peers.read().await.keys().copied().collect() + } + + pub async fn peers(&self) -> Vec { + self.0.peers.read().await.values().cloned().collect() + } + + pub async fn peer(&self, peer_id: PeerId) -> Option { + self.0.peers.read().await.get(&peer_id).cloned() + } + + pub async fn remove_peer(&self, peer_id: PeerId) -> Option { + self.0.peers.write().await.remove(&peer_id) + } + + pub async fn clear(&self) { + self.0.peers.write().await.clear(); + } + pub async fn find_peers(&self) { // If we don't have any peers, try to connect to DNS introducers. - if self.peer_count().await == 0 && self.connect_dns().await { + if self.len().await == 0 && self.connect_dns().await { return; }