Move swarm and protocols handler into swarm crate. (#1188)

Move swarm and protocols handler into swarm crate.
This commit is contained in:
Toralf Wittner 2019-07-04 14:47:59 +02:00 committed by GitHub
parent ef9cb056b2
commit 68c36d87d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 392 additions and 376 deletions

View File

@ -16,22 +16,23 @@ secp256k1 = ["libp2p-core/secp256k1", "libp2p-secio/secp256k1"]
[dependencies]
bytes = "0.4"
futures = "0.1"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "./misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.1.0", path = "./misc/multihash" }
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.1.0", path = "misc/multihash" }
lazy_static = "1.2"
libp2p-mplex = { version = "0.10.0", path = "./muxers/mplex" }
libp2p-identify = { version = "0.10.0", path = "./protocols/identify" }
libp2p-kad = { version = "0.10.0", path = "./protocols/kad" }
libp2p-floodsub = { version = "0.10.0", path = "./protocols/floodsub" }
libp2p-ping = { version = "0.10.0", path = "./protocols/ping" }
libp2p-plaintext = { version = "0.10.0", path = "./protocols/plaintext" }
libp2p-ratelimit = { version = "0.10.0", path = "./transports/ratelimit" }
libp2p-core = { version = "0.10.0", path = "./core" }
libp2p-core-derive = { version = "0.10.0", path = "./misc/core-derive" }
libp2p-secio = { version = "0.10.0", path = "./protocols/secio", default-features = false }
libp2p-uds = { version = "0.10.0", path = "./transports/uds" }
libp2p-wasm-ext = { version = "0.3.0", path = "./transports/wasm-ext" }
libp2p-yamux = { version = "0.10.0", path = "./muxers/yamux" }
libp2p-mplex = { version = "0.10.0", path = "muxers/mplex" }
libp2p-identify = { version = "0.10.0", path = "protocols/identify" }
libp2p-kad = { version = "0.10.0", path = "protocols/kad" }
libp2p-floodsub = { version = "0.10.0", path = "protocols/floodsub" }
libp2p-ping = { version = "0.10.0", path = "protocols/ping" }
libp2p-plaintext = { version = "0.10.0", path = "protocols/plaintext" }
libp2p-ratelimit = { version = "0.10.0", path = "transports/ratelimit" }
libp2p-core = { version = "0.10.0", path = "core" }
libp2p-core-derive = { version = "0.10.0", path = "misc/core-derive" }
libp2p-secio = { version = "0.10.0", path = "protocols/secio", default-features = false }
libp2p-swarm = { version = "0.1.0", path = "swarm" }
libp2p-uds = { version = "0.10.0", path = "transports/uds" }
libp2p-wasm-ext = { version = "0.3.0", path = "transports/wasm-ext" }
libp2p-yamux = { version = "0.10.0", path = "muxers/yamux" }
parking_lot = "0.8"
smallvec = "0.6"
tokio-codec = "0.1"
@ -40,12 +41,12 @@ tokio-io = "0.1"
wasm-timer = "0.1"
[target.'cfg(not(any(target_os = "emscripten", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.2.0", path = "./protocols/deflate" }
libp2p-dns = { version = "0.10.0", path = "./transports/dns" }
libp2p-mdns = { version = "0.10.0", path = "./misc/mdns" }
libp2p-noise = { version = "0.8.0", path = "./protocols/noise" }
libp2p-tcp = { version = "0.10.0", path = "./transports/tcp" }
libp2p-websocket = { version = "0.10.0", path = "./transports/websocket", optional = true }
libp2p-deflate = { version = "0.2.0", path = "protocols/deflate" }
libp2p-dns = { version = "0.10.0", path = "transports/dns" }
libp2p-mdns = { version = "0.10.0", path = "misc/mdns" }
libp2p-noise = { version = "0.8.0", path = "protocols/noise" }
libp2p-tcp = { version = "0.10.0", path = "transports/tcp" }
libp2p-websocket = { version = "0.10.0", path = "transports/websocket", optional = true }
[dev-dependencies]
env_logger = "0.6.0"
@ -74,6 +75,7 @@ members = [
"protocols/ping",
"protocols/plaintext",
"protocols/secio",
"swarm",
"transports/dns",
"transports/ratelimit",
"transports/tcp",

View File

@ -21,7 +21,7 @@ log = "0.4"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "../misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.1.0", path = "../misc/multihash" }
multistream-select = { version = "0.4.0", path = "../misc/multistream-select" }
futures = { version = "0.1", features = ["use_std"] }
futures = "0.1"
parking_lot = "0.8"
protobuf = "2.3"
quick-error = "1.2"
@ -44,6 +44,7 @@ untrusted = { version = "0.6" }
[dev-dependencies]
env_logger = "0.6"
libp2p-ping = { version = "0.10.0", path = "../protocols/ping" }
libp2p-swarm = { version = "0.1.0", path = "../swarm" }
libp2p-tcp = { version = "0.10.0", path = "../transports/tcp" }
libp2p-mplex = { version = "0.10.0", path = "../muxers/mplex" }
libp2p-secio = { version = "0.10.0", path = "../protocols/secio" }

View File

@ -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
}
}
}

