Skip to content

Commit

Permalink
Add missing methods to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 14, 2024
1 parent 8a59e22 commit 2264793
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/chia-client/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 26 additions & 2 deletions crates/chia-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerId> {
self.0.peers.read().await.keys().copied().collect()
}

pub async fn peers(&self) -> Vec<Peer> {
self.0.peers.read().await.values().cloned().collect()
}

pub async fn peer(&self, peer_id: PeerId) -> Option<Peer> {
self.0.peers.read().await.get(&peer_id).cloned()
}

pub async fn remove_peer(&self, peer_id: PeerId) -> Option<Peer> {
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;
}

Expand Down

0 comments on commit 2264793

Please sign in to comment.