mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-25 15:51:34 +00:00
feat(swarm): rename NetworkBehaviourAction
to ToSwarm
Resolves #3123. Pull-Request: #3658.
This commit is contained in:
@ -141,7 +141,7 @@ pub trait NetworkBehaviour: 'static {
|
||||
/// sent from the handler to the behaviour are invoked with
|
||||
/// [`NetworkBehaviour::on_connection_handler_event`],
|
||||
/// and the behaviour can send a message to the handler by making [`NetworkBehaviour::poll`]
|
||||
/// return [`NetworkBehaviourAction::NotifyHandler`].
|
||||
/// return [`ToSwarm::NotifyHandler`].
|
||||
///
|
||||
/// Note that the handler is returned to the [`NetworkBehaviour`] on connection failure and
|
||||
/// connection closing.
|
||||
@ -276,7 +276,7 @@ pub trait NetworkBehaviour: 'static {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>>;
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>>;
|
||||
}
|
||||
|
||||
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
|
||||
@ -318,12 +318,14 @@ pub trait PollParameters {
|
||||
fn local_peer_id(&self) -> &PeerId;
|
||||
}
|
||||
|
||||
/// An action that a [`NetworkBehaviour`] can trigger in the [`Swarm`]
|
||||
/// in whose context it is executing.
|
||||
#[deprecated(note = "Use `ToSwarm` instead.")]
|
||||
pub type NetworkBehaviourAction<TOutEvent, TInEvent> = ToSwarm<TOutEvent, TInEvent>;
|
||||
|
||||
/// A command issued from a [`NetworkBehaviour`] for the [`Swarm`].
|
||||
///
|
||||
/// [`Swarm`]: super::Swarm
|
||||
#[derive(Debug)]
|
||||
pub enum NetworkBehaviourAction<TOutEvent, TInEvent> {
|
||||
pub enum ToSwarm<TOutEvent, TInEvent> {
|
||||
/// Instructs the `Swarm` to return an event when it is being polled.
|
||||
GenerateEvent(TOutEvent),
|
||||
|
||||
@ -381,7 +383,7 @@ pub enum NetworkBehaviourAction<TOutEvent, TInEvent> {
|
||||
/// with the given peer.
|
||||
///
|
||||
/// Note: Closing a connection via
|
||||
/// [`NetworkBehaviourAction::CloseConnection`] does not inform the
|
||||
/// [`ToSwarm::CloseConnection`] does not inform the
|
||||
/// corresponding [`ConnectionHandler`](crate::ConnectionHandler).
|
||||
/// Closing a connection via a [`ConnectionHandler`](crate::ConnectionHandler) can be done
|
||||
/// either in a collaborative manner across [`ConnectionHandler`](crate::ConnectionHandler)s
|
||||
@ -395,31 +397,31 @@ pub enum NetworkBehaviourAction<TOutEvent, TInEvent> {
|
||||
},
|
||||
}
|
||||
|
||||
impl<TOutEvent, TInEventOld> NetworkBehaviourAction<TOutEvent, TInEventOld> {
|
||||
impl<TOutEvent, TInEventOld> ToSwarm<TOutEvent, TInEventOld> {
|
||||
/// Map the handler event.
|
||||
pub fn map_in<TInEventNew>(
|
||||
self,
|
||||
f: impl FnOnce(TInEventOld) -> TInEventNew,
|
||||
) -> NetworkBehaviourAction<TOutEvent, TInEventNew> {
|
||||
) -> ToSwarm<TOutEvent, TInEventNew> {
|
||||
match self {
|
||||
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
|
||||
NetworkBehaviourAction::Dial { opts } => NetworkBehaviourAction::Dial { opts },
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
ToSwarm::GenerateEvent(e) => ToSwarm::GenerateEvent(e),
|
||||
ToSwarm::Dial { opts } => ToSwarm::Dial { opts },
|
||||
ToSwarm::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event,
|
||||
} => NetworkBehaviourAction::NotifyHandler {
|
||||
} => ToSwarm::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event: f(event),
|
||||
},
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
||||
ToSwarm::ReportObservedAddr { address, score } => {
|
||||
ToSwarm::ReportObservedAddr { address, score }
|
||||
}
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
ToSwarm::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
} => NetworkBehaviourAction::CloseConnection {
|
||||
} => ToSwarm::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
},
|
||||
@ -427,31 +429,28 @@ impl<TOutEvent, TInEventOld> NetworkBehaviourAction<TOutEvent, TInEventOld> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TOutEvent, THandlerIn> NetworkBehaviourAction<TOutEvent, THandlerIn> {
|
||||
impl<TOutEvent, THandlerIn> ToSwarm<TOutEvent, THandlerIn> {
|
||||
/// Map the event the swarm will return.
|
||||
pub fn map_out<E>(
|
||||
self,
|
||||
f: impl FnOnce(TOutEvent) -> E,
|
||||
) -> NetworkBehaviourAction<E, THandlerIn> {
|
||||
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> ToSwarm<E, THandlerIn> {
|
||||
match self {
|
||||
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(f(e)),
|
||||
NetworkBehaviourAction::Dial { opts } => NetworkBehaviourAction::Dial { opts },
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
ToSwarm::GenerateEvent(e) => ToSwarm::GenerateEvent(f(e)),
|
||||
ToSwarm::Dial { opts } => ToSwarm::Dial { opts },
|
||||
ToSwarm::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event,
|
||||
} => NetworkBehaviourAction::NotifyHandler {
|
||||
} => ToSwarm::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event,
|
||||
},
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
||||
ToSwarm::ReportObservedAddr { address, score } => {
|
||||
ToSwarm::ReportObservedAddr { address, score }
|
||||
}
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
ToSwarm::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
} => NetworkBehaviourAction::CloseConnection {
|
||||
} => ToSwarm::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
},
|
||||
|
@ -18,7 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use crate::behaviour::{self, NetworkBehaviour, PollParameters, ToSwarm};
|
||||
use crate::connection::ConnectionId;
|
||||
use crate::{ConnectionDenied, THandler, THandlerInEvent, THandlerOutEvent};
|
||||
use either::Either;
|
||||
@ -156,7 +156,7 @@ where
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
let event = match self {
|
||||
Either::Left(behaviour) => futures::ready!(behaviour.poll(cx, params))
|
||||
.map_out(Either::Left)
|
||||
|
@ -27,8 +27,8 @@ use crate::handler::{
|
||||
};
|
||||
use crate::upgrade::SendWrapper;
|
||||
use crate::{
|
||||
ConnectionDenied, NetworkBehaviour, NetworkBehaviourAction, PollParameters, THandler,
|
||||
THandlerInEvent, THandlerOutEvent,
|
||||
ConnectionDenied, NetworkBehaviour, PollParameters, THandler, THandlerInEvent,
|
||||
THandlerOutEvent, ToSwarm,
|
||||
};
|
||||
use either::Either;
|
||||
use futures::future;
|
||||
@ -182,7 +182,7 @@ where
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
inner.poll(cx, params)
|
||||
} else {
|
||||
|
@ -30,7 +30,7 @@ use std::num::NonZeroU8;
|
||||
/// Options to configure a dial to a known or unknown peer.
|
||||
///
|
||||
/// Used in [`Swarm::dial`](crate::Swarm::dial) and
|
||||
/// [`NetworkBehaviourAction::Dial`](crate::behaviour::NetworkBehaviourAction::Dial).
|
||||
/// [`ToSwarm::Dial`](crate::behaviour::ToSwarm::Dial).
|
||||
///
|
||||
/// To construct use either of:
|
||||
///
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use crate::behaviour::{FromSwarm, NetworkBehaviour, PollParameters, ToSwarm};
|
||||
use crate::connection::ConnectionId;
|
||||
use crate::handler::{
|
||||
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
||||
@ -54,7 +54,7 @@ impl NetworkBehaviour for Behaviour {
|
||||
&mut self,
|
||||
_: &mut Context<'_>,
|
||||
_: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use crate::behaviour::{FromSwarm, NetworkBehaviour, PollParameters, ToSwarm};
|
||||
use crate::connection::ConnectionId;
|
||||
use crate::handler::{
|
||||
ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
||||
@ -57,7 +57,7 @@ impl NetworkBehaviour for Behaviour {
|
||||
&mut self,
|
||||
_: &mut Context<'_>,
|
||||
_: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
|
@ -90,11 +90,13 @@ pub mod derive_prelude {
|
||||
pub use crate::ConnectionHandlerSelect;
|
||||
pub use crate::DialError;
|
||||
pub use crate::NetworkBehaviour;
|
||||
#[allow(deprecated)]
|
||||
pub use crate::NetworkBehaviourAction;
|
||||
pub use crate::PollParameters;
|
||||
pub use crate::THandler;
|
||||
pub use crate::THandlerInEvent;
|
||||
pub use crate::THandlerOutEvent;
|
||||
pub use crate::ToSwarm;
|
||||
pub use either::Either;
|
||||
pub use futures::prelude as futures;
|
||||
pub use libp2p_core::transport::ListenerId;
|
||||
@ -106,11 +108,13 @@ pub mod derive_prelude {
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use crate::connection::ConnectionLimit;
|
||||
#[allow(deprecated)]
|
||||
pub use behaviour::NetworkBehaviourAction;
|
||||
pub use behaviour::{
|
||||
AddressChange, CloseConnection, ConnectionClosed, DialFailure, ExpiredExternalAddr,
|
||||
ExpiredListenAddr, ExternalAddresses, FromSwarm, ListenAddresses, ListenFailure,
|
||||
ListenerClosed, ListenerError, NetworkBehaviour, NetworkBehaviourAction, NewExternalAddr,
|
||||
NewListenAddr, NotifyHandler, PollParameters,
|
||||
ListenerClosed, ListenerError, NetworkBehaviour, NewExternalAddr, NewListenAddr, NotifyHandler,
|
||||
PollParameters, ToSwarm,
|
||||
};
|
||||
#[allow(deprecated)]
|
||||
pub use connection::pool::{ConnectionCounters, ConnectionLimits};
|
||||
@ -699,7 +703,7 @@ where
|
||||
/// order in which addresses are used to connect to) as well as
|
||||
/// how long the address is retained in the list, depending on
|
||||
/// how frequently it is reported by the `NetworkBehaviour` via
|
||||
/// [`NetworkBehaviourAction::ReportObservedAddr`] or explicitly
|
||||
/// [`ToSwarm::ReportObservedAddr`] or explicitly
|
||||
/// through this method.
|
||||
pub fn add_external_address(&mut self, a: Multiaddr, s: AddressScore) -> AddAddressResult {
|
||||
let result = self.external_addrs.add(a.clone(), s);
|
||||
@ -1188,13 +1192,11 @@ where
|
||||
|
||||
fn handle_behaviour_event(
|
||||
&mut self,
|
||||
event: NetworkBehaviourAction<TBehaviour::OutEvent, THandlerInEvent<TBehaviour>>,
|
||||
event: ToSwarm<TBehaviour::OutEvent, THandlerInEvent<TBehaviour>>,
|
||||
) -> Option<SwarmEvent<TBehaviour::OutEvent, THandlerErr<TBehaviour>>> {
|
||||
match event {
|
||||
NetworkBehaviourAction::GenerateEvent(event) => {
|
||||
return Some(SwarmEvent::Behaviour(event))
|
||||
}
|
||||
NetworkBehaviourAction::Dial { opts } => {
|
||||
ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)),
|
||||
ToSwarm::Dial { opts } => {
|
||||
let peer_id = opts.get_or_parse_peer_id();
|
||||
if let Ok(()) = self.dial(opts) {
|
||||
if let Ok(Some(peer_id)) = peer_id {
|
||||
@ -1202,7 +1204,7 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
ToSwarm::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event,
|
||||
@ -1221,7 +1223,7 @@ where
|
||||
|
||||
self.pending_event = Some((peer_id, handler, event));
|
||||
}
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||
ToSwarm::ReportObservedAddr { address, score } => {
|
||||
// Maps the given `observed_addr`, representing an address of the local
|
||||
// node observed by a remote peer, onto the locally known listen addresses
|
||||
// to yield one or more addresses of the local node that may be publicly
|
||||
@ -1251,7 +1253,7 @@ where
|
||||
self.add_external_address(addr, score);
|
||||
}
|
||||
}
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
ToSwarm::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
} => match connection {
|
||||
@ -2314,7 +2316,7 @@ mod tests {
|
||||
|
||||
/// Establishes multiple connections between two peers,
|
||||
/// after which one peer disconnects the other
|
||||
/// using [`NetworkBehaviourAction::CloseConnection`] returned by a [`NetworkBehaviour`].
|
||||
/// using [`ToSwarm::CloseConnection`] returned by a [`NetworkBehaviour`].
|
||||
///
|
||||
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
|
||||
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
|
||||
@ -2352,12 +2354,14 @@ mod tests {
|
||||
if reconnected {
|
||||
return Poll::Ready(());
|
||||
}
|
||||
swarm2.behaviour.inner().next_action.replace(
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
swarm2
|
||||
.behaviour
|
||||
.inner()
|
||||
.next_action
|
||||
.replace(ToSwarm::CloseConnection {
|
||||
peer_id: swarm1_id,
|
||||
connection: CloseConnection::All,
|
||||
},
|
||||
);
|
||||
});
|
||||
state = State::Disconnecting;
|
||||
continue;
|
||||
}
|
||||
@ -2382,7 +2386,7 @@ mod tests {
|
||||
|
||||
/// Establishes multiple connections between two peers,
|
||||
/// after which one peer closes a single connection
|
||||
/// using [`NetworkBehaviourAction::CloseConnection`] returned by a [`NetworkBehaviour`].
|
||||
/// using [`ToSwarm::CloseConnection`] returned by a [`NetworkBehaviour`].
|
||||
///
|
||||
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
|
||||
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
|
||||
@ -2421,7 +2425,7 @@ mod tests {
|
||||
let conn_id =
|
||||
swarm2.behaviour.on_connection_established[num_connections / 2].1;
|
||||
swarm2.behaviour.inner().next_action.replace(
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
ToSwarm::CloseConnection {
|
||||
peer_id: swarm1_id,
|
||||
connection: CloseConnection::One(conn_id),
|
||||
},
|
||||
|
@ -23,8 +23,8 @@ use crate::behaviour::{
|
||||
FromSwarm, ListenerClosed, ListenerError, NewExternalAddr, NewListenAddr, NewListener,
|
||||
};
|
||||
use crate::{
|
||||
ConnectionDenied, ConnectionHandler, ConnectionId, NetworkBehaviour, NetworkBehaviourAction,
|
||||
PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
|
||||
ConnectionDenied, ConnectionHandler, ConnectionId, NetworkBehaviour, PollParameters, THandler,
|
||||
THandlerInEvent, THandlerOutEvent, ToSwarm,
|
||||
};
|
||||
use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, Endpoint};
|
||||
use libp2p_identity::PeerId;
|
||||
@ -48,7 +48,7 @@ where
|
||||
/// The next action to return from `poll`.
|
||||
///
|
||||
/// An action is only returned once.
|
||||
pub next_action: Option<NetworkBehaviourAction<TOutEvent, THandler::InEvent>>,
|
||||
pub next_action: Option<ToSwarm<TOutEvent, THandler::InEvent>>,
|
||||
}
|
||||
|
||||
impl<THandler, TOutEvent> MockBehaviour<THandler, TOutEvent>
|
||||
@ -114,7 +114,7 @@ where
|
||||
&mut self,
|
||||
_: &mut Context,
|
||||
_: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
self.next_action.take().map_or(Poll::Pending, Poll::Ready)
|
||||
}
|
||||
|
||||
@ -579,7 +579,7 @@ where
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
args: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
self.poll += 1;
|
||||
self.inner.poll(cx, args)
|
||||
}
|
||||
|
Reference in New Issue
Block a user