Expand description
High-level network manager.
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:
- A network identity of the local node in form of a
PeerId
. - 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 thetransport
module for more information. - 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 ConnectionHandler
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.
Modules
- Once a connection to a remote peer is established, a
ConnectionHandler
negotiates and handles one or more specific protocols on the connection.
Structs
- An record in a prioritised list of addresses.
- Network connection information.
- Implementation of
ConnectionHandler
that combines two protocols into one. - Information about a connection limit.
- The configurable connection limits.
- Implementation of
IntoConnectionHandler
that combines two protocols into one. - Information about the connections obtained by
Swarm::network_info()
. - A
ConnectionHandler
that opens a new substream for each request. - Configuration parameters for the
OneShotHandler
- Configuration of inbound or outbound substream protocol(s) for a
ConnectionHandler
. - Contains the state of the network, plus the way it should behave.
- A
SwarmBuilder
provides an API for configuring and constructing aSwarm
. - Parameters passed to
poll()
, that theNetworkBehaviour
has access to.
Enums
- The result of adding an address to an ordered list of addresses with associated scores.
- The “score” of an address w.r.t. an ordered collection of addresses.
- The options which connections to close.
- Errors that can occur in the context of an established
Connection
. - Event produced by a handler.
- Error that can happen on an outbound substream opening attempt.
- The possible failures of dialing.
- How long the connection should be kept alive.
- An action that a
NetworkBehaviour
can trigger in theSwarm
in whose context it is executing. - The options w.r.t. which connection handler to notify of an event.
- Errors that can occur in the context of a pending
Connection
. - Event generated by the
Swarm
.
Traits
- A handler for a set of protocols used on a connection with a remote.
- Implemented on objects that can run a
Future
in the background. - Prototype for a
ConnectionHandler
. - A
NetworkBehaviour
defines the behaviour of the local node on the network. - Parameters passed to
poll()
, that theNetworkBehaviour
has access to.
Type Definitions
- Substream for which a protocol has been chosen.
- Errors that can occur in the context of a pending incoming
Connection
. - Errors that can occur in the context of a pending outgoing
Connection
.
Derive Macros
- Generates a delegating
NetworkBehaviour
implementation for the struct this is used for. See the trait documentation for better description.