mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 01:21:21 +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:
@ -20,41 +20,38 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identity,
|
||||
multiaddr::Protocol,
|
||||
noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
const NAMESPACE: &str = "rendezvous";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
swarm.dial(rendezvous_point_address.clone()).unwrap();
|
||||
|
||||
|
@ -20,10 +20,9 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identify, identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
identify, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -31,30 +30,30 @@ use std::time::Duration;
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)
|
||||
.unwrap()
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
key_pair.public(),
|
||||
key.public(),
|
||||
)),
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})
|
||||
.unwrap()
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());
|
||||
|
||||
|
@ -20,10 +20,9 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -31,26 +30,26 @@ use std::time::Duration;
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)
|
||||
.unwrap()
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})
|
||||
.unwrap()
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
// In production the external address should be the publicly facing IP address of the rendezvous point.
|
||||
// This address is recorded in the registration entry by the rendezvous point.
|
||||
|
@ -22,37 +22,34 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identify, identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
identify, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
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::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
key_pair.public(),
|
||||
key.public(),
|
||||
)),
|
||||
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap());
|
||||
|
||||
@ -90,6 +87,8 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
|
Reference in New Issue
Block a user