Skip to content

Commit

Permalink
add peers command
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed May 8, 2024
1 parent 3811700 commit a16e454
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ libp2p = { version = "0.53.2", features = [
multiaddr = "0.18.1"
serde = "1.0.196"
serde_json = "1.0.113"
tokio = { version = "1.35.1", features = ["macros", "rt", "rt-multi-thread"] }
tokio = { version = "1.35.1", features = [
"io-std",
"macros",
"rt",
"rt-multi-thread",
] }
toml = "0.8.9"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
31 changes: 27 additions & 4 deletions examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{dcutr, identify, identity, noise, ping, relay, yamux, Multiaddr, PeerId};
use multiaddr::Protocol;
use tokio::io::AsyncBufReadExt;
use tracing::info;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -173,8 +174,25 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

block_on(async {
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()).lines();

loop {
match swarm.next().await.unwrap() {
let event = tokio::select! {
Some(event) = swarm.next() => event,
Ok(Some(line)) = stdin.next_line() => {
match line.trim() {
"peers" => {
for peer in swarm.connected_peers() {
info!(peer=%peer, "Connected peer");
}
}
_ => info!("Unknown command"),
}
continue;
}
};

match event {
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "Listening on address");
}
Expand All @@ -185,20 +203,25 @@ async fn main() -> Result<(), Box<dyn Error>> {
info!("Relay accepted our reservation request");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
info!(?event)
info!(?event, "\x1b[33mrelay\x1b[0m");
}
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
info!(?event)
info!(?event, "\x1b[32mdcutr\x1b[0m");
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
info!(?event)
info!(?event, "\x1b[34midentify\x1b[0m");
}
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
info!(peer=%peer_id, ?endpoint, "Established new connection");
}
SwarmEvent::ConnectionClosed {
peer_id, endpoint, ..
} => {
info!(peer=%peer_id, ?endpoint, "Closed connection");
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
info!(peer=?peer_id, "Outgoing connection failed: {error}");
}
Expand Down

0 comments on commit a16e454

Please sign in to comment.