[−][src]Trait libp2p::swarm::NetworkBehaviour
A behaviour for the network. Allows customizing the swarm.
This trait has been designed to be composable. Multiple implementations can be combined into one that handles all the behaviours at once.
Deriving NetworkBehaviour
Crate users can implement this trait by using the the #[derive(NetworkBehaviour)]
proc macro re-exported by the libp2p
crate. The macro generates a delegating trait
implementation for the struct
, which delegates method calls to all trait members. Any events
generated by struct members are delegated to [NetworkBehaviourEventProcess
] implementations
which are expected to be provided by the user.
Optionally one can implement a custom poll
function, which needs to be tagged with the
#[behaviour(poll_method = "poll")]
attribute, and would be called last with no parameters.
By default the derive sets the NetworkBehaviour::OutEvent
as ()
but this can be overriden
with #[behaviour(out_event = "AnotherType")]
.
#[behaviour(ignore)]
can be added on a struct field to disable generation of delegation to
the fields which do not implement NetworkBehaviour
.
Associated Types
type ProtocolsHandler: IntoProtocolsHandler
Handler for all the protocols the network behaviour supports.
type OutEvent: 'static + Send
Event generated by the NetworkBehaviour
and that the swarm will report back.
Required methods
fn new_handler(&mut self) -> Self::ProtocolsHandler
Creates a new ProtocolsHandler
for a connection with a peer.
Every time an incoming connection is opened, and every time we start dialing a node, this method is called.
The returned object is a handler for that specific connection, and will be moved to a background task dedicated to that connection.
The network behaviour (ie. the implementation of this trait) and the handlers it has
spawned (ie. the objects returned by new_handler
) can communicate by passing messages.
Messages sent from the handler to the behaviour are injected with inject_node_event
, and
the behaviour can send a message to the handler by making poll
return SendEvent
.
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>
Addresses that this behaviour is aware of for this specific peer, and that may allow reaching the peer.
The addresses will be tried in the order returned by this function, which means that they should be ordered by decreasing likelihood of reachability. In other words, the first address should be the most likely to be reachable.
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint)
Indicates the behaviour that we connected to the node with the given peer id through the given endpoint.
This node now has a handler (as spawned by new_handler
) running in the background.
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint)
Indicates the behaviour that we disconnected from the node with the given peer id. The endpoint is the one we used to be connected to.
There is no handler running anymore for this node. Any event that has been sent to it may or may not have been processed by the handler.
fn inject_node_event(
&mut self,
peer_id: PeerId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
&mut self,
peer_id: PeerId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
Informs the behaviour about an event generated by the handler dedicated to the peer identified by peer_id
.
for the behaviour.
The peer_id
is guaranteed to be in a connected state. In other words, inject_connected
has previously been called with this PeerId
.
fn poll(
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
Polls for things that swarm should do.
This API mimics the API of the Stream
trait. The method may register the current task in
order to wake it up at a later point in time.
Provided methods
fn inject_replaced(
&mut self,
peer_id: PeerId,
closed_endpoint: ConnectedPoint,
new_endpoint: ConnectedPoint
)
&mut self,
peer_id: PeerId,
closed_endpoint: ConnectedPoint,
new_endpoint: ConnectedPoint
)
Indicates the behaviour that we replace the connection from the node with another.
The handler that used to be dedicated to this node has been destroyed and replaced with a new one. Any event that has been sent to it may or may not have been processed.
The default implementation of this method calls inject_disconnected
followed with
inject_connected
. This is a logically safe way to implement this behaviour. However, you
may want to overwrite this method in the situations where this isn't appropriate.
fn inject_addr_reach_failure(
&mut self,
_peer_id: Option<&PeerId>,
_addr: &Multiaddr,
_error: &dyn Error
)
&mut self,
_peer_id: Option<&PeerId>,
_addr: &Multiaddr,
_error: &dyn Error
)
Indicates to the behaviour that we tried to reach an address, but failed.
If we were trying to reach a specific node, its ID is passed as parameter. If this is the
last address to attempt for the given node, then inject_dial_failure
is called afterwards.
fn inject_dial_failure(&mut self, _peer_id: &PeerId)
Indicates to the behaviour that we tried to dial all the addresses known for a node, but failed.
The peer_id
is guaranteed to be in a disconnected state. In other words,
inject_connected
has not been called, or inject_disconnected
has been called since then.
fn inject_new_listen_addr(&mut self, _addr: &Multiaddr)
Indicates to the behaviour that we have started listening on a new multiaddr.
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr)
Indicates to the behaviour that a new multiaddr we were listening on has expired, which means that we are no longer listening in it.
fn inject_new_external_addr(&mut self, _addr: &Multiaddr)
Indicates to the behaviour that we have discovered a new external address for us.
fn inject_listener_error(
&mut self,
_id: ListenerId,
_err: &(dyn Error + 'static)
)
&mut self,
_id: ListenerId,
_err: &(dyn Error + 'static)
)
A listener experienced an error.
fn inject_listener_closed(&mut self, _id: ListenerId)
A listener closed.
Implementors
impl NetworkBehaviour for Floodsub
[src]
type ProtocolsHandler = OneShotHandler<FloodsubConfig, FloodsubRpc, InnerMessage>
type OutEvent = FloodsubEvent
fn new_handler(&mut self) -> <Floodsub as NetworkBehaviour>::ProtocolsHandler
[src]
fn addresses_of_peer(&mut self, &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, id: PeerId, ConnectedPoint)
[src]
fn inject_disconnected(&mut self, id: &PeerId, ConnectedPoint)
[src]
fn inject_node_event(&mut self, propagation_source: PeerId, event: InnerMessage)
[src]
fn poll(
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Floodsub as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Floodsub as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Floodsub as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Floodsub as NetworkBehaviour>::OutEvent>>
impl NetworkBehaviour for Gossipsub
[src]
type ProtocolsHandler = GossipsubHandler
type OutEvent = GossipsubEvent
fn new_handler(&mut self) -> <Gossipsub as NetworkBehaviour>::ProtocolsHandler
[src]
fn addresses_of_peer(&mut self, &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, id: PeerId, ConnectedPoint)
[src]
fn inject_disconnected(&mut self, id: &PeerId, ConnectedPoint)
[src]
fn inject_node_event(&mut self, propagation_source: PeerId, event: GossipsubRpc)
[src]
fn poll(
&mut self,
cx: &mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Gossipsub as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Gossipsub as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
cx: &mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Gossipsub as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Gossipsub as NetworkBehaviour>::OutEvent>>
impl NetworkBehaviour for Identify
[src]
type ProtocolsHandler = IdentifyHandler
type OutEvent = IdentifyEvent
fn new_handler(&mut self) -> <Identify as NetworkBehaviour>::ProtocolsHandler
[src]
fn addresses_of_peer(&mut self, &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint)
[src]
fn inject_disconnected(&mut self, peer_id: &PeerId, ConnectedPoint)
[src]
fn inject_node_event(
&mut self,
peer_id: PeerId,
event: <<Identify as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
[src]
&mut self,
peer_id: PeerId,
event: <<Identify as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
fn poll(
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Identify as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Identify as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Identify as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Identify as NetworkBehaviour>::OutEvent>>
impl NetworkBehaviour for Mdns
[src]
type ProtocolsHandler = DummyProtocolsHandler
type OutEvent = MdnsEvent
fn new_handler(&mut self) -> <Mdns as NetworkBehaviour>::ProtocolsHandler
[src]
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, PeerId, ConnectedPoint)
[src]
fn inject_disconnected(&mut self, &PeerId, ConnectedPoint)
[src]
fn inject_node_event(
&mut self,
PeerId,
_ev: <<Mdns as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
[src]
&mut self,
PeerId,
_ev: <<Mdns as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
fn poll(
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Mdns as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Mdns as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Mdns as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Mdns as NetworkBehaviour>::OutEvent>>
impl NetworkBehaviour for Ping
[src]
type ProtocolsHandler = PingHandler
type OutEvent = PingEvent
fn new_handler(&mut self) -> <Ping as NetworkBehaviour>::ProtocolsHandler
[src]
fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, PeerId, ConnectedPoint)
[src]
fn inject_disconnected(&mut self, &PeerId, ConnectedPoint)
[src]
fn inject_node_event(
&mut self,
peer: PeerId,
result: Result<PingSuccess, PingFailure>
)
[src]
&mut self,
peer: PeerId,
result: Result<PingSuccess, PingFailure>
)
fn poll(
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<Void, PingEvent>>
[src]
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<Void, PingEvent>>
impl NetworkBehaviour for DummyBehaviour
[src]
type ProtocolsHandler = DummyProtocolsHandler
type OutEvent = Void
fn new_handler(
&mut self
) -> <DummyBehaviour as NetworkBehaviour>::ProtocolsHandler
[src]
&mut self
) -> <DummyBehaviour as NetworkBehaviour>::ProtocolsHandler
fn addresses_of_peer(&mut self, &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, PeerId, ConnectedPoint)
[src]
fn inject_disconnected(&mut self, &PeerId, ConnectedPoint)
[src]
fn inject_node_event(
&mut self,
PeerId,
<<DummyBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
[src]
&mut self,
PeerId,
<<DummyBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent
)
fn poll(
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<DummyBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <DummyBehaviour as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
&mut Context,
&mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<DummyBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <DummyBehaviour as NetworkBehaviour>::OutEvent>>
impl<TBehaviour> NetworkBehaviour for Toggle<TBehaviour> where
TBehaviour: NetworkBehaviour,
[src]
TBehaviour: NetworkBehaviour,
type ProtocolsHandler = ToggleIntoProtoHandler<<TBehaviour as NetworkBehaviour>::ProtocolsHandler>
type OutEvent = <TBehaviour as NetworkBehaviour>::OutEvent
fn new_handler(
&mut self
) -> <Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler
[src]
&mut self
) -> <Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint)
[src]
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint)
[src]
fn inject_replaced(
&mut self,
peer_id: PeerId,
closed_endpoint: ConnectedPoint,
new_endpoint: ConnectedPoint
)
[src]
&mut self,
peer_id: PeerId,
closed_endpoint: ConnectedPoint,
new_endpoint: ConnectedPoint
)
fn inject_node_event(
&mut self,
peer_id: PeerId,
event: <<<Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
[src]
&mut self,
peer_id: PeerId,
event: <<<Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
error: &dyn Error
)
[src]
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
error: &dyn Error
)
fn inject_dial_failure(&mut self, peer_id: &PeerId)
[src]
fn inject_new_listen_addr(&mut self, addr: &Multiaddr)
[src]
fn inject_expired_listen_addr(&mut self, addr: &Multiaddr)
[src]
fn inject_new_external_addr(&mut self, addr: &Multiaddr)
[src]
fn poll(
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<<Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, <Toggle<TBehaviour> as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
cx: &mut Context,
params: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<<Toggle<TBehaviour> as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, <Toggle<TBehaviour> as NetworkBehaviour>::OutEvent>>
impl<TStore> NetworkBehaviour for Kademlia<TStore> where
TStore: RecordStore<'a> + Send + 'static,
[src]
TStore: RecordStore<'a> + Send + 'static,
type ProtocolsHandler = KademliaHandler<QueryId>
type OutEvent = KademliaEvent
fn new_handler(
&mut self
) -> <Kademlia<TStore> as NetworkBehaviour>::ProtocolsHandler
[src]
&mut self
) -> <Kademlia<TStore> as NetworkBehaviour>::ProtocolsHandler
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>
[src]
fn inject_connected(&mut self, peer: PeerId, endpoint: ConnectedPoint)
[src]
fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
err: &dyn Error
)
[src]
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
err: &dyn Error
)
fn inject_dial_failure(&mut self, peer_id: &PeerId)
[src]
fn inject_disconnected(&mut self, id: &PeerId, _old_endpoint: ConnectedPoint)
[src]
fn inject_replaced(
&mut self,
peer_id: PeerId,
_old: ConnectedPoint,
new_endpoint: ConnectedPoint
)
[src]
&mut self,
peer_id: PeerId,
_old: ConnectedPoint,
new_endpoint: ConnectedPoint
)
fn inject_node_event(
&mut self,
source: PeerId,
event: KademliaHandlerEvent<QueryId>
)
[src]
&mut self,
source: PeerId,
event: KademliaHandlerEvent<QueryId>
)
fn poll(
&mut self,
cx: &mut Context,
parameters: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Kademlia<TStore> as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Kademlia<TStore> as NetworkBehaviour>::OutEvent>>
[src]
&mut self,
cx: &mut Context,
parameters: &mut impl PollParameters
) -> Poll<NetworkBehaviourAction<<<Kademlia<TStore> as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent, <Kademlia<TStore> as NetworkBehaviour>::OutEvent>>