View File

@ -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};

View File

@ -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> {

View File

@ -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};

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
nodes::raw_swarm::ConnectedPoint,
ConnectedPoint,
either::EitherError,
transport::{Transport, TransportError, ListenerEvent}
};

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{
nodes::raw_swarm::ConnectedPoint,
ConnectedPoint,
transport::{Transport, TransportError, ListenerEvent}
};
use futures::{prelude::*, try_ready};

View File

@ -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};

View File

@ -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;

View File

@ -23,13 +23,13 @@ use libp2p_core::identity;
use libp2p_core::multiaddr::multiaddr;
use libp2p_core::nodes::raw_swarm::{RawSwarm, RawSwarmEvent, RawSwarmReachError, PeerState, UnknownPeerDialErr, IncomingError};
use libp2p_core::{PeerId, Transport, upgrade, upgrade::InboundUpgradeExt, upgrade::OutboundUpgradeExt};
use libp2p_core::protocols_handler::{
use libp2p_swarm::{
ProtocolsHandler,
KeepAlive,
SubstreamProtocol,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr,
NodeHandlerWrapperBuilder
protocols_handler::NodeHandlerWrapperBuilder
};
use rand::seq::SliceRandom;
use std::io;

View File

@ -22,7 +22,7 @@ use futures::{future, prelude::*};
use libp2p_core::identity;
use libp2p_core::nodes::raw_swarm::{RawSwarm, RawSwarmEvent, IncomingError};
use libp2p_core::{Transport, upgrade, upgrade::OutboundUpgradeExt, upgrade::InboundUpgradeExt};
use libp2p_core::protocols_handler::{
use libp2p_swarm::{
ProtocolsHandler,
KeepAlive,
SubstreamProtocol,

View File

@ -80,7 +80,7 @@ fn main() {
mdns: libp2p::mdns::Mdns<TSubstream>,
}
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::mdns::MdnsEvent> for MyBehaviour<TSubstream> {
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::mdns::MdnsEvent> for MyBehaviour<TSubstream> {
fn inject_event(&mut self, event: libp2p::mdns::MdnsEvent) {
match event {
libp2p::mdns::MdnsEvent::Discovered(list) => {
@ -99,7 +99,7 @@ fn main() {
}
}
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::floodsub::FloodsubEvent> for MyBehaviour<TSubstream> {
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::floodsub::FloodsubEvent> for MyBehaviour<TSubstream> {
// Called when `floodsub` produces an event.
fn inject_event(&mut self, message: libp2p::floodsub::FloodsubEvent) {
if let libp2p::floodsub::FloodsubEvent::Message(message) = message {

View File

@ -47,15 +47,15 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
let name = &ast.ident;
let (_, ty_generics, where_clause) = ast.generics.split_for_impl();
let multiaddr = quote!{::libp2p::core::Multiaddr};
let trait_to_impl = quote!{::libp2p::core::swarm::NetworkBehaviour};
let net_behv_event_proc = quote!{::libp2p::core::swarm::NetworkBehaviourEventProcess};
let trait_to_impl = quote!{::libp2p::swarm::NetworkBehaviour};
let net_behv_event_proc = quote!{::libp2p::swarm::NetworkBehaviourEventProcess};
let either_ident = quote!{::libp2p::core::either::EitherOutput};
let network_behaviour_action = quote!{::libp2p::core::swarm::NetworkBehaviourAction};
let into_protocols_handler = quote!{::libp2p::core::protocols_handler::IntoProtocolsHandler};
let protocols_handler = quote!{::libp2p::core::protocols_handler::ProtocolsHandler};
let into_proto_select_ident = quote!{::libp2p::core::protocols_handler::IntoProtocolsHandlerSelect};
let network_behaviour_action = quote!{::libp2p::swarm::NetworkBehaviourAction};
let into_protocols_handler = quote!{::libp2p::swarm::IntoProtocolsHandler};
let protocols_handler = quote!{::libp2p::swarm::ProtocolsHandler};
let into_proto_select_ident = quote!{::libp2p::swarm::IntoProtocolsHandlerSelect};
let peer_id = quote!{::libp2p::core::PeerId};
let connected_point = quote!{::libp2p::core::swarm::ConnectedPoint};
let connected_point = quote!{::libp2p::core::ConnectedPoint};
// Name of the type parameter that represents the substream.
let substream_generic = {
@ -68,7 +68,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
quote!{#n}
};
let poll_parameters = quote!{::libp2p::core::swarm::PollParameters};
let poll_parameters = quote!{::libp2p::swarm::PollParameters};
// Build the generics.
let impl_generics = {

View File

@ -22,7 +22,7 @@ use libp2p_core_derive::*;
/// Small utility to check that a type implements `NetworkBehaviour`.
#[allow(dead_code)]
fn require_net_behaviour<T: libp2p::core::swarm::NetworkBehaviour>() {}
fn require_net_behaviour<T: libp2p::swarm::NetworkBehaviour>() {}
// TODO: doesn't compile
/*#[test]
@ -40,7 +40,7 @@ fn one_field() {
ping: libp2p::ping::Ping<TSubstream>,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
@ -60,12 +60,12 @@ fn two_fields() {
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
@ -88,17 +88,17 @@ fn three_fields() {
foo: String,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::kad::KademliaEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::kad::KademliaEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::kad::KademliaEvent) {
}
}
@ -119,18 +119,18 @@ fn custom_polling() {
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
}
}
impl<TSubstream> Foo<TSubstream> {
fn foo<T>(&mut self) -> libp2p::futures::Async<libp2p::core::swarm::NetworkBehaviourAction<T, ()>> { libp2p::futures::Async::NotReady }
fn foo<T>(&mut self) -> libp2p::futures::Async<libp2p::swarm::NetworkBehaviourAction<T, ()>> { libp2p::futures::Async::NotReady }
}
#[allow(dead_code)]
@ -149,12 +149,12 @@ fn custom_event_no_polling() {
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
}
}
@ -175,18 +175,18 @@ fn custom_event_and_polling() {
identify: libp2p::identify::Identify<TSubstream>,
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {
}
}
impl<TSubstream> libp2p::core::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
impl<TSubstream> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo<TSubstream> {
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {
}
}
impl<TSubstream> Foo<TSubstream> {
fn foo<T>(&mut self) -> libp2p::futures::Async<libp2p::core::swarm::NetworkBehaviourAction<T, String>> { libp2p::futures::Async::NotReady }
fn foo<T>(&mut self) -> libp2p::futures::Async<libp2p::swarm::NetworkBehaviourAction<T, String>> { libp2p::futures::Async::NotReady }
}
#[allow(dead_code)]

View File

@ -14,6 +14,7 @@ data-encoding = "2.0"
dns-parser = "0.8"
futures = "0.1"
libp2p-core = { version = "0.10.0", path = "../../core" }
libp2p-swarm = { version = "0.1.0", path = "../../swarm" }
log = "0.4"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "../multiaddr" }
net2 = "0.2"

View File

@ -20,10 +20,15 @@
use crate::service::{MdnsService, MdnsPacket};
use futures::prelude::*;
use libp2p_core::{address_translation, ConnectedPoint, Multiaddr, PeerId, multiaddr::Protocol};
use libp2p_swarm::{
NetworkBehaviour,
NetworkBehaviourAction,
PollParameters,
ProtocolsHandler,
protocols_handler::DummyProtocolsHandler
};
use log::warn;
use libp2p_core::protocols_handler::{DummyProtocolsHandler, ProtocolsHandler};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{address_translation, Multiaddr, PeerId, multiaddr::Protocol};
use smallvec::SmallVec;
use std::{cmp, fmt, io, iter, marker::PhantomData, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -16,6 +16,7 @@ cuckoofilter = "0.3.2"
fnv = "1.0"
futures = "0.1"
libp2p-core = { version = "0.10.0", path = "../../core" }
libp2p-swarm = { version = "0.1.0", path = "../../swarm" }
protobuf = "2.3"
rand = "0.6"
smallvec = "0.6.5"

View File

@ -23,8 +23,14 @@ use crate::topic::{Topic, TopicHash};
use cuckoofilter::CuckooFilter;
use fnv::FnvHashSet;
use futures::prelude::*;
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{protocols_handler::ProtocolsHandler, protocols_handler::OneShotHandler, Multiaddr, PeerId};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{
NetworkBehaviour,
NetworkBehaviourAction,
PollParameters,
ProtocolsHandler,
OneShotHandler
};
use rand;
use smallvec::SmallVec;
use std::{collections::VecDeque, iter, marker::PhantomData};

View File

@ -14,6 +14,7 @@ bytes = "0.4"
fnv = "1"
futures = "0.1"
libp2p-core = { version = "0.10.0", path = "../../core" }
libp2p-swarm = { version = "0.1.0", path = "../../swarm" }
log = "0.4.1"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "../../misc/multiaddr" }
parking_lot = "0.8"

View File

@ -22,9 +22,22 @@ use crate::listen_handler::IdentifyListenHandler;
use crate::periodic_id_handler::{PeriodicIdHandler, PeriodicIdHandlerEvent};
use crate::protocol::{IdentifyInfo, IdentifySender, IdentifySenderFuture};
use futures::prelude::*;
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerSelect, ProtocolsHandlerUpgrErr};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{Multiaddr, PeerId, PublicKey, either::EitherOutput, upgrade::Negotiated};
use libp2p_core::{
ConnectedPoint,
Multiaddr,
PeerId,
PublicKey,
either::EitherOutput,
upgrade::Negotiated
};
use libp2p_swarm::{
NetworkBehaviour,
NetworkBehaviourAction,
PollParameters,
ProtocolsHandler,
ProtocolsHandlerSelect,
ProtocolsHandlerUpgrErr
};
use smallvec::SmallVec;
use std::{collections::HashMap, collections::VecDeque, io};
use tokio_io::{AsyncRead, AsyncWrite};
@ -227,11 +240,11 @@ mod tests {
upgrade::{self, OutboundUpgradeExt, InboundUpgradeExt},
muxing::StreamMuxer,
Multiaddr,
Swarm,
Transport
};
use libp2p_tcp::TcpConfig;
use libp2p_secio::SecioConfig;
use libp2p_swarm::Swarm;
use libp2p_mplex::MplexConfig;
use rand::Rng;
use std::{fmt, io};

View File

@ -20,15 +20,13 @@
use crate::protocol::{IdentifySender, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::{
protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
},
upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade, Negotiated}
use libp2p_core::upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade, Negotiated};
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use smallvec::SmallVec;
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -20,15 +20,13 @@
use crate::protocol::{RemoteInfo, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::{
protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
},
upgrade::{DeniedUpgrade, OutboundUpgrade}
use libp2p_core::upgrade::{DeniedUpgrade, OutboundUpgrade};
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use std::{io, marker::PhantomData, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -23,7 +23,8 @@ use crate::structs_proto;
use futures::{future::{self, FutureResult}, Async, AsyncSink, Future, Poll, Sink, Stream};
use futures::try_ready;
use libp2p_core::{
Multiaddr, PublicKey,
Multiaddr,
PublicKey,
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}
};
use log::{debug, trace};

View File

@ -19,6 +19,7 @@ either = "1.5"
fnv = "1.0"
futures = "0.1"
libp2p-core = { version = "0.10.0", path = "../../core" }
libp2p-swarm = { version = "0.1.0", path = "../../swarm" }
log = "0.4"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "../../misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.1.0", path = "../../misc/multihash" }

View File

@ -27,8 +27,8 @@ use crate::query::{Query, QueryId, QueryPool, QueryConfig, QueryPoolState};
use crate::record::{MemoryRecordStorage, RecordStore, Record, RecordStorageError};
use fnv::{FnvHashMap, FnvHashSet};
use futures::{prelude::*, stream};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{protocols_handler::ProtocolsHandler, Multiaddr, PeerId};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler};
use multihash::Multihash;
use smallvec::SmallVec;
use std::{borrow::Cow, error, marker::PhantomData, time::Duration, num::NonZeroU8};

View File

@ -25,7 +25,7 @@ use super::*;
use crate::kbucket::Distance;
use futures::future;
use libp2p_core::{
Swarm,
PeerId,
Transport,
identity,
transport::{MemoryTransport, boxed::Boxed},
@ -35,6 +35,7 @@ use libp2p_core::{
upgrade,
};
use libp2p_secio::SecioConfig;
use libp2p_swarm::Swarm;
use libp2p_yamux as yamux;
use rand::{Rng, random, thread_rng};
use std::{collections::HashSet, iter::FromIterator, io, num::NonZeroU8, u64};

View File

@ -24,14 +24,17 @@ use crate::protocol::{
};
use crate::record::Record;
use futures::prelude::*;
use libp2p_core::protocols_handler::{
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use libp2p_core::{upgrade, either::EitherOutput, InboundUpgrade, OutboundUpgrade, upgrade::Negotiated};
use libp2p_core::{
either::EitherOutput,
upgrade::{self, InboundUpgrade, OutboundUpgrade, Negotiated}
};
use multihash::Multihash;
use std::{borrow::Cow, error, fmt, io, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -13,6 +13,7 @@ categories = ["network-programming", "asynchronous"]
arrayvec = "0.4"
bytes = "0.4"
libp2p-core = { version = "0.10.0", path = "../../core" }
libp2p-swarm = { version = "0.1.0", path = "../../swarm" }
log = "0.4.1"
multiaddr = { package = "parity-multiaddr", version = "0.5.0", path = "../../misc/multiaddr" }
futures = "0.1"

View File

@ -20,12 +20,12 @@
use crate::protocol;
use futures::prelude::*;
use libp2p_core::ProtocolsHandlerEvent;
use libp2p_core::protocols_handler::{
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerUpgrErr,
ProtocolsHandlerEvent
};
use std::{error::Error, io, fmt, num::NonZeroU32, time::Duration};
use std::collections::VecDeque;

View File

@ -48,8 +48,8 @@ pub use handler::{PingConfig, PingResult, PingSuccess, PingFailure};
use handler::PingHandler;
use futures::prelude::*;
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{Multiaddr, PeerId};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use std::collections::VecDeque;
use std::marker::PhantomData;
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -23,7 +23,6 @@
use libp2p_core::{
Multiaddr,
PeerId,
Swarm,
identity,
muxing::StreamMuxer,
upgrade::{self, OutboundUpgradeExt, InboundUpgradeExt},
@ -32,6 +31,7 @@ use libp2p_core::{
use libp2p_ping::*;
use libp2p_yamux as yamux;
use libp2p_secio::SecioConfig;
use libp2p_swarm::Swarm;
use libp2p_tcp::TcpConfig;
use futures::{future, prelude::*};
use std::{fmt, io, time::Duration, sync::mpsc::sync_channel};

View File

@ -191,6 +191,8 @@ pub use libp2p_plaintext as plaintext;
pub use libp2p_ratelimit as ratelimit;
#[doc(inline)]
pub use libp2p_secio as secio;
#[doc(inline)]
pub use libp2p_swarm as swarm;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_tcp as tcp;
@ -211,13 +213,15 @@ pub mod simple;
pub use self::core::{
identity,
Transport, PeerId, Swarm,
PeerId,
Transport,
transport::TransportError,
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt}
};
pub use libp2p_core_derive::NetworkBehaviour;
pub use self::multiaddr::{Multiaddr, multiaddr as build_multiaddr};
pub use self::simple::SimpleProtocol;
pub use self::swarm::Swarm;
pub use self::transport_ext::TransportExt;
use futures::prelude::*;

24
swarm/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "libp2p-swarm"
edition = "2018"
description = "The libp2p swarm"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]
[dependencies]
futures = "0.1"
libp2p-core = { version = "0.10.0", path = "../core" }
smallvec = "0.6"
tokio-io = "0.1"
wasm-timer = "0.1"
void = "1"
[dev-dependencies]
libp2p-mplex = { version = "0.10.0", path = "../muxers/mplex" }
quickcheck = "0.8"
rand = "0.6"

View File

@ -18,11 +18,8 @@
// 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 crate::protocols_handler::{IntoProtocolsHandler, ProtocolsHandler};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use futures::prelude::*;
use std::error;

View File

@ -18,7 +18,68 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::{
//! 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`.
//! 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.
//!
//! # 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. It is the `NetworkBehaviour` that
//! controls what happens on the network. Multiple types that implement
//! `NetworkBehaviour` can be composed into a single behaviour.
//!
//! # Protocols Handler
//!
//! 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.
//!
mod behaviour;
mod registry;
pub mod protocols_handler;
pub mod toggle;
pub use behaviour::{
NetworkBehaviour,
NetworkBehaviourAction,
NetworkBehaviourEventProcess,
PollParameters
};
pub use protocols_handler::{
IntoProtocolsHandler,
IntoProtocolsHandlerSelect,
KeepAlive,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerSelect,
ProtocolsHandlerUpgrErr,
OneShotHandler,
SubstreamProtocol
};
use protocols_handler::{NodeHandlerWrapperBuilder, NodeHandlerWrapper, NodeHandlerWrapperError};
use futures::prelude::*;
use libp2p_core::{
Transport, Multiaddr, PeerId, InboundUpgrade, OutboundUpgrade, UpgradeInfo, ProtocolName,
muxing::StreamMuxer,
nodes::{
@ -27,11 +88,9 @@ use crate::{
node::Substream,
raw_swarm::{self, RawSwarm, RawSwarmEvent}
},
protocols_handler::{NodeHandlerWrapperBuilder, NodeHandlerWrapper, NodeHandlerWrapperError, IntoProtocolsHandler, ProtocolsHandler},
swarm::{PollParameters, NetworkBehaviour, NetworkBehaviourAction, registry::{Addresses, AddressIntoIter}},
transport::TransportError,
transport::TransportError
};
use futures::prelude::*;
use registry::{Addresses, AddressIntoIter};
use smallvec::SmallVec;
use std::{error, fmt, io, ops::{Deref, DerefMut}};
use std::collections::HashSet;
@ -471,12 +530,18 @@ where TBehaviour: NetworkBehaviour,
#[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 crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, SwarmBuilder};
use libp2p_core::{
ConnectedPoint,
identity,
Multiaddr,
PeerId,
PublicKey,
transport::dummy::{DummyStream, DummyTransport}
};
use libp2p_mplex::Multiplex;
use futures::prelude::*;
use multiaddr::Multiaddr;
use std::marker::PhantomData;
use tokio_io::{AsyncRead, AsyncWrite};
use void::Void;
@ -526,7 +591,7 @@ mod tests {
#[test]
fn test_build_swarm() {
let id = get_random_id();
let transport = DummyTransport::new();
let transport = DummyTransport::<(PeerId, Multiplex<DummyStream>)>::new();
let behaviour = DummyBehaviour{marker: PhantomData};
let swarm = SwarmBuilder::new(transport, behaviour, id.into())
.incoming_limit(Some(4)).build();
@ -536,7 +601,7 @@ mod tests {
#[test]
fn test_build_swarm_with_max_listeners_none() {
let id = get_random_id();
let transport = DummyTransport::new();
let transport = DummyTransport::<(PeerId, Multiplex<DummyStream>)>::new();
let behaviour = DummyBehaviour{marker: PhantomData};
let swarm = SwarmBuilder::new(transport, behaviour, id.into()).build();
assert!(swarm.raw_swarm.incoming_limit().is_none())

View File

@ -18,21 +18,15 @@
// 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 crate::protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use futures::prelude::*;
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, DeniedUpgrade};
use std::marker::PhantomData;
use tokio_io::{AsyncRead, AsyncWrite};
use void::Void;

View File

@ -18,20 +18,15 @@
// 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 crate::protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use futures::prelude::*;
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade};
use std::marker::PhantomData;
/// Wrapper around a protocol handler that turns the input event into something else.

View File

@ -18,20 +18,15 @@
// 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 crate::protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use futures::prelude::*;
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade};
/// Wrapper around a protocol handler that turns the output event into something else.
pub struct MapOutEvent<TProtoHandler, TMap> {

View File

@ -37,25 +37,6 @@
//! > 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;
@ -63,6 +44,23 @@ mod node_handler;
mod one_shot;
mod select;
use futures::prelude::*;
use libp2p_core::{
ConnectedPoint,
PeerId,
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError},
};
use std::{cmp::Ordering, error, fmt, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
use wasm_timer::Instant;
pub use dummy::DummyProtocolsHandler;
pub use map_in::MapInEvent;
pub use map_out::MapOutEvent;
pub use node_handler::{NodeHandlerWrapper, NodeHandlerWrapperBuilder, NodeHandlerWrapperError};
pub use one_shot::OneShotHandler;
pub use select::{IntoProtocolsHandlerSelect, ProtocolsHandlerSelect};
/// 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

View File

@ -18,20 +18,22 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::{
use crate::protocols_handler::{
KeepAlive,
ProtocolsHandler,
IntoProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use futures::prelude::*;
use libp2p_core::{
ConnectedPoint,
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,
}
upgrade::{self, InboundUpgradeApply, OutboundUpgradeApply}
};
use futures::prelude::*;
use std::{error, fmt, time::Duration};
use wasm_timer::{Delay, Timeout};

View File

@ -19,11 +19,14 @@
// DEALINGS IN THE SOFTWARE.
use crate::protocols_handler::{
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr,
KeepAlive,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr,
SubstreamProtocol
};
use crate::upgrade::{InboundUpgrade, OutboundUpgrade};
use futures::prelude::*;
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade};
use smallvec::SmallVec;
use std::{error, marker::PhantomData, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -18,28 +18,21 @@
// 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 crate::protocols_handler::{
KeepAlive,
SubstreamProtocol,
IntoProtocolsHandler,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr,
};
use futures::prelude::*;
use libp2p_core::{
ConnectedPoint,
PeerId,
either::{EitherError, EitherOutput},
upgrade::{InboundUpgrade, OutboundUpgrade, EitherUpgrade, SelectUpgrade, UpgradeError}
};
use std::cmp;
use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::Multiaddr;
use libp2p_core::Multiaddr;
use smallvec::SmallVec;
use std::{collections::VecDeque, num::NonZeroUsize};
@ -180,8 +180,10 @@ fn isort(xs: &mut [Record]) {
#[cfg(test)]
mod tests {
use crate::Multiaddr;
use quickcheck::QuickCheck;
use libp2p_core::multiaddr::{Multiaddr, Protocol};
use quickcheck::{Arbitrary, Gen, QuickCheck};
use rand::Rng;
use std::num::NonZeroUsize;
use super::{isort, Addresses, Record};
#[test]
@ -223,11 +225,6 @@ mod tests {
#[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);

View File

@ -18,19 +18,21 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::{
use crate::{NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters};
use crate::protocols_handler::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr,
IntoProtocolsHandler
};
use libp2p_core::{
ConnectedPoint,
PeerId,
Multiaddr,
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,
upgrade::{InboundUpgrade, OutboundUpgrade, DeniedUpgrade, EitherUpgrade}
};
use futures::prelude::*;
use std::error;