mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 07:11:38 +00:00
swarm: Split off "keep alive" functionality from DummyConnectionHandler
(#2859)
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.
This commit is contained in:
@ -42,7 +42,8 @@
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::{identity, ping, Multiaddr, PeerId};
|
||||
use libp2p::{identity, ping, Multiaddr, NetworkBehaviour, PeerId};
|
||||
use libp2p_swarm::keep_alive;
|
||||
use std::error::Error;
|
||||
|
||||
#[async_std::main]
|
||||
@ -53,14 +54,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let transport = libp2p::development_transport(local_key).await?;
|
||||
|
||||
// Create a ping network behaviour.
|
||||
//
|
||||
// For illustrative purposes, the ping protocol is configured to
|
||||
// keep the connection alive, so a continuous sequence of pings
|
||||
// can be observed.
|
||||
let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true));
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
let mut swarm = Swarm::new(transport, Behaviour::default(), local_peer_id);
|
||||
|
||||
// Tell the swarm to listen on all interfaces and a random, OS-assigned
|
||||
// port.
|
||||
@ -82,3 +76,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Our network behaviour.
|
||||
///
|
||||
/// For illustrative purposes, this includes the [`KeepAlive`](behaviour::KeepAlive) behaviour so a continuous sequence of
|
||||
/// pings can be observed.
|
||||
#[derive(NetworkBehaviour, Default)]
|
||||
struct Behaviour {
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
}
|
||||
|
Reference in New Issue
Block a user