Hannes d5ea93dd71
feat(swarm): Make executor for connection tasks explicit (#3097)
Previously, the executor for connection tasks silently defaulted to a `futures::executor::ThreadPool`. This causes issues such as https://github.com/libp2p/rust-libp2p/issues/2230.

With this patch, we force the user to choose, which executor they want to run the connection tasks on which results in overall simpler API with less footguns.

Closes #3068.
2022-11-15 14:26:03 +00:00

142 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, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p::{rendezvous, tokio_development_transport};
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::with_tokio_executor(
tokio_development_transport(identity.clone()).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,
}