mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 23:31:33 +00:00
Move swarm and protocols handler into swarm crate. (#1188)
Move swarm and protocols handler into swarm crate.
This commit is contained in:
112
core/src/lib.rs
112
core/src/lib.rs
@ -18,46 +18,22 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//! Transport, protocol upgrade and swarm systems of *libp2p*.
|
||||
//! Transports, upgrades, multiplexing and node handling of *libp2p*.
|
||||
//!
|
||||
//! This crate contains all the core traits and mechanisms of the transport and swarm systems
|
||||
//! of *libp2p*.
|
||||
//! The main concepts of libp2p-core are:
|
||||
//!
|
||||
//! # Overview
|
||||
//!
|
||||
//! This documentation focuses on the concepts of *libp2p-core*, and is interesting mostly if you
|
||||
//! want to extend *libp2p* with new protocols. If you only want to use libp2p, you might find the
|
||||
//! documentation of the main *libp2p* crate more interesting.
|
||||
//!
|
||||
//! The main concepts of libp2p are:
|
||||
//!
|
||||
//! - A `PeerId` is a unique global identifier for a node on the network. Each node must have a
|
||||
//! different `PeerId`. Normally, a `PeerId` is the hash of the public key used to negotiate
|
||||
//! encryption on the communication channel, thereby guaranteeing that they cannot be spoofed.
|
||||
//! - The `Transport` trait defines how to reach a remote node or listen for incoming remote
|
||||
//! connections. See the `transport` module.
|
||||
//! - The `Swarm` struct contains all active and pending connections to remotes and manages the
|
||||
//! state of all the substreams that have been opened, and all the upgrades that were built upon
|
||||
//! these substreams.
|
||||
//! - Use the `NetworkBehaviour` trait to customize the behaviour of a `Swarm`. It is the
|
||||
//! `NetworkBehaviour` that controls what happens on the network. Multiple types that implement
|
||||
//! `NetworkBehaviour` can be composed into a single behaviour.
|
||||
//! - The `StreamMuxer` trait is implemented on structs that hold a connection to a remote and can
|
||||
//! subdivide this connection into multiple substreams. See the `muxing` module.
|
||||
//! - The `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` traits define how to upgrade each
|
||||
//! individual substream to use a protocol. See the `upgrade` module.
|
||||
//! - The `ProtocolsHandler` trait defines how each active connection to a remote should behave:
|
||||
//! how to handle incoming substreams, which protocols are supported, when to open a new
|
||||
//! outbound substream, etc. See the `protocols_handler` trait.
|
||||
//!
|
||||
//! # High-level APIs vs low-level APIs
|
||||
//!
|
||||
//! This crate provides two sets of APIs:
|
||||
//!
|
||||
//! - The low-level APIs are contained within the `nodes` module. See the documentation for more
|
||||
//! information.
|
||||
//! - The high-level APIs include the concepts of `Swarm`, `ProtocolsHandler` and `NetworkBehaviour`.
|
||||
|
||||
//! - A [`PeerId`] is a unique global identifier for a node on the network.
|
||||
//! Each node must have a different `PeerId`. Normally, a `PeerId` is the
|
||||
//! hash of the public key used to negotiate encryption on the
|
||||
//! communication channel, thereby guaranteeing that they cannot be spoofed.
|
||||
//! - The [`Transport`] trait defines how to reach a remote node or listen for
|
||||
//! incoming remote connections. See the `transport` module.
|
||||
//! - The [`StreamMuxer`] trait is implemented on structs that hold a connection
|
||||
//! to a remote and can subdivide this connection into multiple substreams.
|
||||
//! See the `muxing` module.
|
||||
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits
|
||||
//! define how to upgrade each individual substream to use a protocol.
|
||||
//! See the `upgrade` module.
|
||||
|
||||
/// Multi-address re-export.
|
||||
pub use multiaddr;
|
||||
@ -74,18 +50,13 @@ pub mod either;
|
||||
pub mod identity;
|
||||
pub mod muxing;
|
||||
pub mod nodes;
|
||||
pub mod protocols_handler;
|
||||
pub mod swarm;
|
||||
pub mod transport;
|
||||
pub mod upgrade;
|
||||
|
||||
pub use multiaddr::Multiaddr;
|
||||
pub use muxing::StreamMuxer;
|
||||
pub use nodes::raw_swarm::ConnectedPoint;
|
||||
pub use peer_id::PeerId;
|
||||
pub use protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent};
|
||||
pub use identity::PublicKey;
|
||||
pub use swarm::Swarm;
|
||||
pub use transport::Transport;
|
||||
pub use translation::address_translation;
|
||||
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, UpgradeError, ProtocolName};
|
||||
@ -129,3 +100,58 @@ impl Endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
/// How we connected to a node.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConnectedPoint {
|
||||
/// We dialed the node.
|
||||
Dialer {
|
||||
/// Multiaddress that was successfully dialed.
|
||||
address: Multiaddr,
|
||||
},
|
||||
/// We received the node.
|
||||
Listener {
|
||||
/// Address of the listener that received the connection.
|
||||
listen_addr: Multiaddr,
|
||||
/// Stack of protocols used to send back data to the remote.
|
||||
send_back_addr: Multiaddr,
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'_ ConnectedPoint> for Endpoint {
|
||||
fn from(endpoint: &'_ ConnectedPoint) -> Endpoint {
|
||||
endpoint.to_endpoint()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConnectedPoint> for Endpoint {
|
||||
fn from(endpoint: ConnectedPoint) -> Endpoint {
|
||||
endpoint.to_endpoint()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectedPoint {
|
||||
/// Turns the `ConnectedPoint` into the corresponding `Endpoint`.
|
||||
pub fn to_endpoint(&self) -> Endpoint {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
|
||||
ConnectedPoint::Listener { .. } => Endpoint::Listener
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if we are `Dialer`.
|
||||
pub fn is_dialer(&self) -> bool {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { .. } => true,
|
||||
ConnectedPoint::Listener { .. } => false
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if we are `Listener`.
|
||||
pub fn is_listener(&self) -> bool {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { .. } => false,
|
||||
ConnectedPoint::Listener { .. } => true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ pub mod listeners;
|
||||
pub mod node;
|
||||
pub mod raw_swarm;
|
||||
|
||||
pub use self::collection::ConnectionInfo;
|
||||
pub use self::node::Substream;
|
||||
pub use self::handled_node::{NodeHandlerEvent, NodeHandlerEndpoint};
|
||||
pub use self::raw_swarm::{ConnectedPoint, Peer, RawSwarm, RawSwarmEvent};
|
||||
pub use collection::ConnectionInfo;
|
||||
pub use node::Substream;
|
||||
pub use handled_node::{NodeHandlerEvent, NodeHandlerEndpoint};
|
||||
pub use raw_swarm::{Peer, RawSwarm, RawSwarmEvent};
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
use crate::muxing::StreamMuxer;
|
||||
use crate::{
|
||||
Endpoint, Multiaddr, PeerId, address_translation,
|
||||
ConnectedPoint, Multiaddr, PeerId, address_translation,
|
||||
nodes::{
|
||||
collection::{
|
||||
CollectionEvent,
|
||||
@ -619,67 +619,6 @@ where TTrans: Transport
|
||||
}
|
||||
}
|
||||
|
||||
/// How we connected to a node.
|
||||
// TODO: move definition
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConnectedPoint {
|
||||
/// We dialed the node.
|
||||
Dialer {
|
||||
/// Multiaddress that was successfully dialed.
|
||||
address: Multiaddr,
|
||||
},
|
||||
/// We received the node.
|
||||
Listener {
|
||||
/// Address of the listener that received the connection.
|
||||
listen_addr: Multiaddr,
|
||||
/// Stack of protocols used to send back data to the remote.
|
||||
send_back_addr: Multiaddr,
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ConnectedPoint> for Endpoint {
|
||||
#[inline]
|
||||
fn from(endpoint: &'a ConnectedPoint) -> Endpoint {
|
||||
endpoint.to_endpoint()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConnectedPoint> for Endpoint {
|
||||
#[inline]
|
||||
fn from(endpoint: ConnectedPoint) -> Endpoint {
|
||||
endpoint.to_endpoint()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectedPoint {
|
||||
/// Turns the `ConnectedPoint` into the corresponding `Endpoint`.
|
||||
#[inline]
|
||||
pub fn to_endpoint(&self) -> Endpoint {
|
||||
match *self {
|
||||
ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
|
||||
ConnectedPoint::Listener { .. } => Endpoint::Listener,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if we are `Dialer`.
|
||||
#[inline]
|
||||
pub fn is_dialer(&self) -> bool {
|
||||
match *self {
|
||||
ConnectedPoint::Dialer { .. } => true,
|
||||
ConnectedPoint::Listener { .. } => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if we are `Listener`.
|
||||
#[inline]
|
||||
pub fn is_listener(&self) -> bool {
|
||||
match *self {
|
||||
ConnectedPoint::Dialer { .. } => false,
|
||||
ConnectedPoint::Listener { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about an incoming connection currently being negotiated.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct IncomingInfo<'a> {
|
||||
|
@ -1,104 +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::{
|
||||
protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
},
|
||||
upgrade::{
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
DeniedUpgrade,
|
||||
}
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::marker::PhantomData;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use void::Void;
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that doesn't handle anything.
|
||||
pub struct DummyProtocolsHandler<TSubstream> {
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream> Default for DummyProtocolsHandler<TSubstream> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
DummyProtocolsHandler {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> ProtocolsHandler for DummyProtocolsHandler<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type InEvent = Void;
|
||||
type OutEvent = Void;
|
||||
type Error = Void;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = DeniedUpgrade;
|
||||
type OutboundProtocol = DeniedUpgrade;
|
||||
type OutboundOpenInfo = Void;
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
SubstreamProtocol::new(DeniedUpgrade)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
_: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output
|
||||
) {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
_: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
|
||||
_: Self::OutboundOpenInfo
|
||||
) {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, _: Self::InEvent) {}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive { KeepAlive::No }
|
||||
|
||||
#[inline]
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Void,
|
||||
> {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
@ -1,117 +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::{
|
||||
protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
},
|
||||
upgrade::{
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
}
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// Wrapper around a protocol handler that turns the input event into something else.
|
||||
pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
|
||||
inner: TProtoHandler,
|
||||
map: TMap,
|
||||
marker: PhantomData<TNewIn>,
|
||||
}
|
||||
|
||||
impl<TProtoHandler, TMap, TNewIn> MapInEvent<TProtoHandler, TNewIn, TMap> {
|
||||
/// Creates a `MapInEvent`.
|
||||
#[inline]
|
||||
pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
|
||||
MapInEvent {
|
||||
inner,
|
||||
map,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler, TNewIn, TMap>
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
|
||||
{
|
||||
type InEvent = TNewIn;
|
||||
type OutEvent = TProtoHandler::OutEvent;
|
||||
type Error = TProtoHandler::Error;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
type InboundProtocol = TProtoHandler::InboundProtocol;
|
||||
type OutboundProtocol = TProtoHandler::OutboundProtocol;
|
||||
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
self.inner.listen_protocol()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_inbound(protocol)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_outbound(protocol, info)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, event: TNewIn) {
|
||||
if let Some(event) = (self.map)(event) {
|
||||
self.inner.inject_event(event);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
||||
self.inner.inject_dial_upgrade_error(info, error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
self.inner.connection_keep_alive()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Self::Error,
|
||||
> {
|
||||
self.inner.poll()
|
||||
}
|
||||
}
|
@ -1,119 +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::{
|
||||
protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
},
|
||||
upgrade::{
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
}
|
||||
};
|
||||
use futures::prelude::*;
|
||||
|
||||
/// Wrapper around a protocol handler that turns the output event into something else.
|
||||
pub struct MapOutEvent<TProtoHandler, TMap> {
|
||||
inner: TProtoHandler,
|
||||
map: TMap,
|
||||
}
|
||||
|
||||
impl<TProtoHandler, TMap> MapOutEvent<TProtoHandler, TMap> {
|
||||
/// Creates a `MapOutEvent`.
|
||||
#[inline]
|
||||
pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
|
||||
MapOutEvent {
|
||||
inner,
|
||||
map,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TProtoHandler, TMap, TNewOut> ProtocolsHandler for MapOutEvent<TProtoHandler, TMap>
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut,
|
||||
{
|
||||
type InEvent = TProtoHandler::InEvent;
|
||||
type OutEvent = TNewOut;
|
||||
type Error = TProtoHandler::Error;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
type InboundProtocol = TProtoHandler::InboundProtocol;
|
||||
type OutboundProtocol = TProtoHandler::OutboundProtocol;
|
||||
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
self.inner.listen_protocol()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_inbound(protocol)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_outbound(protocol, info)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, event: Self::InEvent) {
|
||||
self.inner.inject_event(event)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
||||
self.inner.inject_dial_upgrade_error(info, error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
self.inner.connection_keep_alive()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Self::Error,
|
||||
> {
|
||||
Ok(self.inner.poll()?.map(|ev| {
|
||||
match ev {
|
||||
ProtocolsHandlerEvent::Custom(ev) => ProtocolsHandlerEvent::Custom((self.map)(ev)),
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info }
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
@ -1,507 +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.
|
||||
|
||||
//! Once a connection to a remote peer is established, a `ProtocolsHandler` negotiates
|
||||
//! and handles one or more specific protocols on the connection.
|
||||
//!
|
||||
//! Protocols are negotiated and used on individual substreams of the connection.
|
||||
//! Thus a `ProtocolsHandler` defines the inbound and outbound upgrades to apply
|
||||
//! when creating a new inbound or outbound substream, respectively, and is notified
|
||||
//! by a `Swarm` when these upgrades have been successfully applied, including the
|
||||
//! final output of the upgrade. A `ProtocolsHandler` can then continue communicating
|
||||
//! with the peer over the substream using the negotiated protocol(s).
|
||||
//!
|
||||
//! Two `ProtocolsHandler`s can be composed with [`ProtocolsHandler::select()`]
|
||||
//! in order to build a new handler supporting the combined set of protocols,
|
||||
//! with methods being dispatched to the appropriate handler according to the
|
||||
//! used protocol(s) determined by the associated types of the handlers.
|
||||
//!
|
||||
//! > **Note**: A `ProtocolsHandler` handles one or more protocols in the context of a single
|
||||
//! > connection with a remote. In order to handle a protocol that requires knowledge of
|
||||
//! > the network as a whole, see the `NetworkBehaviour` trait.
|
||||
|
||||
use crate::nodes::raw_swarm::ConnectedPoint;
|
||||
use crate::PeerId;
|
||||
use crate::upgrade::{
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
UpgradeError,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::{cmp::Ordering, error, fmt, time::Duration};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
pub use self::dummy::DummyProtocolsHandler;
|
||||
pub use self::map_in::MapInEvent;
|
||||
pub use self::map_out::MapOutEvent;
|
||||
pub use self::node_handler::{NodeHandlerWrapper, NodeHandlerWrapperBuilder, NodeHandlerWrapperError};
|
||||
pub use self::one_shot::OneShotHandler;
|
||||
pub use self::select::{IntoProtocolsHandlerSelect, ProtocolsHandlerSelect};
|
||||
|
||||
mod dummy;
|
||||
mod map_in;
|
||||
mod map_out;
|
||||
mod node_handler;
|
||||
mod one_shot;
|
||||
mod select;
|
||||
|
||||
/// A handler for a set of protocols used on a connection with a remote.
|
||||
///
|
||||
/// This trait should be implemented for a type that maintains the state for
|
||||
/// the execution of a specific protocol with a remote.
|
||||
///
|
||||
/// # Handling a protocol
|
||||
///
|
||||
/// Communication with a remote over a set of protocols is initiated in one of two ways:
|
||||
///
|
||||
/// 1. Dialing by initiating a new outbound substream. In order to do so,
|
||||
/// [`ProtocolsHandler::poll()`] must return an [`OutboundSubstreamRequest`], providing an
|
||||
/// instance of [`ProtocolsHandler::OutboundUpgrade`] that is used to negotiate the
|
||||
/// protocol(s). Upon success, [`ProtocolsHandler::inject_fully_negotiated_outbound`]
|
||||
/// is called with the final output of the upgrade.
|
||||
///
|
||||
/// 2. Listening by accepting a new inbound substream. When a new inbound substream
|
||||
/// is created on a connection, [`ProtocolsHandler::listen_protocol`] is called
|
||||
/// to obtain an instance of [`ProtocolsHandler::InboundUpgrade`] that is used to
|
||||
/// negotiate the protocol(s). Upon success,
|
||||
/// [`ProtocolsHandler::inject_fully_negotiated_inbound`] is called with the final
|
||||
/// output of the upgrade.
|
||||
///
|
||||
/// # Connection Keep-Alive
|
||||
///
|
||||
/// A `ProtocolsHandler` can influence the lifetime of the underlying connection
|
||||
/// through [`ProtocolsHandler::connection_keep_alive`]. That is, the protocol
|
||||
/// implemented by the handler can include conditions for terminating the connection.
|
||||
/// The lifetime of successfully negotiated substreams is fully controlled by the handler.
|
||||
///
|
||||
/// Implementors of this trait should keep in mind that the connection can be closed at any time.
|
||||
/// When a connection is closed gracefully, the substreams used by the handler may still
|
||||
/// continue reading data until the remote closes its side of the connection.
|
||||
pub trait ProtocolsHandler {
|
||||
/// Custom event that can be received from the outside.
|
||||
type InEvent;
|
||||
/// Custom event that can be produced by the handler and that will be returned to the outside.
|
||||
type OutEvent;
|
||||
/// The type of errors returned by [`ProtocolsHandler::poll`].
|
||||
type Error: error::Error;
|
||||
/// The type of substreams on which the protocol(s) are negotiated.
|
||||
type Substream: AsyncRead + AsyncWrite;
|
||||
/// The inbound upgrade for the protocol(s) used by the handler.
|
||||
type InboundProtocol: InboundUpgrade<Self::Substream>;
|
||||
/// The outbound upgrade for the protocol(s) used by the handler.
|
||||
type OutboundProtocol: OutboundUpgrade<Self::Substream>;
|
||||
/// The type of additional information passed to an `OutboundSubstreamRequest`.
|
||||
type OutboundOpenInfo;
|
||||
|
||||
/// The [`InboundUpgrade`] to apply on inbound substreams to negotiate the
|
||||
/// desired protocols.
|
||||
///
|
||||
/// > **Note**: The returned `InboundUpgrade` should always accept all the generally
|
||||
/// > supported protocols, even if in a specific context a particular one is
|
||||
/// > not supported, (eg. when only allowing one substream at a time for a protocol).
|
||||
/// > This allows a remote to put the list of supported protocols in a cache.
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol>;
|
||||
|
||||
/// Injects the output of a successful upgrade on a new inbound substream.
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
|
||||
);
|
||||
|
||||
/// Injects the output of a successful upgrade on a new outbound substream.
|
||||
///
|
||||
/// The second argument is the information that was previously passed to
|
||||
/// [`ProtocolsHandlerEvent::OutboundSubstreamRequest`].
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
);
|
||||
|
||||
/// Injects an event coming from the outside in the handler.
|
||||
fn inject_event(&mut self, event: Self::InEvent);
|
||||
|
||||
/// Indicates to the handler that upgrading a substream to the given protocol has failed.
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
info: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error
|
||||
>
|
||||
);
|
||||
|
||||
/// Returns until when the connection should be kept alive.
|
||||
///
|
||||
/// This method is called by the `Swarm` after each invocation of
|
||||
/// [`ProtocolsHandler::poll`] to determine if the connection and the associated
|
||||
/// `ProtocolsHandler`s should be kept alive as far as this handler is concerned
|
||||
/// and if so, for how long.
|
||||
///
|
||||
/// Returning [`KeepAlive::No`] indicates that the connection should be
|
||||
/// closed and this handler destroyed immediately.
|
||||
///
|
||||
/// Returning [`KeepAlive::Until`] indicates that the connection may be closed
|
||||
/// and this handler destroyed after the specified `Instant`.
|
||||
///
|
||||
/// Returning [`KeepAlive::Yes`] indicates that the connection should
|
||||
/// be kept alive until the next call to this method.
|
||||
///
|
||||
/// > **Note**: The connection is always closed and the handler destroyed
|
||||
/// > when [`ProtocolsHandler::poll`] returns an error. Furthermore, the
|
||||
/// > connection may be closed for reasons outside of the control
|
||||
/// > of the handler.
|
||||
fn connection_keep_alive(&self) -> KeepAlive;
|
||||
|
||||
/// Should behave like `Stream::poll()`.
|
||||
///
|
||||
/// Returning an error will close the connection to the remote.
|
||||
fn poll(&mut self) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Self::Error
|
||||
>;
|
||||
|
||||
/// Adds a closure that turns the input event into something else.
|
||||
#[inline]
|
||||
fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap>
|
||||
where
|
||||
Self: Sized,
|
||||
TMap: Fn(&TNewIn) -> Option<&Self::InEvent>,
|
||||
{
|
||||
MapInEvent::new(self, map)
|
||||
}
|
||||
|
||||
/// Adds a closure that turns the output event into something else.
|
||||
#[inline]
|
||||
fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap>
|
||||
where
|
||||
Self: Sized,
|
||||
TMap: FnMut(Self::OutEvent) -> TNewOut,
|
||||
{
|
||||
MapOutEvent::new(self, map)
|
||||
}
|
||||
|
||||
/// Creates a new `ProtocolsHandler` that selects either this handler or
|
||||
/// `other` by delegating methods calls appropriately.
|
||||
///
|
||||
/// > **Note**: The largest `KeepAlive` returned by the two handlers takes precedence,
|
||||
/// > i.e. is returned from [`ProtocolsHandler::connection_keep_alive`] by the returned
|
||||
/// > handler.
|
||||
#[inline]
|
||||
fn select<TProto2>(self, other: TProto2) -> ProtocolsHandlerSelect<Self, TProto2>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ProtocolsHandlerSelect::new(self, other)
|
||||
}
|
||||
|
||||
/// Creates a builder that allows creating a `NodeHandler` that handles this protocol
|
||||
/// exclusively.
|
||||
///
|
||||
/// > **Note**: This method should not be redefined in a custom `ProtocolsHandler`.
|
||||
#[inline]
|
||||
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
IntoProtocolsHandler::into_node_handler_builder(self)
|
||||
}
|
||||
|
||||
/// Builds an implementation of `NodeHandler` that handles this protocol exclusively.
|
||||
///
|
||||
/// > **Note**: This is a shortcut for `self.into_node_handler_builder().build()`.
|
||||
#[inline]
|
||||
#[deprecated(note = "Use into_node_handler_builder instead")]
|
||||
fn into_node_handler(self) -> NodeHandlerWrapper<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
#![allow(deprecated)]
|
||||
self.into_node_handler_builder().build()
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration of inbound or outbound substream protocol(s)
|
||||
/// for a [`ProtocolsHandler`].
|
||||
///
|
||||
/// The inbound substream protocol(s) are defined by [`ProtocolsHandler::listen_protocol`]
|
||||
/// and the outbound substream protocol(s) by [`ProtocolsHandlerEvent::OutboundSubstreamRequest`].
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct SubstreamProtocol<TUpgrade> {
|
||||
upgrade: TUpgrade,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl<TUpgrade> SubstreamProtocol<TUpgrade> {
|
||||
/// Create a new `ListenProtocol` from the given upgrade.
|
||||
///
|
||||
/// The default timeout for applying the given upgrade on a substream is
|
||||
/// 10 seconds.
|
||||
pub fn new(upgrade: TUpgrade) -> SubstreamProtocol<TUpgrade> {
|
||||
SubstreamProtocol {
|
||||
upgrade,
|
||||
timeout: Duration::from_secs(10),
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a function over the protocol upgrade.
|
||||
pub fn map_upgrade<U, F>(self, f: F) -> SubstreamProtocol<U>
|
||||
where
|
||||
F: FnOnce(TUpgrade) -> U,
|
||||
{
|
||||
SubstreamProtocol {
|
||||
upgrade: f(self.upgrade),
|
||||
timeout: self.timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a new timeout for the protocol upgrade.
|
||||
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
self
|
||||
}
|
||||
|
||||
/// Borrows the contained protocol upgrade.
|
||||
pub fn upgrade(&self) -> &TUpgrade {
|
||||
&self.upgrade
|
||||
}
|
||||
|
||||
/// Borrows the timeout for the protocol upgrade.
|
||||
pub fn timeout(&self) -> &Duration {
|
||||
&self.timeout
|
||||
}
|
||||
|
||||
/// Converts the substream protocol configuration into the contained upgrade.
|
||||
pub fn into_upgrade(self) -> TUpgrade {
|
||||
self.upgrade
|
||||
}
|
||||
}
|
||||
|
||||
impl<TUpgrade> From<TUpgrade> for SubstreamProtocol<TUpgrade> {
|
||||
fn from(upgrade: TUpgrade) -> SubstreamProtocol<TUpgrade> {
|
||||
SubstreamProtocol::new(upgrade)
|
||||
}
|
||||
}
|
||||
|
||||
/// Event produced by a handler.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom> {
|
||||
/// Request a new outbound substream to be opened with the remote.
|
||||
OutboundSubstreamRequest {
|
||||
/// The protocol(s) to apply on the substream.
|
||||
protocol: SubstreamProtocol<TConnectionUpgrade>,
|
||||
/// User-defined information, passed back when the substream is open.
|
||||
info: TOutboundOpenInfo,
|
||||
},
|
||||
|
||||
/// Other event.
|
||||
Custom(TCustom),
|
||||
}
|
||||
|
||||
/// Event produced by a handler.
|
||||
impl<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
|
||||
ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
|
||||
{
|
||||
/// If this is an `OutboundSubstreamRequest`, maps the `info` member from a
|
||||
/// `TOutboundOpenInfo` to something else.
|
||||
#[inline]
|
||||
pub fn map_outbound_open_info<F, I>(
|
||||
self,
|
||||
map: F,
|
||||
) -> ProtocolsHandlerEvent<TConnectionUpgrade, I, TCustom>
|
||||
where
|
||||
F: FnOnce(TOutboundOpenInfo) -> I,
|
||||
{
|
||||
match self {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol,
|
||||
info: map(info),
|
||||
}
|
||||
}
|
||||
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
|
||||
}
|
||||
}
|
||||
|
||||
/// If this is an `OutboundSubstreamRequest`, maps the protocol (`TConnectionUpgrade`)
|
||||
/// to something else.
|
||||
#[inline]
|
||||
pub fn map_protocol<F, I>(
|
||||
self,
|
||||
map: F,
|
||||
) -> ProtocolsHandlerEvent<I, TOutboundOpenInfo, TCustom>
|
||||
where
|
||||
F: FnOnce(TConnectionUpgrade) -> I,
|
||||
{
|
||||
match self {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: protocol.map_upgrade(map),
|
||||
info,
|
||||
}
|
||||
}
|
||||
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
|
||||
}
|
||||
}
|
||||
|
||||
/// If this is a `Custom` event, maps the content to something else.
|
||||
#[inline]
|
||||
pub fn map_custom<F, I>(
|
||||
self,
|
||||
map: F,
|
||||
) -> ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, I>
|
||||
where
|
||||
F: FnOnce(TCustom) -> I,
|
||||
{
|
||||
match self {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info }
|
||||
}
|
||||
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(map(val)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Error that can happen on an outbound substream opening attempt.
|
||||
#[derive(Debug)]
|
||||
pub enum ProtocolsHandlerUpgrErr<TUpgrErr> {
|
||||
/// The opening attempt timed out before the negotiation was fully completed.
|
||||
Timeout,
|
||||
/// There was an error in the timer used.
|
||||
Timer,
|
||||
/// Error while upgrading the substream to the protocol we want.
|
||||
Upgrade(UpgradeError<TUpgrErr>),
|
||||
}
|
||||
|
||||
impl<TUpgrErr> fmt::Display for ProtocolsHandlerUpgrErr<TUpgrErr>
|
||||
where
|
||||
TUpgrErr: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ProtocolsHandlerUpgrErr::Timeout => {
|
||||
write!(f, "Timeout error while opening a substream")
|
||||
},
|
||||
ProtocolsHandlerUpgrErr::Timer => {
|
||||
write!(f, "Timer error while opening a substream")
|
||||
},
|
||||
ProtocolsHandlerUpgrErr::Upgrade(err) => write!(f, "{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TUpgrErr> error::Error for ProtocolsHandlerUpgrErr<TUpgrErr>
|
||||
where
|
||||
TUpgrErr: error::Error + 'static
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||
match self {
|
||||
ProtocolsHandlerUpgrErr::Timeout => None,
|
||||
ProtocolsHandlerUpgrErr::Timer => None,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prototype for a `ProtocolsHandler`.
|
||||
pub trait IntoProtocolsHandler {
|
||||
/// The protocols handler.
|
||||
type Handler: ProtocolsHandler;
|
||||
|
||||
/// Builds the protocols handler.
|
||||
///
|
||||
/// The `PeerId` is the id of the node the handler is going to handle.
|
||||
fn into_handler(self, remote_peer_id: &PeerId, connected_point: &ConnectedPoint) -> Self::Handler;
|
||||
|
||||
/// Return the handler's inbound protocol.
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol;
|
||||
|
||||
/// Builds an implementation of `IntoProtocolsHandler` that handles both this protocol and the
|
||||
/// other one together.
|
||||
fn select<TProto2>(self, other: TProto2) -> IntoProtocolsHandlerSelect<Self, TProto2>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
IntoProtocolsHandlerSelect::new(self, other)
|
||||
}
|
||||
|
||||
/// Creates a builder that will allow creating a `NodeHandler` that handles this protocol
|
||||
/// exclusively.
|
||||
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
NodeHandlerWrapperBuilder::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoProtocolsHandler for T
|
||||
where T: ProtocolsHandler
|
||||
{
|
||||
type Handler = Self;
|
||||
|
||||
fn into_handler(self, _: &PeerId, _: &ConnectedPoint) -> Self {
|
||||
self
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
self.listen_protocol().into_upgrade()
|
||||
}
|
||||
}
|
||||
|
||||
/// How long the connection should be kept alive.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum KeepAlive {
|
||||
/// If nothing new happens, the connection should be closed at the given `Instant`.
|
||||
Until(Instant),
|
||||
/// Keep the connection alive.
|
||||
Yes,
|
||||
/// Close the connection as soon as possible.
|
||||
No,
|
||||
}
|
||||
|
||||
impl KeepAlive {
|
||||
/// Returns true for `Yes`, false otherwise.
|
||||
pub fn is_yes(&self) -> bool {
|
||||
match *self {
|
||||
KeepAlive::Yes => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for KeepAlive {
|
||||
fn partial_cmp(&self, other: &KeepAlive) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for KeepAlive {
|
||||
fn cmp(&self, other: &KeepAlive) -> Ordering {
|
||||
use self::KeepAlive::*;
|
||||
|
||||
match (self, other) {
|
||||
(No, No) | (Yes, Yes) => Ordering::Equal,
|
||||
(No, _) | (_, Yes) => Ordering::Less,
|
||||
(_, No) | (Yes, _) => Ordering::Greater,
|
||||
(Until(t1), Until(t2)) => t1.cmp(t2),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,323 +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::{
|
||||
PeerId,
|
||||
nodes::collection::ConnectionInfo,
|
||||
nodes::handled_node::{NodeHandler, NodeHandlerEndpoint, NodeHandlerEvent},
|
||||
nodes::handled_node_tasks::IntoNodeHandler,
|
||||
nodes::raw_swarm::ConnectedPoint,
|
||||
protocols_handler::{KeepAlive, ProtocolsHandler, IntoProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
|
||||
upgrade::{
|
||||
self,
|
||||
InboundUpgradeApply,
|
||||
OutboundUpgradeApply,
|
||||
}
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::{error, fmt, time::Duration};
|
||||
use wasm_timer::{Delay, Timeout};
|
||||
|
||||
/// Prototype for a `NodeHandlerWrapper`.
|
||||
pub struct NodeHandlerWrapperBuilder<TIntoProtoHandler> {
|
||||
/// The underlying handler.
|
||||
handler: TIntoProtoHandler,
|
||||
}
|
||||
|
||||
impl<TIntoProtoHandler> NodeHandlerWrapperBuilder<TIntoProtoHandler>
|
||||
where
|
||||
TIntoProtoHandler: IntoProtocolsHandler
|
||||
{
|
||||
/// Builds a `NodeHandlerWrapperBuilder`.
|
||||
#[inline]
|
||||
pub(crate) fn new(handler: TIntoProtoHandler) -> Self {
|
||||
NodeHandlerWrapperBuilder {
|
||||
handler,
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the `NodeHandlerWrapper`.
|
||||
#[deprecated(note = "Pass the NodeHandlerWrapperBuilder directly")]
|
||||
#[inline]
|
||||
pub fn build(self) -> NodeHandlerWrapper<TIntoProtoHandler>
|
||||
where TIntoProtoHandler: ProtocolsHandler
|
||||
{
|
||||
NodeHandlerWrapper {
|
||||
handler: self.handler,
|
||||
negotiating_in: Vec::new(),
|
||||
negotiating_out: Vec::new(),
|
||||
queued_dial_upgrades: Vec::new(),
|
||||
unique_dial_upgrade_id: 0,
|
||||
shutdown: Shutdown::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TIntoProtoHandler, TProtoHandler, TConnInfo> IntoNodeHandler<(TConnInfo, ConnectedPoint)>
|
||||
for NodeHandlerWrapperBuilder<TIntoProtoHandler>
|
||||
where
|
||||
TIntoProtoHandler: IntoProtocolsHandler<Handler = TProtoHandler>,
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
TConnInfo: ConnectionInfo<PeerId = PeerId>,
|
||||
{
|
||||
type Handler = NodeHandlerWrapper<TIntoProtoHandler::Handler>;
|
||||
|
||||
fn into_handler(self, remote_info: &(TConnInfo, ConnectedPoint)) -> Self::Handler {
|
||||
NodeHandlerWrapper {
|
||||
handler: self.handler.into_handler(&remote_info.0.peer_id(), &remote_info.1),
|
||||
negotiating_in: Vec::new(),
|
||||
negotiating_out: Vec::new(),
|
||||
queued_dial_upgrades: Vec::new(),
|
||||
unique_dial_upgrade_id: 0,
|
||||
shutdown: Shutdown::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps around an implementation of `ProtocolsHandler`, and implements `NodeHandler`.
|
||||
// TODO: add a caching system for protocols that are supported or not
|
||||
pub struct NodeHandlerWrapper<TProtoHandler>
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
{
|
||||
/// The underlying handler.
|
||||
handler: TProtoHandler,
|
||||
/// Futures that upgrade incoming substreams.
|
||||
negotiating_in:
|
||||
Vec<Timeout<InboundUpgradeApply<TProtoHandler::Substream, TProtoHandler::InboundProtocol>>>,
|
||||
/// Futures that upgrade outgoing substreams. The first element of the tuple is the userdata
|
||||
/// to pass back once successfully opened.
|
||||
negotiating_out: Vec<(
|
||||
TProtoHandler::OutboundOpenInfo,
|
||||
Timeout<OutboundUpgradeApply<TProtoHandler::Substream, TProtoHandler::OutboundProtocol>>,
|
||||
)>,
|
||||
/// For each outbound substream request, how to upgrade it. The first element of the tuple
|
||||
/// is the unique identifier (see `unique_dial_upgrade_id`).
|
||||
queued_dial_upgrades: Vec<(u64, TProtoHandler::OutboundProtocol)>,
|
||||
/// Unique identifier assigned to each queued dial upgrade.
|
||||
unique_dial_upgrade_id: u64,
|
||||
/// The currently planned connection & handler shutdown.
|
||||
shutdown: Shutdown,
|
||||
}
|
||||
|
||||
/// The options for a planned connection & handler shutdown.
|
||||
///
|
||||
/// A shutdown is planned anew based on the the return value of
|
||||
/// [`ProtocolsHandler::connection_keep_alive`] of the underlying handler
|
||||
/// after every invocation of [`ProtocolsHandler::poll`].
|
||||
///
|
||||
/// A planned shutdown is always postponed for as long as there are ingoing
|
||||
/// or outgoing substreams being negotiated, i.e. it is a graceful, "idle"
|
||||
/// shutdown.
|
||||
enum Shutdown {
|
||||
/// No shutdown is planned.
|
||||
None,
|
||||
/// A shut down is planned as soon as possible.
|
||||
Asap,
|
||||
/// A shut down is planned for when a `Delay` has elapsed.
|
||||
Later(Delay)
|
||||
}
|
||||
|
||||
/// Error generated by the `NodeHandlerWrapper`.
|
||||
#[derive(Debug)]
|
||||
pub enum NodeHandlerWrapperError<TErr> {
|
||||
/// Error generated by the handler.
|
||||
Handler(TErr),
|
||||
/// The connection has been deemed useless and has been closed.
|
||||
UselessTimeout,
|
||||
}
|
||||
|
||||
impl<TErr> From<TErr> for NodeHandlerWrapperError<TErr> {
|
||||
fn from(err: TErr) -> NodeHandlerWrapperError<TErr> {
|
||||
NodeHandlerWrapperError::Handler(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl<TErr> fmt::Display for NodeHandlerWrapperError<TErr>
|
||||
where
|
||||
TErr: fmt::Display
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
NodeHandlerWrapperError::Handler(err) => write!(f, "{}", err),
|
||||
NodeHandlerWrapperError::UselessTimeout =>
|
||||
write!(f, "Node has been closed due to inactivity"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TErr> error::Error for NodeHandlerWrapperError<TErr>
|
||||
where
|
||||
TErr: error::Error + 'static
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||
match self {
|
||||
NodeHandlerWrapperError::Handler(err) => Some(err),
|
||||
NodeHandlerWrapperError::UselessTimeout => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TProtoHandler> NodeHandler for NodeHandlerWrapper<TProtoHandler>
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
{
|
||||
type InEvent = TProtoHandler::InEvent;
|
||||
type OutEvent = TProtoHandler::OutEvent;
|
||||
type Error = NodeHandlerWrapperError<TProtoHandler::Error>;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
// The first element of the tuple is the unique upgrade identifier
|
||||
// (see `unique_dial_upgrade_id`).
|
||||
type OutboundOpenInfo = (u64, TProtoHandler::OutboundOpenInfo, Duration);
|
||||
|
||||
fn inject_substream(
|
||||
&mut self,
|
||||
substream: Self::Substream,
|
||||
endpoint: NodeHandlerEndpoint<Self::OutboundOpenInfo>,
|
||||
) {
|
||||
match endpoint {
|
||||
NodeHandlerEndpoint::Listener => {
|
||||
let protocol = self.handler.listen_protocol();
|
||||
let timeout = protocol.timeout().clone();
|
||||
let upgrade = upgrade::apply_inbound(substream, protocol.into_upgrade());
|
||||
let with_timeout = Timeout::new(upgrade, timeout);
|
||||
self.negotiating_in.push(with_timeout);
|
||||
}
|
||||
NodeHandlerEndpoint::Dialer((upgrade_id, user_data, timeout)) => {
|
||||
let pos = match self
|
||||
.queued_dial_upgrades
|
||||
.iter()
|
||||
.position(|(id, _)| id == &upgrade_id)
|
||||
{
|
||||
Some(p) => p,
|
||||
None => {
|
||||
debug_assert!(false, "Received an upgrade with an invalid upgrade ID");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let (_, proto_upgrade) = self.queued_dial_upgrades.remove(pos);
|
||||
let upgrade = upgrade::apply_outbound(substream, proto_upgrade);
|
||||
let with_timeout = Timeout::new(upgrade, timeout);
|
||||
self.negotiating_out.push((user_data, with_timeout));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, event: Self::InEvent) {
|
||||
self.handler.inject_event(event);
|
||||
}
|
||||
|
||||
fn poll(&mut self) -> Poll<NodeHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error> {
|
||||
// Continue negotiation of newly-opened substreams on the listening side.
|
||||
// We remove each element from `negotiating_in` one by one and add them back if not ready.
|
||||
for n in (0..self.negotiating_in.len()).rev() {
|
||||
let mut in_progress = self.negotiating_in.swap_remove(n);
|
||||
match in_progress.poll() {
|
||||
Ok(Async::Ready(upgrade)) =>
|
||||
self.handler.inject_fully_negotiated_inbound(upgrade),
|
||||
Ok(Async::NotReady) => self.negotiating_in.push(in_progress),
|
||||
// TODO: return a diagnostic event?
|
||||
Err(_err) => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Continue negotiation of newly-opened substreams.
|
||||
// We remove each element from `negotiating_out` one by one and add them back if not ready.
|
||||
for n in (0..self.negotiating_out.len()).rev() {
|
||||
let (upgr_info, mut in_progress) = self.negotiating_out.swap_remove(n);
|
||||
match in_progress.poll() {
|
||||
Ok(Async::Ready(upgrade)) => {
|
||||
self.handler.inject_fully_negotiated_outbound(upgrade, upgr_info);
|
||||
}
|
||||
Ok(Async::NotReady) => {
|
||||
self.negotiating_out.push((upgr_info, in_progress));
|
||||
}
|
||||
Err(err) => {
|
||||
let err = if err.is_elapsed() {
|
||||
ProtocolsHandlerUpgrErr::Timeout
|
||||
} else if err.is_timer() {
|
||||
ProtocolsHandlerUpgrErr::Timer
|
||||
} else {
|
||||
debug_assert!(err.is_inner());
|
||||
let err = err.into_inner().expect("Timeout error is one of {elapsed, \
|
||||
timer, inner}; is_elapsed and is_timer are both false; error is \
|
||||
inner; QED");
|
||||
ProtocolsHandlerUpgrErr::Upgrade(err)
|
||||
};
|
||||
|
||||
self.handler.inject_dial_upgrade_error(upgr_info, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Poll the handler at the end so that we see the consequences of the method
|
||||
// calls on `self.handler`.
|
||||
let poll_result = self.handler.poll()?;
|
||||
|
||||
// Ask the handler whether it wants the connection (and the handler itself)
|
||||
// to be kept alive, which determines the planned shutdown, if any.
|
||||
match (&mut self.shutdown, self.handler.connection_keep_alive()) {
|
||||
(Shutdown::Later(d), KeepAlive::Until(t)) =>
|
||||
if d.deadline() != t {
|
||||
d.reset(t)
|
||||
},
|
||||
(_, KeepAlive::Until(t)) => self.shutdown = Shutdown::Later(Delay::new(t)),
|
||||
(_, KeepAlive::No) => self.shutdown = Shutdown::Asap,
|
||||
(_, KeepAlive::Yes) => self.shutdown = Shutdown::None
|
||||
};
|
||||
|
||||
match poll_result {
|
||||
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
||||
return Ok(Async::Ready(NodeHandlerEvent::Custom(event)));
|
||||
}
|
||||
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol,
|
||||
info,
|
||||
}) => {
|
||||
let id = self.unique_dial_upgrade_id;
|
||||
let timeout = protocol.timeout().clone();
|
||||
self.unique_dial_upgrade_id += 1;
|
||||
self.queued_dial_upgrades.push((id, protocol.into_upgrade()));
|
||||
return Ok(Async::Ready(
|
||||
NodeHandlerEvent::OutboundSubstreamRequest((id, info, timeout)),
|
||||
));
|
||||
}
|
||||
Async::NotReady => (),
|
||||
};
|
||||
|
||||
// Check if the connection (and handler) should be shut down.
|
||||
// As long as we're still negotiating substreams, shutdown is always postponed.
|
||||
if self.negotiating_in.is_empty() && self.negotiating_out.is_empty() {
|
||||
match self.shutdown {
|
||||
Shutdown::None => {},
|
||||
Shutdown::Asap => return Err(NodeHandlerWrapperError::UselessTimeout),
|
||||
Shutdown::Later(ref mut delay) => match delay.poll() {
|
||||
Ok(Async::Ready(_)) | Err(_) =>
|
||||
return Err(NodeHandlerWrapperError::UselessTimeout),
|
||||
Ok(Async::NotReady) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
@ -1,240 +0,0 @@
|
||||
// Copyright 2019 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::protocols_handler::{
|
||||
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr,
|
||||
SubstreamProtocol
|
||||
};
|
||||
use crate::upgrade::{InboundUpgrade, OutboundUpgrade};
|
||||
use futures::prelude::*;
|
||||
use smallvec::SmallVec;
|
||||
use std::{error, marker::PhantomData, time::Duration};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that opens a new substream for each individual message.
|
||||
///
|
||||
/// This struct is meant to be a helper for other implementations to use.
|
||||
// TODO: Debug
|
||||
pub struct OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<TSubstream>,
|
||||
{
|
||||
/// The upgrade for inbound substreams.
|
||||
listen_protocol: SubstreamProtocol<TInProto>,
|
||||
/// If `Some`, something bad happened and we should shut down the handler with an error.
|
||||
pending_error:
|
||||
Option<ProtocolsHandlerUpgrErr<<TOutProto as OutboundUpgrade<TSubstream>>::Error>>,
|
||||
/// Queue of events to produce in `poll()`.
|
||||
events_out: SmallVec<[TOutEvent; 4]>,
|
||||
/// Queue of outbound substreams to open.
|
||||
dial_queue: SmallVec<[TOutProto; 4]>,
|
||||
/// Current number of concurrent outbound substreams being opened.
|
||||
dial_negotiated: u32,
|
||||
/// Maximum number of concurrent outbound substreams being opened. Value is never modified.
|
||||
max_dial_negotiated: u32,
|
||||
/// Value to return from `connection_keep_alive`.
|
||||
keep_alive: KeepAlive,
|
||||
/// After the given duration has elapsed, an inactive connection will shutdown.
|
||||
inactive_timeout: Duration,
|
||||
/// Pin the `TSubstream` generic.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<TSubstream>,
|
||||
{
|
||||
/// Creates a `OneShotHandler`.
|
||||
#[inline]
|
||||
pub fn new(
|
||||
listen_protocol: SubstreamProtocol<TInProto>,
|
||||
inactive_timeout: Duration
|
||||
) -> Self {
|
||||
OneShotHandler {
|
||||
listen_protocol,
|
||||
pending_error: None,
|
||||
events_out: SmallVec::new(),
|
||||
dial_queue: SmallVec::new(),
|
||||
dial_negotiated: 0,
|
||||
max_dial_negotiated: 8,
|
||||
keep_alive: KeepAlive::Yes,
|
||||
inactive_timeout,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of pending requests.
|
||||
#[inline]
|
||||
pub fn pending_requests(&self) -> u32 {
|
||||
self.dial_negotiated + self.dial_queue.len() as u32
|
||||
}
|
||||
|
||||
/// Returns a reference to the listen protocol configuration.
|
||||
///
|
||||
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
|
||||
/// > substreams, not the ones already being negotiated.
|
||||
#[inline]
|
||||
pub fn listen_protocol_ref(&self) -> &SubstreamProtocol<TInProto> {
|
||||
&self.listen_protocol
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the listen protocol configuration.
|
||||
///
|
||||
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
|
||||
/// > substreams, not the ones already being negotiated.
|
||||
#[inline]
|
||||
pub fn listen_protocol_mut(&mut self) -> &mut SubstreamProtocol<TInProto> {
|
||||
&mut self.listen_protocol
|
||||
}
|
||||
|
||||
/// Opens an outbound substream with `upgrade`.
|
||||
#[inline]
|
||||
pub fn send_request(&mut self, upgrade: TOutProto) {
|
||||
self.keep_alive = KeepAlive::Yes;
|
||||
self.dial_queue.push(upgrade);
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent> Default
|
||||
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<TSubstream>,
|
||||
TInProto: InboundUpgrade<TSubstream> + Default,
|
||||
{
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
OneShotHandler::new(SubstreamProtocol::new(Default::default()), Duration::from_secs(10))
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent> ProtocolsHandler
|
||||
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
TInProto: InboundUpgrade<TSubstream>,
|
||||
TOutProto: OutboundUpgrade<TSubstream>,
|
||||
TInProto::Output: Into<TOutEvent>,
|
||||
TOutProto::Output: Into<TOutEvent>,
|
||||
TOutProto::Error: error::Error + 'static,
|
||||
SubstreamProtocol<TInProto>: Clone,
|
||||
{
|
||||
type InEvent = TOutProto;
|
||||
type OutEvent = TOutEvent;
|
||||
type Error = ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error,
|
||||
>;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = TInProto;
|
||||
type OutboundProtocol = TOutProto;
|
||||
type OutboundOpenInfo = ();
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
self.listen_protocol.clone()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
out: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output,
|
||||
) {
|
||||
// If we're shutting down the connection for inactivity, reset the timeout.
|
||||
if !self.keep_alive.is_yes() {
|
||||
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
|
||||
}
|
||||
|
||||
self.events_out.push(out.into());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
out: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
||||
_: Self::OutboundOpenInfo,
|
||||
) {
|
||||
self.dial_negotiated -= 1;
|
||||
|
||||
if self.dial_negotiated == 0 && self.dial_queue.is_empty() {
|
||||
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
|
||||
}
|
||||
|
||||
self.events_out.push(out.into());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, event: Self::InEvent) {
|
||||
self.send_request(event);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
_: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error,
|
||||
>,
|
||||
) {
|
||||
if self.pending_error.is_none() {
|
||||
self.pending_error = Some(error);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
self.keep_alive
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Self::Error,
|
||||
> {
|
||||
if let Some(err) = self.pending_error.take() {
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
if !self.events_out.is_empty() {
|
||||
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(
|
||||
self.events_out.remove(0),
|
||||
)));
|
||||
} else {
|
||||
self.events_out.shrink_to_fit();
|
||||
}
|
||||
|
||||
if !self.dial_queue.is_empty() {
|
||||
if self.dial_negotiated < self.max_dial_negotiated {
|
||||
self.dial_negotiated += 1;
|
||||
return Ok(Async::Ready(
|
||||
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(self.dial_queue.remove(0)),
|
||||
info: (),
|
||||
},
|
||||
));
|
||||
}
|
||||
} else {
|
||||
self.dial_queue.shrink_to_fit();
|
||||
}
|
||||
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
@ -1,247 +0,0 @@
|
||||
// Copyright 2019 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::{
|
||||
PeerId,
|
||||
either::EitherError,
|
||||
either::EitherOutput,
|
||||
protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
IntoProtocolsHandler,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
},
|
||||
nodes::raw_swarm::ConnectedPoint,
|
||||
upgrade::{
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
EitherUpgrade,
|
||||
SelectUpgrade,
|
||||
UpgradeError,
|
||||
}
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::cmp;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Implementation of `IntoProtocolsHandler` that combines two protocols into one.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IntoProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
/// The first protocol.
|
||||
proto1: TProto1,
|
||||
/// The second protocol.
|
||||
proto2: TProto2,
|
||||
}
|
||||
|
||||
impl<TProto1, TProto2> IntoProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
/// Builds a `IntoProtocolsHandlerSelect`.
|
||||
#[inline]
|
||||
pub(crate) fn new(proto1: TProto1, proto2: TProto2) -> Self {
|
||||
IntoProtocolsHandlerSelect {
|
||||
proto1,
|
||||
proto2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TProto1, TProto2, TSubstream> IntoProtocolsHandler for IntoProtocolsHandlerSelect<TProto1, TProto2>
|
||||
where
|
||||
TProto1: IntoProtocolsHandler,
|
||||
TProto2: IntoProtocolsHandler,
|
||||
TProto1::Handler: ProtocolsHandler<Substream = TSubstream>,
|
||||
TProto2::Handler: ProtocolsHandler<Substream = TSubstream>,
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
<TProto1::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<TSubstream>,
|
||||
<TProto2::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<TSubstream>,
|
||||
<TProto1::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<TSubstream>,
|
||||
<TProto2::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<TSubstream>
|
||||
{
|
||||
type Handler = ProtocolsHandlerSelect<TProto1::Handler, TProto2::Handler>;
|
||||
|
||||
fn into_handler(self, remote_peer_id: &PeerId, connected_point: &ConnectedPoint) -> Self::Handler {
|
||||
ProtocolsHandlerSelect {
|
||||
proto1: self.proto1.into_handler(remote_peer_id, connected_point),
|
||||
proto2: self.proto2.into_handler(remote_peer_id, connected_point),
|
||||
}
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
SelectUpgrade::new(self.proto1.inbound_protocol(), self.proto2.inbound_protocol())
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that combines two protocols into one.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
/// The first protocol.
|
||||
proto1: TProto1,
|
||||
/// The second protocol.
|
||||
proto2: TProto2,
|
||||
}
|
||||
|
||||
impl<TProto1, TProto2> ProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
/// Builds a `ProtocolsHandlerSelect`.
|
||||
#[inline]
|
||||
pub(crate) fn new(proto1: TProto1, proto2: TProto2) -> Self {
|
||||
ProtocolsHandlerSelect {
|
||||
proto1,
|
||||
proto2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TProto1, TProto2>
|
||||
ProtocolsHandler for ProtocolsHandlerSelect<TProto1, TProto2>
|
||||
where
|
||||
TProto1: ProtocolsHandler<Substream = TSubstream>,
|
||||
TProto2: ProtocolsHandler<Substream = TSubstream>,
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
TProto1::InboundProtocol: InboundUpgrade<TSubstream>,
|
||||
TProto2::InboundProtocol: InboundUpgrade<TSubstream>,
|
||||
TProto1::OutboundProtocol: OutboundUpgrade<TSubstream>,
|
||||
TProto2::OutboundProtocol: OutboundUpgrade<TSubstream>
|
||||
{
|
||||
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
|
||||
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
|
||||
type Error = EitherError<TProto1::Error, TProto2::Error>;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = SelectUpgrade<<TProto1 as ProtocolsHandler>::InboundProtocol, <TProto2 as ProtocolsHandler>::InboundProtocol>;
|
||||
type OutboundProtocol = EitherUpgrade<TProto1::OutboundProtocol, TProto2::OutboundProtocol>;
|
||||
type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>;
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
let proto1 = self.proto1.listen_protocol();
|
||||
let proto2 = self.proto2.listen_protocol();
|
||||
let timeout = std::cmp::max(proto1.timeout(), proto2.timeout()).clone();
|
||||
SubstreamProtocol::new(SelectUpgrade::new(proto1.into_upgrade(), proto2.into_upgrade()))
|
||||
.with_timeout(timeout)
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_outbound(&mut self, protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output, endpoint: Self::OutboundOpenInfo) {
|
||||
match (protocol, endpoint) {
|
||||
(EitherOutput::First(protocol), EitherOutput::First(info)) =>
|
||||
self.proto1.inject_fully_negotiated_outbound(protocol, info),
|
||||
(EitherOutput::Second(protocol), EitherOutput::Second(info)) =>
|
||||
self.proto2.inject_fully_negotiated_outbound(protocol, info),
|
||||
(EitherOutput::First(_), EitherOutput::Second(_)) =>
|
||||
panic!("wrong API usage: the protocol doesn't match the upgrade info"),
|
||||
(EitherOutput::Second(_), EitherOutput::First(_)) =>
|
||||
panic!("wrong API usage: the protocol doesn't match the upgrade info")
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_inbound(&mut self, protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output) {
|
||||
match protocol {
|
||||
EitherOutput::First(protocol) =>
|
||||
self.proto1.inject_fully_negotiated_inbound(protocol),
|
||||
EitherOutput::Second(protocol) =>
|
||||
self.proto2.inject_fully_negotiated_inbound(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_event(&mut self, event: Self::InEvent) {
|
||||
match event {
|
||||
EitherOutput::First(event) => self.proto1.inject_event(event),
|
||||
EitherOutput::Second(event) => self.proto2.inject_event(event),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
||||
match (info, error) {
|
||||
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timer) => {
|
||||
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
|
||||
},
|
||||
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timeout) => {
|
||||
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
|
||||
},
|
||||
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
|
||||
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
|
||||
},
|
||||
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(err)))) => {
|
||||
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
|
||||
},
|
||||
(EitherOutput::First(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(_)))) => {
|
||||
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
|
||||
},
|
||||
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timeout) => {
|
||||
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
|
||||
},
|
||||
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timer) => {
|
||||
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
|
||||
},
|
||||
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
|
||||
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
|
||||
},
|
||||
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(err)))) => {
|
||||
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
|
||||
},
|
||||
(EitherOutput::Second(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(_)))) => {
|
||||
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
cmp::max(self.proto1.connection_keep_alive(), self.proto2.connection_keep_alive())
|
||||
}
|
||||
|
||||
fn poll(&mut self) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>, Self::Error> {
|
||||
|
||||
match self.proto1.poll().map_err(EitherError::A)? {
|
||||
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
||||
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(EitherOutput::First(event))));
|
||||
},
|
||||
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol,
|
||||
info,
|
||||
}) => {
|
||||
return Ok(Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: protocol.map_upgrade(EitherUpgrade::A),
|
||||
info: EitherOutput::First(info),
|
||||
}));
|
||||
},
|
||||
Async::NotReady => ()
|
||||
};
|
||||
|
||||
match self.proto2.poll().map_err(EitherError::B)? {
|
||||
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
||||
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(EitherOutput::Second(event))));
|
||||
},
|
||||
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol,
|
||||
info,
|
||||
}) => {
|
||||
return Ok(Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: protocol.map_upgrade(EitherUpgrade::B),
|
||||
info: EitherOutput::Second(info),
|
||||
}));
|
||||
},
|
||||
Async::NotReady => ()
|
||||
};
|
||||
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
@ -1,51 +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.
|
||||
|
||||
//! High level manager of the network.
|
||||
//!
|
||||
//! A [`Swarm`] contains the state of the network as a whole. The entire behaviour of a
|
||||
//! libp2p network can be controlled through the `Swarm`.
|
||||
//!
|
||||
//! # Initializing a Swarm
|
||||
//!
|
||||
//! Creating a `Swarm` requires three things:
|
||||
//!
|
||||
//! 1. A network identity of the local node in form of a [`PeerId`].
|
||||
//! 2. An implementation of the [`Transport`] trait. This is the type that will be used in
|
||||
//! order to reach nodes on the network based on their address. See the [`transport`] module
|
||||
//! for more information.
|
||||
//! 3. An implementation of the [`NetworkBehaviour`] trait. This is a state machine that
|
||||
//! defines how the swarm should behave once it is connected to a node.
|
||||
//!
|
||||
//! # Network Behaviour
|
||||
//!
|
||||
//! The `NetworkBehaviour` trait is implemented on types that indicate to the swarm how it should
|
||||
//! behave. This includes which protocols are supported and which nodes to try to connect to.
|
||||
//!
|
||||
|
||||
mod behaviour;
|
||||
mod swarm;
|
||||
mod registry;
|
||||
|
||||
pub mod toggle;
|
||||
|
||||
pub use crate::nodes::raw_swarm::ConnectedPoint;
|
||||
pub use self::behaviour::{NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters};
|
||||
pub use self::swarm::{SwarmPollParameters, ExpandedSwarm, Swarm, SwarmBuilder};
|
@ -1,224 +0,0 @@
|
||||
// Copyright 2019 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::{
|
||||
Multiaddr, PeerId,
|
||||
nodes::raw_swarm::ConnectedPoint,
|
||||
protocols_handler::{IntoProtocolsHandler, ProtocolsHandler},
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::error;
|
||||
|
||||
/// A behaviour for the network. Allows customizing the swarm.
|
||||
///
|
||||
/// This trait has been designed to be composable. Multiple implementations can be combined into
|
||||
/// one that handles all the behaviours at once.
|
||||
pub trait NetworkBehaviour {
|
||||
/// Handler for all the protocols the network behaviour supports.
|
||||
type ProtocolsHandler: IntoProtocolsHandler;
|
||||
|
||||
/// Event generated by the `NetworkBehaviour` and that the swarm will report back.
|
||||
type OutEvent;
|
||||
|
||||
/// Creates a new `ProtocolsHandler` for a connection with a peer.
|
||||
///
|
||||
/// Every time an incoming connection is opened, and every time we start dialing a node, this
|
||||
/// method is called.
|
||||
///
|
||||
/// The returned object is a handler for that specific connection, and will be moved to a
|
||||
/// background task dedicated to that connection.
|
||||
///
|
||||
/// The network behaviour (ie. the implementation of this trait) and the handlers it has
|
||||
/// spawned (ie. the objects returned by `new_handler`) can communicate by passing messages.
|
||||
/// Messages sent from the handler to the behaviour are injected with `inject_node_event`, and
|
||||
/// the behaviour can send a message to the handler by making `poll` return `SendEvent`.
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler;
|
||||
|
||||
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
|
||||
/// reaching the peer.
|
||||
///
|
||||
/// The addresses will be tried in the order returned by this function, which means that they
|
||||
/// should be ordered by decreasing likelihood of reachability. In other words, the first
|
||||
/// address should be the most likely to be reachable.
|
||||
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
|
||||
|
||||
/// Indicates the behaviour that we connected to the node with the given peer id through the
|
||||
/// given endpoint.
|
||||
///
|
||||
/// This node now has a handler (as spawned by `new_handler`) running in the background.
|
||||
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint);
|
||||
|
||||
/// Indicates the behaviour that we disconnected from the node with the given peer id. The
|
||||
/// endpoint is the one we used to be connected to.
|
||||
///
|
||||
/// There is no handler running anymore for this node. Any event that has been sent to it may
|
||||
/// or may not have been processed by the handler.
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint);
|
||||
|
||||
/// Indicates the behaviour that we replace the connection from the node with another.
|
||||
///
|
||||
/// The handler that used to be dedicated to this node has been destroyed and replaced with a
|
||||
/// new one. Any event that has been sent to it may or may not have been processed.
|
||||
///
|
||||
/// The default implementation of this method calls `inject_disconnected` followed with
|
||||
/// `inject_connected`. This is a logically safe way to implement this behaviour. However, you
|
||||
/// may want to overwrite this method in the situations where this isn't appropriate.
|
||||
fn inject_replaced(&mut self, peer_id: PeerId, closed_endpoint: ConnectedPoint, new_endpoint: ConnectedPoint) {
|
||||
self.inject_disconnected(&peer_id, closed_endpoint);
|
||||
self.inject_connected(peer_id, new_endpoint);
|
||||
}
|
||||
|
||||
/// Informs the behaviour about an event generated by the handler dedicated to the peer identified by `peer_id`.
|
||||
/// for the behaviour.
|
||||
///
|
||||
/// The `peer_id` is guaranteed to be in a connected state. In other words, `inject_connected`
|
||||
/// has previously been called with this `PeerId`.
|
||||
fn inject_node_event(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
|
||||
);
|
||||
|
||||
/// Indicates to the behaviour that we tried to reach an address, but failed.
|
||||
///
|
||||
/// If we were trying to reach a specific node, its ID is passed as parameter. If this is the
|
||||
/// last address to attempt for the given node, then `inject_dial_failure` is called afterwards.
|
||||
fn inject_addr_reach_failure(&mut self, _peer_id: Option<&PeerId>, _addr: &Multiaddr, _error: &dyn error::Error) {
|
||||
}
|
||||
|
||||
/// Indicates to the behaviour that we tried to dial all the addresses known for a node, but
|
||||
/// failed.
|
||||
///
|
||||
/// The `peer_id` is guaranteed to be in a disconnected state. In other words,
|
||||
/// `inject_connected` has not been called, or `inject_disconnected` has been called since then.
|
||||
fn inject_dial_failure(&mut self, _peer_id: &PeerId) {
|
||||
}
|
||||
|
||||
/// Indicates to the behaviour that we have started listening on a new multiaddr.
|
||||
fn inject_new_listen_addr(&mut self, _addr: &Multiaddr) {
|
||||
}
|
||||
|
||||
/// Indicates to the behaviour that a new multiaddr we were listening on has expired,
|
||||
/// which means that we are no longer listening in it.
|
||||
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) {
|
||||
}
|
||||
|
||||
/// Indicates to the behaviour that we have discovered a new external address for us.
|
||||
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) {
|
||||
}
|
||||
|
||||
/// Polls for things that swarm should do.
|
||||
///
|
||||
/// This API mimics the API of the `Stream` trait. The method may register the current task in
|
||||
/// order to wake it up at a later point in time.
|
||||
fn poll(&mut self, params: &mut impl PollParameters)
|
||||
-> Async<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>;
|
||||
}
|
||||
|
||||
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
|
||||
pub trait PollParameters {
|
||||
/// Iterator returned by [`supported_protocols`].
|
||||
type SupportedProtocolsIter: ExactSizeIterator<Item = Vec<u8>>;
|
||||
/// Iterator returned by [`listened_addresses`].
|
||||
type ListenedAddressesIter: ExactSizeIterator<Item = Multiaddr>;
|
||||
/// Iterator returned by [`external_addresses`].
|
||||
type ExternalAddressesIter: ExactSizeIterator<Item = Multiaddr>;
|
||||
|
||||
/// Returns the list of protocol the behaviour supports when a remote negotiates a protocol on
|
||||
/// an inbound substream.
|
||||
///
|
||||
/// The iterator's elements are the ASCII names as reported on the wire.
|
||||
///
|
||||
/// Note that the list is computed once at initialization and never refreshed.
|
||||
fn supported_protocols(&self) -> Self::SupportedProtocolsIter;
|
||||
|
||||
/// Returns the list of the addresses we're listening on.
|
||||
fn listened_addresses(&self) -> Self::ListenedAddressesIter;
|
||||
|
||||
/// Returns the list of the addresses nodes can use to reach us.
|
||||
fn external_addresses(&self) -> Self::ExternalAddressesIter;
|
||||
|
||||
/// Returns the peer id of the local node.
|
||||
fn local_peer_id(&self) -> &PeerId;
|
||||
}
|
||||
|
||||
/// Used when deriving `NetworkBehaviour`. When deriving `NetworkBehaviour`, must be implemented
|
||||
/// for all the possible event types generated by the various fields.
|
||||
// TODO: document how the custom behaviour works and link this here
|
||||
pub trait NetworkBehaviourEventProcess<TEvent> {
|
||||
/// Called when one of the fields of the type you're deriving `NetworkBehaviour` on generates
|
||||
/// an event.
|
||||
fn inject_event(&mut self, event: TEvent);
|
||||
}
|
||||
|
||||
/// An action that a [`NetworkBehaviour`] can trigger in the [`Swarm`]
|
||||
/// in whose context it is executing.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
|
||||
/// Instructs the `Swarm` to return an event when it is being polled.
|
||||
GenerateEvent(TOutEvent),
|
||||
|
||||
/// Instructs the swarm to dial the given multiaddress, with no knowledge of the `PeerId` that
|
||||
/// may be reached.
|
||||
DialAddress {
|
||||
/// The address to dial.
|
||||
address: Multiaddr,
|
||||
},
|
||||
|
||||
/// Instructs the swarm to dial a known `PeerId`.
|
||||
///
|
||||
/// The `addresses_of_peer` method is called to determine which addresses to attempt to reach.
|
||||
///
|
||||
/// If we were already trying to dial this node, the addresses that are not yet in the queue of
|
||||
/// addresses to try are added back to this queue.
|
||||
///
|
||||
/// On success, [`NetworkBehaviour::inject_connected`] is invoked.
|
||||
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
|
||||
DialPeer {
|
||||
/// The peer to try reach.
|
||||
peer_id: PeerId,
|
||||
},
|
||||
|
||||
/// Instructs the `Swarm` to send a message to the handler dedicated to the connection with the peer.
|
||||
///
|
||||
/// If the `Swarm` is connected to the peer, the message is delivered to the remote's
|
||||
/// protocol handler. If there is no connection to the peer, the message is ignored.
|
||||
/// To ensure delivery, the `NetworkBehaviour` must keep track of connected peers.
|
||||
///
|
||||
/// Note that even if the peer is currently connected, connections can get closed
|
||||
/// at any time and thus the message may not reach its destination.
|
||||
SendEvent {
|
||||
/// The peer to which to send the message.
|
||||
peer_id: PeerId,
|
||||
/// The message to send.
|
||||
event: TInEvent,
|
||||
},
|
||||
|
||||
/// Informs the `Swarm` about a multi-address observed by a remote for
|
||||
/// the local node.
|
||||
///
|
||||
/// It is advisable to issue `ReportObservedAddr` actions at a fixed frequency
|
||||
/// per node. This way address information will be more accurate over time
|
||||
/// and individual outliers carry less weight.
|
||||
ReportObservedAddr {
|
||||
/// The observed address of the local node.
|
||||
address: Multiaddr,
|
||||
},
|
||||
}
|
@ -1,261 +0,0 @@
|
||||
// Copyright 2019 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::Multiaddr;
|
||||
use smallvec::SmallVec;
|
||||
use std::{collections::VecDeque, num::NonZeroUsize};
|
||||
|
||||
/// Hold a ranked collection of [`Multiaddr`] values.
|
||||
///
|
||||
/// Every address has an associated score and iterating over addresses will return them
|
||||
/// in order from highest to lowest. When reaching the limit, addresses with the lowest
|
||||
/// score will be dropped first.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Addresses {
|
||||
/// The ranked sequence of addresses.
|
||||
registry: SmallVec<[Record; 8]>,
|
||||
/// Number of historical reports. Similar to `reports.capacity()`.
|
||||
limit: NonZeroUsize,
|
||||
/// Queue of last reports. Every new report is added to the queue. If the queue reaches its
|
||||
/// capacity, we also pop the first element.
|
||||
reports: VecDeque<Multiaddr>,
|
||||
}
|
||||
|
||||
// An address record associates a score to a Multiaddr.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Record {
|
||||
score: u32,
|
||||
addr: Multiaddr
|
||||
}
|
||||
|
||||
impl Default for Addresses {
|
||||
fn default() -> Self {
|
||||
Addresses::new(NonZeroUsize::new(200).expect("200 > 0"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Addresses {
|
||||
/// Create a new address collection of bounded length.
|
||||
pub fn new(limit: NonZeroUsize) -> Self {
|
||||
Addresses {
|
||||
registry: SmallVec::new(),
|
||||
limit,
|
||||
reports: VecDeque::with_capacity(limit.get()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a [`Multiaddr`] to the collection.
|
||||
///
|
||||
/// Adding an existing address is interpreted as additional
|
||||
/// confirmation and thus increases its score.
|
||||
pub fn add(&mut self, a: Multiaddr) {
|
||||
|
||||
let oldest = if self.reports.len() == self.limit.get() {
|
||||
self.reports.pop_front()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(oldest) = oldest {
|
||||
if let Some(in_registry) = self.registry.iter_mut().find(|r| r.addr == oldest) {
|
||||
in_registry.score = in_registry.score.saturating_sub(1);
|
||||
isort(&mut self.registry);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove addresses that have a score of 0.
|
||||
while self.registry.last().map(|e| e.score == 0).unwrap_or(false) {
|
||||
self.registry.pop();
|
||||
}
|
||||
|
||||
self.reports.push_back(a.clone());
|
||||
|
||||
for r in &mut self.registry {
|
||||
if r.addr == a {
|
||||
r.score = r.score.saturating_add(1);
|
||||
isort(&mut self.registry);
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
let r = Record { score: 1, addr: a };
|
||||
self.registry.push(r)
|
||||
}
|
||||
|
||||
/// Return an iterator over all [`Multiaddr`] values.
|
||||
///
|
||||
/// The iteration is ordered by descending score.
|
||||
pub fn iter(&self) -> AddressIter<'_> {
|
||||
AddressIter { items: &self.registry, offset: 0 }
|
||||
}
|
||||
|
||||
/// Return an iterator over all [`Multiaddr`] values.
|
||||
///
|
||||
/// The iteration is ordered by descending score.
|
||||
pub fn into_iter(self) -> AddressIntoIter {
|
||||
AddressIntoIter { items: self.registry }
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over [`Multiaddr`] values.
|
||||
#[derive(Clone)]
|
||||
pub struct AddressIter<'a> {
|
||||
items: &'a [Record],
|
||||
offset: usize
|
||||
}
|
||||
|
||||
impl<'a> Iterator for AddressIter<'a> {
|
||||
type Item = &'a Multiaddr;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.offset == self.items.len() {
|
||||
return None
|
||||
}
|
||||
let item = &self.items[self.offset];
|
||||
self.offset += 1;
|
||||
Some(&item.addr)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let n = self.items.len() - self.offset;
|
||||
(n, Some(n))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ExactSizeIterator for AddressIter<'a> {}
|
||||
|
||||
/// An iterator over [`Multiaddr`] values.
|
||||
#[derive(Clone)]
|
||||
pub struct AddressIntoIter {
|
||||
items: SmallVec<[Record; 8]>,
|
||||
}
|
||||
|
||||
impl Iterator for AddressIntoIter {
|
||||
type Item = Multiaddr;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if !self.items.is_empty() {
|
||||
Some(self.items.remove(0).addr)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let n = self.items.len();
|
||||
(n, Some(n))
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for AddressIntoIter {}
|
||||
|
||||
// Reverse insertion sort.
|
||||
fn isort(xs: &mut [Record]) {
|
||||
for i in 1 .. xs.len() {
|
||||
for j in (1 ..= i).rev() {
|
||||
if xs[j].score <= xs[j - 1].score {
|
||||
break
|
||||
}
|
||||
xs.swap(j, j - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Multiaddr;
|
||||
use quickcheck::QuickCheck;
|
||||
use super::{isort, Addresses, Record};
|
||||
|
||||
#[test]
|
||||
fn isort_sorts() {
|
||||
fn property(xs: Vec<u32>) -> bool {
|
||||
let mut xs = xs.into_iter()
|
||||
.map(|s| Record { score: s, addr: Multiaddr::empty() })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
isort(&mut xs);
|
||||
|
||||
for i in 1 .. xs.len() {
|
||||
assert!(xs[i - 1].score >= xs[i].score)
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
QuickCheck::new().quickcheck(property as fn(Vec<u32>) -> bool)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_reports_disappear() {
|
||||
let mut addresses = Addresses::default();
|
||||
|
||||
// Add an address a single time.
|
||||
let single: Multiaddr = "/tcp/2108".parse().unwrap();
|
||||
addresses.add(single.clone());
|
||||
assert!(addresses.iter().find(|a| **a == single).is_some());
|
||||
|
||||
// Then fill `addresses` with random stuff.
|
||||
let other: Multiaddr = "/tcp/120".parse().unwrap();
|
||||
for _ in 0 .. 2000 {
|
||||
addresses.add(other.clone());
|
||||
}
|
||||
|
||||
// Check that `single` disappeared from the list.
|
||||
assert!(addresses.iter().find(|a| **a == single).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_score_equals_last_n_reports() {
|
||||
use multiaddr::Protocol;
|
||||
use quickcheck::{Arbitrary, Gen};
|
||||
use rand::Rng;
|
||||
use std::num::NonZeroUsize;
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
|
||||
struct Ma(Multiaddr);
|
||||
|
||||
impl Arbitrary for Ma {
|
||||
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
||||
Ma(Protocol::Tcp(g.gen::<u16>() % 16).into())
|
||||
}
|
||||
}
|
||||
|
||||
fn property(xs: Vec<Ma>, n: u8) -> bool {
|
||||
let n = std::cmp::max(n, 1);
|
||||
let mut addresses = Addresses::new(NonZeroUsize::new(usize::from(n)).unwrap());
|
||||
for Ma(a) in &xs {
|
||||
addresses.add(a.clone())
|
||||
}
|
||||
for r in &addresses.registry {
|
||||
let count = xs.iter()
|
||||
.rev()
|
||||
.take(usize::from(n))
|
||||
.filter(|Ma(x)| x == &r.addr)
|
||||
.count();
|
||||
if r.score as usize != count {
|
||||
return false
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
QuickCheck::new().quickcheck(property as fn(Vec<Ma>, u8) -> bool)
|
||||
}
|
||||
}
|
@ -1,544 +0,0 @@
|
||||
// Copyright 2019 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::{
|
||||
Transport, Multiaddr, PeerId, InboundUpgrade, OutboundUpgrade, UpgradeInfo, ProtocolName,
|
||||
muxing::StreamMuxer,
|
||||
nodes::{
|
||||
collection::ConnectionInfo,
|
||||
handled_node::NodeHandler,
|
||||
node::Substream,
|
||||
raw_swarm::{self, RawSwarm, RawSwarmEvent}
|
||||
},
|
||||
protocols_handler::{NodeHandlerWrapperBuilder, NodeHandlerWrapper, NodeHandlerWrapperError, IntoProtocolsHandler, ProtocolsHandler},
|
||||
swarm::{PollParameters, NetworkBehaviour, NetworkBehaviourAction, registry::{Addresses, AddressIntoIter}},
|
||||
transport::TransportError,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use smallvec::SmallVec;
|
||||
use std::{error, fmt, io, ops::{Deref, DerefMut}};
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Contains the state of the network, plus the way it should behave.
|
||||
pub type Swarm<TTransport, TBehaviour, TConnInfo = PeerId> = ExpandedSwarm<
|
||||
TTransport,
|
||||
TBehaviour,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
|
||||
<TBehaviour as NetworkBehaviour>::ProtocolsHandler,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error,
|
||||
TConnInfo,
|
||||
>;
|
||||
|
||||
/// Contains the state of the network, plus the way it should behave.
|
||||
pub struct ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo = PeerId>
|
||||
where
|
||||
TTransport: Transport,
|
||||
{
|
||||
raw_swarm: RawSwarm<
|
||||
TTransport,
|
||||
TInEvent,
|
||||
TOutEvent,
|
||||
NodeHandlerWrapperBuilder<THandler>,
|
||||
NodeHandlerWrapperError<THandlerErr>,
|
||||
TConnInfo,
|
||||
PeerId,
|
||||
>,
|
||||
|
||||
/// Handles which nodes to connect to and how to handle the events sent back by the protocol
|
||||
/// handlers.
|
||||
behaviour: TBehaviour,
|
||||
|
||||
/// List of protocols that the behaviour says it supports.
|
||||
supported_protocols: SmallVec<[Vec<u8>; 16]>,
|
||||
|
||||
/// List of multiaddresses we're listening on.
|
||||
listened_addrs: SmallVec<[Multiaddr; 8]>,
|
||||
|
||||
/// List of multiaddresses we're listening on, after account for external IP addresses and
|
||||
/// similar mechanisms.
|
||||
external_addrs: Addresses,
|
||||
|
||||
/// List of nodes for which we deny any incoming connection.
|
||||
banned_peers: HashSet<PeerId>,
|
||||
}
|
||||
|
||||
impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> Deref for
|
||||
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
||||
where
|
||||
TTransport: Transport,
|
||||
{
|
||||
type Target = TBehaviour;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.behaviour
|
||||
}
|
||||
}
|
||||
|
||||
impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> DerefMut for
|
||||
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
||||
where
|
||||
TTransport: Transport,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.behaviour
|
||||
}
|
||||
}
|
||||
|
||||
impl<TTransport, TBehaviour, TMuxer, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
||||
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
||||
where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
|
||||
TMuxer: StreamMuxer + Send + Sync + 'static,
|
||||
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
||||
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
||||
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
||||
TTransport::Error: Send + 'static,
|
||||
TTransport::Listener: Send + 'static,
|
||||
TTransport::ListenerUpgrade: Send + 'static,
|
||||
TTransport::Dial: Send + 'static,
|
||||
THandlerErr: error::Error,
|
||||
THandler: IntoProtocolsHandler + Send + 'static,
|
||||
<THandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEvent, OutEvent = TOutEvent, Substream = Substream<TMuxer>, Error = THandlerErr> + Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<NodeHandlerWrapper<<THandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
||||
{
|
||||
/// Builds a new `Swarm`.
|
||||
pub fn new(transport: TTransport, behaviour: TBehaviour, local_peer_id: PeerId) -> Self {
|
||||
SwarmBuilder::new(transport, behaviour, local_peer_id)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Returns the transport passed when building this object.
|
||||
pub fn transport(me: &Self) -> &TTransport {
|
||||
me.raw_swarm.transport()
|
||||
}
|
||||
|
||||
/// Starts listening on the given address.
|
||||
///
|
||||
/// Returns an error if the address is not supported.
|
||||
pub fn listen_on(me: &mut Self, addr: Multiaddr) -> Result<(), TransportError<TTransport::Error>> {
|
||||
me.raw_swarm.listen_on(addr)
|
||||
}
|
||||
|
||||
/// Tries to dial the given address.
|
||||
///
|
||||
/// Returns an error if the address is not supported.
|
||||
pub fn dial_addr(me: &mut Self, addr: Multiaddr) -> Result<(), TransportError<TTransport::Error>> {
|
||||
let handler = me.behaviour.new_handler();
|
||||
me.raw_swarm.dial(addr, handler.into_node_handler_builder())
|
||||
}
|
||||
|
||||
/// Tries to reach the given peer using the elements in the topology.
|
||||
///
|
||||
/// Has no effect if we are already connected to that peer, or if no address is known for the
|
||||
/// peer.
|
||||
pub fn dial(me: &mut Self, peer_id: PeerId) {
|
||||
let addrs = me.behaviour.addresses_of_peer(&peer_id);
|
||||
match me.raw_swarm.peer(peer_id.clone()) {
|
||||
raw_swarm::Peer::NotConnected(peer) => {
|
||||
let handler = me.behaviour.new_handler().into_node_handler_builder();
|
||||
if peer.connect_iter(addrs, handler).is_err() {
|
||||
me.behaviour.inject_dial_failure(&peer_id);
|
||||
}
|
||||
},
|
||||
raw_swarm::Peer::PendingConnect(mut peer) => {
|
||||
peer.append_multiaddr_attempts(addrs)
|
||||
},
|
||||
raw_swarm::Peer::Connected(_) | raw_swarm::Peer::LocalNode => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an iterator that produces the list of addresses we're listening on.
|
||||
pub fn listeners(me: &Self) -> impl Iterator<Item = &Multiaddr> {
|
||||
me.raw_swarm.listen_addrs()
|
||||
}
|
||||
|
||||
/// Returns an iterator that produces the list of addresses that other nodes can use to reach
|
||||
/// us.
|
||||
pub fn external_addresses(me: &Self) -> impl Iterator<Item = &Multiaddr> {
|
||||
me.external_addrs.iter()
|
||||
}
|
||||
|
||||
/// Returns the peer ID of the swarm passed as parameter.
|
||||
pub fn local_peer_id(me: &Self) -> &PeerId {
|
||||
&me.raw_swarm.local_peer_id()
|
||||
}
|
||||
|
||||
/// Adds an external address.
|
||||
///
|
||||
/// An external address is an address we are listening on but that accounts for things such as
|
||||
/// NAT traversal.
|
||||
pub fn add_external_address(me: &mut Self, addr: Multiaddr) {
|
||||
me.external_addrs.add(addr)
|
||||
}
|
||||
|
||||
/// Returns the connection info of a node, or `None` if we're not connected to it.
|
||||
// TODO: should take &self instead of &mut self, but the API in raw_swarm requires &mut
|
||||
pub fn connection_info(me: &mut Self, peer_id: &PeerId) -> Option<TConnInfo> {
|
||||
if let Some(mut n) = me.raw_swarm.peer(peer_id.clone()).into_connected() {
|
||||
Some(n.connection_info().clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Bans a peer by its peer ID.
|
||||
///
|
||||
/// Any incoming connection and any dialing attempt will immediately be rejected.
|
||||
/// This function has no effect is the peer is already banned.
|
||||
pub fn ban_peer_id(me: &mut Self, peer_id: PeerId) {
|
||||
me.banned_peers.insert(peer_id.clone());
|
||||
if let Some(c) = me.raw_swarm.peer(peer_id).into_connected() {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// Unbans a peer.
|
||||
pub fn unban_peer_id(me: &mut Self, peer_id: PeerId) {
|
||||
me.banned_peers.remove(&peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
impl<TTransport, TBehaviour, TMuxer, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo> Stream for
|
||||
ExpandedSwarm<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr, TConnInfo>
|
||||
where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
|
||||
TMuxer: StreamMuxer + Send + Sync + 'static,
|
||||
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
||||
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
||||
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
||||
TTransport::Error: Send + 'static,
|
||||
TTransport::Listener: Send + 'static,
|
||||
TTransport::ListenerUpgrade: Send + 'static,
|
||||
TTransport::Dial: Send + 'static,
|
||||
THandlerErr: error::Error,
|
||||
THandler: IntoProtocolsHandler + Send + 'static,
|
||||
<THandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEvent, OutEvent = TOutEvent, Substream = Substream<TMuxer>, Error = THandlerErr> + Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<THandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<NodeHandlerWrapper<<THandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
||||
{
|
||||
type Item = TBehaviour::OutEvent;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, io::Error> {
|
||||
loop {
|
||||
let mut raw_swarm_not_ready = false;
|
||||
|
||||
match self.raw_swarm.poll() {
|
||||
Async::NotReady => raw_swarm_not_ready = true,
|
||||
Async::Ready(RawSwarmEvent::NodeEvent { conn_info, event }) => {
|
||||
self.behaviour.inject_node_event(conn_info.peer_id().clone(), event);
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::Connected { conn_info, endpoint }) => {
|
||||
if self.banned_peers.contains(conn_info.peer_id()) {
|
||||
self.raw_swarm.peer(conn_info.peer_id().clone())
|
||||
.into_connected()
|
||||
.expect("the RawSwarm just notified us that we were connected; QED")
|
||||
.close();
|
||||
} else {
|
||||
self.behaviour.inject_connected(conn_info.peer_id().clone(), endpoint);
|
||||
}
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::NodeClosed { conn_info, endpoint, .. }) => {
|
||||
self.behaviour.inject_disconnected(conn_info.peer_id(), endpoint);
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::Replaced { new_info, closed_endpoint, endpoint, .. }) => {
|
||||
self.behaviour.inject_replaced(new_info.peer_id().clone(), closed_endpoint, endpoint);
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::IncomingConnection(incoming)) => {
|
||||
let handler = self.behaviour.new_handler();
|
||||
incoming.accept(handler.into_node_handler_builder());
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::NewListenerAddress { listen_addr }) => {
|
||||
if !self.listened_addrs.contains(&listen_addr) {
|
||||
self.listened_addrs.push(listen_addr.clone())
|
||||
}
|
||||
self.behaviour.inject_new_listen_addr(&listen_addr);
|
||||
}
|
||||
Async::Ready(RawSwarmEvent::ExpiredListenerAddress { listen_addr }) => {
|
||||
self.listened_addrs.retain(|a| a != &listen_addr);
|
||||
self.behaviour.inject_expired_listen_addr(&listen_addr);
|
||||
}
|
||||
Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::DialError { peer_id, multiaddr, error, new_state }) => {
|
||||
self.behaviour.inject_addr_reach_failure(Some(&peer_id), &multiaddr, &error);
|
||||
if let raw_swarm::PeerState::NotConnected = new_state {
|
||||
self.behaviour.inject_dial_failure(&peer_id);
|
||||
}
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::UnknownPeerDialError { multiaddr, error, .. }) => {
|
||||
self.behaviour.inject_addr_reach_failure(None, &multiaddr, &error);
|
||||
},
|
||||
}
|
||||
|
||||
let behaviour_poll = {
|
||||
let mut parameters = SwarmPollParameters {
|
||||
local_peer_id: &mut self.raw_swarm.local_peer_id(),
|
||||
supported_protocols: &self.supported_protocols,
|
||||
listened_addrs: &self.listened_addrs,
|
||||
external_addrs: &self.external_addrs
|
||||
};
|
||||
self.behaviour.poll(&mut parameters)
|
||||
};
|
||||
|
||||
match behaviour_poll {
|
||||
Async::NotReady if raw_swarm_not_ready => return Ok(Async::NotReady),
|
||||
Async::NotReady => (),
|
||||
Async::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
|
||||
return Ok(Async::Ready(Some(event)))
|
||||
},
|
||||
Async::Ready(NetworkBehaviourAction::DialAddress { address }) => {
|
||||
let _ = ExpandedSwarm::dial_addr(self, address);
|
||||
},
|
||||
Async::Ready(NetworkBehaviourAction::DialPeer { peer_id }) => {
|
||||
if self.banned_peers.contains(&peer_id) {
|
||||
self.behaviour.inject_dial_failure(&peer_id);
|
||||
} else {
|
||||
ExpandedSwarm::dial(self, peer_id);
|
||||
}
|
||||
},
|
||||
Async::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) => {
|
||||
if let Some(mut peer) = self.raw_swarm.peer(peer_id).into_connected() {
|
||||
peer.send_event(event);
|
||||
}
|
||||
},
|
||||
Async::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) => {
|
||||
for addr in self.raw_swarm.nat_traversal(&address) {
|
||||
if self.external_addrs.iter().all(|a| *a != addr) {
|
||||
self.behaviour.inject_new_external_addr(&addr);
|
||||
}
|
||||
self.external_addrs.add(addr)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
|
||||
// TODO: #[derive(Debug)]
|
||||
pub struct SwarmPollParameters<'a> {
|
||||
local_peer_id: &'a PeerId,
|
||||
supported_protocols: &'a [Vec<u8>],
|
||||
listened_addrs: &'a [Multiaddr],
|
||||
external_addrs: &'a Addresses,
|
||||
}
|
||||
|
||||
impl<'a> PollParameters for SwarmPollParameters<'a> {
|
||||
type SupportedProtocolsIter = std::vec::IntoIter<Vec<u8>>;
|
||||
type ListenedAddressesIter = std::vec::IntoIter<Multiaddr>;
|
||||
type ExternalAddressesIter = AddressIntoIter;
|
||||
|
||||
fn supported_protocols(&self) -> Self::SupportedProtocolsIter {
|
||||
self.supported_protocols.to_vec().into_iter()
|
||||
}
|
||||
|
||||
fn listened_addresses(&self) -> Self::ListenedAddressesIter {
|
||||
self.listened_addrs.to_vec().into_iter()
|
||||
}
|
||||
|
||||
fn external_addresses(&self) -> Self::ExternalAddressesIter {
|
||||
self.external_addrs.clone().into_iter()
|
||||
}
|
||||
|
||||
fn local_peer_id(&self) -> &PeerId {
|
||||
self.local_peer_id
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SwarmBuilder<TTransport, TBehaviour> {
|
||||
incoming_limit: Option<u32>,
|
||||
local_peer_id: PeerId,
|
||||
transport: TTransport,
|
||||
behaviour: TBehaviour,
|
||||
}
|
||||
|
||||
impl<TTransport, TBehaviour, TMuxer, TConnInfo> SwarmBuilder<TTransport, TBehaviour>
|
||||
where TBehaviour: NetworkBehaviour,
|
||||
TMuxer: StreamMuxer + Send + Sync + 'static,
|
||||
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
|
||||
<TMuxer as StreamMuxer>::Substream: Send + 'static,
|
||||
TTransport: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
||||
TTransport::Error: Send + 'static,
|
||||
TTransport::Listener: Send + 'static,
|
||||
TTransport::ListenerUpgrade: Send + 'static,
|
||||
TTransport::Dial: Send + 'static,
|
||||
<TBehaviour as NetworkBehaviour>::ProtocolsHandler: Send + 'static,
|
||||
<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler: ProtocolsHandler<Substream = Substream<TMuxer>> + Send + 'static,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent: Send + 'static,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent: Send + 'static,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::Error: Send + 'static,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InboundProtocol as InboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Substream<TMuxer>> + Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::Info: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter: Send + 'static,
|
||||
<<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Future: Send + 'static,
|
||||
<<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutboundProtocol as OutboundUpgrade<Substream<TMuxer>>>::Error: Send + 'static,
|
||||
<NodeHandlerWrapper<<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler> as NodeHandler>::OutboundOpenInfo: Send + 'static, // TODO: shouldn't be necessary
|
||||
TConnInfo: ConnectionInfo<PeerId = PeerId> + fmt::Debug + Clone + Send + 'static,
|
||||
{
|
||||
pub fn new(transport: TTransport, behaviour: TBehaviour, local_peer_id: PeerId) -> Self {
|
||||
SwarmBuilder {
|
||||
incoming_limit: None,
|
||||
local_peer_id,
|
||||
transport,
|
||||
behaviour,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn incoming_limit(mut self, incoming_limit: Option<u32>) -> Self {
|
||||
self.incoming_limit = incoming_limit;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(mut self) -> Swarm<TTransport, TBehaviour, TConnInfo> {
|
||||
let supported_protocols = self.behaviour
|
||||
.new_handler()
|
||||
.inbound_protocol()
|
||||
.protocol_info()
|
||||
.into_iter()
|
||||
.map(|info| info.protocol_name().to_vec())
|
||||
.collect();
|
||||
|
||||
let raw_swarm = RawSwarm::new_with_incoming_limit(self.transport, self.local_peer_id, self.incoming_limit);
|
||||
|
||||
ExpandedSwarm {
|
||||
raw_swarm,
|
||||
behaviour: self.behaviour,
|
||||
supported_protocols,
|
||||
listened_addrs: SmallVec::new(),
|
||||
external_addrs: Addresses::default(),
|
||||
banned_peers: HashSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{identity, PeerId, PublicKey};
|
||||
use crate::protocols_handler::{DummyProtocolsHandler, ProtocolsHandler};
|
||||
use crate::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters, SwarmBuilder};
|
||||
use crate::tests::dummy_transport::DummyTransport;
|
||||
use futures::prelude::*;
|
||||
use multiaddr::Multiaddr;
|
||||
use std::marker::PhantomData;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use void::Void;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DummyBehaviour<TSubstream> {
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
trait TSubstream: AsyncRead + AsyncWrite {}
|
||||
|
||||
impl<TSubstream> NetworkBehaviour
|
||||
for DummyBehaviour<TSubstream>
|
||||
where TSubstream: AsyncRead + AsyncWrite
|
||||
{
|
||||
type ProtocolsHandler = DummyProtocolsHandler<TSubstream>;
|
||||
type OutEvent = Void;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
DummyProtocolsHandler::default()
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
Vec::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 impl PollParameters) ->
|
||||
Async<NetworkBehaviourAction<<Self::ProtocolsHandler as
|
||||
ProtocolsHandler>::InEvent, Self::OutEvent>>
|
||||
{
|
||||
Async::NotReady
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn get_random_id() -> PublicKey {
|
||||
identity::Keypair::generate_ed25519().public()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_swarm() {
|
||||
let id = get_random_id();
|
||||
let transport = DummyTransport::new();
|
||||
let behaviour = DummyBehaviour{marker: PhantomData};
|
||||
let swarm = SwarmBuilder::new(transport, behaviour, id.into())
|
||||
.incoming_limit(Some(4)).build();
|
||||
assert_eq!(swarm.raw_swarm.incoming_limit(), Some(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_swarm_with_max_listeners_none() {
|
||||
let id = get_random_id();
|
||||
let transport = DummyTransport::new();
|
||||
let behaviour = DummyBehaviour{marker: PhantomData};
|
||||
let swarm = SwarmBuilder::new(transport, behaviour, id.into()).build();
|
||||
assert!(swarm.raw_swarm.incoming_limit().is_none())
|
||||
}
|
||||
}
|
@ -1,248 +0,0 @@
|
||||
// Copyright 2019 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::{
|
||||
either::EitherOutput,
|
||||
protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
IntoProtocolsHandler
|
||||
},
|
||||
swarm::{NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
||||
upgrade::{InboundUpgrade, OutboundUpgrade, DeniedUpgrade, EitherUpgrade},
|
||||
PeerId, Multiaddr, nodes::ConnectedPoint, swarm::PollParameters,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::error;
|
||||
|
||||
/// Implementation of `NetworkBehaviour` that can be either in the disabled or enabled state.
|
||||
///
|
||||
/// The state can only be chosen at initialization.
|
||||
pub struct Toggle<TBehaviour> {
|
||||
inner: Option<TBehaviour>,
|
||||
}
|
||||
|
||||
impl<TBehaviour> From<Option<TBehaviour>> for Toggle<TBehaviour> {
|
||||
fn from(inner: Option<TBehaviour>) -> Self {
|
||||
Toggle { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<TBehaviour> NetworkBehaviour for Toggle<TBehaviour>
|
||||
where
|
||||
TBehaviour: NetworkBehaviour
|
||||
{
|
||||
type ProtocolsHandler = ToggleIntoProtoHandler<TBehaviour::ProtocolsHandler>;
|
||||
type OutEvent = TBehaviour::OutEvent;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
ToggleIntoProtoHandler {
|
||||
inner: self.inner.as_mut().map(|i| i.new_handler())
|
||||
}
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
|
||||
self.inner.as_mut().map(|b| b.addresses_of_peer(peer_id)).unwrap_or_else(Vec::new)
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_connected(peer_id, endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_disconnected(peer_id, endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_replaced(&mut self, peer_id: PeerId, closed_endpoint: ConnectedPoint, new_endpoint: ConnectedPoint) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_replaced(peer_id, closed_endpoint, new_endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_node_event(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
|
||||
) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_node_event(peer_id, event);
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_addr_reach_failure(&mut self, peer_id: Option<&PeerId>, addr: &Multiaddr, error: &dyn error::Error) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_addr_reach_failure(peer_id, addr, error)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_dial_failure(&mut self, peer_id: &PeerId) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_dial_failure(peer_id)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_new_listen_addr(&mut self, addr: &Multiaddr) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_new_listen_addr(addr)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_expired_listen_addr(&mut self, addr: &Multiaddr) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_expired_listen_addr(addr)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_new_external_addr(addr)
|
||||
}
|
||||
}
|
||||
|
||||
fn poll(&mut self, params: &mut impl PollParameters)
|
||||
-> Async<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
|
||||
{
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.poll(params)
|
||||
} else {
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TEvent, TBehaviour> NetworkBehaviourEventProcess<TEvent> for Toggle<TBehaviour>
|
||||
where
|
||||
TBehaviour: NetworkBehaviourEventProcess<TEvent>
|
||||
{
|
||||
fn inject_event(&mut self, event: TEvent) {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.inject_event(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of `IntoProtocolsHandler` that can be in the disabled state.
|
||||
pub struct ToggleIntoProtoHandler<TInner> {
|
||||
inner: Option<TInner>,
|
||||
}
|
||||
|
||||
impl<TInner> IntoProtocolsHandler for ToggleIntoProtoHandler<TInner>
|
||||
where
|
||||
TInner: IntoProtocolsHandler
|
||||
{
|
||||
type Handler = ToggleProtoHandler<TInner::Handler>;
|
||||
|
||||
fn into_handler(self, remote_peer_id: &PeerId, connected_point: &ConnectedPoint) -> Self::Handler {
|
||||
ToggleProtoHandler {
|
||||
inner: self.inner.map(|h| h.into_handler(remote_peer_id, connected_point))
|
||||
}
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
if let Some(inner) = self.inner.as_ref() {
|
||||
EitherUpgrade::A(inner.inbound_protocol())
|
||||
} else {
|
||||
EitherUpgrade::B(DeniedUpgrade)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that can be in the disabled state.
|
||||
pub struct ToggleProtoHandler<TInner> {
|
||||
inner: Option<TInner>,
|
||||
}
|
||||
|
||||
impl<TInner> ProtocolsHandler for ToggleProtoHandler<TInner>
|
||||
where
|
||||
TInner: ProtocolsHandler,
|
||||
{
|
||||
type InEvent = TInner::InEvent;
|
||||
type OutEvent = TInner::OutEvent;
|
||||
type Error = TInner::Error;
|
||||
type Substream = TInner::Substream;
|
||||
type InboundProtocol = EitherUpgrade<TInner::InboundProtocol, DeniedUpgrade>;
|
||||
type OutboundProtocol = TInner::OutboundProtocol;
|
||||
type OutboundOpenInfo = TInner::OutboundOpenInfo;
|
||||
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
||||
if let Some(inner) = self.inner.as_ref() {
|
||||
inner.listen_protocol().map_upgrade(EitherUpgrade::A)
|
||||
} else {
|
||||
SubstreamProtocol::new(EitherUpgrade::B(DeniedUpgrade))
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
out: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
|
||||
) {
|
||||
let out = match out {
|
||||
EitherOutput::First(out) => out,
|
||||
EitherOutput::Second(v) => void::unreachable(v),
|
||||
};
|
||||
|
||||
self.inner.as_mut().expect("Can't receive an inbound substream if disabled; QED")
|
||||
.inject_fully_negotiated_inbound(out)
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
out: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inner.as_mut().expect("Can't receive an outbound substream if disabled; QED")
|
||||
.inject_fully_negotiated_outbound(out, info)
|
||||
}
|
||||
|
||||
fn inject_event(&mut self, event: Self::InEvent) {
|
||||
self.inner.as_mut().expect("Can't receive events if disabled; QED")
|
||||
.inject_event(event)
|
||||
}
|
||||
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, err: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
||||
self.inner.as_mut().expect("Can't receive an outbound substream if disabled; QED")
|
||||
.inject_dial_upgrade_error(info, err)
|
||||
}
|
||||
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
self.inner.as_ref().map(|h| h.connection_keep_alive())
|
||||
.unwrap_or(KeepAlive::No)
|
||||
}
|
||||
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
||||
Self::Error,
|
||||
> {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.poll()
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::{
|
||||
nodes::raw_swarm::ConnectedPoint,
|
||||
ConnectedPoint,
|
||||
either::EitherError,
|
||||
transport::{Transport, TransportError, ListenerEvent}
|
||||
};
|
||||
|
@ -19,7 +19,7 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::{
|
||||
nodes::raw_swarm::ConnectedPoint,
|
||||
ConnectedPoint,
|
||||
transport::{Transport, TransportError, ListenerEvent}
|
||||
};
|
||||
use futures::{prelude::*, try_ready};
|
||||
|
@ -25,7 +25,7 @@
|
||||
//! any desired protocols. The rest of the module defines combinators for
|
||||
//! modifying a transport through composition with other transports or protocol upgrades.
|
||||
|
||||
use crate::{InboundUpgrade, OutboundUpgrade, nodes::raw_swarm::ConnectedPoint};
|
||||
use crate::{InboundUpgrade, OutboundUpgrade, ConnectedPoint};
|
||||
use futures::prelude::*;
|
||||
use multiaddr::Multiaddr;
|
||||
use std::{error, fmt};
|
||||
|
@ -18,7 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::nodes::ConnectedPoint;
|
||||
use crate::ConnectedPoint;
|
||||
use crate::upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade, UpgradeError, ProtocolName};
|
||||
use futures::{future::Either, prelude::*};
|
||||
use log::debug;
|
||||
|
Reference in New Issue
Block a user