From 68c36d87d3727df6c18c1b36e662f714a0996a92 Mon Sep 17 00:00:00 2001 From: Toralf Wittner Date: Thu, 4 Jul 2019 14:47:59 +0200 Subject: [PATCH] Move swarm and protocols handler into swarm crate. (#1188) Move swarm and protocols handler into swarm crate. --- Cargo.toml | 44 +++---- core/Cargo.toml | 3 +- core/src/lib.rs | 112 +++++++++++------- core/src/nodes/mod.rs | 8 +- core/src/nodes/raw_swarm.rs | 63 +--------- core/src/swarm.rs | 51 -------- core/src/transport/and_then.rs | 2 +- core/src/transport/map.rs | 2 +- core/src/transport/mod.rs | 2 +- core/src/upgrade/apply.rs | 2 +- core/tests/raw_swarm_dial_error.rs | 4 +- core/tests/raw_swarm_simult.rs | 2 +- examples/chat.rs | 4 +- misc/core-derive/src/lib.rs | 16 +-- misc/core-derive/tests/test.rs | 30 ++--- misc/mdns/Cargo.toml | 1 + misc/mdns/src/behaviour.rs | 11 +- protocols/floodsub/Cargo.toml | 1 + protocols/floodsub/src/layer.rs | 10 +- protocols/identify/Cargo.toml | 1 + protocols/identify/src/identify.rs | 21 +++- protocols/identify/src/listen_handler.rs | 16 ++- protocols/identify/src/periodic_id_handler.rs | 16 ++- protocols/identify/src/protocol.rs | 3 +- protocols/kad/Cargo.toml | 1 + protocols/kad/src/behaviour.rs | 4 +- protocols/kad/src/behaviour/test.rs | 3 +- protocols/kad/src/handler.rs | 7 +- protocols/ping/Cargo.toml | 1 + protocols/ping/src/handler.rs | 4 +- protocols/ping/src/lib.rs | 4 +- protocols/ping/tests/ping.rs | 2 +- src/lib.rs | 6 +- swarm/Cargo.toml | 24 ++++ {core/src/swarm => swarm/src}/behaviour.rs | 7 +- core/src/swarm/swarm.rs => swarm/src/lib.rs | 87 ++++++++++++-- .../src/protocols_handler/dummy.rs | 20 ++-- .../src/protocols_handler/map_in.rs | 19 ++- .../src/protocols_handler/map_out.rs | 19 ++- {core => swarm}/src/protocols_handler/mod.rs | 36 +++--- .../src/protocols_handler/node_handler.rs | 20 ++-- .../src/protocols_handler/one_shot.rs | 7 +- .../src/protocols_handler/select.rs | 33 ++---- {core/src/swarm => swarm/src}/registry.rs | 13 +- {core/src/swarm => swarm/src}/toggle.rs | 26 ++-- 45 files changed, 392 insertions(+), 376 deletions(-) delete mode 100644 core/src/swarm.rs create mode 100644 swarm/Cargo.toml rename {core/src/swarm => swarm/src}/behaviour.rs (98%) rename core/src/swarm/swarm.rs => swarm/src/lib.rs (91%) rename {core => swarm}/src/protocols_handler/dummy.rs (91%) rename {core => swarm}/src/protocols_handler/map_in.rs (93%) rename {core => swarm}/src/protocols_handler/map_out.rs (93%) rename {core => swarm}/src/protocols_handler/mod.rs (97%) rename {core => swarm}/src/protocols_handler/node_handler.rs (97%) rename {core => swarm}/src/protocols_handler/one_shot.rs (98%) rename {core => swarm}/src/protocols_handler/select.rs (96%) rename {core/src/swarm => swarm/src}/registry.rs (97%) rename {core/src/swarm => swarm/src}/toggle.rs (95%) diff --git a/Cargo.toml b/Cargo.toml index 02cac7a7..78953545 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/core/Cargo.toml b/core/Cargo.toml index e98477dd..f308a87f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -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" } diff --git a/core/src/lib.rs b/core/src/lib.rs index e7ef36aa..1cf1cd2c 100644 --- a/core/src/lib.rs +++ b/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 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 + } + } +} + diff --git a/core/src/nodes/mod.rs b/core/src/nodes/mod.rs index 9950c8b6..73ec6cd9 100644 --- a/core/src/nodes/mod.rs +++ b/core/src/nodes/mod.rs @@ -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}; diff --git a/core/src/nodes/raw_swarm.rs b/core/src/nodes/raw_swarm.rs index 4713e76a..6c047ee8 100644 --- a/core/src/nodes/raw_swarm.rs +++ b/core/src/nodes/raw_swarm.rs @@ -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 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> { diff --git a/core/src/swarm.rs b/core/src/swarm.rs deleted file mode 100644 index 8096572e..00000000 --- a/core/src/swarm.rs +++ /dev/null @@ -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}; diff --git a/core/src/transport/and_then.rs b/core/src/transport/and_then.rs index 29fb275d..d9dc3af6 100644 --- a/core/src/transport/and_then.rs +++ b/core/src/transport/and_then.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - nodes::raw_swarm::ConnectedPoint, + ConnectedPoint, either::EitherError, transport::{Transport, TransportError, ListenerEvent} }; diff --git a/core/src/transport/map.rs b/core/src/transport/map.rs index 23538b1e..3d7ca286 100644 --- a/core/src/transport/map.rs +++ b/core/src/transport/map.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - nodes::raw_swarm::ConnectedPoint, + ConnectedPoint, transport::{Transport, TransportError, ListenerEvent} }; use futures::{prelude::*, try_ready}; diff --git a/core/src/transport/mod.rs b/core/src/transport/mod.rs index c15e6d58..ac500ec4 100644 --- a/core/src/transport/mod.rs +++ b/core/src/transport/mod.rs @@ -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}; diff --git a/core/src/upgrade/apply.rs b/core/src/upgrade/apply.rs index 1bb5cf8a..d9a3e7a1 100644 --- a/core/src/upgrade/apply.rs +++ b/core/src/upgrade/apply.rs @@ -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; diff --git a/core/tests/raw_swarm_dial_error.rs b/core/tests/raw_swarm_dial_error.rs index 49b90242..3f164833 100644 --- a/core/tests/raw_swarm_dial_error.rs +++ b/core/tests/raw_swarm_dial_error.rs @@ -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; diff --git a/core/tests/raw_swarm_simult.rs b/core/tests/raw_swarm_simult.rs index 811c9c2e..49b1fc5f 100644 --- a/core/tests/raw_swarm_simult.rs +++ b/core/tests/raw_swarm_simult.rs @@ -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, diff --git a/examples/chat.rs b/examples/chat.rs index f67f29de..65f8589c 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -80,7 +80,7 @@ fn main() { mdns: libp2p::mdns::Mdns, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for MyBehaviour { + impl libp2p::swarm::NetworkBehaviourEventProcess for MyBehaviour { fn inject_event(&mut self, event: libp2p::mdns::MdnsEvent) { match event { libp2p::mdns::MdnsEvent::Discovered(list) => { @@ -99,7 +99,7 @@ fn main() { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for MyBehaviour { + impl libp2p::swarm::NetworkBehaviourEventProcess for MyBehaviour { // Called when `floodsub` produces an event. fn inject_event(&mut self, message: libp2p::floodsub::FloodsubEvent) { if let libp2p::floodsub::FloodsubEvent::Message(message) = message { diff --git a/misc/core-derive/src/lib.rs b/misc/core-derive/src/lib.rs index 77795574..f6ab944a 100644 --- a/misc/core-derive/src/lib.rs +++ b/misc/core-derive/src/lib.rs @@ -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 = { diff --git a/misc/core-derive/tests/test.rs b/misc/core-derive/tests/test.rs index aaaa193f..fcc1bfe6 100644 --- a/misc/core-derive/tests/test.rs +++ b/misc/core-derive/tests/test.rs @@ -22,7 +22,7 @@ use libp2p_core_derive::*; /// Small utility to check that a type implements `NetworkBehaviour`. #[allow(dead_code)] -fn require_net_behaviour() {} +fn require_net_behaviour() {} // TODO: doesn't compile /*#[test] @@ -40,7 +40,7 @@ fn one_field() { ping: libp2p::ping::Ping, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } @@ -60,12 +60,12 @@ fn two_fields() { identify: libp2p::identify::Identify, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } @@ -88,17 +88,17 @@ fn three_fields() { foo: String, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::kad::KademliaEvent) { } } @@ -119,18 +119,18 @@ fn custom_polling() { identify: libp2p::identify::Identify, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) { } } impl Foo { - fn foo(&mut self) -> libp2p::futures::Async> { libp2p::futures::Async::NotReady } + fn foo(&mut self) -> libp2p::futures::Async> { libp2p::futures::Async::NotReady } } #[allow(dead_code)] @@ -149,12 +149,12 @@ fn custom_event_no_polling() { identify: libp2p::identify::Identify, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) { } } @@ -175,18 +175,18 @@ fn custom_event_and_polling() { identify: libp2p::identify::Identify, } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::ping::PingEvent) { } } - impl libp2p::core::swarm::NetworkBehaviourEventProcess for Foo { + impl libp2p::swarm::NetworkBehaviourEventProcess for Foo { fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) { } } impl Foo { - fn foo(&mut self) -> libp2p::futures::Async> { libp2p::futures::Async::NotReady } + fn foo(&mut self) -> libp2p::futures::Async> { libp2p::futures::Async::NotReady } } #[allow(dead_code)] diff --git a/misc/mdns/Cargo.toml b/misc/mdns/Cargo.toml index 38bbcd93..528812dd 100644 --- a/misc/mdns/Cargo.toml +++ b/misc/mdns/Cargo.toml @@ -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" diff --git a/misc/mdns/src/behaviour.rs b/misc/mdns/src/behaviour.rs index fee65995..7d933211 100644 --- a/misc/mdns/src/behaviour.rs +++ b/misc/mdns/src/behaviour.rs @@ -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}; diff --git a/protocols/floodsub/Cargo.toml b/protocols/floodsub/Cargo.toml index 9e1f70e8..31d35767 100644 --- a/protocols/floodsub/Cargo.toml +++ b/protocols/floodsub/Cargo.toml @@ -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" diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index fc1f0136..684226fa 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -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}; diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 27497d3f..0938beee 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -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" diff --git a/protocols/identify/src/identify.rs b/protocols/identify/src/identify.rs index 816ebbef..d2ddc4f9 100644 --- a/protocols/identify/src/identify.rs +++ b/protocols/identify/src/identify.rs @@ -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}; diff --git a/protocols/identify/src/listen_handler.rs b/protocols/identify/src/listen_handler.rs index 344e04c3..2d72bfbc 100644 --- a/protocols/identify/src/listen_handler.rs +++ b/protocols/identify/src/listen_handler.rs @@ -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}; diff --git a/protocols/identify/src/periodic_id_handler.rs b/protocols/identify/src/periodic_id_handler.rs index 1df09e53..d9773ddf 100644 --- a/protocols/identify/src/periodic_id_handler.rs +++ b/protocols/identify/src/periodic_id_handler.rs @@ -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}; diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index bcd43be8..76dda76c 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -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}; diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index c31c7aff..544e30b4 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -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" } diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 9703490c..501b8845 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -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}; diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 3d629dbf..a2b36676 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -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}; diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index fbc0bf24..6c4949b4 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -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}; diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index 77779bcb..981ac181 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -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" diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 8b767e3d..0c3116bf 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -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; diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 0e541c06..1353ffa1 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -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}; diff --git a/protocols/ping/tests/ping.rs b/protocols/ping/tests/ping.rs index 75e9c9cb..3a6b7661 100644 --- a/protocols/ping/tests/ping.rs +++ b/protocols/ping/tests/ping.rs @@ -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}; diff --git a/src/lib.rs b/src/lib.rs index be744b91..62496cfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml new file mode 100644 index 00000000..209aff2b --- /dev/null +++ b/swarm/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "libp2p-swarm" +edition = "2018" +description = "The libp2p swarm" +version = "0.1.0" +authors = ["Parity Technologies "] +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" + diff --git a/core/src/swarm/behaviour.rs b/swarm/src/behaviour.rs similarity index 98% rename from core/src/swarm/behaviour.rs rename to swarm/src/behaviour.rs index 98f52f28..4b0daeb5 100644 --- a/core/src/swarm/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -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; diff --git a/core/src/swarm/swarm.rs b/swarm/src/lib.rs similarity index 91% rename from core/src/swarm/swarm.rs rename to swarm/src/lib.rs index e2d0e1f6..eb2ef108 100644 --- a/core/src/swarm/swarm.rs +++ b/swarm/src/lib.rs @@ -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)>::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)>::new(); let behaviour = DummyBehaviour{marker: PhantomData}; let swarm = SwarmBuilder::new(transport, behaviour, id.into()).build(); assert!(swarm.raw_swarm.incoming_limit().is_none()) diff --git a/core/src/protocols_handler/dummy.rs b/swarm/src/protocols_handler/dummy.rs similarity index 91% rename from core/src/protocols_handler/dummy.rs rename to swarm/src/protocols_handler/dummy.rs index ae57a4e8..a9719b85 100644 --- a/core/src/protocols_handler/dummy.rs +++ b/swarm/src/protocols_handler/dummy.rs @@ -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; diff --git a/core/src/protocols_handler/map_in.rs b/swarm/src/protocols_handler/map_in.rs similarity index 93% rename from core/src/protocols_handler/map_in.rs rename to swarm/src/protocols_handler/map_in.rs index 6c60719a..e478e58f 100644 --- a/core/src/protocols_handler/map_in.rs +++ b/swarm/src/protocols_handler/map_in.rs @@ -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. diff --git a/core/src/protocols_handler/map_out.rs b/swarm/src/protocols_handler/map_out.rs similarity index 93% rename from core/src/protocols_handler/map_out.rs rename to swarm/src/protocols_handler/map_out.rs index 0d54006c..5815d949 100644 --- a/core/src/protocols_handler/map_out.rs +++ b/swarm/src/protocols_handler/map_out.rs @@ -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 { diff --git a/core/src/protocols_handler/mod.rs b/swarm/src/protocols_handler/mod.rs similarity index 97% rename from core/src/protocols_handler/mod.rs rename to swarm/src/protocols_handler/mod.rs index 0f3d0710..855d95d4 100644 --- a/core/src/protocols_handler/mod.rs +++ b/swarm/src/protocols_handler/mod.rs @@ -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 diff --git a/core/src/protocols_handler/node_handler.rs b/swarm/src/protocols_handler/node_handler.rs similarity index 97% rename from core/src/protocols_handler/node_handler.rs rename to swarm/src/protocols_handler/node_handler.rs index dcaa9d13..0ac6abfb 100644 --- a/core/src/protocols_handler/node_handler.rs +++ b/swarm/src/protocols_handler/node_handler.rs @@ -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}; diff --git a/core/src/protocols_handler/one_shot.rs b/swarm/src/protocols_handler/one_shot.rs similarity index 98% rename from core/src/protocols_handler/one_shot.rs rename to swarm/src/protocols_handler/one_shot.rs index ffc3aa7e..c685dfb9 100644 --- a/core/src/protocols_handler/one_shot.rs +++ b/swarm/src/protocols_handler/one_shot.rs @@ -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}; diff --git a/core/src/protocols_handler/select.rs b/swarm/src/protocols_handler/select.rs similarity index 96% rename from core/src/protocols_handler/select.rs rename to swarm/src/protocols_handler/select.rs index 5635e742..074920b1 100644 --- a/core/src/protocols_handler/select.rs +++ b/swarm/src/protocols_handler/select.rs @@ -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}; diff --git a/core/src/swarm/registry.rs b/swarm/src/registry.rs similarity index 97% rename from core/src/swarm/registry.rs rename to swarm/src/registry.rs index e4a064b4..ad00135d 100644 --- a/core/src/swarm/registry.rs +++ b/swarm/src/registry.rs @@ -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); diff --git a/core/src/swarm/toggle.rs b/swarm/src/toggle.rs similarity index 95% rename from core/src/swarm/toggle.rs rename to swarm/src/toggle.rs index 9e30e572..f6cadd82 100644 --- a/core/src/swarm/toggle.rs +++ b/swarm/src/toggle.rs @@ -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;