mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-26 08:11:39 +00:00
feat(libp2p): add SwarmBuilder
Introduce the new `libp2p::SwarmBuilder`. Users should use the new `libp2p::SwarmBuilder` instead of the now deprecated `libp2p::swarm::SwarmBuilder`. See `libp2p::SwarmBuilder` docs on how to use the new builder. Fixes #3657. Fixes #3563. Fixes #3179. Pull-Request: #4120.
This commit is contained in:
@ -21,20 +21,11 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use clap::Parser;
|
||||
use futures::{
|
||||
executor::{block_on, ThreadPool},
|
||||
future::{Either, FutureExt},
|
||||
stream::StreamExt,
|
||||
};
|
||||
use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
|
||||
use libp2p::{
|
||||
core::{
|
||||
multiaddr::{Multiaddr, Protocol},
|
||||
muxing::StreamMuxerBox,
|
||||
transport::Transport,
|
||||
upgrade,
|
||||
},
|
||||
dcutr, dns, identify, identity, noise, ping, quic, relay,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
core::multiaddr::{Multiaddr, Protocol},
|
||||
dcutr, identify, identity, noise, ping, relay,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, PeerId,
|
||||
};
|
||||
use log::info;
|
||||
@ -78,37 +69,12 @@ impl FromStr for Mode {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let opts = Opts::parse();
|
||||
|
||||
let local_key = generate_ed25519(opts.secret_key_seed);
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let (relay_transport, client) = relay::client::new(local_peer_id);
|
||||
|
||||
let transport = {
|
||||
let relay_tcp_quic_transport = relay_transport
|
||||
.or_transport(tcp::async_io::Transport::new(
|
||||
tcp::Config::default().port_reuse(true),
|
||||
))
|
||||
.upgrade(upgrade::Version::V1)
|
||||
.authenticate(noise::Config::new(&local_key).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.or_transport(quic::async_std::Transport::new(quic::Config::new(
|
||||
&local_key,
|
||||
)));
|
||||
|
||||
block_on(dns::async_std::Transport::system(relay_tcp_quic_transport))
|
||||
.unwrap()
|
||||
.map(|either_output, _| match either_output {
|
||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
})
|
||||
.boxed()
|
||||
};
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Behaviour {
|
||||
relay_client: relay::client::Behaviour,
|
||||
@ -117,21 +83,27 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
dcutr: dcutr::Behaviour,
|
||||
}
|
||||
|
||||
let behaviour = Behaviour {
|
||||
relay_client: client,
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/TODO/0.0.1".to_string(),
|
||||
local_key.public(),
|
||||
)),
|
||||
dcutr: dcutr::Behaviour::new(local_peer_id),
|
||||
};
|
||||
|
||||
let mut swarm = match ThreadPool::new() {
|
||||
Ok(tp) => SwarmBuilder::with_executor(transport, behaviour, local_peer_id, tp),
|
||||
Err(_) => SwarmBuilder::without_executor(transport, behaviour, local_peer_id),
|
||||
}
|
||||
.build();
|
||||
let mut swarm =
|
||||
libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed))
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default().port_reuse(true).nodelay(true),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_quic()
|
||||
.with_dns()?
|
||||
.with_relay_client(noise::Config::new, yamux::Config::default)?
|
||||
.with_behaviour(|keypair, relay_behaviour| Behaviour {
|
||||
relay_client: relay_behaviour,
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/TODO/0.0.1".to_string(),
|
||||
keypair.public(),
|
||||
)),
|
||||
dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()),
|
||||
})?
|
||||
.build();
|
||||
|
||||
swarm
|
||||
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap())
|
||||
|
Reference in New Issue
Block a user