mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-30 11:11:21 +00:00
swarm/src/either: Implement NetworkBehaviour
on Either
(#2370)
Implement `NetworkBehaviour` on `either::Either<L, R>` where both L and R both implement `NetworkBehaviour`. Add NetworkBehaviour derive tests for Either and Toggle Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
parent
dd9e0a14de
commit
ab7b5a4574
@ -19,4 +19,5 @@ quote = "1.0"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
libp2p = { path = "../" }
|
libp2p = { path = "../" }
|
||||||
|
either = "1.6.0"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
|
@ -324,3 +324,98 @@ fn event_process_false() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_toggle() {
|
||||||
|
use libp2p::swarm::behaviour::toggle::Toggle;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(NetworkBehaviour)]
|
||||||
|
#[behaviour(event_process = true)]
|
||||||
|
struct Foo {
|
||||||
|
identify: libp2p::identify::Identify,
|
||||||
|
ping: Toggle<libp2p::ping::Ping>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo {
|
||||||
|
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo {
|
||||||
|
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn foo() {
|
||||||
|
require_net_behaviour::<Foo>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_either() {
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(NetworkBehaviour)]
|
||||||
|
#[behaviour(event_process = true)]
|
||||||
|
struct Foo {
|
||||||
|
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
|
||||||
|
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::kad::KademliaEvent> for Foo {
|
||||||
|
fn inject_event(&mut self, _: libp2p::kad::KademliaEvent) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl
|
||||||
|
libp2p::swarm::NetworkBehaviourEventProcess<
|
||||||
|
Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>,
|
||||||
|
> for Foo
|
||||||
|
{
|
||||||
|
fn inject_event(
|
||||||
|
&mut self,
|
||||||
|
_: Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn foo() {
|
||||||
|
require_net_behaviour::<Foo>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_event_with_either() {
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
|
enum BehaviourOutEvent {
|
||||||
|
Kad(libp2p::kad::KademliaEvent),
|
||||||
|
PingOrIdentify(Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(NetworkBehaviour)]
|
||||||
|
#[behaviour(out_event = "BehaviourOutEvent", event_process = false)]
|
||||||
|
struct Foo {
|
||||||
|
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
|
||||||
|
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<libp2p::kad::KademliaEvent> for BehaviourOutEvent {
|
||||||
|
fn from(event: libp2p::kad::KademliaEvent) -> Self {
|
||||||
|
BehaviourOutEvent::Kad(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>> for BehaviourOutEvent {
|
||||||
|
fn from(event: Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>) -> Self {
|
||||||
|
BehaviourOutEvent::PingOrIdentify(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn foo() {
|
||||||
|
require_net_behaviour::<Foo>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,9 +10,12 @@
|
|||||||
|
|
||||||
- Move `swarm::Toggle` to `swarm::behaviour::Toggle` (see [PR 2375]).
|
- Move `swarm::Toggle` to `swarm::behaviour::Toggle` (see [PR 2375]).
|
||||||
|
|
||||||
|
- Implement `swarm::NetworkBehaviour` on `either::Either` (see [PR 2370]).
|
||||||
|
|
||||||
[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
|
[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
|
||||||
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350
|
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350
|
||||||
[PR 2362]: https://github.com/libp2p/rust-libp2p/pull/2362
|
[PR 2362]: https://github.com/libp2p/rust-libp2p/pull/2362
|
||||||
|
[PR 2370]: https://github.com/libp2p/rust-libp2p/pull/2370
|
||||||
[PR 2375]: https://github.com/libp2p/rust-libp2p/pull/2375
|
[PR 2375]: https://github.com/libp2p/rust-libp2p/pull/2375
|
||||||
|
|
||||||
# 0.32.0 [2021-11-16]
|
# 0.32.0 [2021-11-16]
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
pub mod either;
|
||||||
pub mod toggle;
|
pub mod toggle;
|
||||||
|
|
||||||
use crate::dial_opts::DialOpts;
|
use crate::dial_opts::DialOpts;
|
||||||
@ -718,6 +719,50 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<TInEventOld, TOutEvent, THandlerOld> NetworkBehaviourAction<TOutEvent, THandlerOld>
|
||||||
|
where
|
||||||
|
THandlerOld: IntoProtocolsHandler,
|
||||||
|
<THandlerOld as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventOld>,
|
||||||
|
{
|
||||||
|
/// Map the handler and handler event.
|
||||||
|
pub fn map_handler_and_in<THandlerNew, TInEventNew>(
|
||||||
|
self,
|
||||||
|
f_handler: impl FnOnce(THandlerOld) -> THandlerNew,
|
||||||
|
f_in_event: impl FnOnce(TInEventOld) -> TInEventNew,
|
||||||
|
) -> NetworkBehaviourAction<TOutEvent, THandlerNew>
|
||||||
|
where
|
||||||
|
THandlerNew: IntoProtocolsHandler,
|
||||||
|
<THandlerNew as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventNew>,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
|
||||||
|
NetworkBehaviourAction::Dial { opts, handler } => NetworkBehaviourAction::Dial {
|
||||||
|
opts,
|
||||||
|
handler: f_handler(handler),
|
||||||
|
},
|
||||||
|
NetworkBehaviourAction::NotifyHandler {
|
||||||
|
peer_id,
|
||||||
|
handler,
|
||||||
|
event,
|
||||||
|
} => NetworkBehaviourAction::NotifyHandler {
|
||||||
|
peer_id,
|
||||||
|
handler,
|
||||||
|
event: f_in_event(event),
|
||||||
|
},
|
||||||
|
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||||
|
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
||||||
|
}
|
||||||
|
NetworkBehaviourAction::CloseConnection {
|
||||||
|
peer_id,
|
||||||
|
connection,
|
||||||
|
} => NetworkBehaviourAction::CloseConnection {
|
||||||
|
peer_id,
|
||||||
|
connection,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The options w.r.t. which connection handler to notify of an event.
|
/// The options w.r.t. which connection handler to notify of an event.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum NotifyHandler {
|
pub enum NotifyHandler {
|
||||||
|
248
swarm/src/behaviour/either.rs
Normal file
248
swarm/src/behaviour/either.rs
Normal file
@ -0,0 +1,248 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
use crate::protocols_handler::{either::IntoEitherHandler, IntoProtocolsHandler, ProtocolsHandler};
|
||||||
|
use crate::{
|
||||||
|
DialError, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
|
||||||
|
PollParameters,
|
||||||
|
};
|
||||||
|
use either::Either;
|
||||||
|
use libp2p_core::{
|
||||||
|
connection::{ConnectionId, ListenerId},
|
||||||
|
ConnectedPoint, Multiaddr, PeerId,
|
||||||
|
};
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
type ProtocolsHandler = IntoEitherHandler<L::ProtocolsHandler, R::ProtocolsHandler>;
|
||||||
|
type OutEvent = Either<L::OutEvent, R::OutEvent>;
|
||||||
|
|
||||||
|
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_connected(&mut self, peer_id: &PeerId) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_connected(peer_id),
|
||||||
|
Either::Right(b) => b.inject_connected(peer_id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_disconnected(&mut self, peer_id: &PeerId) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_disconnected(peer_id),
|
||||||
|
Either::Right(b) => b.inject_disconnected(peer_id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_connection_established(
|
||||||
|
&mut self,
|
||||||
|
peer_id: &PeerId,
|
||||||
|
connection: &ConnectionId,
|
||||||
|
endpoint: &ConnectedPoint,
|
||||||
|
errors: Option<&Vec<Multiaddr>>,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => {
|
||||||
|
a.inject_connection_established(peer_id, connection, endpoint, errors)
|
||||||
|
}
|
||||||
|
Either::Right(b) => {
|
||||||
|
b.inject_connection_established(peer_id, connection, endpoint, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_connection_closed(
|
||||||
|
&mut self,
|
||||||
|
peer_id: &PeerId,
|
||||||
|
connection: &ConnectionId,
|
||||||
|
endpoint: &ConnectedPoint,
|
||||||
|
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||||
|
) {
|
||||||
|
match (self, handler) {
|
||||||
|
(Either::Left(behaviour), Either::Left(handler)) => {
|
||||||
|
behaviour.inject_connection_closed(peer_id, connection, endpoint, handler)
|
||||||
|
}
|
||||||
|
(Either::Right(behaviour), Either::Right(handler)) => {
|
||||||
|
behaviour.inject_connection_closed(peer_id, connection, endpoint, handler)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_address_change(
|
||||||
|
&mut self,
|
||||||
|
peer_id: &PeerId,
|
||||||
|
connection: &ConnectionId,
|
||||||
|
old: &ConnectedPoint,
|
||||||
|
new: &ConnectedPoint,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_address_change(peer_id, connection, old, new),
|
||||||
|
Either::Right(b) => b.inject_address_change(peer_id, connection, old, new),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_event(
|
||||||
|
&mut self,
|
||||||
|
peer_id: PeerId,
|
||||||
|
connection: ConnectionId,
|
||||||
|
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
|
||||||
|
) {
|
||||||
|
match (self, event) {
|
||||||
|
(Either::Left(behaviour), Either::Left(event)) => {
|
||||||
|
behaviour.inject_event(peer_id, connection, event)
|
||||||
|
}
|
||||||
|
(Either::Right(behaviour), Either::Right(event)) => {
|
||||||
|
behaviour.inject_event(peer_id, connection, event)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_dial_failure(
|
||||||
|
&mut self,
|
||||||
|
peer_id: Option<PeerId>,
|
||||||
|
handler: Self::ProtocolsHandler,
|
||||||
|
error: &DialError,
|
||||||
|
) {
|
||||||
|
match (self, handler) {
|
||||||
|
(Either::Left(behaviour), IntoEitherHandler::Left(handler)) => {
|
||||||
|
behaviour.inject_dial_failure(peer_id, handler, error)
|
||||||
|
}
|
||||||
|
(Either::Right(behaviour), IntoEitherHandler::Right(handler)) => {
|
||||||
|
behaviour.inject_dial_failure(peer_id, handler, error)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_listen_failure(
|
||||||
|
&mut self,
|
||||||
|
local_addr: &Multiaddr,
|
||||||
|
send_back_addr: &Multiaddr,
|
||||||
|
handler: Self::ProtocolsHandler,
|
||||||
|
) {
|
||||||
|
match (self, handler) {
|
||||||
|
(Either::Left(behaviour), IntoEitherHandler::Left(handler)) => {
|
||||||
|
behaviour.inject_listen_failure(local_addr, send_back_addr, handler)
|
||||||
|
}
|
||||||
|
(Either::Right(behaviour), IntoEitherHandler::Right(handler)) => {
|
||||||
|
behaviour.inject_listen_failure(local_addr, send_back_addr, handler)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_new_listener(&mut self, id: ListenerId) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_new_listener(id),
|
||||||
|
Either::Right(b) => b.inject_new_listener(id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_new_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_new_listen_addr(id, addr),
|
||||||
|
Either::Right(b) => b.inject_new_listen_addr(id, addr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_expired_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_expired_listen_addr(id, addr),
|
||||||
|
Either::Right(b) => b.inject_expired_listen_addr(id, addr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_new_external_addr(addr),
|
||||||
|
Either::Right(b) => b.inject_new_external_addr(addr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_expired_external_addr(&mut self, addr: &Multiaddr) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_expired_external_addr(addr),
|
||||||
|
Either::Right(b) => b.inject_expired_external_addr(addr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn std::error::Error + 'static)) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_listener_error(id, err),
|
||||||
|
Either::Right(b) => b.inject_listener_error(id, err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &std::io::Error>) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_listener_closed(id, reason),
|
||||||
|
Either::Right(b) => b.inject_listener_closed(id, reason),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
params: &mut impl PollParameters,
|
||||||
|
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||||
|
let event = match self {
|
||||||
|
Either::Left(behaviour) => futures::ready!(behaviour.poll(cx, params))
|
||||||
|
.map_out(|e| Either::Left(e))
|
||||||
|
.map_handler_and_in(|h| IntoEitherHandler::Left(h), |e| Either::Left(e)),
|
||||||
|
Either::Right(behaviour) => futures::ready!(behaviour.poll(cx, params))
|
||||||
|
.map_out(|e| Either::Right(e))
|
||||||
|
.map_handler_and_in(|h| IntoEitherHandler::Right(h), |e| Either::Right(e)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Poll::Ready(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<TEvent, TBehaviourLeft, TBehaviourRight> NetworkBehaviourEventProcess<TEvent>
|
||||||
|
for Either<TBehaviourLeft, TBehaviourRight>
|
||||||
|
where
|
||||||
|
TBehaviourLeft: NetworkBehaviourEventProcess<TEvent>,
|
||||||
|
TBehaviourRight: NetworkBehaviourEventProcess<TEvent>,
|
||||||
|
{
|
||||||
|
fn inject_event(&mut self, event: TEvent) {
|
||||||
|
match self {
|
||||||
|
Either::Left(a) => a.inject_event(event),
|
||||||
|
Either::Right(b) => b.inject_event(event),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user