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::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::{
DialError, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters, ProtocolsHandler, ProtocolsHandlerUpgrErr,
ConnectionHandler, ConnectionHandlerUpgrErr, DialError, IntoConnectionHandler,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
use std::collections::{HashMap, HashSet, VecDeque};
use std::task::{Context, Poll};
@ -62,7 +62,7 @@ pub enum UpgradeError {
#[error("Failed to dial peer.")]
Dial,
#[error("Failed to establish substream: {0}.")]
Handler(ProtocolsHandlerUpgrErr<void::Void>),
Handler(ConnectionHandlerUpgrErr<void::Void>),
}
pub struct Behaviour {
@ -83,10 +83,10 @@ impl Behaviour {
}
impl NetworkBehaviour for Behaviour {
type ProtocolsHandler = handler::Prototype;
type ConnectionHandler = handler::Prototype;
type OutEvent = Event;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
handler::Prototype::UnknownConnection
}
@ -142,7 +142,7 @@ impl NetworkBehaviour for Behaviour {
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
handler: Self::ProtocolsHandler,
handler: Self::ConnectionHandler,
_error: &DialError,
) {
match handler {
@ -187,7 +187,7 @@ impl NetworkBehaviour for Behaviour {
peer_id: &PeerId,
connection_id: &ConnectionId,
connected_point: &ConnectedPoint,
_handler: <<Self as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler,
_handler: <<Self as NetworkBehaviour>::ConnectionHandler as IntoConnectionHandler>::Handler,
_remaining_established: usize,
) {
if !connected_point.is_relayed() {
@ -209,7 +209,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
event_source: PeerId,
connection: ConnectionId,
handler_event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
handler_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
) {
match handler_event {
Either::Left(handler::relayed::Event::InboundConnectRequest {
@ -313,7 +313,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_cx: &mut Context<'_>,
poll_parameters: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(poll_parameters));
}

View File

@ -23,9 +23,9 @@ use either::Either;
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::{self, DeniedUpgrade};
use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::protocols_handler::DummyProtocolsHandler;
use libp2p_swarm::protocols_handler::SendWrapper;
use libp2p_swarm::{IntoProtocolsHandler, ProtocolsHandler};
use libp2p_swarm::handler::DummyConnectionHandler;
use libp2p_swarm::handler::SendWrapper;
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler};
pub mod direct;
pub mod relayed;
@ -43,8 +43,8 @@ pub enum Role {
Listener,
}
impl IntoProtocolsHandler for Prototype {
type Handler = Either<relayed::Handler, Either<direct::Handler, DummyProtocolsHandler>>;
impl IntoConnectionHandler for Prototype {
type Handler = Either<relayed::Handler, Either<direct::Handler, DummyConnectionHandler>>;
fn into_handler(self, _remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
match self {
@ -52,7 +52,7 @@ impl IntoProtocolsHandler for Prototype {
if endpoint.is_relayed() {
Either::Left(relayed::Handler::new(endpoint.clone()))
} else {
Either::Right(Either::Right(DummyProtocolsHandler::default()))
Either::Right(Either::Right(DummyConnectionHandler::default()))
}
}
Self::DirectConnection {
@ -68,7 +68,7 @@ impl IntoProtocolsHandler for Prototype {
}
}
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
match self {
Prototype::UnknownConnection => upgrade::EitherUpgrade::A(SendWrapper(
upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}),

View File

@ -18,13 +18,13 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! [`ProtocolsHandler`] handling direct connection upgraded through a relayed connection.
//! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection.
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade};
use libp2p_swarm::{
KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
};
use std::task::{Context, Poll};
use void::Void;
@ -48,10 +48,10 @@ impl Handler {
}
}
impl ProtocolsHandler for Handler {
impl ConnectionHandler for Handler {
type InEvent = void::Void;
type OutEvent = Event;
type Error = ProtocolsHandlerUpgrErr<std::io::Error>;
type Error = ConnectionHandlerUpgrErr<std::io::Error>;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
type OutboundOpenInfo = Void;
@ -80,7 +80,7 @@ impl ProtocolsHandler for Handler {
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
_: ProtocolsHandlerUpgrErr<
_: ConnectionHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
@ -94,7 +94,7 @@ impl ProtocolsHandler for Handler {
&mut self,
_: &mut Context<'_>,
) -> Poll<
ProtocolsHandlerEvent<
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
@ -103,7 +103,7 @@ impl ProtocolsHandler for Handler {
> {
if !self.reported {
self.reported = true;
return Poll::Ready(ProtocolsHandlerEvent::Custom(
return Poll::Ready(ConnectionHandlerEvent::Custom(
Event::DirectConnectionUpgradeSucceeded {
relayed_connection_id: self.relayed_connection_id,
},

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! [`ProtocolsHandler`] handling relayed connection potentially upgraded to a direct connection.
//! [`ConnectionHandler`] handling relayed connection potentially upgraded to a direct connection.
use crate::protocol;
use futures::future::{BoxFuture, FutureExt};
@ -28,10 +28,10 @@ use libp2p_core::either::{EitherError, EitherOutput};
use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::upgrade::{self, DeniedUpgrade, NegotiationError, UpgradeError};
use libp2p_core::ConnectedPoint;
use libp2p_swarm::protocols_handler::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_swarm::{
KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::fmt;
@ -81,11 +81,11 @@ pub enum Event {
remote_addr: Multiaddr,
},
InboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr<void::Void>,
error: ConnectionHandlerUpgrErr<void::Void>,
},
InboundConnectNegotiated(Vec<Multiaddr>),
OutboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr<void::Void>,
error: ConnectionHandlerUpgrErr<void::Void>,
},
OutboundConnectNegotiated {
remote_addrs: Vec<Multiaddr>,
@ -131,17 +131,17 @@ pub struct Handler {
endpoint: ConnectedPoint,
/// A pending fatal error that results in the connection being closed.
pending_error: Option<
ProtocolsHandlerUpgrErr<
ConnectionHandlerUpgrErr<
EitherError<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
>,
>,
/// Queue of events to return when polled.
queued_events: VecDeque<
ProtocolsHandlerEvent<
<Self as ProtocolsHandler>::OutboundProtocol,
<Self as ProtocolsHandler>::OutboundOpenInfo,
<Self as ProtocolsHandler>::OutEvent,
<Self as ProtocolsHandler>::Error,
ConnectionHandlerEvent<
<Self as ConnectionHandler>::OutboundProtocol,
<Self as ConnectionHandler>::OutboundOpenInfo,
<Self as ConnectionHandler>::OutEvent,
<Self as ConnectionHandler>::Error,
>,
>,
/// Inbound connects, accepted by the behaviour, pending completion.
@ -163,10 +163,10 @@ impl Handler {
}
}
impl ProtocolsHandler for Handler {
impl ConnectionHandler for Handler {
type InEvent = Command;
type OutEvent = Event;
type Error = ProtocolsHandlerUpgrErr<
type Error = ConnectionHandlerUpgrErr<
EitherError<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
>;
type InboundProtocol = upgrade::EitherUpgrade<protocol::inbound::Upgrade, DeniedUpgrade>;
@ -199,9 +199,9 @@ impl ProtocolsHandler for Handler {
EitherOutput::First(inbound_connect) => {
let remote_addr = match &self.endpoint {
ConnectedPoint::Dialer { address, role_override: _ } => address.clone(),
ConnectedPoint::Listener { ..} => unreachable!("`<Handler as ProtocolsHandler>::listen_protocol` denies all incoming substreams as a listener."),
ConnectedPoint::Listener { ..} => unreachable!("`<Handler as ConnectionHandler>::listen_protocol` denies all incoming substreams as a listener."),
};
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::InboundConnectRequest {
inbound_connect,
remote_addr,
@ -224,7 +224,7 @@ impl ProtocolsHandler for Handler {
self.endpoint.is_listener(),
"A connection dialer never initiates a connection upgrade."
);
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::OutboundConnectNegotiated {
remote_addrs: obs_addrs,
attempt,
@ -236,7 +236,7 @@ impl ProtocolsHandler for Handler {
match event {
Command::Connect { obs_addrs, attempt } => {
self.queued_events
.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
.push_back(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(
protocol::outbound::Upgrade::new(obs_addrs),
attempt,
@ -259,31 +259,31 @@ impl ProtocolsHandler for Handler {
fn inject_listen_upgrade_error(
&mut self,
_: Self::InboundOpenInfo,
error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
match error {
ProtocolsHandlerUpgrErr::Timeout => {
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
ConnectionHandlerUpgrErr::Timeout => {
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::InboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr::Timeout,
error: ConnectionHandlerUpgrErr::Timeout,
},
));
}
ProtocolsHandlerUpgrErr::Timer => {
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
ConnectionHandlerUpgrErr::Timer => {
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::InboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr::Timer,
error: ConnectionHandlerUpgrErr::Timer,
},
));
}
ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
// The remote merely doesn't support the DCUtR protocol.
// This is no reason to close the connection, which may
// successfully communicate with other protocols already.
self.keep_alive = KeepAlive::No;
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::InboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::Failed,
)),
},
@ -305,25 +305,25 @@ impl ProtocolsHandler for Handler {
fn inject_dial_upgrade_error(
&mut self,
_open_info: Self::OutboundOpenInfo,
error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
self.keep_alive = KeepAlive::No;
match error {
ProtocolsHandlerUpgrErr::Timeout => {
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
ConnectionHandlerUpgrErr::Timeout => {
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::OutboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr::Timeout,
error: ConnectionHandlerUpgrErr::Timeout,
},
));
}
ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
// The remote merely doesn't support the DCUtR protocol.
// This is no reason to close the connection, which may
// successfully communicate with other protocols already.
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
Event::OutboundNegotiationFailed {
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::Failed,
)),
},
@ -346,7 +346,7 @@ impl ProtocolsHandler for Handler {
&mut self,
cx: &mut Context<'_>,
) -> Poll<
ProtocolsHandlerEvent<
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
@ -356,7 +356,7 @@ impl ProtocolsHandler for Handler {
// Check for a pending (fatal) error.
if let Some(err) = self.pending_error.take() {
// The handler will not be polled again by the `Swarm`.
return Poll::Ready(ProtocolsHandlerEvent::Close(err));
return Poll::Ready(ConnectionHandlerEvent::Close(err));
}
// Return queued events.
@ -367,13 +367,13 @@ impl ProtocolsHandler for Handler {
while let Poll::Ready(Some(result)) = self.inbound_connects.poll_next_unpin(cx) {
match result {
Ok(addresses) => {
return Poll::Ready(ProtocolsHandlerEvent::Custom(
return Poll::Ready(ConnectionHandlerEvent::Custom(
Event::InboundConnectNegotiated(addresses),
));
}
Err(e) => {
return Poll::Ready(ProtocolsHandlerEvent::Close(
ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))),
return Poll::Ready(ConnectionHandlerEvent::Close(
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))),
))
}
}