| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #![doc = include_str!("../README.md")] |
| |
|
| | use futures::StreamExt; |
| | use libp2p::{ |
| | identify, noise, ping, rendezvous, |
| | swarm::{NetworkBehaviour, SwarmEvent}, |
| | tcp, yamux, |
| | }; |
| | use std::error::Error; |
| | use std::time::Duration; |
| | use tracing_subscriber::EnvFilter; |
| |
|
| | #[tokio::main] |
| | async fn main() -> Result<(), Box<dyn Error>> { |
| | let _ = tracing_subscriber::fmt() |
| | .with_env_filter(EnvFilter::from_default_env()) |
| | .try_init(); |
| |
|
| | |
| | |
| | let keypair = libp2p::identity::Keypair::ed25519_from_bytes([0; 32]).unwrap(); |
| |
|
| | let mut swarm = libp2p::SwarmBuilder::with_existing_identity(keypair) |
| | .with_tokio() |
| | .with_tcp( |
| | tcp::Config::default(), |
| | noise::Config::new, |
| | yamux::Config::default, |
| | )? |
| | .with_behaviour(|key| MyBehaviour { |
| | identify: identify::Behaviour::new(identify::Config::new( |
| | "rendezvous-example/1.0.0".to_string(), |
| | key.public(), |
| | )), |
| | rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()), |
| | ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), |
| | })? |
| | .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) |
| | .build(); |
| |
|
| | let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap()); |
| |
|
| | while let Some(event) = swarm.next().await { |
| | match event { |
| | SwarmEvent::ConnectionEstablished { peer_id, .. } => { |
| | tracing::info!("Connected to {}", peer_id); |
| | } |
| | SwarmEvent::ConnectionClosed { peer_id, .. } => { |
| | tracing::info!("Disconnected from {}", peer_id); |
| | } |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( |
| | rendezvous::server::Event::PeerRegistered { peer, registration }, |
| | )) => { |
| | tracing::info!( |
| | "Peer {} registered for namespace '{}'", |
| | peer, |
| | registration.namespace |
| | ); |
| | } |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( |
| | rendezvous::server::Event::DiscoverServed { |
| | enquirer, |
| | registrations, |
| | }, |
| | )) => { |
| | tracing::info!( |
| | "Served peer {} with {} registrations", |
| | enquirer, |
| | registrations.len() |
| | ); |
| | } |
| | other => { |
| | tracing::debug!("Unhandled {:?}", other); |
| | } |
| | } |
| | } |
| |
|
| | Ok(()) |
| | } |
| |
|
| | #[derive(NetworkBehaviour)] |
| | struct MyBehaviour { |
| | identify: identify::Behaviour, |
| | rendezvous: rendezvous::server::Behaviour, |
| | ping: ping::Behaviour, |
| | } |
| |
|