use std::collections::{BTreeMap, HashSet};
use std::fmt::Debug;
use futures_util::{Sink, Stream};
use crate::address_book::NodeInfo;
pub trait DiscoveryStrategy<ID, N>
where
N: NodeInfo<ID>,
{
type Error;
fn next_node(
&self,
previous: Option<&DiscoveryResult<ID, N>>,
) -> impl Future<Output = Result<Option<ID>, Self::Error>>;
}
pub trait DiscoveryProtocol<ID, N>
where
N: NodeInfo<ID>,
{
type Error;
type Message;
fn alice(
&self,
tx: &mut (impl Sink<Self::Message, Error = impl Debug> + Unpin),
rx: &mut (impl Stream<Item = Result<Self::Message, impl Debug>> + Unpin),
) -> impl Future<Output = Result<DiscoveryResult<ID, N>, Self::Error>>;
fn bob(
&self,
tx: &mut (impl Sink<Self::Message, Error = impl Debug> + Unpin),
rx: &mut (impl Stream<Item = Result<Self::Message, impl Debug>> + Unpin),
) -> impl Future<Output = Result<DiscoveryResult<ID, N>, Self::Error>>;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DiscoveryResult<ID, N>
where
N: NodeInfo<ID>,
{
pub remote_node_id: ID,
pub transport_infos: BTreeMap<ID, N::Transports>,
pub topics: HashSet<[u8; 32]>,
}
impl<ID, N> DiscoveryResult<ID, N>
where
N: NodeInfo<ID>,
{
pub fn new(remote_node_id: ID) -> Self {
Self {
remote_node_id,
transport_infos: BTreeMap::new(),
topics: HashSet::new(),
}
}
}
pub trait LocalTopics {
type Error;
fn topics(&self) -> impl Future<Output = Result<HashSet<[u8; 32]>, Self::Error>>;
}