use crate::behaviour::{ExpiredExternalAddr, FromSwarm, NewExternalAddr};
#[allow(deprecated)]
use crate::IntoConnectionHandler;
use libp2p_core::Multiaddr;
use std::collections::HashSet;
const MAX_LOCAL_EXTERNAL_ADDRS: usize = 20;
#[derive(Debug, Clone)]
pub struct ExternalAddresses {
addresses: HashSet<Multiaddr>,
limit: usize,
}
impl Default for ExternalAddresses {
fn default() -> Self {
Self {
addresses: Default::default(),
limit: MAX_LOCAL_EXTERNAL_ADDRS,
}
}
}
impl ExternalAddresses {
pub fn iter(&self) -> impl ExactSizeIterator<Item = &Multiaddr> {
self.addresses.iter()
}
#[allow(deprecated)]
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>)
where
THandler: IntoConnectionHandler,
{
match event {
FromSwarm::NewExternalAddr(NewExternalAddr { addr, .. }) => {
if self.addresses.len() < self.limit {
self.addresses.insert((*addr).clone());
}
}
FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr, .. }) => {
self.addresses.remove(addr);
}
_ => {}
}
}
}