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

refactor(swarm)!: deprecate PollParameters where possible #3153

Merged
merged 15 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
// DEALINGS IN THE SOFTWARE.

mod either;
mod listen_addresses;
pub mod toggle;

pub use listen_addresses::ListenAddresses;

use crate::dial_opts::DialOpts;
use crate::handler::{ConnectionHandler, IntoConnectionHandler};
use crate::{AddressRecord, AddressScore, DialError};
Expand Down Expand Up @@ -402,6 +405,10 @@ pub trait PollParameters {
fn supported_protocols(&self) -> Self::SupportedProtocolsIter;

/// Returns the list of the addresses we're listening on.
#[deprecated(
since = "0.41.0",
note = "Use `libp2p_swarm::ListenAddresses` instead."
)]
fn listened_addresses(&self) -> Self::ListenedAddressesIter;

/// Returns the list of the addresses nodes can use to reach us.
Expand Down
33 changes: 33 additions & 0 deletions swarm/src/behaviour/listen_addresses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::behaviour::{ExpiredListenAddr, FromSwarm, NewListenAddr};
use crate::IntoConnectionHandler;
use libp2p_core::Multiaddr;
use std::collections::HashSet;

/// Utility struct for tracking the addresses a [`Swarm`](crate::Swarm) is listening on.
#[derive(Debug, Default, Clone)]
pub struct ListenAddresses {
addresses: HashSet<Multiaddr>,
}

impl ListenAddresses {
/// Returns an [`Iterator`] over all listen addresses.
pub fn iter(&self) -> impl ExactSizeIterator<Item = &Multiaddr> {
self.addresses.iter()
}

/// Feed a [`SwarmEvent`] to this struct.
pub fn on_event<THandler>(&mut self, event: &FromSwarm<THandler>)
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
where
THandler: IntoConnectionHandler,
{
match event {
FromSwarm::NewListenAddr(NewListenAddr { addr, .. }) => {
self.addresses.insert((*addr).clone());
}
FromSwarm::ExpiredListenAddr(ExpiredListenAddr { addr, .. }) => {
self.addresses.insert((*addr).clone());
}
_ => {}
}
}
}
3 changes: 2 additions & 1 deletion swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ pub mod derive_prelude {
}

pub use behaviour::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
CloseConnection, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
};
pub use connection::pool::{ConnectionCounters, ConnectionLimits};
pub use connection::{
Expand Down