// Copyright 2021 COMIT Network. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. use futures::StreamExt; use libp2p::core::identity; use libp2p::core::PeerId; use libp2p::ping::{Ping, PingEvent}; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::NetworkBehaviour; use libp2p::{development_transport, rendezvous}; #[tokio::main] async fn main() { env_logger::init(); let bytes = [0u8; 32]; let key = identity::ed25519::SecretKey::from_bytes(bytes).expect("we always pass 32 bytes"); let identity = identity::Keypair::Ed25519(key.into()); let mut swarm = Swarm::new( development_transport(identity.clone()).await.unwrap(), MyBehaviour { rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()), ping: Ping::default(), }, PeerId::from(identity.public()), ); log::info!("Local peer id: {}", swarm.local_peer_id()); swarm .listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap()) .unwrap(); while let Some(event) = swarm.next().await { match event { SwarmEvent::ConnectionEstablished { peer_id, .. } => { log::info!("Connected to {}", peer_id); } SwarmEvent::ConnectionClosed { peer_id, .. } => { log::info!("Disconnected from {}", peer_id); } SwarmEvent::Behaviour(MyEvent::Rendezvous( rendezvous::server::Event::PeerRegistered { peer, registration }, )) => { log::info!( "Peer {} registered for namespace '{}'", peer, registration.namespace ); } SwarmEvent::Behaviour(MyEvent::Rendezvous( rendezvous::server::Event::DiscoverServed { enquirer, registrations, }, )) => { log::info!( "Served peer {} with {} registrations", enquirer, registrations.len() ); } other => { log::debug!("Unhandled {:?}", other); } } } } #[derive(Debug)] enum MyEvent { Rendezvous(rendezvous::server::Event), Ping(PingEvent), } impl From for MyEvent { fn from(event: rendezvous::server::Event) -> Self { MyEvent::Rendezvous(event) } } impl From for MyEvent { fn from(event: PingEvent) -> Self { MyEvent::Ping(event) } } #[derive(NetworkBehaviour)] #[behaviour(event_process = false)] #[behaviour(out_event = "MyEvent")] struct MyBehaviour { rendezvous: rendezvous::server::Behaviour, ping: Ping, }