Fix concerns

This commit is contained in:
Pierre Krieger
2017-12-07 12:56:44 +01:00
parent 4881c4a6a1
commit 4f981e922d
4 changed files with 26 additions and 34 deletions

View File

@ -28,7 +28,7 @@ extern crate tokio_io;
use bytes::BytesMut; use bytes::BytesMut;
use futures::{Stream, Sink, Future}; use futures::{Stream, Sink, Future};
use swarm::Transport; use swarm::{Transport, 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;
@ -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)) Ok(length_delimited::Framed::<_, BytesMut>::new(socket))
}); }));
let dialer = with_echo.dial(swarm::multiaddr::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap()) let dialer = with_echo.dial(swarm::multiaddr::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap())
.unwrap_or_else(|_| panic!()) .unwrap_or_else(|_| panic!())

View File

@ -26,12 +26,9 @@ extern crate libp2p_tcp_transport as tcp;
extern crate tokio_core; extern crate tokio_core;
extern crate tokio_io; extern crate tokio_io;
use bytes::BytesMut;
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::io::Error as IoError; use swarm::{Transport, SimpleProtocol};
use std::iter;
use swarm::{Transport, ConnectionUpgrade};
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;
@ -50,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)) Ok(length_delimited::Framed::new(socket))
}); }));
let future = with_echo.listen_on(swarm::multiaddr::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap()) let future = with_echo.listen_on(swarm::multiaddr::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap())
.unwrap_or_else(|_| panic!()).0 .unwrap_or_else(|_| panic!()).0

View File

@ -149,3 +149,4 @@ pub use self::connection_reuse::ConnectionReuse;
pub use self::multiaddr::Multiaddr; pub use self::multiaddr::Multiaddr;
pub use self::muxing::StreamMuxer; pub use self::muxing::StreamMuxer;
pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade}; pub use self::transport::{ConnectionUpgrade, PlainTextConfig, Transport, UpgradedNode, OrUpgrade};
pub use self::transport::SimpleProtocol;

View File

@ -102,25 +102,6 @@ pub trait Transport {
upgrade: upgrade, 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<N, F, O>(self, name: N, upgrade: F)
-> UpgradedNode<Self, SimpleProtocolUpgrade<F>>
where Self: Sized,
N: Into<Bytes>,
F: Fn(Self::RawConn) -> O,
O: IntoFuture<Error = IoError>,
{
UpgradedNode {
transports: self,
upgrade: SimpleProtocolUpgrade {
name: name.into(),
upgrade: Arc::new(upgrade),
},
}
}
} }
/// Dummy implementation of `Transport` that just denies every single attempt. /// Dummy implementation of `Transport` that just denies every single attempt.
@ -184,26 +165,39 @@ impl<A, B> Transport for OrTransport<A, B>
} }
} }
/// Implementation of `ConnetionUpgrade`. See `with_simple_protocol_upgrade`. /// Implementation of `ConnectionUpgrade`. Convenient to use with small protocols.
#[derive(Debug)] #[derive(Debug)]
pub struct SimpleProtocolUpgrade<F> { pub struct SimpleProtocol<F> {
name: Bytes, name: Bytes,
// Note: we put the closure `F` in an `Arc` because Rust closures aren't automatically clonable // Note: we put the closure `F` in an `Arc` because Rust closures aren't automatically clonable
// yet. // yet.
upgrade: Arc<F>, upgrade: Arc<F>,
} }
impl<F> Clone for SimpleProtocolUpgrade<F> { impl<F> SimpleProtocol<F> {
/// Builds a `SimpleProtocol`.
#[inline]
pub fn new<N>(name: N, upgrade: F) -> SimpleProtocol<F>
where N: Into<Bytes>
{
SimpleProtocol {
name: name.into(),
upgrade: Arc::new(upgrade),
}
}
}
impl<F> Clone for SimpleProtocol<F> {
#[inline] #[inline]
fn clone(&self) -> Self { fn clone(&self) -> Self {
SimpleProtocolUpgrade { SimpleProtocol {
name: self.name.clone(), name: self.name.clone(),
upgrade: self.upgrade.clone(), upgrade: self.upgrade.clone(),
} }
} }
} }
impl<C, F, O> ConnectionUpgrade<C> for SimpleProtocolUpgrade<F> impl<C, F, O> ConnectionUpgrade<C> for SimpleProtocol<F>
where C: AsyncRead + AsyncWrite, where C: AsyncRead + AsyncWrite,
F: Fn(C) -> O, F: Fn(C) -> O,
O: IntoFuture<Error = IoError>, O: IntoFuture<Error = IoError>,