use std::{collections::HashMap, sync::Arc};
use log::info;
use karyon_core::{async_runtime::Executor, crypto::KeyPair};
use karyon_net::Endpoint;
use crate::{
config::Config, conn_queue::ConnQueue, discovery::Discovery, monitor::Monitor, peer::Peer,
peer_pool::PeerPool, protocol::Protocol, PeerID, Result,
};
pub struct Backend {
config: Arc<Config>,
key_pair: KeyPair,
peer_id: PeerID,
monitor: Arc<Monitor>,
discovery: Arc<Discovery>,
peer_pool: Arc<PeerPool>,
}
impl Backend {
pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc<Backend> {
let config = Arc::new(config);
let monitor = Arc::new(Monitor::new(config.clone()));
let conn_queue = ConnQueue::new();
let peer_id = PeerID::try_from(key_pair.public())
.expect("Derive a peer id from the provided key pair.");
info!("PeerID: {}", peer_id);
let peer_pool = PeerPool::new(
&peer_id,
conn_queue.clone(),
config.clone(),
monitor.clone(),
ex.clone(),
);
let discovery = Discovery::new(
key_pair,
&peer_id,
conn_queue,
config.clone(),
monitor.clone(),
ex,
);
Arc::new(Self {
key_pair: key_pair.clone(),
peer_id,
monitor,
discovery,
config,
peer_pool,
})
}
pub async fn run(self: &Arc<Self>) -> Result<()> {
self.peer_pool.start().await?;
self.discovery.start().await?;
Ok(())
}
pub async fn attach_protocol<P: Protocol>(
&self,
c: impl Fn(Arc<Peer>) -> Arc<dyn Protocol> + Send + Sync + 'static,
) -> Result<()> {
self.peer_pool.attach_protocol::<P>(Box::new(c)).await
}
pub async fn peers(&self) -> usize {
self.peer_pool.peers_len().await
}
pub fn config(&self) -> Arc<Config> {
self.config.clone()
}
pub fn peer_id(&self) -> &PeerID {
&self.peer_id
}
pub fn key_pair(&self) -> &KeyPair {
&self.key_pair
}
pub async fn inbound_peers(&self) -> HashMap<PeerID, Endpoint> {
self.peer_pool.inbound_peers().await
}
pub async fn outbound_peers(&self) -> HashMap<PeerID, Endpoint> {
self.peer_pool.outbound_peers().await
}
pub fn monitor(&self) -> Arc<Monitor> {
self.monitor.clone()
}
pub async fn shutdown(&self) {
self.discovery.shutdown().await;
self.peer_pool.shutdown().await;
}
}