Skip to main content

rust_ipfs/p2p/
protocol.rs

1use connexa::prelude::{
2    swarm::{
3        self, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler,
4        THandlerInEvent, ToSwarm,
5    },
6    transport::{transport::PortUse, Endpoint},
7    Multiaddr, PeerId, StreamProtocol,
8};
9
10use std::convert::Infallible;
11use std::task::{Context, Poll};
12
13mod handler;
14
15#[derive(Default, Debug)]
16pub struct Behaviour {
17    protocol: Vec<StreamProtocol>,
18}
19
20impl Behaviour {
21    pub fn iter(&self) -> impl Iterator<Item = String> + '_ {
22        self.protocol.iter().map(|s| s.to_string())
23    }
24}
25
26impl NetworkBehaviour for Behaviour {
27    type ConnectionHandler = handler::Handler;
28    type ToSwarm = Infallible;
29
30    fn handle_pending_inbound_connection(
31        &mut self,
32        _: ConnectionId,
33        _: &Multiaddr,
34        _: &Multiaddr,
35    ) -> Result<(), ConnectionDenied> {
36        Ok(())
37    }
38
39    fn handle_pending_outbound_connection(
40        &mut self,
41        _: ConnectionId,
42        _: Option<PeerId>,
43        _: &[Multiaddr],
44        _: Endpoint,
45    ) -> Result<Vec<Multiaddr>, ConnectionDenied> {
46        Ok(vec![])
47    }
48
49    fn handle_established_inbound_connection(
50        &mut self,
51        _: ConnectionId,
52        _: PeerId,
53        _: &Multiaddr,
54        _: &Multiaddr,
55    ) -> Result<THandler<Self>, ConnectionDenied> {
56        Ok(handler::Handler::default())
57    }
58
59    fn handle_established_outbound_connection(
60        &mut self,
61        _: ConnectionId,
62        _: PeerId,
63        _: &Multiaddr,
64        _: Endpoint,
65        _: PortUse,
66    ) -> Result<THandler<Self>, ConnectionDenied> {
67        Ok(handler::Handler::default())
68    }
69
70    fn on_connection_handler_event(
71        &mut self,
72        _: PeerId,
73        _: ConnectionId,
74        event: swarm::THandlerOutEvent<Self>,
75    ) {
76        match event {
77            handler::Out::Protocol(protocol) => {
78                if self.protocol.ne(&protocol) {
79                    self.protocol = protocol;
80                }
81            }
82        }
83    }
84
85    fn on_swarm_event(&mut self, _: FromSwarm) {}
86
87    fn poll(&mut self, _: &mut Context) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
88        Poll::Pending
89    }
90}