Wrap multistream-select streams under a Negotiated (#1001)

This commit is contained in:
Pierre Krieger
2019-03-19 17:27:30 +01:00
committed by GitHub
parent 63e9e39538
commit 96e559b503
24 changed files with 162 additions and 111 deletions

View File

@ -23,7 +23,7 @@
use bytes::Bytes;
use futures::{future, prelude::*};
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}};
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}};
use std::{io, iter};
use tokio_codec::{FramedRead, FramedWrite};
use tokio_io::{AsyncRead, AsyncWrite};
@ -51,11 +51,11 @@ impl<C> InboundUpgrade<C> for Observed
where
C: AsyncRead + AsyncWrite + Send + 'static
{
type Output = Sender<C>;
type Output = Sender<Negotiated<C>>;
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_inbound(self, conn: C, _: Self::Info) -> Self::Future {
fn upgrade_inbound(self, conn: Negotiated<C>, _: Self::Info) -> Self::Future {
let io = FramedWrite::new(conn, UviBytes::default());
Box::new(future::ok(Sender { io }))
}
@ -69,10 +69,10 @@ where
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_outbound(self, conn: C, _: Self::Info) -> Self::Future {
fn upgrade_outbound(self, conn: Negotiated<C>, _: Self::Info) -> Self::Future {
let io = FramedRead::new(conn, UviBytes::default());
let future = io.into_future()
.map_err(|(e, _): (io::Error, FramedRead<C, UviBytes>)| e)
.map_err(|(e, _): (io::Error, FramedRead<Negotiated<C>, UviBytes>)| e)
.and_then(move |(bytes, _)| {
if let Some(b) = bytes {
let ma = Multiaddr::from_bytes(b.to_vec())
@ -100,7 +100,7 @@ impl<C: AsyncWrite> Sender<C> {
#[cfg(test)]
mod tests {
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade}};
use libp2p_core::{Multiaddr, upgrade::{apply_inbound, apply_outbound}};
use tokio::runtime::current_thread;
use tokio::net::{TcpListener, TcpStream};
use super::*;
@ -115,17 +115,19 @@ mod tests {
let server = server.incoming()
.into_future()
.map_err(|(e, _)| e.into())
.map_err(|_| panic!())
.and_then(move |(conn, _)| {
Observed::new().upgrade_inbound(conn.unwrap(), b"/paritytech/observed-address/0.1.0")
apply_inbound(conn.unwrap(), Observed::new())
})
.map_err(|_| panic!())
.and_then(move |sender| sender.send_address(observed_addr1));
let client = TcpStream::connect(&server_addr)
.map_err(|e| e.into())
.map_err(|_| panic!())
.and_then(|conn| {
Observed::new().upgrade_outbound(conn, b"/paritytech/observed-address/0.1.0")
apply_outbound(conn, Observed::new())
})
.map_err(|_| panic!())
.map(move |addr| {
eprintln!("{} {}", addr, observed_addr2);
assert_eq!(addr, observed_addr2)
@ -133,7 +135,7 @@ mod tests {
current_thread::block_on_all(future::lazy(move || {
current_thread::spawn(server.map_err(|e| panic!("server error: {}", e)).map(|_| ()));
client.map_err(|e| panic!("client error: {}", e))
client
}))
.unwrap();
}