mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-29 10:41:21 +00:00
Allow customizing the address in the examples, plus minor improvements
This commit is contained in:
parent
2a2c2f3f63
commit
c9f55ceb97
@ -29,12 +29,16 @@ extern crate tokio_io;
|
|||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Future, Sink, Stream};
|
use futures::{Future, Sink, Stream};
|
||||||
|
use std::env;
|
||||||
use swarm::{UpgradeExt, SimpleProtocol, Transport};
|
use swarm::{UpgradeExt, SimpleProtocol, Transport};
|
||||||
use tcp::TcpConfig;
|
use tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::codec::length_delimited;
|
use tokio_io::codec::length_delimited;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Determine which address to dial.
|
||||||
|
let target_addr = env::args().nth(1).unwrap_or("/ip4/127.0.0.1/tcp/10333".to_owned());
|
||||||
|
|
||||||
// We start by building the tokio engine that will run all the sockets.
|
// We start by building the tokio engine that will run all the sockets.
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
|
||||||
@ -58,10 +62,14 @@ fn main() {
|
|||||||
plain_text.or_upgrade(secio)
|
plain_text.or_upgrade(secio)
|
||||||
})
|
})
|
||||||
|
|
||||||
.with_upgrade(multiplex::MultiplexConfig);
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
let transport: swarm::ConnectionReuse<_, _> = transport.into();
|
.with_upgrade(multiplex::MultiplexConfig)
|
||||||
|
// The object returned by the call to `with_upgrade(MultiplexConfig)` can't be used as a
|
||||||
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
|
// a `Transport`.
|
||||||
|
.into_connection_reuse()
|
||||||
|
|
||||||
let transport = transport
|
|
||||||
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
||||||
// just for this example.
|
// just for this example.
|
||||||
// For this purpose, we create a `SimpleProtocol` struct.
|
// For this purpose, we create a `SimpleProtocol` struct.
|
||||||
@ -77,9 +85,9 @@ fn main() {
|
|||||||
// incoming connections, and that will automatically apply all the selected protocols on top
|
// incoming connections, and that will automatically apply all the selected protocols on top
|
||||||
// of any opened stream.
|
// of any opened stream.
|
||||||
|
|
||||||
// We use it to dial `/ip4/127.0.0.1/tcp/10333`.
|
// We use it to dial the address.
|
||||||
let dialer = transport
|
let dialer = transport
|
||||||
.dial_and_listen(swarm::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap())
|
.dial_and_listen(swarm::Multiaddr::new(&target_addr).expect("invalid multiaddr"))
|
||||||
.unwrap_or_else(|_| panic!("unsupported multiaddr protocol ; should never happen"))
|
.unwrap_or_else(|_| panic!("unsupported multiaddr protocol ; should never happen"))
|
||||||
.and_then(|(incoming, echo)| {
|
.and_then(|(incoming, echo)| {
|
||||||
// `echo` is what the closure used when initializing "echo" returns.
|
// `echo` is what the closure used when initializing "echo" returns.
|
||||||
|
@ -23,17 +23,22 @@ extern crate futures;
|
|||||||
extern crate libp2p_secio as secio;
|
extern crate libp2p_secio as secio;
|
||||||
extern crate libp2p_swarm as swarm;
|
extern crate libp2p_swarm as swarm;
|
||||||
extern crate libp2p_tcp_transport as tcp;
|
extern crate libp2p_tcp_transport as tcp;
|
||||||
|
extern crate multiplex;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
|
||||||
use futures::future::{Future, IntoFuture, loop_fn, Loop};
|
use futures::future::{Future, IntoFuture, loop_fn, Loop};
|
||||||
use futures::{Stream, Sink};
|
use futures::{Stream, Sink};
|
||||||
|
use std::env;
|
||||||
use swarm::{Transport, UpgradeExt, SimpleProtocol};
|
use swarm::{Transport, UpgradeExt, SimpleProtocol};
|
||||||
use tcp::TcpConfig;
|
use tcp::TcpConfig;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use tokio_io::codec::length_delimited;
|
use tokio_io::codec::length_delimited;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Determine which address to listen to.
|
||||||
|
let listen_addr = env::args().nth(1).unwrap_or("/ip4/0.0.0.0/tcp/10333".to_owned());
|
||||||
|
|
||||||
// We start by building the tokio engine that will run all the sockets.
|
// We start by building the tokio engine that will run all the sockets.
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
|
||||||
@ -57,8 +62,16 @@ fn main() {
|
|||||||
plain_text.or_upgrade(secio)
|
plain_text.or_upgrade(secio)
|
||||||
})
|
})
|
||||||
|
|
||||||
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||||
// just for this example.
|
.with_upgrade(multiplex::MultiplexConfig)
|
||||||
|
// The object returned by the call to `with_upgrade(MultiplexConfig)` can't be used as a
|
||||||
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
||||||
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
||||||
|
// a `Transport`.
|
||||||
|
.into_connection_reuse()
|
||||||
|
|
||||||
|
// On top of both mutiplex and plaintext/secio, we use the "echo" protocol, which is a
|
||||||
|
// custom protocol just for this example.
|
||||||
// For this purpose, we create a `SimpleProtocol` struct.
|
// For this purpose, we create a `SimpleProtocol` struct.
|
||||||
.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
|
.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
|
||||||
// This closure is called whenever a stream using the "echo" protocol has been
|
// This closure is called whenever a stream using the "echo" protocol has been
|
||||||
@ -72,10 +85,13 @@ fn main() {
|
|||||||
// incoming connections, and that will automatically apply all the selected protocols on top
|
// incoming connections, and that will automatically apply all the selected protocols on top
|
||||||
// of any opened stream.
|
// of any opened stream.
|
||||||
|
|
||||||
// We use it to listen on `/ip4/127.0.0.1/tcp/10333`.
|
// We use it to listen on the address.
|
||||||
let future = transport.listen_on(swarm::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap())
|
let (listener, address) = transport
|
||||||
.unwrap_or_else(|_| panic!("unsupported multiaddr protocol ; should never happen")).0
|
.listen_on(swarm::Multiaddr::new(&listen_addr).expect("invalid multiaddr"))
|
||||||
|
.unwrap_or_else(|_| panic!("unsupported multiaddr protocol ; should never happen"));
|
||||||
|
println!("Now listening on {:?}", address);
|
||||||
|
|
||||||
|
let future = listener
|
||||||
.filter_map(|(socket, client_addr)| {
|
.filter_map(|(socket, client_addr)| {
|
||||||
let client_addr = client_addr.to_string();
|
let client_addr = client_addr.to_string();
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
//! in a complex chain of protocols negotiation.
|
//! in a complex chain of protocols negotiation.
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use connection_reuse::ConnectionReuse;
|
||||||
use futures::{Async, Poll, Stream};
|
use futures::{Async, Poll, Stream};
|
||||||
use futures::future::{self, FromErr, Future, FutureResult, IntoFuture};
|
use futures::future::{self, FromErr, Future, FutureResult, IntoFuture};
|
||||||
use multiaddr::Multiaddr;
|
use multiaddr::Multiaddr;
|
||||||
@ -706,6 +707,13 @@ where
|
|||||||
T: Transport + 'a,
|
T: Transport + 'a,
|
||||||
C: ConnectionUpgrade<T::RawConn> + 'a,
|
C: ConnectionUpgrade<T::RawConn> + 'a,
|
||||||
{
|
{
|
||||||
|
/// Turns this upgraded node into a `ConnectionReuse`. If the `Output` implements the
|
||||||
|
/// `StreamMuxer` trait, the returned object will implement `Transport` and `MuxedTransport`.
|
||||||
|
#[inline]
|
||||||
|
pub fn into_connection_reuse(self) -> ConnectionReuse<T, C> {
|
||||||
|
From::from(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Tries to dial on the `Multiaddr` using the transport that was passed to `new`, then upgrade
|
/// Tries to dial on the `Multiaddr` using the transport that was passed to `new`, then upgrade
|
||||||
/// the connection.
|
/// the connection.
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user