Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
4.2 KiB
Rust
Raw Normal View History

// 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.
/// Examples for the rendezvous protocol:
///
/// 1. Run the rendezvous server:
/// ```
/// RUST_LOG=info cargo run --bin rendezvous-example
/// ```
/// 2. Register a peer:
/// ```
/// RUST_LOG=info cargo run --bin rzv-register
/// ```
/// 3. Try to discover the peer from (2):
/// ```
/// RUST_LOG=info cargo run --bin rzv-discover
/// ```
/// 4. Try to discover with identify:
/// ```
/// RUST_LOG=info cargo run --bin rzv-identify
/// ```
use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identify, identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, PeerId, Transport,
};
use std::time::Duration;
#[tokio::main]
async fn main() {
env_logger::init();
let key_pair = identity::Keypair::generate_ed25519();
let mut swarm = SwarmBuilder::with_tokio_executor(
tcp::tokio::Transport::default()
.upgrade(Version::V1Lazy)
.authenticate(noise::NoiseAuthenticated::xx(&key_pair).unwrap())
.multiplex(yamux::YamuxConfig::default())
.boxed(),
MyBehaviour {
identify: identify::Behaviour::new(identify::Config::new(
"rendezvous-example/1.0.0".to_string(),
key_pair.public(),
)),
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
)
.build();
log::info!("Local peer id: {}", swarm.local_peer_id());
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, .. } => {
log::info!("Connected to {}", peer_id);
}
SwarmEvent::ConnectionClosed { peer_id, .. } => {
log::info!("Disconnected from {}", peer_id);
}
SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous(
rendezvous::server::Event::PeerRegistered { peer, registration },
)) => {
log::info!(
"Peer {} registered for namespace '{}'",
peer,
registration.namespace
);
}
SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous(
rendezvous::server::Event::DiscoverServed {
enquirer,
registrations,
},
)) => {
log::info!(
"Served peer {} with {} registrations",
enquirer,
registrations.len()
);
}
other => {
log::debug!("Unhandled {:?}", other);
}
}
}
}
#[derive(NetworkBehaviour)]
struct MyBehaviour {
identify: identify::Behaviour,
rendezvous: rendezvous::server::Behaviour,
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
}