use std::collections::HashMap;
use std::sync::Arc;
use iroh::address_lookup::{AddressLookup, EndpointData, EndpointInfo, Item};
use iroh_base::{EndpointAddr, EndpointId};
use n0_future::boxed::BoxStream;
use n0_future::stream::StreamExt;
#[derive(Debug, Clone)]
pub struct StaticAddressLookup {
map: Arc<HashMap<EndpointId, EndpointAddr>>,
}
impl StaticAddressLookup {
pub fn new(addrs: impl IntoIterator<Item = EndpointAddr>) -> Self {
let map = addrs
.into_iter()
.map(|addr| (addr.id, addr))
.collect::<HashMap<_, _>>();
Self { map: Arc::new(map) }
}
}
const PROVENANCE: &str = "triblespace-net/static";
impl AddressLookup for StaticAddressLookup {
fn publish(&self, _data: &EndpointData) {}
fn resolve(
&self,
endpoint_id: EndpointId,
) -> Option<BoxStream<Result<Item, iroh::address_lookup::Error>>> {
match self.map.get(&endpoint_id) {
Some(addr) => {
let data = EndpointData::from_iter(addr.addrs.iter().cloned());
let info = EndpointInfo::from_parts(endpoint_id, data);
let item = Item::new(info, PROVENANCE, None);
Some(n0_future::stream::once(Ok(item)).boxed())
}
None => Some(n0_future::stream::empty().boxed()),
}
}
}