mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 07:11:38 +00:00
Simplify handling of upgrade information. (#761)
This commit is contained in:
@ -18,7 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use crate::rpc_proto;
|
||||
use futures::future;
|
||||
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, PeerId};
|
||||
@ -42,12 +42,12 @@ impl FloodsubConfig {
|
||||
}
|
||||
|
||||
impl UpgradeInfo for FloodsubConfig {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/floodsub/1.0.0".into(), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/floodsub/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +60,7 @@ where
|
||||
type Future = future::FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
|
||||
}
|
||||
}
|
||||
@ -74,7 +74,7 @@ where
|
||||
type Future = future::FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use futures::{future::{self, FutureResult}, Async, AsyncSink, Future, Poll, Sink, Stream};
|
||||
use libp2p_core::{
|
||||
Multiaddr, PublicKey,
|
||||
@ -134,12 +134,11 @@ pub struct IdentifyInfo {
|
||||
}
|
||||
|
||||
impl UpgradeInfo for IdentifyProtocolConfig {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once((Bytes::from("/ipfs/id/1.0.0"), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/ipfs/id/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,7 +150,7 @@ where
|
||||
type Error = IoError;
|
||||
type Future = FutureResult<Self::Output, IoError>;
|
||||
|
||||
fn upgrade_inbound(self, socket: C, _: ()) -> Self::Future {
|
||||
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
|
||||
trace!("Upgrading inbound connection");
|
||||
let socket = Framed::new(socket, codec::UviBytes::default());
|
||||
let sender = IdentifySender { inner: socket };
|
||||
@ -167,7 +166,7 @@ where
|
||||
type Error = IoError;
|
||||
type Future = IdentifyOutboundFuture<C>;
|
||||
|
||||
fn upgrade_outbound(self, socket: C, _: ()) -> Self::Future {
|
||||
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
|
||||
IdentifyOutboundFuture {
|
||||
inner: Framed::new(socket, codec::UviBytes::<BytesMut>::default()),
|
||||
}
|
||||
|
@ -26,7 +26,7 @@
|
||||
//! The `Stream` component is used to poll the underlying transport, and the `Sink` component is
|
||||
//! used to send messages.
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use futures::{future, sink, stream, Sink, Stream};
|
||||
use libp2p_core::{InboundUpgrade, Multiaddr, OutboundUpgrade, PeerId, UpgradeInfo};
|
||||
use multihash::Multihash;
|
||||
@ -136,12 +136,12 @@ impl Into<protobuf_structs::dht::Message_Peer> for KadPeer {
|
||||
pub struct KademliaProtocolConfig;
|
||||
|
||||
impl UpgradeInfo for KademliaProtocolConfig {
|
||||
type NamesIter = iter::Once<(Bytes, ())>;
|
||||
type UpgradeId = ();
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/ipfs/kad/1.0.0".into(), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/ipfs/kad/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,7 +154,7 @@ where
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_inbound(self, incoming: C, _: ()) -> Self::Future {
|
||||
fn upgrade_inbound(self, incoming: C, _: Self::Info) -> Self::Future {
|
||||
future::ok(
|
||||
Framed::new(incoming, codec::UviBytes::default())
|
||||
.from_err::<IoError>()
|
||||
@ -180,7 +180,7 @@ where
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, incoming: C, _: ()) -> Self::Future {
|
||||
fn upgrade_outbound(self, incoming: C, _: Self::Info) -> Self::Future {
|
||||
future::ok(
|
||||
Framed::new(incoming, codec::UviBytes::default())
|
||||
.from_err::<IoError>()
|
||||
|
@ -46,11 +46,11 @@ impl Observed {
|
||||
}
|
||||
|
||||
impl UpgradeInfo for Observed {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once((Bytes::from("/paritytech/observed-address/0.1.0"), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/paritytech/observed-address/0.1.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ where
|
||||
type Error = io::Error;
|
||||
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
|
||||
|
||||
fn upgrade_inbound(self, conn: C, _: ()) -> Self::Future {
|
||||
fn upgrade_inbound(self, conn: C, _: Self::Info) -> Self::Future {
|
||||
let io = FramedWrite::new(conn, UviBytes::default());
|
||||
Box::new(future::ok(Sender { io }))
|
||||
}
|
||||
@ -76,7 +76,7 @@ where
|
||||
type Error = io::Error;
|
||||
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
|
||||
|
||||
fn upgrade_outbound(self, conn: C, _: ()) -> Self::Future {
|
||||
fn upgrade_outbound(self, conn: 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)
|
||||
@ -126,14 +126,14 @@ mod tests {
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(move |(conn, _)| {
|
||||
Observed::new().upgrade_inbound(conn.unwrap(), ())
|
||||
Observed::new().upgrade_inbound(conn.unwrap(), b"/paritytech/observed-address/0.1.0")
|
||||
})
|
||||
.and_then(move |sender| sender.send_address(observed_addr1));
|
||||
|
||||
let client = TcpStream::connect(&server_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(|conn| {
|
||||
Observed::new().upgrade_outbound(conn, ())
|
||||
Observed::new().upgrade_outbound(conn, b"/paritytech/observed-address/0.1.0")
|
||||
})
|
||||
.map(move |addr| {
|
||||
eprintln!("{} {}", addr, observed_addr2);
|
||||
|
@ -44,11 +44,11 @@ impl<TUserData> Default for Ping<TUserData> {
|
||||
}
|
||||
|
||||
impl<TUserData> UpgradeInfo for Ping<TUserData> {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/ipfs/ping/1.0.0".into(), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/ipfs/ping/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ where
|
||||
type Future = FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
let listener = PingListener {
|
||||
inner: Framed::new(socket, Codec),
|
||||
state: PingListenerState::Listening,
|
||||
@ -79,7 +79,7 @@ where
|
||||
type Future = FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
|
||||
let dialer = PingDialer {
|
||||
inner: Framed::new(socket, Codec),
|
||||
need_writer_flush: false,
|
||||
@ -341,14 +341,14 @@ mod tests {
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(|(c, _)| {
|
||||
Ping::<()>::default().upgrade_inbound(c.unwrap(), ())
|
||||
Ping::<()>::default().upgrade_inbound(c.unwrap(), b"/ipfs/ping/1.0.0")
|
||||
})
|
||||
.flatten();
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(|c| {
|
||||
Ping::<()>::default().upgrade_outbound(c, ())
|
||||
Ping::<()>::default().upgrade_outbound(c, b"/ipfs/ping/1.0.0")
|
||||
})
|
||||
.and_then(|mut pinger| {
|
||||
pinger.ping(());
|
||||
@ -371,14 +371,14 @@ mod tests {
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(|(c, _)| {
|
||||
Ping::<u32>::default().upgrade_inbound(c.unwrap(), ())
|
||||
Ping::<u32>::default().upgrade_inbound(c.unwrap(), b"/ipfs/ping/1.0.0")
|
||||
})
|
||||
.flatten();
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(|c| {
|
||||
Ping::<u32>::default().upgrade_outbound(c, ())
|
||||
Ping::<u32>::default().upgrade_outbound(c, b"/ipfs/ping/1.0.0")
|
||||
})
|
||||
.and_then(|mut pinger| {
|
||||
for n in 0..20 {
|
||||
|
@ -9,7 +9,6 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
|
||||
categories = ["network-programming", "asynchronous"]
|
||||
|
||||
[dependencies]
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
libp2p-core = { version = "0.1.0", path = "../../core" }
|
||||
void = "1"
|
||||
|
@ -18,12 +18,10 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
extern crate bytes;
|
||||
extern crate futures;
|
||||
extern crate libp2p_core;
|
||||
extern crate void;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::future::{self, FutureResult};
|
||||
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use std::iter;
|
||||
@ -33,11 +31,11 @@ use void::Void;
|
||||
pub struct PlainTextConfig;
|
||||
|
||||
impl UpgradeInfo for PlainTextConfig {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once((Bytes::from("/plaintext/1.0.0"), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/plaintext/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +44,7 @@ impl<C> InboundUpgrade<C> for PlainTextConfig {
|
||||
type Error = Void;
|
||||
type Future = FutureResult<C, Self::Error>;
|
||||
|
||||
fn upgrade_inbound(self, i: C, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_inbound(self, i: C, _: Self::Info) -> Self::Future {
|
||||
future::ok(i)
|
||||
}
|
||||
}
|
||||
@ -56,7 +54,7 @@ impl<C> OutboundUpgrade<C> for PlainTextConfig {
|
||||
type Error = Void;
|
||||
type Future = FutureResult<C, Self::Error>;
|
||||
|
||||
fn upgrade_outbound(self, i: C, _: Self::UpgradeId) -> Self::Future {
|
||||
fn upgrade_outbound(self, i: C, _: Self::Info) -> Self::Future {
|
||||
future::ok(i)
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ pub use self::error::SecioError;
|
||||
|
||||
#[cfg(feature = "secp256k1")]
|
||||
use asn1_der::{traits::FromDerEncoded, traits::FromDerObject, DerObject};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use ed25519_dalek::Keypair as Ed25519KeyPair;
|
||||
use futures::stream::MapErr as StreamMapErr;
|
||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||
@ -193,7 +193,7 @@ impl SecioConfig {
|
||||
self
|
||||
}
|
||||
|
||||
fn handshake<T>(self, socket: T, _: ()) -> impl Future<Item=SecioOutput<T>, Error=SecioError>
|
||||
fn handshake<T>(self, socket: T) -> impl Future<Item=SecioOutput<T>, Error=SecioError>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Send + 'static
|
||||
{
|
||||
@ -371,11 +371,11 @@ where
|
||||
}
|
||||
|
||||
impl UpgradeInfo for SecioConfig {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
type Info = &'static [u8];
|
||||
type InfoIter = iter::Once<Self::Info>;
|
||||
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/secio/1.0.0".into(), ()))
|
||||
fn protocol_info(&self) -> Self::InfoIter {
|
||||
iter::once(b"/secio/1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
@ -387,8 +387,8 @@ where
|
||||
type Error = SecioError;
|
||||
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
|
||||
|
||||
fn upgrade_inbound(self, socket: T, id: Self::UpgradeId) -> Self::Future {
|
||||
Box::new(self.handshake(socket, id))
|
||||
fn upgrade_inbound(self, socket: T, _: Self::Info) -> Self::Future {
|
||||
Box::new(self.handshake(socket))
|
||||
}
|
||||
}
|
||||
|
||||
@ -400,8 +400,8 @@ where
|
||||
type Error = SecioError;
|
||||
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
|
||||
|
||||
fn upgrade_outbound(self, socket: T, id: Self::UpgradeId) -> Self::Future {
|
||||
Box::new(self.handshake(socket, id))
|
||||
fn upgrade_outbound(self, socket: T, _: Self::Info) -> Self::Future {
|
||||
Box::new(self.handshake(socket))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user