Oliver Wangler 303490103b
protocols/rendezvous: Improve examples (#2229)
* Add support for the `Identify` protocol to the server, such that the
  `register_with_identify` example works as intended
* Add discovery loop to the `discovery` example and demonstrate cookie
  usage
* Drop explicit dependency on async_std

Co-authored-by: Max Inden <mail@max-inden.de>
2021-09-25 18:39:49 +02:00

137 lines
4.5 KiB
Rust

// 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::identify::Identify;
use libp2p::identify::IdentifyConfig;
use libp2p::identify::IdentifyEvent;
use libp2p::ping;
use libp2p::ping::{Ping, PingEvent};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::NetworkBehaviour;
use libp2p::{development_transport, rendezvous};
/// Examples for the rendezvous protocol:
///
/// 1. Run the rendezvous server:
/// RUST_LOG=info cargo run --example rendezvous_point
/// 2. Register a peer:
/// RUST_LOG=info cargo run --example register_with_identify
/// 3. Try to discover the peer from (2):
/// RUST_LOG=info cargo run --example discover
#[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 {
identify: Identify::new(IdentifyConfig::new(
"rendezvous-example/1.0.0".to_string(),
identity.public(),
)),
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
ping: Ping::new(ping::Config::new().with_keep_alive(true)),
},
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),
Identify(IdentifyEvent),
}
impl From<rendezvous::server::Event> for MyEvent {
fn from(event: rendezvous::server::Event) -> Self {
MyEvent::Rendezvous(event)
}
}
impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
MyEvent::Ping(event)
}
}
impl From<IdentifyEvent> for MyEvent {
fn from(event: IdentifyEvent) -> Self {
MyEvent::Identify(event)
}
}
#[derive(NetworkBehaviour)]
#[behaviour(event_process = false)]
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
identify: Identify,
rendezvous: rendezvous::server::Behaviour,
ping: Ping,
}