use crate::pump::WindowedPump;
use crate::transport::ByteTransport;
use crate::window::MAX_WINDOW;
use super::link::PeerLink;
use super::peer::PeerId;
use super::session::SessionConfig;
use super::table::{PeerTable, TableError};
pub struct Node<const N: usize> {
table: PeerTable<N>,
}
impl<const N: usize> Node<N> {
pub const fn new(local: PeerId) -> Self {
Self {
table: PeerTable::new(local),
}
}
pub const fn with(local: PeerId, config: SessionConfig) -> Self {
Self {
table: PeerTable::with(local, config),
}
}
pub const fn id(&self) -> PeerId {
self.table.local()
}
pub const fn config(&self) -> SessionConfig {
self.table.config()
}
pub const fn table(&self) -> &PeerTable<N> {
&self.table
}
pub fn table_mut(&mut self) -> &mut PeerTable<N> {
&mut self.table
}
pub fn established(&self) -> usize {
self.table.established()
}
pub fn connect<Tx, Rx>(
&mut self,
pump: WindowedPump<Tx, Rx, MAX_WINDOW>,
) -> Result<PeerLink<Tx, Rx>, TableError>
where
Tx: ByteTransport,
Rx: ByteTransport<Error = Tx::Error>,
{
PeerLink::connect(&mut self.table, pump)
}
pub fn accept<Tx, Rx>(&self, pump: WindowedPump<Tx, Rx, MAX_WINDOW>) -> PeerLink<Tx, Rx>
where
Tx: ByteTransport,
Rx: ByteTransport<Error = Tx::Error>,
{
PeerLink::accept(&self.table, pump)
}
}