mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-17 20:11:22 +00:00
Add a Ping behaviour instead of PeriodicPing and PingListen (#815)
* Add a Ping behaviour instead of PeriodicPing and PingListen * Fix tests
This commit is contained in:
@ -22,8 +22,6 @@
|
||||
extern crate libp2p;
|
||||
extern crate void;
|
||||
|
||||
use void::Void;
|
||||
|
||||
/// Small utility to check that a type implements `NetworkBehaviour`.
|
||||
#[allow(dead_code)]
|
||||
fn require_net_behaviour<T: libp2p::core::swarm::NetworkBehaviour<libp2p::core::topology::MemoryTopology>>() {}
|
||||
@ -41,12 +39,11 @@ fn one_field() {
|
||||
#[allow(dead_code)]
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Foo<TSubstream> {
|
||||
ping: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,13 +57,17 @@ fn two_fields() {
|
||||
#[allow(dead_code)]
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Foo<TSubstream> {
|
||||
ping_dialer: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping_listener: libp2p::ping::PingListen<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
identify: libp2p::identify::Identify<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,16 +81,15 @@ fn three_fields() {
|
||||
#[allow(dead_code)]
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Foo<TSubstream> {
|
||||
ping_dialer: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping_listener: libp2p::ping::PingListen<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
identify: libp2p::identify::Identify<TSubstream>,
|
||||
kad: libp2p::kad::Kademlia<TSubstream>,
|
||||
#[behaviour(ignore)]
|
||||
foo: String,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,6 +98,11 @@ fn three_fields() {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::kad::KademliaOut> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::kad::KademliaOut) {
|
||||
}
|
||||
}
|
||||
|
||||
fn foo<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite>() {
|
||||
require_net_behaviour::<Foo<TSubstream>>();
|
||||
}
|
||||
@ -109,13 +114,12 @@ fn custom_polling() {
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(poll_method = "foo")]
|
||||
struct Foo<TSubstream> {
|
||||
ping: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
identify: libp2p::identify::Identify<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,13 +143,12 @@ fn custom_event_no_polling() {
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "String")]
|
||||
struct Foo<TSubstream> {
|
||||
ping: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
identify: libp2p::identify::Identify<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,13 +168,12 @@ fn custom_event_and_polling() {
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(poll_method = "foo", out_event = "String")]
|
||||
struct Foo<TSubstream> {
|
||||
ping: libp2p::ping::PeriodicPing<TSubstream>,
|
||||
ping: libp2p::ping::Ping<TSubstream>,
|
||||
identify: libp2p::identify::Identify<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<Void> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, event: Void) {
|
||||
void::unreachable(event)
|
||||
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
|
||||
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,84 +0,0 @@
|
||||
// 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 crate::dial_handler::PeriodicPingHandler;
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use libp2p_core::{protocols_handler::ProtocolsHandler, PeerId};
|
||||
use std::marker::PhantomData;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use void::Void;
|
||||
|
||||
/// Network behaviour that handles receiving pings sent by other nodes.
|
||||
pub struct PeriodicPing<TSubstream> {
|
||||
/// Marker to pin the generics.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> PeriodicPing<TSubstream> {
|
||||
/// Creates a `PeriodicPing`.
|
||||
pub fn new() -> Self {
|
||||
PeriodicPing {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> Default for PeriodicPing<TSubstream> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
PeriodicPing::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for PeriodicPing<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type ProtocolsHandler = PeriodicPingHandler<TSubstream>;
|
||||
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,
|
||||
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||
) {
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
_: &mut PollParameters<TTopology>,
|
||||
) -> Async<
|
||||
NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
> {
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
@ -81,12 +81,99 @@
|
||||
//! ```
|
||||
//!
|
||||
|
||||
pub use self::dial_layer::PeriodicPing;
|
||||
pub use self::listen_layer::PingListen;
|
||||
|
||||
pub mod dial_handler;
|
||||
pub mod listen_handler;
|
||||
pub mod protocol;
|
||||
|
||||
mod dial_layer;
|
||||
mod listen_layer;
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::either::EitherOutput;
|
||||
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use libp2p_core::{protocols_handler::ProtocolsHandler, protocols_handler::ProtocolsHandlerSelect, PeerId};
|
||||
use std::{marker::PhantomData, time::Duration};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Network behaviour that handles receiving pings sent by other nodes and periodically pings the
|
||||
/// nodes we connect to.
|
||||
pub struct Ping<TSubstream> {
|
||||
/// Marker to pin the generics.
|
||||
marker: PhantomData<TSubstream>,
|
||||
/// Queue of events to report to the user.
|
||||
events: Vec<PingEvent>,
|
||||
}
|
||||
|
||||
/// Event generated by the `Ping` behaviour.
|
||||
pub enum PingEvent {
|
||||
/// We have successfully pinged a peer we are connected to.
|
||||
PingSuccess {
|
||||
/// Id of the peer that we pinged.
|
||||
peer: PeerId,
|
||||
/// Time elapsed between when we sent the ping and when we received the response.
|
||||
time: Duration,
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> Ping<TSubstream> {
|
||||
/// Creates a `Ping`.
|
||||
pub fn new() -> Self {
|
||||
Ping {
|
||||
marker: PhantomData,
|
||||
events: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> Default for Ping<TSubstream> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Ping::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for Ping<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type ProtocolsHandler = ProtocolsHandlerSelect<listen_handler::PingListenHandler<TSubstream>, dial_handler::PeriodicPingHandler<TSubstream>>;
|
||||
type OutEvent = PingEvent;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
listen_handler::PingListenHandler::new()
|
||||
.select(dial_handler::PeriodicPingHandler::new())
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, _: PeerId, _: ConnectedPoint) {}
|
||||
|
||||
fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {}
|
||||
|
||||
fn inject_node_event(
|
||||
&mut self,
|
||||
source: PeerId,
|
||||
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||
) {
|
||||
match event {
|
||||
EitherOutput::Second(dial_handler::OutEvent::PingSuccess(time)) => {
|
||||
self.events.push(PingEvent::PingSuccess {
|
||||
peer: source,
|
||||
time,
|
||||
})
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
_: &mut PollParameters<TTopology>,
|
||||
) -> Async<
|
||||
NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
> {
|
||||
if !self.events.is_empty() {
|
||||
return Async::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)));
|
||||
}
|
||||
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
||||
|
@ -1,84 +0,0 @@
|
||||
// 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 crate::listen_handler::PingListenHandler;
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use libp2p_core::{protocols_handler::ProtocolsHandler, PeerId};
|
||||
use std::marker::PhantomData;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use void::Void;
|
||||
|
||||
/// Network behaviour that handles receiving pings sent by other nodes.
|
||||
pub struct PingListen<TSubstream> {
|
||||
/// Marker to pin the generics.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> PingListen<TSubstream> {
|
||||
/// Creates a `PingListen`.
|
||||
pub fn new() -> Self {
|
||||
PingListen {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> Default for PingListen<TSubstream> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
PingListen::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for PingListen<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type ProtocolsHandler = PingListenHandler<TSubstream>;
|
||||
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,
|
||||
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
|
||||
) {
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
_: &mut PollParameters<TTopology>,
|
||||
) -> Async<
|
||||
NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
> {
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user