Trait tet_libp2p_swarm::NetworkBehaviour[][src]

pub trait NetworkBehaviour: Send + 'static {
    type ProtocolsHandler: IntoProtocolsHandler;
    type OutEvent: Send + 'static;
    fn new_handler(&mut self) -> Self::ProtocolsHandler;
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
fn inject_connected(&mut self, peer_id: &PeerId);
fn inject_disconnected(&mut self, peer_id: &PeerId);
fn inject_event(
        &mut self,
        peer_id: PeerId,
        connection: ConnectionId,
        event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
    );
fn poll(
        &mut self,
        cx: &mut Context<'_>,
        params: &mut impl PollParameters
    ) -> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>; fn inject_connection_established(
        &mut self,
        _: &PeerId,
        _: &ConnectionId,
        _: &ConnectedPoint
    ) { ... }
fn inject_connection_closed(
        &mut self,
        _: &PeerId,
        _: &ConnectionId,
        _: &ConnectedPoint
    ) { ... }
fn inject_address_change(
        &mut self,
        _: &PeerId,
        _: &ConnectionId,
        _old: &ConnectedPoint,
        _new: &ConnectedPoint
    ) { ... }
fn inject_addr_reach_failure(
        &mut self,
        _peer_id: Option<&PeerId>,
        _addr: &Multiaddr,
        _error: &dyn Error
    ) { ... }
fn inject_dial_failure(&mut self, _peer_id: &PeerId) { ... }
fn inject_new_listen_addr(&mut self, _addr: &Multiaddr) { ... }
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) { ... }
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) { ... }
fn inject_listener_error(
        &mut self,
        _id: ListenerId,
        _err: &(dyn Error + 'static)
    ) { ... }
fn inject_listener_closed(
        &mut self,
        _id: ListenerId,
        _reason: Result<(), &Error>
    ) { ... } }

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.

By default the derive sets the NetworkBehaviour::OutEvent as () but this can be overridden with #[behaviour(out_event = "AnotherType")].

Struct members that don’t implement NetworkBehaviour must be annotated with #[behaviour(ignore)].

By default, events generated by the remaining members are delegated to NetworkBehaviourEventProcess implementations. Those must be provided by the user on the type that NetworkBehaviour is derived on.

Alternatively, users can specify #[behaviour(event_process = false)]. In this case, users should provide a custom out_event and implement From for each of the event types generated by the struct members. Not processing events within the derived NetworkBehaviour will cause them to be emitted as part of polling the swarm in SwarmEvent::Behaviour.

Optionally one can provide a custom poll function through the #[behaviour(poll_method = "poll")] attribute. This function must have the same signature as the NetworkBehaviour function and will be called last within the generated NetworkBehaviour implementation.

Associated Types

type ProtocolsHandler: IntoProtocolsHandler[src]

Handler for all the protocols the network behaviour supports.

type OutEvent: Send + 'static[src]

Event generated by the NetworkBehaviour and that the swarm will report back.

Loading content...

Required methods

fn new_handler(&mut self) -> Self::ProtocolsHandler[src]

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_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>[src]

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)[src]

Indicates the behaviour that we connected to the node with the given peer id.

This node now has a handler (as spawned by new_handler) running in the background.

This method is only called when the connection to the peer is established, preceded by inject_connection_established.

fn inject_disconnected(&mut self, peer_id: &PeerId)[src]

Indicates the behaviour that we disconnected from the node with the given peer id.

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.

This method is only called when the last established connection to the peer is closed, preceded by inject_connection_closed.

fn inject_event(
    &mut self,
    peer_id: PeerId,
    connection: ConnectionId,
    event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
)
[src]

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>>
[src]

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.

Loading content...

Provided methods

fn inject_connection_established(
    &mut self,
    _: &PeerId,
    _: &ConnectionId,
    _: &ConnectedPoint
)
[src]

Informs the behaviour about a newly established connection to a peer.

fn inject_connection_closed(
    &mut self,
    _: &PeerId,
    _: &ConnectionId,
    _: &ConnectedPoint
)
[src]

Informs the behaviour about a closed connection to a peer.

A call to this method is always paired with an earlier call to inject_connection_established with the same peer ID, connection ID and endpoint.

fn inject_address_change(
    &mut self,
    _: &PeerId,
    _: &ConnectionId,
    _old: &ConnectedPoint,
    _new: &ConnectedPoint
)
[src]

Informs the behaviour that the ConnectedPoint of an existing connection has changed.

fn inject_addr_reach_failure(
    &mut self,
    _peer_id: Option<&PeerId>,
    _addr: &Multiaddr,
    _error: &dyn Error
)
[src]

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)[src]

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)[src]

Indicates to the behaviour that we have started listening on a new multiaddr.

fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr)[src]

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)[src]

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)
)
[src]

A listener experienced an error.

fn inject_listener_closed(
    &mut self,
    _id: ListenerId,
    _reason: Result<(), &Error>
)
[src]

A listener closed.

Loading content...

Implementors

impl NetworkBehaviour for DummyBehaviour[src]

type ProtocolsHandler = DummyProtocolsHandler

type OutEvent = Void

impl<TBehaviour> NetworkBehaviour for Toggle<TBehaviour> where
    TBehaviour: NetworkBehaviour
[src]

type ProtocolsHandler = ToggleIntoProtoHandler<TBehaviour::ProtocolsHandler>

type OutEvent = TBehaviour::OutEvent

Loading content...