Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm/: Rename references of protocol handler to connection handler #2640

Merged
merged 7 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.27.2
mxinden marked this conversation as resolved.
Show resolved Hide resolved

- Replace references of Protocol Handler with Connection Handler . See [PR 2640].
mxinden marked this conversation as resolved.
Show resolved Hide resolved

[PR 2640]: https://github.com/libp2p/rust-libp2p/pull/2640

# 0.27.1

- Allow mixing of ignored fields. See [PR 2570].
Expand Down
2 changes: 1 addition & 1 deletion swarm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive"
edition = "2021"
rust-version = "1.56.1"
description = "Procedural macros of libp2p-core"
version = "0.27.1"
version = "0.27.2"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
18 changes: 9 additions & 9 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
let net_behv_event_proc = quote! {::libp2p::swarm::NetworkBehaviourEventProcess};
let either_ident = quote! {::libp2p::core::either::EitherOutput};
let network_behaviour_action = quote! {::libp2p::swarm::NetworkBehaviourAction};
let into_protocols_handler = quote! {::libp2p::swarm::IntoConnectionHandler};
let protocols_handler = quote! {::libp2p::swarm::ConnectionHandler};
let into_connection_handler = quote! {::libp2p::swarm::IntoConnectionHandler};
let connection_handler = quote! {::libp2p::swarm::ConnectionHandler};
let into_proto_select_ident = quote! {::libp2p::swarm::IntoConnectionHandlerSelect};
let peer_id = quote! {::libp2p::core::PeerId};
let connection_id = quote! {::libp2p::core::connection::ConnectionId};
Expand Down Expand Up @@ -371,7 +371,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
});

