| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | use futures::StreamExt; |
| | use libp2p::{ |
| | identify, noise, ping, rendezvous, |
| | swarm::{NetworkBehaviour, SwarmEvent}, |
| | tcp, yamux, Multiaddr, |
| | }; |
| | use std::time::Duration; |
| | use tracing_subscriber::EnvFilter; |
| |
|
| | #[tokio::main] |
| | async fn main() { |
| | let _ = tracing_subscriber::fmt() |
| | .with_env_filter(EnvFilter::from_default_env()) |
| | .try_init(); |
| |
|
| | let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap(); |
| | let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN" |
| | .parse() |
| | .unwrap(); |
| |
|
| | let mut swarm = libp2p::SwarmBuilder::with_new_identity() |
| | .with_tokio() |
| | .with_tcp( |
| | tcp::Config::default(), |
| | noise::Config::new, |
| | yamux::Config::default, |
| | ) |
| | .unwrap() |
| | .with_behaviour(|key| MyBehaviour { |
| | identify: identify::Behaviour::new(identify::Config::new( |
| | "rendezvous-example/1.0.0".to_string(), |
| | key.public(), |
| | )), |
| | rendezvous: rendezvous::client::Behaviour::new(key.clone()), |
| | ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), |
| | }) |
| | .unwrap() |
| | .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) |
| | .build(); |
| |
|
| | let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()); |
| |
|
| | swarm.dial(rendezvous_point_address.clone()).unwrap(); |
| |
|
| | while let Some(event) = swarm.next().await { |
| | match event { |
| | SwarmEvent::NewListenAddr { address, .. } => { |
| | tracing::info!("Listening on {}", address); |
| | } |
| | SwarmEvent::ConnectionClosed { |
| | peer_id, |
| | cause: Some(error), |
| | .. |
| | } if peer_id == rendezvous_point => { |
| | tracing::error!("Lost connection to rendezvous point {}", error); |
| | } |
| | |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Identify(identify::Event::Received { |
| | info, |
| | .. |
| | })) => { |
| | |
| | |
| | swarm.add_external_address(info.observed_addr); |
| | if let Err(error) = swarm.behaviour_mut().rendezvous.register( |
| | rendezvous::Namespace::from_static("rendezvous"), |
| | rendezvous_point, |
| | None, |
| | ) { |
| | tracing::error!("Failed to register: {error}"); |
| | return; |
| | } |
| | } |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( |
| | rendezvous::client::Event::Registered { |
| | namespace, |
| | ttl, |
| | rendezvous_node, |
| | }, |
| | )) => { |
| | tracing::info!( |
| | "Registered for namespace '{}' at rendezvous point {} for the next {} seconds", |
| | namespace, |
| | rendezvous_node, |
| | ttl |
| | ); |
| | } |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( |
| | rendezvous::client::Event::RegisterFailed { |
| | rendezvous_node, |
| | namespace, |
| | error, |
| | }, |
| | )) => { |
| | tracing::error!( |
| | "Failed to register: rendezvous_node={}, namespace={}, error_code={:?}", |
| | rendezvous_node, |
| | namespace, |
| | error |
| | ); |
| | return; |
| | } |
| | SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event { |
| | peer, |
| | result: Ok(rtt), |
| | .. |
| | })) if peer != rendezvous_point => { |
| | tracing::info!("Ping to {} is {}ms", peer, rtt.as_millis()) |
| | } |
| | other => { |
| | tracing::debug!("Unhandled {:?}", other); |
| | } |
| | } |
| | } |
| | } |
| |
|
| | #[derive(NetworkBehaviour)] |
| | struct MyBehaviour { |
| | identify: identify::Behaviour, |
| | rendezvous: rendezvous::client::Behaviour, |
| | ping: ping::Behaviour, |
| | } |
| |
|