Remove PeriodicIdentifyListen and IdentifyListen (#769)

* Remove PeriodicIdentifyListen and IdentifyListen

* Fix tests

* Fix core-derive
This commit is contained in:
Pierre Krieger 2018-12-13 13:53:19 +01:00 committed by GitHub
parent 7da651bf32
commit 0803e36d5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 249 deletions

View File

@ -63,7 +63,7 @@ fn three_fields() {
struct Foo<TSubstream> {
ping_dialer: libp2p::ping::PeriodicPing<TSubstream>,
ping_listener: libp2p::ping::PingListen<TSubstream>,
identify: libp2p::identify::PeriodicIdentify<TSubstream>,
identify: libp2p::identify::Identify<TSubstream>,
#[behaviour(ignore)]
foo: String,
}
@ -79,14 +79,13 @@ fn event_handler() {
#[derive(NetworkBehaviour)]
struct Foo<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> {
#[behaviour(handler = "foo")]
identify: libp2p::identify::PeriodicIdentify<TSubstream>,
ping: libp2p::ping::PingListen<TSubstream>,
}
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> Foo<TSubstream> {
// TODO: for some reason, the parameter cannot be `PeriodicIdentifyEvent` or we
// TODO: for some reason, the parameter cannot be the event type or we
// get a compilation error ; figure out why or open an issue to Rust
fn foo<TTopology>(&mut self, ev: <libp2p::identify::PeriodicIdentify<TSubstream> as libp2p::core::swarm::NetworkBehaviour<TTopology>>::OutEvent) {
let libp2p::identify::PeriodicIdentifyEvent::Identified { .. } = ev;
fn foo<TTopology>(&mut self, ev: <libp2p::ping::PingListen<TSubstream> as libp2p::core::swarm::NetworkBehaviour<TTopology>>::OutEvent) {
}
}
@ -102,7 +101,7 @@ fn custom_polling() {
#[behaviour(poll_method = "foo")]
struct Foo<TSubstream> {
ping: libp2p::ping::PeriodicPing<TSubstream>,
identify: libp2p::identify::PeriodicIdentify<TSubstream>,
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> Foo<TSubstream> {
@ -121,7 +120,7 @@ fn custom_event_no_polling() {
#[behaviour(out_event = "String")]
struct Foo<TSubstream> {
ping: libp2p::ping::PeriodicPing<TSubstream>,
identify: libp2p::identify::PeriodicIdentify<TSubstream>,
identify: libp2p::identify::Identify<TSubstream>,
}
fn foo<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite>() {
@ -136,7 +135,7 @@ fn custom_event_and_polling() {
#[behaviour(poll_method = "foo", out_event = "String")]
struct Foo<TSubstream> {
ping: libp2p::ping::PeriodicPing<TSubstream>,
identify: libp2p::identify::PeriodicIdentify<TSubstream>,
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> Foo<TSubstream> {

View File

@ -83,8 +83,6 @@ extern crate void;
pub use self::identify::{Identify, IdentifyEvent};
pub use self::id_transport::IdentifyTransport;
pub use self::listen_layer::IdentifyListen;
pub use self::periodic_id_layer::{PeriodicIdentify, PeriodicIdentifyEvent};
pub use self::topology::IdentifyTopology;
pub mod listen_handler;
@ -93,7 +91,5 @@ pub mod protocol;
mod identify;
mod id_transport;
mod listen_layer;
mod periodic_id_layer;
mod structs_proto;
mod topology;

View File

@ -1,122 +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::protocol::{IdentifyInfo, IdentifySenderFuture};
use crate::listen_handler::IdentifyListenHandler;
use futures::prelude::*;
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{protocols_handler::ProtocolsHandler, Multiaddr, PeerId};
use smallvec::SmallVec;
use std::collections::HashMap;
use tokio_io::{AsyncRead, AsyncWrite};
use void::Void;
/// Network behaviour that automatically identifies nodes periodically, and returns information
/// about them.
pub struct IdentifyListen<TSubstream> {
/// Information to send back to remotes.
send_back_info: IdentifyInfo,
/// For each peer we're connected to, the observed address to send back to it.
observed_addresses: HashMap<PeerId, Multiaddr>,
/// List of futures that send back information back to remotes.
futures: SmallVec<[IdentifySenderFuture<TSubstream>; 4]>,
}
impl<TSubstream> IdentifyListen<TSubstream> {
/// Creates a `IdentifyListen`.
pub fn new(info: IdentifyInfo) -> Self {
IdentifyListen {
send_back_info: info,
observed_addresses: HashMap::new(),
futures: SmallVec::new(),
}
}
/// Gets the information that is sent back to remotes.
#[inline]
pub fn infos(&self) -> &IdentifyInfo {
&self.send_back_info
}
/// Modifies the information to send back to remotes.
#[inline]
pub fn infos_mut(&mut self) -> &mut IdentifyInfo {
&mut self.send_back_info
}
}
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for IdentifyListen<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
type ProtocolsHandler = IdentifyListenHandler<TSubstream>;
type OutEvent = Void;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
IdentifyListenHandler::new()
}
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
let observed = match endpoint {
ConnectedPoint::Dialer { address } => address,
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr,
};
self.observed_addresses.insert(peer_id, observed);
}
fn inject_disconnected(&mut self, peer_id: &PeerId, _: ConnectedPoint) {
self.observed_addresses.remove(peer_id);
}
fn inject_node_event(
&mut self,
peer_id: PeerId,
sender: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
) {
let observed = self.observed_addresses.get(&peer_id)
.expect("We only receive events from nodes we're connected to ; we insert into the \
hashmap when we connect to a node and remove only when we disconnect; QED");
let future = sender.send(self.send_back_info.clone(), &observed);
self.futures.push(future);
}
fn poll(
&mut self,
_: &mut PollParameters<TTopology>,
) -> Async<
NetworkBehaviourAction<
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
Self::OutEvent,
>,
> {
// Removes each future one by one, and pushes them back if they're not ready.
for n in (0..self.futures.len()).rev() {
let mut future = self.futures.swap_remove(n);
match future.poll() {
Ok(Async::Ready(())) => {}
Ok(Async::NotReady) => self.futures.push(future),
Err(_) => {},
}
}
Async::NotReady
}
}

View File

@ -1,115 +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::periodic_id_handler::{PeriodicIdHandler, PeriodicIdHandlerEvent};
use crate::protocol::IdentifyInfo;
use futures::prelude::*;
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{protocols_handler::ProtocolsHandler, Multiaddr, PeerId};
use std::{collections::VecDeque, marker::PhantomData};
use tokio_io::{AsyncRead, AsyncWrite};
use void::Void;
/// Network behaviour that automatically identifies nodes periodically, and returns information
/// about them.
pub struct PeriodicIdentify<TSubstream> {
/// Events that need to be produced outside when polling..
events: VecDeque<NetworkBehaviourAction<Void, PeriodicIdentifyEvent>>,
/// Marker to pin the generics.
marker: PhantomData<TSubstream>,
}
impl<TSubstream> PeriodicIdentify<TSubstream> {
/// Creates a `PeriodicIdentify`.
pub fn new() -> Self {
PeriodicIdentify {
events: VecDeque::new(),
marker: PhantomData,
}
}
}
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for PeriodicIdentify<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
type ProtocolsHandler = PeriodicIdHandler<TSubstream>;
type OutEvent = PeriodicIdentifyEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
PeriodicIdHandler::new()
}
fn inject_connected(&mut self, _: PeerId, _: ConnectedPoint) {}
fn inject_disconnected(&mut self, _: &PeerId, _: ConnectedPoint) {}
fn inject_node_event(
&mut self,
peer_id: PeerId,
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
) {
match event {
PeriodicIdHandlerEvent::Identified(remote) => {
self.events
.push_back(NetworkBehaviourAction::ReportObservedAddr {
address: remote.observed_addr.clone(),
});
self.events
.push_back(NetworkBehaviourAction::GenerateEvent(PeriodicIdentifyEvent::Identified {
peer_id: peer_id,
info: remote.info,
observed_addr: remote.observed_addr,
}));
}
_ => (), // TODO: exhaustive pattern
}
}
fn poll(
&mut self,
_: &mut PollParameters<TTopology>,
) -> Async<
NetworkBehaviourAction<
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
Self::OutEvent,
>,
> {
if let Some(event) = self.events.pop_front() {
return Async::Ready(event);
}
Async::NotReady
}
}
/// Event generated by the `PeriodicIdentify`.
#[derive(Debug, Clone)]
pub enum PeriodicIdentifyEvent {
/// We obtained identification information from the remote
Identified {
/// Peer that has been successfully identified.
peer_id: PeerId,
/// Information of the remote.
info: IdentifyInfo,
/// Address the remote observes us as.
observed_addr: Multiaddr,
},
}