// The [`ConnectionHandler`] associated type.
let protocols_handler_ty = {
let connection_handler_ty = {
let mut ph_ty = None;
for field in data_struct_fields.iter() {
let ty = &field.ty;
Expand Down Expand Up @@ -402,7 +402,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {

match out_handler {
Some(h) => {
out_handler = Some(quote! { #into_protocols_handler::select(#h, #builder) })
out_handler = Some(quote! { #into_connection_handler::select(#h, #builder) })
}
ref mut h @ None => *h = Some(builder),
}
Expand Down Expand Up @@ -476,7 +476,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {

match out_handler {
Some(h) => {
out_handler = Some(quote! { #into_protocols_handler::select(#h, #builder) })
out_handler = Some(quote! { #into_connection_handler::select(#h, #builder) })
}
ref mut h @ None => *h = Some(builder),
}
Expand Down Expand Up @@ -530,11 +530,11 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
impl #impl_generics #trait_to_impl for #name #ty_generics
#where_clause
{
type ConnectionHandler = #protocols_handler_ty;
type ConnectionHandler = #connection_handler_ty;
type OutEvent = #out_event;

fn new_handler(&mut self) -> Self::ConnectionHandler {
use #into_protocols_handler;
use #into_connection_handler;
#new_handler
}

Expand All @@ -552,7 +552,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
#(#inject_address_change_stmts);*
}

fn inject_connection_closed(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point, handlers: <Self::ConnectionHandler as #into_protocols_handler>::Handler, remaining_established: usize) {
fn inject_connection_closed(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point, handlers: <Self::ConnectionHandler as #into_connection_handler>::Handler, remaining_established: usize) {
#(#inject_connection_closed_stmts);*
}

Expand Down Expand Up @@ -596,7 +596,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
&mut self,
peer_id: #peer_id,
connection_id: #connection_id,
event: <<Self::ConnectionHandler as #into_protocols_handler>::Handler as #protocols_handler>::OutEvent
event: <<Self::ConnectionHandler as #into_connection_handler>::Handler as #connection_handler>::OutEvent
) {
match event {
#(#inject_node_event_stmts),*
Expand Down
3 changes: 3 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

- Rename `IncomingInfo::to_connected_point` to `IncomingInfo::create_connected_point`. See [PR 2620].

- Rename `TProtoHandler` to `TConnectionHandler`, `ToggleProtoHandler` to `ToggleConnectionHandler`, `ToggleIntoProtoHandler` to `ToggleIntoConnectionHandler`. See [PR 2640].

[PR 2529]: https://github.com/libp2p/rust-libp2p/pull/2529
[PR 2610]: https://github.com/libp2p/rust-libp2p/pull/2610
[PR 2620]: https://github.com/libp2p/rust-libp2p/pull/2620
[PR 2640]: https://github.com/libp2p/rust-libp2p/pull/2640

# 0.35.0

Expand Down
32 changes: 16 additions & 16 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ impl<TBehaviour> NetworkBehaviour for Toggle<TBehaviour>
where
TBehaviour: NetworkBehaviour,
{
type ConnectionHandler = ToggleIntoProtoHandler<TBehaviour::ConnectionHandler>;
type ConnectionHandler = ToggleIntoConnectionHandler<TBehaviour::ConnectionHandler>;
type OutEvent = TBehaviour::OutEvent;

fn new_handler(&mut self) -> Self::ConnectionHandler {
ToggleIntoProtoHandler {
ToggleIntoConnectionHandler {
inner: self.inner.as_mut().map(|i| i.new_handler()),
}
}
Expand Down Expand Up @@ -223,9 +223,9 @@ where
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(inner) = self.inner.as_mut() {
inner
.poll(cx, params)
.map(|action| action.map_handler(|h| ToggleIntoProtoHandler { inner: Some(h) }))
inner.poll(cx, params).map(|action| {
action.map_handler(|h| ToggleIntoConnectionHandler { inner: Some(h) })
})
} else {
Poll::Pending
}
Expand All @@ -244,22 +244,22 @@ where
}

/// Implementation of `IntoConnectionHandler` that can be in the disabled state.
pub struct ToggleIntoProtoHandler<TInner> {
pub struct ToggleIntoConnectionHandler<TInner> {
inner: Option<TInner>,
}

impl<TInner> IntoConnectionHandler for ToggleIntoProtoHandler<TInner>
impl<TInner> IntoConnectionHandler for ToggleIntoConnectionHandler<TInner>
where
TInner: IntoConnectionHandler,
{
type Handler = ToggleProtoHandler<TInner::Handler>;
type Handler = ToggleConnectionHandler<TInner::Handler>;

fn into_handler(
self,
remote_peer_id: &PeerId,
connected_point: &ConnectedPoint,
) -> Self::Handler {
ToggleProtoHandler {
ToggleConnectionHandler {
inner: self
.inner
.map(|h| h.into_handler(remote_peer_id, connected_point)),
Expand All @@ -276,11 +276,11 @@ where
}

/// Implementation of [`ConnectionHandler`] that can be in the disabled state.
pub struct ToggleProtoHandler<TInner> {
pub struct ToggleConnectionHandler<TInner> {
inner: Option<TInner>,
}

impl<TInner> ConnectionHandler for ToggleProtoHandler<TInner>
impl<TInner> ConnectionHandler for ToggleConnectionHandler<TInner>
where
TInner: ConnectionHandler,
{
Expand Down Expand Up @@ -426,7 +426,7 @@ mod tests {
use super::*;
use crate::handler::DummyConnectionHandler;

/// A disabled [`ToggleProtoHandler`] can receive listen upgrade errors in
/// A disabled [`ToggleConnectionHandler`] can receive listen upgrade errors in
/// the following two cases:
///
/// 1. Protocol negotiation on an incoming stream failed with no protocol
Expand All @@ -435,14 +435,14 @@ mod tests {
/// 2. When combining [`ConnectionHandler`] implementations a single
/// [`ConnectionHandler`] might be notified of an inbound upgrade error
/// unrelated to its own upgrade logic. For example when nesting a
/// [`ToggleProtoHandler`] in a
/// [`ConnectionHandlerSelect`](crate::protocols_handler::ConnectionHandlerSelect)
/// [`ToggleConnectionHandler`] in a
/// [`ConnectionHandlerSelect`](crate::connection_handler::ConnectionHandlerSelect)
/// the former might receive an inbound upgrade error even when disabled.
///
/// [`ToggleProtoHandler`] should ignore the error in both of these cases.
/// [`ToggleConnectionHandler`] should ignore the error in both of these cases.
#[test]
fn ignore_listen_upgrade_error_when_disabled() {
let mut handler = ToggleProtoHandler::<DummyConnectionHandler> { inner: None };
let mut handler = ToggleConnectionHandler::<DummyConnectionHandler> { inner: None };

handler.inject_listen_upgrade_error(Either::Right(()), ConnectionHandlerUpgrErr::Timeout);
}
Expand Down
5 changes: 4 additions & 1 deletion swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ where
/// Begins an orderly shutdown of the connection, returning the connection
/// handler and a `Future` that resolves when connection shutdown is complete.
pub fn close(self) -> (THandler, Close<StreamMuxerBox>) {
(self.handler.into_protocols_handler(), self.muxing.close().0)
(
self.handler.into_connection_handler(),
self.muxing.close().0,
)
}

/// Polls the handler and the substream, forwarding events from the former to the latter and
Expand Down
40 changes: 20 additions & 20 deletions swarm/src/connection/handler_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@ use std::{error, fmt, pin::Pin, task::Context, task::Poll, time::Duration};
/// - Driving substream upgrades
/// - Handling connection timeout
// TODO: add a caching system for protocols that are supported or not
pub struct HandlerWrapper<TProtoHandler>
pub struct HandlerWrapper<TConnectionHandler>
where
TProtoHandler: ConnectionHandler,
TConnectionHandler: ConnectionHandler,
{
/// The underlying handler.
handler: TProtoHandler,
handler: TConnectionHandler,
/// Futures that upgrade incoming substreams.
negotiating_in: FuturesUnordered<
SubstreamUpgrade<
TProtoHandler::InboundOpenInfo,
TConnectionHandler::InboundOpenInfo,
InboundUpgradeApply<
Substream<StreamMuxerBox>,
SendWrapper<TProtoHandler::InboundProtocol>,
SendWrapper<TConnectionHandler::InboundProtocol>,
>,
>,
>,
/// Futures that upgrade outgoing substreams.
negotiating_out: FuturesUnordered<
SubstreamUpgrade<
TProtoHandler::OutboundOpenInfo,
TConnectionHandler::OutboundOpenInfo,
OutboundUpgradeApply<
Substream<StreamMuxerBox>,
SendWrapper<TProtoHandler::OutboundProtocol>,
SendWrapper<TConnectionHandler::OutboundProtocol>,
>,
>,
>,
/// For each outbound substream request, how to upgrade it. The first element of the tuple
/// is the unique identifier (see `unique_dial_upgrade_id`).
queued_dial_upgrades: Vec<(u64, SendWrapper<TProtoHandler::OutboundProtocol>)>,
queued_dial_upgrades: Vec<(u64, SendWrapper<TConnectionHandler::OutboundProtocol>)>,
/// Unique identifier assigned to each queued dial upgrade.
unique_dial_upgrade_id: u64,
/// The currently planned connection & handler shutdown.
Expand All @@ -79,7 +79,7 @@ where
substream_upgrade_protocol_override: Option<upgrade::Version>,
}

impl<TProtoHandler: ConnectionHandler> std::fmt::Debug for HandlerWrapper<TProtoHandler> {
impl<TConnectionHandler: ConnectionHandler> std::fmt::Debug for HandlerWrapper<TConnectionHandler> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HandlerWrapper")
.field("negotiating_in", &self.negotiating_in)
Expand All @@ -94,9 +94,9 @@ impl<TProtoHandler: ConnectionHandler> std::fmt::Debug for HandlerWrapper<TProto
}
}

impl<TProtoHandler: ConnectionHandler> HandlerWrapper<TProtoHandler> {
impl<TConnectionHandler: ConnectionHandler> HandlerWrapper<TConnectionHandler> {
pub(crate) fn new(
handler: TProtoHandler,
handler: TConnectionHandler,
substream_upgrade_protocol_override: Option<upgrade::Version>,
) -> Self {
Self {
Expand All @@ -110,7 +110,7 @@ impl<TProtoHandler: ConnectionHandler> HandlerWrapper<TProtoHandler> {
}
}

pub(crate) fn into_protocols_handler(self) -> TProtoHandler {
pub(crate) fn into_connection_handler(self) -> TConnectionHandler {
self.handler
}
}
Expand Down Expand Up @@ -224,22 +224,22 @@ where
}
}

pub type OutboundOpenInfo<TProtoHandler> = (
pub type OutboundOpenInfo<TConnectionHandler> = (
u64,
<TProtoHandler as ConnectionHandler>::OutboundOpenInfo,
<TConnectionHandler as ConnectionHandler>::OutboundOpenInfo,
Duration,
);

impl<TProtoHandler> HandlerWrapper<TProtoHandler>
impl<TConnectionHandler> HandlerWrapper<TConnectionHandler>
where
TProtoHandler: ConnectionHandler,
TConnectionHandler: ConnectionHandler,
{
pub fn inject_substream(
&mut self,
substream: Substream<StreamMuxerBox>,
// The first element of the tuple is the unique upgrade identifier
// (see `unique_dial_upgrade_id`).
endpoint: SubstreamEndpoint<OutboundOpenInfo<TProtoHandler>>,
endpoint: SubstreamEndpoint<OutboundOpenInfo<TConnectionHandler>>,
) {
match endpoint {
SubstreamEndpoint::Listener => {
Expand Down Expand Up @@ -290,7 +290,7 @@ where
}
}

pub fn inject_event(&mut self, event: TProtoHandler::InEvent) {
pub fn inject_event(&mut self, event: TConnectionHandler::InEvent) {
self.handler.inject_event(event);
}

Expand All @@ -303,8 +303,8 @@ where
cx: &mut Context<'_>,
) -> Poll<
Result<
Event<OutboundOpenInfo<TProtoHandler>, TProtoHandler::OutEvent>,
Error<TProtoHandler::Error>,
Event<OutboundOpenInfo<TConnectionHandler>, TConnectionHandler::OutEvent>,
Error<TConnectionHandler::Error>,
>,
> {
while let Poll::Ready(Some((user_data, res))) = self.negotiating_in.poll_next_unpin(cx) {
Expand Down
27 changes: 14 additions & 13 deletions swarm/src/handler/map_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ use libp2p_core::Multiaddr;
use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll};

/// Wrapper around a protocol handler that turns the input event into something else.
pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
inner: TProtoHandler,
pub struct MapInEvent<TConnectionHandler, TNewIn, TMap> {
inner: TConnectionHandler,
map: TMap,
marker: PhantomData<TNewIn>,
}

impl<TProtoHandler, TMap, TNewIn> MapInEvent<TProtoHandler, TNewIn, TMap> {
impl<TConnectionHandler, TMap, TNewIn> MapInEvent<TConnectionHandler, TNewIn, TMap> {
/// Creates a `MapInEvent`.
pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
MapInEvent {
inner,
map,
Expand All @@ -44,20 +44,21 @@ impl<TProtoHandler, TMap, TNewIn> MapInEvent<TProtoHandler, TNewIn, TMap> {
}
}

impl<TProtoHandler, TMap, TNewIn> ConnectionHandler for MapInEvent<TProtoHandler, TNewIn, TMap>
impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
for MapInEvent<TConnectionHandler, TNewIn, TMap>
where
TProtoHandler: ConnectionHandler,
TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
TConnectionHandler: ConnectionHandler,
TMap: Fn(TNewIn) -> Option<TConnectionHandler::InEvent>,
TNewIn: Debug + Send + 'static,
TMap: Send + 'static,
{
type InEvent = TNewIn;
type OutEvent = TProtoHandler::OutEvent;
type Error = TProtoHandler::Error;
type InboundProtocol = TProtoHandler::InboundProtocol;
type OutboundProtocol = TProtoHandler::OutboundProtocol;
type InboundOpenInfo = TProtoHandler::InboundOpenInfo;
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
type OutEvent = TConnectionHandler::OutEvent;
type Error = TConnectionHandler::Error;
type InboundProtocol = TConnectionHandler::InboundProtocol;
type OutboundProtocol = TConnectionHandler::OutboundProtocol;
type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;

fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
self.inner.listen_protocol()
Expand Down
Loading