libp2p_swarm/
dummy.rs

1use crate::behaviour::{FromSwarm, NetworkBehaviour, PollParameters, ToSwarm};
2use crate::connection::ConnectionId;
3use crate::handler::{
4    ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
5};
6use crate::{
7    ConnectionDenied, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
8    THandler, THandlerInEvent, THandlerOutEvent,
9};
10use libp2p_core::upgrade::DeniedUpgrade;
11use libp2p_core::Endpoint;
12use libp2p_core::Multiaddr;
13use libp2p_identity::PeerId;
14use std::task::{Context, Poll};
15use void::Void;
16
17/// Implementation of [`NetworkBehaviour`] that doesn't do anything.
18pub struct Behaviour;
19
20impl NetworkBehaviour for Behaviour {
21    type ConnectionHandler = ConnectionHandler;
22    type ToSwarm = Void;
23
24    fn handle_established_inbound_connection(
25        &mut self,
26        _: ConnectionId,
27        _: PeerId,
28        _: &Multiaddr,
29        _: &Multiaddr,
30    ) -> Result<THandler<Self>, ConnectionDenied> {
31        Ok(ConnectionHandler)
32    }
33
34    fn handle_established_outbound_connection(
35        &mut self,
36        _: ConnectionId,
37        _: PeerId,
38        _: &Multiaddr,
39        _: Endpoint,
40    ) -> Result<THandler<Self>, ConnectionDenied> {
41        Ok(ConnectionHandler)
42    }
43
44    fn on_connection_handler_event(
45        &mut self,
46        _: PeerId,
47        _: ConnectionId,
48        event: THandlerOutEvent<Self>,
49    ) {
50        void::unreachable(event)
51    }
52
53    fn poll(
54        &mut self,
55        _: &mut Context<'_>,
56        _: &mut impl PollParameters,
57    ) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
58        Poll::Pending
59    }
60
61    fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
62        match event {
63            FromSwarm::ConnectionEstablished(_)
64            | FromSwarm::ConnectionClosed(_)
65            | FromSwarm::AddressChange(_)
66            | FromSwarm::DialFailure(_)
67            | FromSwarm::ListenFailure(_)
68            | FromSwarm::NewListener(_)
69            | FromSwarm::NewListenAddr(_)
70            | FromSwarm::ExpiredListenAddr(_)
71            | FromSwarm::ListenerError(_)
72            | FromSwarm::ListenerClosed(_)
73            | FromSwarm::NewExternalAddrCandidate(_)
74            | FromSwarm::ExternalAddrExpired(_)
75            | FromSwarm::ExternalAddrConfirmed(_) => {}
76        }
77    }
78}
79
80/// An implementation of [`ConnectionHandler`] that neither handles any protocols nor does it keep the connection alive.
81#[derive(Clone)]
82pub struct ConnectionHandler;
83
84impl crate::handler::ConnectionHandler for ConnectionHandler {
85    type FromBehaviour = Void;
86    type ToBehaviour = Void;
87    type Error = Void;
88    type InboundProtocol = DeniedUpgrade;
89    type OutboundProtocol = DeniedUpgrade;
90    type InboundOpenInfo = ();
91    type OutboundOpenInfo = Void;
92
93    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
94        SubstreamProtocol::new(DeniedUpgrade, ())
95    }
96
97    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
98        void::unreachable(event)
99    }
100
101    fn connection_keep_alive(&self) -> KeepAlive {
102        KeepAlive::No
103    }
104
105    #[allow(deprecated)]
106    fn poll(
107        &mut self,
108        _: &mut Context<'_>,
109    ) -> Poll<
110        ConnectionHandlerEvent<
111            Self::OutboundProtocol,
112            Self::OutboundOpenInfo,
113            Self::ToBehaviour,
114            Self::Error,
115        >,
116    > {
117        Poll::Pending
118    }
119
120    fn on_connection_event(
121        &mut self,
122        event: ConnectionEvent<
123            Self::InboundProtocol,
124            Self::OutboundProtocol,
125            Self::InboundOpenInfo,
126            Self::OutboundOpenInfo,
127        >,
128    ) {
129        match event {
130            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
131                protocol, ..
132            }) => void::unreachable(protocol),
133            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
134                protocol, ..
135            }) => void::unreachable(protocol),
136            ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
137                StreamUpgradeError::Timeout => unreachable!(),
138                StreamUpgradeError::Apply(e) => void::unreachable(e),
139                StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => {
140                    unreachable!("Denied upgrade does not support any protocols")
141                }
142            },
143            ConnectionEvent::AddressChange(_)
144            | ConnectionEvent::ListenUpgradeError(_)
145            | ConnectionEvent::LocalProtocolsChange(_)
146            | ConnectionEvent::RemoteProtocolsChange(_) => {}
147        }
148    }
149}