diff --git a/example/examples/echo-dialer.rs b/example/examples/echo-dialer.rs index 611b6fe3..eb6e84f3 100644 --- a/example/examples/echo-dialer.rs +++ b/example/examples/echo-dialer.rs @@ -28,7 +28,7 @@ extern crate tokio_io; use bytes::BytesMut; use futures::{Stream, Sink, Future}; -use swarm::Transport; +use swarm::{Transport, SimpleProtocol}; use tcp::TcpConfig; use tokio_core::reactor::Core; use tokio_io::codec::length_delimited; @@ -47,9 +47,9 @@ fn main() { } }); - let with_echo = with_secio.with_simple_protocol_upgrade("/echo/1.0.0", |socket| { + let with_echo = with_secio.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| { Ok(length_delimited::Framed::<_, BytesMut>::new(socket)) - }); + })); let dialer = with_echo.dial(swarm::multiaddr::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap()) .unwrap_or_else(|_| panic!()) diff --git a/example/examples/echo-server.rs b/example/examples/echo-server.rs index 233376b8..8cb6fa7b 100644 --- a/example/examples/echo-server.rs +++ b/example/examples/echo-server.rs @@ -26,12 +26,9 @@ extern crate libp2p_tcp_transport as tcp; extern crate tokio_core; extern crate tokio_io; -use bytes::BytesMut; use futures::future::{Future, IntoFuture, loop_fn, Loop}; use futures::{Stream, Sink}; -use std::io::Error as IoError; -use std::iter; -use swarm::{Transport, ConnectionUpgrade}; +use swarm::{Transport, SimpleProtocol}; use tcp::TcpConfig; use tokio_core::reactor::Core; use tokio_io::codec::length_delimited; @@ -50,9 +47,9 @@ fn main() { } }); - let with_echo = with_secio.with_simple_protocol_upgrade("/echo/1.0.0", |socket| { - Ok(length_delimited::Framed::<_, BytesMut>::new(socket)) - }); + let with_echo = with_secio.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| { + Ok(length_delimited::Framed::new(socket)) + })); let future = with_echo.listen_on(swarm::multiaddr::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap()) .unwrap_or_else(|_| panic!()).0 diff --git a/libp2p-swarm/src/lib.rs b/libp2p-swarm/src/lib.rs index 3dce1f3d..a6ad790c 100644 --- a/libp2p-swarm/src/lib.rs +++ b/libp2p-swarm/src/lib.rs @@ -149,3 +149,4 @@ pub use self::connection_reuse::ConnectionReuse; pub use self::multiaddr::Multiaddr; pub use self::muxing::StreamMuxer; pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade}; +pub use self::transport::SimpleProtocol; diff --git a/libp2p-swarm/src/transport.rs b/libp2p-swarm/src/transport.rs index 877cf29b..984bcce6 100644 --- a/libp2p-swarm/src/transport.rs +++ b/libp2p-swarm/src/transport.rs @@ -102,25 +102,6 @@ pub trait Transport { upgrade: upgrade, } } - - /// Wraps this transport inside an upgrade. Similar to `with_upgrade`, but more convenient to - /// use for small protocols. - #[inline] - fn with_simple_protocol_upgrade(self, name: N, upgrade: F) - -> UpgradedNode> - where Self: Sized, - N: Into, - F: Fn(Self::RawConn) -> O, - O: IntoFuture, - { - UpgradedNode { - transports: self, - upgrade: SimpleProtocolUpgrade { - name: name.into(), - upgrade: Arc::new(upgrade), - }, - } - } } /// Dummy implementation of `Transport` that just denies every single attempt. @@ -184,26 +165,39 @@ impl Transport for OrTransport } } -/// Implementation of `ConnetionUpgrade`. See `with_simple_protocol_upgrade`. +/// Implementation of `ConnectionUpgrade`. Convenient to use with small protocols. #[derive(Debug)] -pub struct SimpleProtocolUpgrade { +pub struct SimpleProtocol { name: Bytes, // Note: we put the closure `F` in an `Arc` because Rust closures aren't automatically clonable // yet. upgrade: Arc, } -impl Clone for SimpleProtocolUpgrade { +impl SimpleProtocol { + /// Builds a `SimpleProtocol`. + #[inline] + pub fn new(name: N, upgrade: F) -> SimpleProtocol + where N: Into + { + SimpleProtocol { + name: name.into(), + upgrade: Arc::new(upgrade), + } + } +} + +impl Clone for SimpleProtocol { #[inline] fn clone(&self) -> Self { - SimpleProtocolUpgrade { + SimpleProtocol { name: self.name.clone(), upgrade: self.upgrade.clone(), } } } -impl ConnectionUpgrade for SimpleProtocolUpgrade +impl ConnectionUpgrade for SimpleProtocol where C: AsyncRead + AsyncWrite, F: Fn(C) -> O, O: IntoFuture,