Skip to main content

connexa/behaviour/
dummy.rs

1use libp2p::core::Endpoint;
2use libp2p::core::upgrade::DeniedUpgrade;
3use libp2p::swarm::derive_prelude::PortUse;
4use libp2p::swarm::handler::{
5    ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
6};
7use libp2p::swarm::{
8    ConnectionDenied, ConnectionHandler, ConnectionHandlerEvent, ConnectionId, FromSwarm,
9    NetworkBehaviour, StreamUpgradeError, SubstreamProtocol, THandler, THandlerInEvent,
10    THandlerOutEvent, ToSwarm,
11};
12use libp2p::{Multiaddr, PeerId};
13use std::{
14    convert::Infallible,
15    task::{Context, Poll},
16};
17
18/// Implementation of [`NetworkBehaviour`] that doesn't do anything.
19#[derive(Debug)]
20pub struct Behaviour;
21
22impl NetworkBehaviour for Behaviour {
23    type ConnectionHandler = DummyHandler;
24    type ToSwarm = Infallible;
25
26    fn handle_established_inbound_connection(
27        &mut self,
28        _: ConnectionId,
29        _: PeerId,
30        _: &Multiaddr,
31        _: &Multiaddr,
32    ) -> Result<THandler<Self>, ConnectionDenied> {
33        Ok(DummyHandler)
34    }
35
36    fn handle_established_outbound_connection(
37        &mut self,
38        _: ConnectionId,
39        _: PeerId,
40        _: &Multiaddr,
41        _: Endpoint,
42        _: PortUse,
43    ) -> Result<THandler<Self>, ConnectionDenied> {
44        Ok(DummyHandler)
45    }
46
47    fn on_swarm_event(&mut self, _event: FromSwarm) {}
48
49    fn on_connection_handler_event(
50        &mut self,
51        _: PeerId,
52        _: ConnectionId,
53        event: THandlerOutEvent<Self>,
54    ) {
55        libp2p::core::util::unreachable(event)
56    }
57
58    fn poll(&mut self, _: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
59        Poll::Pending
60    }
61}
62
63/// An implementation of [`ConnectionHandler`] that neither handles any protocols nor does it keep
64/// the connection alive.
65#[derive(Clone)]
66pub struct DummyHandler;
67
68impl ConnectionHandler for DummyHandler {
69    type FromBehaviour = Infallible;
70    type ToBehaviour = Infallible;
71    type InboundProtocol = DeniedUpgrade;
72    type OutboundProtocol = DeniedUpgrade;
73    type InboundOpenInfo = ();
74    type OutboundOpenInfo = ();
75
76    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
77        SubstreamProtocol::new(DeniedUpgrade, ())
78    }
79
80    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
81        libp2p::core::util::unreachable(event)
82    }
83
84    fn poll(
85        &mut self,
86        _: &mut Context<'_>,
87    ) -> Poll<ConnectionHandlerEvent<Self::OutboundProtocol, (), Self::ToBehaviour>> {
88        Poll::Pending
89    }
90
91    fn on_connection_event(
92        &mut self,
93        event: ConnectionEvent<Self::InboundProtocol, Self::OutboundProtocol>,
94    ) {
95        match event {
96            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
97                protocol, ..
98            }) => libp2p::core::util::unreachable(protocol),
99
100            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
101                protocol, ..
102            }) => libp2p::core::util::unreachable(protocol),
103
104            ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
105                StreamUpgradeError::Timeout => unreachable!(),
106                StreamUpgradeError::Apply(e) => libp2p::core::util::unreachable(e),
107                StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => {
108                    unreachable!("Denied upgrade does not support any protocols")
109                }
110            },
111            ConnectionEvent::AddressChange(_)
112            | ConnectionEvent::ListenUpgradeError(_)
113            | ConnectionEvent::LocalProtocolsChange(_)
114            | ConnectionEvent::RemoteProtocolsChange(_)
115            | _ => {}
116        }
117    }
118}