Struct communication::behaviour::P2PNetworkBehaviour[][src]

pub struct P2PNetworkBehaviour<Req: MessageEvent, Res: MessageEvent> { /* fields omitted */ }

The P2PNetworkBehaviour determines the behaviour of the p2p-network. It combines the following protocols from libp2p

  • mDNS for peer discovery within the local network
  • identify-protocol to receive identifying information of the remote peer
  • RequestResponse Protocol for sending generic request Req and response Res messages

The P2PNetworkBehaviour itself is only effective if a new [ExpandedSwarm] is created for it, this swarm is the entry point for all communication to remote peers, and contains the current state.

The P2PNetworkBehaviour implements a custom poll method that creates P2PEvents from the events of the different protocols, it can be polled with the next() or next_event() methods of the [ExpandedSwarm].

Implementations

impl<Req: MessageEvent, Res: MessageEvent> P2PNetworkBehaviour<Req, Res>[src]

pub async fn init_swarm(
    local_keys: Keypair,
    config: BehaviourConfig
) -> Result<Swarm<P2PNetworkBehaviour<Req, Res>>, BehaviourError>
[src]

Creates a new P2PNetworkBehaviour and returns the swarm for it. The returned Swarm<P2PNetworkBehaviour> is the entry point for all communication with remote peers, i.g. to send requests and responses. Additionally to the methods of the P2PNetworkBehaviour there is a range of [libp2p::ExpandedSwarm] functions that can be used for swarm interaction like dialing a new peer.

Example

use async_std::task;
use communication::behaviour::{BehaviourConfig, P2PNetworkBehaviour};
use libp2p::identity::Keypair;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Request {
    Ping,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Response {
    Pong,
}

let local_keys = Keypair::generate_ed25519();
let config = BehaviourConfig::default();
let mut swarm = task::block_on(P2PNetworkBehaviour::<Request, Response>::init_swarm(local_keys, config))
    .expect("Init swarm failed.");

pub fn add_peer_addr(&mut self, peer_id: PeerId, addr: Multiaddr)[src]

pub fn remove_peer_addr(&mut self, peer_id: &PeerId, addr: &Multiaddr)[src]

pub fn remove_peer(&mut self, peer_id: &PeerId) -> Option<Vec<Multiaddr>>[src]

pub fn get_peer_addr(&self, peer_id: &PeerId) -> Option<&Vec<Multiaddr>>[src]

pub fn get_all_peers(&self) -> Vec<&PeerId>[src]

pub fn get_active_mdns_peers(&mut self) -> Vec<&PeerId>[src]

Get the peers discovered by mdns

pub fn send_request(&mut self, peer_id: &PeerId, request: Req) -> RequestId[src]

pub fn send_response(
    &mut self,
    request_id: &RequestId,
    response: Res
) -> Result<(), Res>
[src]

Trait Implementations

impl<Req: MessageEvent, Res: MessageEvent> NetworkBehaviour for P2PNetworkBehaviour<Req, Res> where
    Mdns: NetworkBehaviour,
    Self: NetworkBehaviourEventProcess<<Mdns as NetworkBehaviour>::OutEvent>,
    Identify: NetworkBehaviour,
    Self: NetworkBehaviourEventProcess<<Identify as NetworkBehaviour>::OutEvent>,
    RequestResponse<MessageCodec<Req, Res>>: NetworkBehaviour,
    Self: NetworkBehaviourEventProcess<<RequestResponse<MessageCodec<Req, Res>> as NetworkBehaviour>::OutEvent>,
    Relay: NetworkBehaviour,
    Self: NetworkBehaviourEventProcess<<Relay as NetworkBehaviour>::OutEvent>, 
[src]

type ProtocolsHandler = IntoProtocolsHandlerSelect<IntoProtocolsHandlerSelect<IntoProtocolsHandlerSelect<<Mdns as NetworkBehaviour>::ProtocolsHandler, <Identify as NetworkBehaviour>::ProtocolsHandler>, <RequestResponse<MessageCodec<Req, Res>> as NetworkBehaviour>::ProtocolsHandler>, <Relay as NetworkBehaviour>::ProtocolsHandler>

Handler for all the protocols the network behaviour supports.

type OutEvent = P2PEvent<Req, Res>

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

impl<Req: MessageEvent, Res: MessageEvent> NetworkBehaviourEventProcess<()> for P2PNetworkBehaviour<Req, Res>[src]

impl<Req: MessageEvent, Res: MessageEvent> NetworkBehaviourEventProcess<IdentifyEvent> for P2PNetworkBehaviour<Req, Res>[src]

impl<Req: MessageEvent, Res: MessageEvent> NetworkBehaviourEventProcess<MdnsEvent> for P2PNetworkBehaviour<Req, Res>[src]

impl<Req: MessageEvent, Res: MessageEvent> NetworkBehaviourEventProcess<RequestResponseEvent<Req, Res, Res>> for P2PNetworkBehaviour<Req, Res>[src]

Auto Trait Implementations

impl<Req, Res> !RefUnwindSafe for P2PNetworkBehaviour<Req, Res>

impl<Req, Res> Send for P2PNetworkBehaviour<Req, Res>

impl<Req, Res> !Sync for P2PNetworkBehaviour<Req, Res>

impl<Req, Res> Unpin for P2PNetworkBehaviour<Req, Res> where
    Req: Unpin,
    Res: Unpin

impl<Req, Res> !UnwindSafe for P2PNetworkBehaviour<Req, Res>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,