From a8fe340a02daed3de9c7c27205cfaff190464802 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 19 Jan 2021 12:11:12 +1100 Subject: [PATCH] Rename crate to libp2p-async-await This crate allows the usage of libp2p with async/.await. It can be use for a sequence of message, one shot or request response models. --- Cargo.toml | 2 +- src/lib.rs | 45 ++++++++++++++++++++++----------------------- tests/simple.rs | 6 +++--- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2284a676..deeb0514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "libp2p-nmessage" +name = "libp2p-async-await" version = "0.1.0" authors = ["Thomas Eizinger "] edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index 1a3222ec..70cbab50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,12 +40,12 @@ enum ProtocolState { Poisoned, } -pub struct NMessageHandler { +pub struct Handler { state: ProtocolState, info: &'static [u8], } -impl NMessageHandler { +impl Handler { pub fn new(info: &'static [u8]) -> Self { Self { state: ProtocolState::None, @@ -54,17 +54,17 @@ impl NMessageHandler Self { Self { info } } } -impl UpgradeInfo for NMessageProtocol { +impl UpgradeInfo for ProtocolInfo { type Info = &'static [u8]; type InfoIter = iter::Once<&'static [u8]>; @@ -97,7 +97,7 @@ macro_rules! impl_read_write { impl_read_write!(InboundSubstream); impl_read_write!(OutboundSubstream); -impl InboundUpgrade for NMessageProtocol { +impl InboundUpgrade for ProtocolInfo { type Output = InboundSubstream; type Error = Infallible; type Future = Ready>; @@ -107,7 +107,7 @@ impl InboundUpgrade for NMessageProtocol { } } -impl OutboundUpgrade for NMessageProtocol { +impl OutboundUpgrade for ProtocolInfo { type Output = OutboundSubstream; type Error = Infallible; type Future = Ready>; @@ -127,8 +127,7 @@ pub enum ProtocolOutEvent { Outbound(Result), } -impl ProtocolsHandler - for NMessageHandler +impl ProtocolsHandler for Handler where TInboundOut: Send + 'static, TOutboundOut: Send + 'static, @@ -136,14 +135,14 @@ where { type InEvent = ProtocolInEvent; type OutEvent = ProtocolOutEvent; - type Error = std::io::Error; - type InboundProtocol = NMessageProtocol; - type OutboundProtocol = NMessageProtocol; + type Error = Infallible; + type InboundProtocol = ProtocolInfo; + type OutboundProtocol = ProtocolInfo; type InboundOpenInfo = (); type OutboundOpenInfo = (); fn listen_protocol(&self) -> SubstreamProtocol { - SubstreamProtocol::new(NMessageProtocol::new(self.info), ()) + SubstreamProtocol::new(ProtocolInfo::new(self.info), ()) } fn inject_fully_negotiated_inbound( @@ -309,7 +308,7 @@ where OutboundProtocolState::GotFunctionRequestedSubstream(protocol), ); Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new(NMessageProtocol::new(self.info), ()), + protocol: SubstreamProtocol::new(ProtocolInfo::new(self.info), ()), }) } ProtocolState::Poisoned => { @@ -323,7 +322,7 @@ where } } -pub struct NMessageBehaviour { +pub struct Behaviour { protocol_in_events: VecDeque<(PeerId, ProtocolInEvent)>, protocol_out_events: VecDeque<(PeerId, ProtocolOutEvent)>, @@ -332,15 +331,15 @@ pub struct NMessageBehaviour { info: &'static [u8], } -impl NMessageBehaviour { - /// Constructs a new [`NMessageBehaviour`] with the given protocol info. +impl Behaviour { + /// Constructs a new [`Behaviour`] with the given protocol info. /// /// # Example /// /// ``` - /// # use libp2p_nmessage::NMessageBehaviour; + /// # use libp2p_async_await::Behaviour; /// - /// let _ = NMessageBehaviour::new(b"/foo/bar/1.0.0"); + /// let _ = Behaviour::new(b"/foo/bar/1.0.0"); /// ``` pub fn new(info: &'static [u8]) -> Self { Self { @@ -352,7 +351,7 @@ impl NMessageBehaviour { } } -impl NMessageBehaviour { +impl Behaviour { pub fn do_protocol_listener( &mut self, peer: PeerId, @@ -388,17 +387,17 @@ pub enum BehaviourOutEvent { Outbound(PeerId, Result), } -impl NetworkBehaviour for NMessageBehaviour +impl NetworkBehaviour for Behaviour where I: Send + 'static, O: Send + 'static, E: Send + 'static, { - type ProtocolsHandler = NMessageHandler; + type ProtocolsHandler = Handler; type OutEvent = BehaviourOutEvent; fn new_handler(&mut self) -> Self::ProtocolsHandler { - NMessageHandler::new(self.info) + Handler::new(self.info) } fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec { diff --git a/tests/simple.rs b/tests/simple.rs index fb43512d..bf1a96bf 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -5,7 +5,7 @@ use harness::await_events_or_timeout; use harness::new_connected_swarm_pair; use libp2p::swarm::SwarmEvent; use libp2p::PeerId; -use libp2p_nmessage::{BehaviourOutEvent, NMessageBehaviour}; +use libp2p_async_await::{Behaviour, BehaviourOutEvent}; use tokio::runtime::Handle; mod harness; @@ -55,13 +55,13 @@ impl From> for MyOutEve #[derive(libp2p::NetworkBehaviour)] #[behaviour(out_event = "MyOutEvent", event_process = false)] struct MyBehaviour { - inner: NMessageBehaviour, + inner: Behaviour, } impl MyBehaviour { pub fn new() -> Self { Self { - inner: NMessageBehaviour::new(b"/foo/bar/1.0.0"), + inner: Behaviour::new(b"/foo/bar/1.0.0"), } } }