mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-04 15:12:15 +00:00
Previously, the `DummyConnectionHandler` offered a "keep alive" functionality, i.e. it allowed users to set the value of what is returned from `ConnectionHandler::keep_alive`. This handler is primarily used in tests or `NetworkBehaviour`s that don't open any connections (like mDNS). In all of these cases, it is statically known whether we want to keep connections alive. As such, this functionality is better represented by a static `KeepAliveConnectionHandler` that always returns `KeepAlive::Yes` and a `DummyConnectionHandler` that always returns `KeepAlive::No`. To follow the naming conventions described in https://github.com/libp2p/rust-libp2p/issues/2217, we introduce a top-level `keep_alive` and `dummy` behaviour in `libp2p-swarm` that contains both the `NetworkBehaviour` and `ConnectionHandler` implementation for either case.
143 lines
4.7 KiB
Rust
143 lines
4.7 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;
|
|
use libp2p::ping;
|
|
use libp2p::swarm::{keep_alive, Swarm, SwarmEvent};
|
|
use libp2p::NetworkBehaviour;
|
|
use libp2p::{development_transport, rendezvous};
|
|
use void::Void;
|
|
|
|
/// 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::Behaviour::new(identify::Config::new(
|
|
"rendezvous-example/1.0.0".to_string(),
|
|
identity.public(),
|
|
)),
|
|
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
|
|
ping: ping::Behaviour::new(ping::Config::new()),
|
|
keep_alive: keep_alive::Behaviour,
|
|
},
|
|
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(ping::Event),
|
|
Identify(identify::Event),
|
|
}
|
|
|
|
impl From<rendezvous::server::Event> for MyEvent {
|
|
fn from(event: rendezvous::server::Event) -> Self {
|
|
MyEvent::Rendezvous(event)
|
|
}
|
|
}
|
|
|
|
impl From<ping::Event> for MyEvent {
|
|
fn from(event: ping::Event) -> Self {
|
|
MyEvent::Ping(event)
|
|
}
|
|
}
|
|
|
|
impl From<identify::Event> for MyEvent {
|
|
fn from(event: identify::Event) -> Self {
|
|
MyEvent::Identify(event)
|
|
}
|
|
}
|
|
|
|
impl From<Void> for MyEvent {
|
|
fn from(event: Void) -> Self {
|
|
void::unreachable(event)
|
|
}
|
|
}
|
|
|
|
#[derive(NetworkBehaviour)]
|
|
#[behaviour(event_process = false)]
|
|
#[behaviour(out_event = "MyEvent")]
|
|
struct MyBehaviour {
|
|
identify: identify::Behaviour,
|
|
rendezvous: rendezvous::server::Behaviour,
|
|
ping: ping::Behaviour,
|
|
keep_alive: keep_alive::Behaviour,
|
|
}
|