Expand description
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:
- 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 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.
Re-exports§
pub use protocols_handler::IntoProtocolsHandler;
pub use protocols_handler::IntoProtocolsHandlerSelect;
pub use protocols_handler::KeepAlive;
pub use protocols_handler::ProtocolsHandler;
pub use protocols_handler::ProtocolsHandlerEvent;
pub use protocols_handler::ProtocolsHandlerSelect;
pub use protocols_handler::ProtocolsHandlerUpgrErr;
pub use protocols_handler::OneShotHandler;
pub use protocols_handler::OneShotHandlerConfig;
pub use protocols_handler::SubstreamProtocol;
Modules§
- protocols_
handler - Once a connection to a remote peer is established, a
ProtocolsHandler
negotiates and handles one or more specific protocols on the connection. - toggle
Structs§
- Address
Record - An record in a prioritised list of addresses.
- Dummy
Behaviour - Dummy implementation of
NetworkBehaviour
that doesn’t do anything. - Expanded
Swarm - Contains the state of the network, plus the way it should behave.
- Swarm
Builder - A
SwarmBuilder
provides an API for configuring and constructing aSwarm
, including the underlyingNetwork
. - Swarm
Poll Parameters - Parameters passed to
poll()
, that theNetworkBehaviour
has access to.
Enums§
- AddAddress
Result - The result of adding an address to an ordered list of addresses with associated scores.
- Address
Score - The “score” of an address w.r.t. an ordered collection of addresses.
- Dial
Error - The possible failures of
ExpandedSwarm::dial
. - Dial
Peer Condition - The available conditions under which a new dialing attempt to
a peer is initiated when requested by
NetworkBehaviourAction::DialPeer
. - Network
Behaviour Action - An action that a
NetworkBehaviour
can trigger in theSwarm
in whose context it is executing. - Notify
Handler - The options w.r.t. which connection handler to notify of an event.
- Swarm
Event - Event generated by the
Swarm
.
Traits§
- Network
Behaviour - A behaviour for the network. Allows customizing the swarm.
- Network
Behaviour Event Process - When deriving
NetworkBehaviour
this trait must by default be implemented for all the possible event types generated by the inner behaviours. - Poll
Parameters - Parameters passed to
poll()
, that theNetworkBehaviour
has access to.
Type Aliases§
- Negotiated
Substream - Substream for which a protocol has been chosen.
- Swarm
- Contains the state of the network, plus the way it should behave.