From a565b1f35ac03d7720410fae118f5f2669d0839b Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 8 Nov 2018 14:49:32 +0100 Subject: [PATCH 1/2] Add implementations of NetworkBehaviour for ping --- protocols/ping/src/dial_layer.rs | 83 ++++++++++++++++++++++++++++++ protocols/ping/src/lib.rs | 4 ++ protocols/ping/src/listen_layer.rs | 83 ++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 protocols/ping/src/dial_layer.rs create mode 100644 protocols/ping/src/listen_layer.rs diff --git a/protocols/ping/src/dial_layer.rs b/protocols/ping/src/dial_layer.rs new file mode 100644 index 00000000000..2155085fb49 --- /dev/null +++ b/protocols/ping/src/dial_layer.rs @@ -0,0 +1,83 @@ +// Copyright 2018 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use futures::prelude::*; +use libp2p_core::nodes::{ConnectedPoint, NetworkBehavior, NetworkBehaviorAction}; +use libp2p_core::{nodes::protocols_handler::ProtocolsHandler, PeerId}; +use std::marker::PhantomData; +use tokio_io::{AsyncRead, AsyncWrite}; +use void::Void; +use PeriodicPingHandler; + +/// Network behaviour that handles receiving to pings sent by other nodes. +pub struct PeriodicPingBehaviour { + /// Marker to pin the generics. + marker: PhantomData, +} + +impl PeriodicPingBehaviour { + /// Creates a `PeriodicPingBehaviour`. + pub fn new() -> Self { + PeriodicPingBehaviour { + marker: PhantomData, + } + } +} + +impl Default for PeriodicPingBehaviour { + #[inline] + fn default() -> Self { + PeriodicPingBehaviour::new() + } +} + +impl NetworkBehavior for PeriodicPingBehaviour +where + TSubstream: AsyncRead + AsyncWrite + Send + Sync + 'static, +{ + type ProtocolsHandler = PeriodicPingHandler; + type OutEvent = Void; + + fn new_handler(&mut self) -> Self::ProtocolsHandler { + PeriodicPingHandler::new() + } + + fn inject_connected(&mut self, _: PeerId, _: ConnectedPoint) {} + + fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {} + + fn inject_node_event( + &mut self, + _: PeerId, + _: ::OutEvent, + ) { + } + + fn poll( + &mut self, + ) -> Async< + NetworkBehaviorAction< + ::InEvent, + Self::OutEvent, + >, + > { + Async::NotReady + } +} diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index dacab9cc926..c2adb87aa53 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -98,9 +98,13 @@ extern crate tokio_timer; extern crate void; pub use self::dial_handler::PeriodicPingHandler; +pub use self::dial_layer::PeriodicPingBehaviour; pub use self::listen_handler::PingListenHandler; +pub use self::listen_layer::PingListenBehaviour; pub mod protocol; mod dial_handler; +mod dial_layer; mod listen_handler; +mod listen_layer; diff --git a/protocols/ping/src/listen_layer.rs b/protocols/ping/src/listen_layer.rs new file mode 100644 index 00000000000..1401d8b955e --- /dev/null +++ b/protocols/ping/src/listen_layer.rs @@ -0,0 +1,83 @@ +// Copyright 2018 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use futures::prelude::*; +use libp2p_core::nodes::{ConnectedPoint, NetworkBehavior, NetworkBehaviorAction}; +use libp2p_core::{nodes::protocols_handler::ProtocolsHandler, PeerId}; +use std::marker::PhantomData; +use tokio_io::{AsyncRead, AsyncWrite}; +use void::Void; +use PingListenHandler; + +/// Network behaviour that handles receiving to pings sent by other nodes. +pub struct PingListenBehaviour { + /// Marker to pin the generics. + marker: PhantomData, +} + +impl PingListenBehaviour { + /// Creates a `PingListenBehaviour`. + pub fn new() -> Self { + PingListenBehaviour { + marker: PhantomData, + } + } +} + +impl Default for PingListenBehaviour { + #[inline] + fn default() -> Self { + PingListenBehaviour::new() + } +} + +impl NetworkBehavior for PingListenBehaviour +where + TSubstream: AsyncRead + AsyncWrite + Send + Sync + 'static, +{ + type ProtocolsHandler = PingListenHandler; + type OutEvent = Void; + + fn new_handler(&mut self) -> Self::ProtocolsHandler { + PingListenHandler::new() + } + + fn inject_connected(&mut self, _: PeerId, _: ConnectedPoint) {} + + fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {} + + fn inject_node_event( + &mut self, + _: PeerId, + _: ::OutEvent, + ) { + } + + fn poll( + &mut self, + ) -> Async< + NetworkBehaviorAction< + ::InEvent, + Self::OutEvent, + >, + > { + Async::NotReady + } +} From 8abe9f56d2ceec48d9e61ef361e5ad314226f16a Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 9 Nov 2018 16:31:54 +0100 Subject: [PATCH 2/2] Grumbles --- protocols/ping/src/dial_layer.rs | 2 +- protocols/ping/src/listen_layer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/ping/src/dial_layer.rs b/protocols/ping/src/dial_layer.rs index 2155085fb49..6e1c92c22a7 100644 --- a/protocols/ping/src/dial_layer.rs +++ b/protocols/ping/src/dial_layer.rs @@ -26,7 +26,7 @@ use tokio_io::{AsyncRead, AsyncWrite}; use void::Void; use PeriodicPingHandler; -/// Network behaviour that handles receiving to pings sent by other nodes. +/// Network behaviour that handles receiving pings sent by other nodes. pub struct PeriodicPingBehaviour { /// Marker to pin the generics. marker: PhantomData, diff --git a/protocols/ping/src/listen_layer.rs b/protocols/ping/src/listen_layer.rs index 1401d8b955e..11b2a356529 100644 --- a/protocols/ping/src/listen_layer.rs +++ b/protocols/ping/src/listen_layer.rs @@ -26,7 +26,7 @@ use tokio_io::{AsyncRead, AsyncWrite}; use void::Void; use PingListenHandler; -/// Network behaviour that handles receiving to pings sent by other nodes. +/// Network behaviour that handles receiving pings sent by other nodes. pub struct PingListenBehaviour { /// Marker to pin the generics. marker: PhantomData,