swarm/: Rename ProtocolsHandler to ConnectionHandler (#2527)

A `ProtocolsHandler`, now `ConnectionHandler`, handels a connection, not
a protocol. Thus the name `CONNECTIONHandler` is more appropriate.

Next to the rename of `ProtocolsHandler` this commit renames the `mod
protocols_handler` to `mod handler`. Finally all combinators (e.g.
`ProtocolsHandlerSelect`) are renamed appropriately.
This commit is contained in:
Max Inden
2022-02-21 13:32:24 +01:00
committed by GitHub
parent 6511e6ba45
commit fd2be38faf
50 changed files with 924 additions and 910 deletions

View File

@ -28,8 +28,8 @@ use libp2p_core::upgrade::{
EitherUpgrade, InboundUpgrade, OutboundUpgrade, SelectUpgrade, UpgradeError,
};
use libp2p_swarm::{
KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
};
use smallvec::SmallVec;
use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
@ -42,7 +42,7 @@ use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
pub struct IdentifyHandler {
/// Pending events to yield.
events: SmallVec<
[ProtocolsHandlerEvent<
[ConnectionHandlerEvent<
EitherUpgrade<IdentifyProtocol, IdentifyPushProtocol<OutboundPush>>,
(),
IdentifyHandlerEvent,
@ -70,7 +70,7 @@ pub enum IdentifyHandlerEvent {
/// We received a request for identification.
Identify(ReplySubstream<NegotiatedSubstream>),
/// Failed to identify the remote.
IdentificationError(ProtocolsHandlerUpgrErr<io::Error>),
IdentificationError(ConnectionHandlerUpgrErr<io::Error>),
}
/// Identifying information of the local node that is pushed to a remote.
@ -89,7 +89,7 @@ impl IdentifyHandler {
}
}
impl ProtocolsHandler for IdentifyHandler {
impl ConnectionHandler for IdentifyHandler {
type InEvent = IdentifyPush;
type OutEvent = IdentifyHandlerEvent;
type Error = io::Error;
@ -111,10 +111,10 @@ impl ProtocolsHandler for IdentifyHandler {
_: Self::InboundOpenInfo,
) {
match output {
EitherOutput::First(substream) => self.events.push(ProtocolsHandlerEvent::Custom(
EitherOutput::First(substream) => self.events.push(ConnectionHandlerEvent::Custom(
IdentifyHandlerEvent::Identify(substream),
)),
EitherOutput::Second(info) => self.events.push(ProtocolsHandlerEvent::Custom(
EitherOutput::Second(info) => self.events.push(ConnectionHandlerEvent::Custom(
IdentifyHandlerEvent::Identified(info),
)),
}
@ -127,12 +127,12 @@ impl ProtocolsHandler for IdentifyHandler {
) {
match output {
EitherOutput::First(remote_info) => {
self.events.push(ProtocolsHandlerEvent::Custom(
self.events.push(ConnectionHandlerEvent::Custom(
IdentifyHandlerEvent::Identified(remote_info),
));
self.keep_alive = KeepAlive::No;
}
EitherOutput::Second(()) => self.events.push(ProtocolsHandlerEvent::Custom(
EitherOutput::Second(()) => self.events.push(ConnectionHandlerEvent::Custom(
IdentifyHandlerEvent::IdentificationPushed,
)),
}
@ -140,7 +140,7 @@ impl ProtocolsHandler for IdentifyHandler {
fn inject_event(&mut self, IdentifyPush(push): Self::InEvent) {
self.events
.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
EitherUpgrade::B(IdentifyPushProtocol::outbound(push)),
(),
@ -151,7 +151,7 @@ impl ProtocolsHandler for IdentifyHandler {
fn inject_dial_upgrade_error(
&mut self,
_info: Self::OutboundOpenInfo,
err: ProtocolsHandlerUpgrErr<
err: ConnectionHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
@ -160,7 +160,7 @@ impl ProtocolsHandler for IdentifyHandler {
UpgradeError::Apply(EitherError::A(ioe)) => UpgradeError::Apply(ioe),
UpgradeError::Apply(EitherError::B(ioe)) => UpgradeError::Apply(ioe),
});
self.events.push(ProtocolsHandlerEvent::Custom(
self.events.push(ConnectionHandlerEvent::Custom(
IdentifyHandlerEvent::IdentificationError(err),
));
self.keep_alive = KeepAlive::No;
@ -175,7 +175,7 @@ impl ProtocolsHandler for IdentifyHandler {
&mut self,
cx: &mut Context<'_>,
) -> Poll<
ProtocolsHandlerEvent<
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
IdentifyHandlerEvent,
@ -191,7 +191,7 @@ impl ProtocolsHandler for IdentifyHandler {
Poll::Pending => Poll::Pending,
Poll::Ready(()) => {
self.next_id.reset(self.interval);
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest {
let ev = ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(EitherUpgrade::A(IdentifyProtocol), ()),
};
Poll::Ready(ev)

View File

@ -29,9 +29,8 @@ use libp2p_core::{
};
use libp2p_swarm::{
dial_opts::{self, DialOpts},
AddressScore, DialError, IntoProtocolsHandler, NegotiatedSubstream, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters, ProtocolsHandler,
ProtocolsHandlerUpgrErr,
AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError, IntoConnectionHandler,
NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
use lru::LruCache;
use std::{
@ -213,10 +212,10 @@ impl Identify {
}
impl NetworkBehaviour for Identify {
type ProtocolsHandler = IdentifyHandler;
type ConnectionHandler = IdentifyHandler;
type OutEvent = IdentifyEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
IdentifyHandler::new(self.config.initial_delay, self.config.interval)
}
@ -254,7 +253,7 @@ impl NetworkBehaviour for Identify {
peer_id: &PeerId,
conn: &ConnectionId,
_: &ConnectedPoint,
_: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
_: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
) {
if remaining_established == 0 {
@ -268,7 +267,7 @@ impl NetworkBehaviour for Identify {
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
_: Self::ProtocolsHandler,
_: Self::ConnectionHandler,
error: &DialError,
) {
if let Some(peer_id) = peer_id {
@ -302,7 +301,7 @@ impl NetworkBehaviour for Identify {
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
event: <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
) {
match event {
IdentifyHandlerEvent::Identified(mut info) => {
@ -356,7 +355,7 @@ impl NetworkBehaviour for Identify {
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}
@ -433,7 +432,7 @@ impl NetworkBehaviour for Identify {
Poll::Ready(Err(err)) => {
let event = IdentifyEvent::Error {
peer_id: peer,
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(
err,
)),
};
@ -486,7 +485,7 @@ pub enum IdentifyEvent {
/// The peer with whom the error originated.
peer_id: PeerId,
/// The error that occurred.
error: ProtocolsHandlerUpgrErr<io::Error>,
error: ConnectionHandlerUpgrErr<io::Error>,
},
}