2021-12-12 03:51:02 -08:00
|
|
|
// Copyright 2021 Protocol Labs.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2023-01-12 11:21:02 +00:00
|
|
|
use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
2023-01-18 19:56:32 +11:00
|
|
|
use crate::connection::ConnectionId;
|
2022-11-17 09:28:40 +00:00
|
|
|
use crate::handler::either::IntoEitherHandler;
|
|
|
|
use either::Either;
|
|
|
|
use libp2p_core::{Multiaddr, PeerId};
|
2021-12-12 03:51:02 -08:00
|
|
|
use std::{task::Context, task::Poll};
|
|
|
|
|
|
|
|
/// Implementation of [`NetworkBehaviour`] that can be either of two implementations.
|
|
|
|
impl<L, R> NetworkBehaviour for Either<L, R>
|
|
|
|
where
|
|
|
|
L: NetworkBehaviour,
|
|
|
|
R: NetworkBehaviour,
|
|
|
|
{
|
2022-02-21 13:32:24 +01:00
|
|
|
type ConnectionHandler = IntoEitherHandler<L::ConnectionHandler, R::ConnectionHandler>;
|
2021-12-12 03:51:02 -08:00
|
|
|
type OutEvent = Either<L::OutEvent, R::OutEvent>;
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
2021-12-12 03:51:02 -08:00
|
|
|
match self {
|
|
|
|
Either::Left(a) => IntoEitherHandler::Left(a.new_handler()),
|
|
|
|
Either::Right(b) => IntoEitherHandler::Right(b.new_handler()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
|
|
|
|
match self {
|
|
|
|
Either::Left(a) => a.addresses_of_peer(peer_id),
|
|
|
|
Either::Right(b) => b.addresses_of_peer(peer_id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 09:28:40 +00:00
|
|
|
fn on_swarm_event(&mut self, event: behaviour::FromSwarm<Self::ConnectionHandler>) {
|
2021-12-12 03:51:02 -08:00
|
|
|
match self {
|
2023-01-12 11:21:02 +00:00
|
|
|
Either::Left(b) => b.on_swarm_event(event.map_handler(
|
|
|
|
|h| h.unwrap_left(),
|
|
|
|
|h| match h {
|
|
|
|
Either::Left(h) => h,
|
|
|
|
Either::Right(_) => unreachable!(),
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
Either::Right(b) => b.on_swarm_event(event.map_handler(
|
|
|
|
|h| h.unwrap_right(),
|
|
|
|
|h| match h {
|
|
|
|
Either::Right(h) => h,
|
|
|
|
Either::Left(_) => unreachable!(),
|
|
|
|
},
|
|
|
|
)),
|
2021-12-12 03:51:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 09:28:40 +00:00
|
|
|
fn on_connection_handler_event(
|
2021-12-12 03:51:02 -08:00
|
|
|
&mut self,
|
|
|
|
peer_id: PeerId,
|
2023-01-18 19:56:32 +11:00
|
|
|
connection_id: ConnectionId,
|
2022-11-17 09:28:40 +00:00
|
|
|
event: crate::THandlerOutEvent<Self>,
|
2021-12-12 03:51:02 -08:00
|
|
|
) {
|
|
|
|
match (self, event) {
|
2022-11-17 09:28:40 +00:00
|
|
|
(Either::Left(left), Either::Left(event)) => {
|
2023-01-12 11:21:02 +00:00
|
|
|
left.on_connection_handler_event(peer_id, connection_id, event);
|
2021-12-12 03:51:02 -08:00
|
|
|
}
|
2022-11-17 09:28:40 +00:00
|
|
|
(Either::Right(right), Either::Right(event)) => {
|
2023-01-12 11:21:02 +00:00
|
|
|
right.on_connection_handler_event(peer_id, connection_id, event);
|
2021-12-12 03:51:02 -08:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
params: &mut impl PollParameters,
|
2022-02-21 13:32:24 +01:00
|
|
|
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
2021-12-12 03:51:02 -08:00
|
|
|
let event = match self {
|
|
|
|
Either::Left(behaviour) => futures::ready!(behaviour.poll(cx, params))
|
2022-04-19 12:13:45 +02:00
|
|
|
.map_out(Either::Left)
|
|
|
|
.map_handler_and_in(IntoEitherHandler::Left, Either::Left),
|
2021-12-12 03:51:02 -08:00
|
|
|
Either::Right(behaviour) => futures::ready!(behaviour.poll(cx, params))
|
2022-04-19 12:13:45 +02:00
|
|
|
.map_out(Either::Right)
|
|
|
|
.map_handler_and_in(IntoEitherHandler::Right, Either::Right),
|
2021-12-12 03:51:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Poll::Ready(event)
|
|
|
|
}
|
|
|
|
}
|