use derec_proto::TransportProtocol;
use super::TransportPolicy;
use crate::Error;
impl TransportPolicy {
pub fn admit_peer_endpoints(
&self,
advertised: Vec<&TransportProtocol>,
) -> Result<Vec<TransportProtocol>, Error> {
let offered = advertised.len();
let mut seen: Vec<i32> = Vec::new();
let kept: Vec<TransportProtocol> = advertised
.into_iter()
.filter(|endpoint| match self.check_peer(endpoint) {
Ok(()) => true,
Err(_reason) => {
#[cfg(feature = "logging")]
tracing::warn!(
uri = %endpoint.uri,
error = %_reason,
"dropping a peer-advertised transport endpoint; the peer's \
remaining endpoints, if any, are still recorded",
);
false
}
})
.filter(|endpoint| {
if seen.contains(&endpoint.protocol) {
#[cfg(feature = "logging")]
tracing::warn!(
uri = %endpoint.uri,
protocol = endpoint.protocol,
"dropping a duplicate peer-advertised protocol; the \
earlier entry for it is kept",
);
return false;
}
seen.push(endpoint.protocol);
true
})
.cloned()
.collect();
if kept.is_empty() {
return Err(Error::NoUsableEndpoint { offered });
}
Ok(kept)
}
}