libp2p_swarm/
dummy.rs

1use crate::behaviour::{FromSwarm, NetworkBehaviour, ToSwarm};
2use crate::connection::ConnectionId;
3use crate::handler::{
4    ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
5};
6use crate::{
7    ConnectionDenied, ConnectionHandlerEvent, StreamUpgradeError, SubstreamProtocol, THandler,
8    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(&mut self, _: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
54        Poll::Pending
55    }
56
57    fn on_swarm_event(&mut self, _event: FromSwarm) {}
58}
59
60/// An implementation of [`ConnectionHandler`] that neither handles any protocols nor does it keep the connection alive.
61#[derive(Clone)]
62pub struct ConnectionHandler;
63
64impl crate::handler::ConnectionHandler for ConnectionHandler {
65    type FromBehaviour = Void;
66    type ToBehaviour = Void;
67    type InboundProtocol = DeniedUpgrade;
68    type OutboundProtocol = DeniedUpgrade;
69    type InboundOpenInfo = ();
70    type OutboundOpenInfo = Void;
71
72    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
73        SubstreamProtocol::new(DeniedUpgrade, ())
74    }
75
76    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
77        void::unreachable(event)
78    }
79
80    fn poll(
81        &mut self,
82        _: &mut Context<'_>,
83    ) -> Poll<
84        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
85    > {
86        Poll::Pending
87    }
88
89    fn on_connection_event(
90        &mut self,
91        event: ConnectionEvent<
92            Self::InboundProtocol,
93            Self::OutboundProtocol,
94            Self::InboundOpenInfo,
95            Self::OutboundOpenInfo,
96        >,
97    ) {
98        match event {
99            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
100                protocol, ..
101            }) => void::unreachable(protocol),
102            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
103                protocol, ..
104            }) => void::unreachable(protocol),
105            ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
106                StreamUpgradeError::Timeout => unreachable!(),
107                StreamUpgradeError::Apply(e) => void::unreachable(e),
108                StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => {
109                    unreachable!("Denied upgrade does not support any protocols")
110                }
111            },
112            ConnectionEvent::AddressChange(_)
113            | ConnectionEvent::ListenUpgradeError(_)
114            | ConnectionEvent::LocalProtocolsChange(_)
115            | ConnectionEvent::RemoteProtocolsChange(_) => {}
116        }
117    }
118}