mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-15 02:51:25 +00:00
Update examples to print a listen address. (#1064)
This was no longer the case since https://github.com/libp2p/rust-libp2p/pull/1032.
This commit is contained in:
@ -52,6 +52,7 @@
|
|||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
PeerId,
|
PeerId,
|
||||||
|
Swarm,
|
||||||
NetworkBehaviour,
|
NetworkBehaviour,
|
||||||
identity,
|
identity,
|
||||||
tokio_codec::{FramedRead, LinesCodec}
|
tokio_codec::{FramedRead, LinesCodec}
|
||||||
@ -118,10 +119,6 @@ fn main() {
|
|||||||
libp2p::Swarm::new(transport, behaviour, local_peer_id)
|
libp2p::Swarm::new(transport, behaviour, local_peer_id)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Listen on all interfaces and whatever port the OS assigns
|
|
||||||
let addr = libp2p::Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
|
|
||||||
println!("Listening on {:?}", addr);
|
|
||||||
|
|
||||||
// Reach out to another node if specified
|
// Reach out to another node if specified
|
||||||
if let Some(to_dial) = std::env::args().nth(1) {
|
if let Some(to_dial) = std::env::args().nth(1) {
|
||||||
let dialing = to_dial.clone();
|
let dialing = to_dial.clone();
|
||||||
@ -140,7 +137,11 @@ fn main() {
|
|||||||
let stdin = tokio_stdin_stdout::stdin(0);
|
let stdin = tokio_stdin_stdout::stdin(0);
|
||||||
let mut framed_stdin = FramedRead::new(stdin, LinesCodec::new());
|
let mut framed_stdin = FramedRead::new(stdin, LinesCodec::new());
|
||||||
|
|
||||||
|
// Listen on all interfaces and whatever port the OS assigns
|
||||||
|
libp2p::Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
|
||||||
|
|
||||||
// Kick it off
|
// Kick it off
|
||||||
|
let mut listening = false;
|
||||||
tokio::run(futures::future::poll_fn(move || -> Result<_, ()> {
|
tokio::run(futures::future::poll_fn(move || -> Result<_, ()> {
|
||||||
loop {
|
loop {
|
||||||
match framed_stdin.poll().expect("Error while polling stdin") {
|
match framed_stdin.poll().expect("Error while polling stdin") {
|
||||||
@ -155,7 +156,15 @@ fn main() {
|
|||||||
Async::Ready(Some(_)) => {
|
Async::Ready(Some(_)) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
Async::Ready(None) | Async::NotReady => break,
|
Async::Ready(None) | Async::NotReady => {
|
||||||
|
if !listening {
|
||||||
|
if let Some(a) = Swarm::listeners(&swarm).next() {
|
||||||
|
println!("Listening on {:?}", a);
|
||||||
|
listening = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,10 +60,6 @@ fn main() {
|
|||||||
// and applies the ping behaviour on each connection.
|
// and applies the ping behaviour on each connection.
|
||||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||||
|
|
||||||
// Listen on all interfaces and a random, OS-assigned port.
|
|
||||||
let listen_addr = Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
|
|
||||||
println!("Listening on {:?}", listen_addr);
|
|
||||||
|
|
||||||
// Dial the peer identified by the multi-address given as the second
|
// Dial the peer identified by the multi-address given as the second
|
||||||
// command-line argument, if any.
|
// command-line argument, if any.
|
||||||
if let Some(addr) = env::args().nth(1) {
|
if let Some(addr) = env::args().nth(1) {
|
||||||
@ -79,12 +75,24 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tell the swarm to listen on all interfaces and a random, OS-assigned port.
|
||||||
|
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
|
||||||
|
|
||||||
// Use tokio to drive the `Swarm`.
|
// Use tokio to drive the `Swarm`.
|
||||||
|
let mut listening = false;
|
||||||
tokio::run(future::poll_fn(move || -> Result<_, ()> {
|
tokio::run(future::poll_fn(move || -> Result<_, ()> {
|
||||||
loop {
|
loop {
|
||||||
match swarm.poll().expect("Error while polling swarm") {
|
match swarm.poll().expect("Error while polling swarm") {
|
||||||
Async::Ready(Some(e)) => println!("{:?}", e),
|
Async::Ready(Some(e)) => println!("{:?}", e),
|
||||||
Async::Ready(None) | Async::NotReady => return Ok(Async::NotReady),
|
Async::Ready(None) | Async::NotReady => {
|
||||||
|
if !listening {
|
||||||
|
if let Some(a) = Swarm::listeners(&swarm).next() {
|
||||||
|
println!("Listening on {:?}", a);
|
||||||
|
listening = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(Async::NotReady)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
Reference in New Issue
Block a user