diff --git a/misc/core-derive/tests/test.rs b/misc/core-derive/tests/test.rs index b8d44798..658a975c 100644 --- a/misc/core-derive/tests/test.rs +++ b/misc/core-derive/tests/test.rs @@ -63,7 +63,7 @@ fn three_fields() { struct Foo { ping_dialer: libp2p::ping::PeriodicPing, ping_listener: libp2p::ping::PingListen, - identify: libp2p::identify::PeriodicIdentify, + identify: libp2p::identify::Identify, #[behaviour(ignore)] foo: String, } @@ -79,14 +79,13 @@ fn event_handler() { #[derive(NetworkBehaviour)] struct Foo { #[behaviour(handler = "foo")] - identify: libp2p::identify::PeriodicIdentify, + ping: libp2p::ping::PingListen, } impl Foo { - // 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(&mut self, ev: as libp2p::core::swarm::NetworkBehaviour>::OutEvent) { - let libp2p::identify::PeriodicIdentifyEvent::Identified { .. } = ev; + fn foo(&mut self, ev: as libp2p::core::swarm::NetworkBehaviour>::OutEvent) { } } @@ -102,7 +101,7 @@ fn custom_polling() { #[behaviour(poll_method = "foo")] struct Foo { ping: libp2p::ping::PeriodicPing, - identify: libp2p::identify::PeriodicIdentify, + identify: libp2p::identify::Identify, } impl Foo { @@ -121,7 +120,7 @@ fn custom_event_no_polling() { #[behaviour(out_event = "String")] struct Foo { ping: libp2p::ping::PeriodicPing, - identify: libp2p::identify::PeriodicIdentify, + identify: libp2p::identify::Identify, } fn foo() { @@ -136,7 +135,7 @@ fn custom_event_and_polling() { #[behaviour(poll_method = "foo", out_event = "String")] struct Foo { ping: libp2p::ping::PeriodicPing, - identify: libp2p::identify::PeriodicIdentify, + identify: libp2p::identify::Identify, } impl Foo { diff --git a/protocols/identify/src/lib.rs b/protocols/identify/src/lib.rs index b14cf449..9e7633b9 100644 --- a/protocols/identify/src/lib.rs +++ b/protocols/identify/src/lib.rs @@ -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; diff --git a/protocols/identify/src/listen_layer.rs b/protocols/identify/src/listen_layer.rs deleted file mode 100644 index 27fae73a..00000000 --- a/protocols/identify/src/listen_layer.rs +++ /dev/null @@ -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 { - /// 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, - /// List of futures that send back information back to remotes. - futures: SmallVec<[IdentifySenderFuture; 4]>, -} - -impl IdentifyListen { - /// 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 NetworkBehaviour for IdentifyListen -where - TSubstream: AsyncRead + AsyncWrite, -{ - type ProtocolsHandler = IdentifyListenHandler; - 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: ::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, - ) -> Async< - NetworkBehaviourAction< - ::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 - } -} diff --git a/protocols/identify/src/periodic_id_layer.rs b/protocols/identify/src/periodic_id_layer.rs deleted file mode 100644 index e369edcc..00000000 --- a/protocols/identify/src/periodic_id_layer.rs +++ /dev/null @@ -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 { - /// Events that need to be produced outside when polling.. - events: VecDeque>, - /// Marker to pin the generics. - marker: PhantomData, -} - -impl PeriodicIdentify { - /// Creates a `PeriodicIdentify`. - pub fn new() -> Self { - PeriodicIdentify { - events: VecDeque::new(), - marker: PhantomData, - } - } -} - -impl NetworkBehaviour for PeriodicIdentify -where - TSubstream: AsyncRead + AsyncWrite, -{ - type ProtocolsHandler = PeriodicIdHandler; - 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: ::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, - ) -> Async< - NetworkBehaviourAction< - ::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, - }, -}