use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use tokio::sync::{RwLock, watch};
mod api;
mod lifecycle;
mod receive;
mod startup;
#[cfg(test)]
mod tests;
use super::handler::DhtLocalPeerLookup;
use super::peer_storage::DhtPeerStorage;
use super::store::DhtItemStore;
use super::task::DhtTaskQueue;
use super::task_impl::DhtTaskContext;
use super::token_tracker::TokenTracker;
#[derive(Debug, Clone)]
pub struct DhtEngineConfig {
pub port: u16,
pub port_range: Option<Vec<u16>>,
pub listen_addr: Option<IpAddr>,
pub bootstrap_nodes: Vec<SocketAddr>,
pub self_id: [u8; 20],
pub dht_file_path: Option<PathBuf>,
pub refresh_check_interval: Duration,
pub query_timeout: Duration,
pub token_rotation_interval: Duration,
pub node_contact_interval: Duration,
pub max_concurrent_lookups: usize,
pub max_stored_items: usize,
pub bootstrap_on_start: bool,
pub bootstrap_timeout: Duration,
pub cleanup_interval: Duration,
pub save_interval: Duration,
pub persistence_max_age: Duration,
}
impl Default for DhtEngineConfig {
fn default() -> Self {
Self {
port: 6881,
port_range: None,
listen_addr: None,
bootstrap_nodes: Vec::new(),
self_id: [0u8; 20],
dht_file_path: None,
refresh_check_interval: Duration::from_secs(300), query_timeout: Duration::from_secs(10),
token_rotation_interval: Duration::from_secs(600), node_contact_interval: Duration::from_secs(900), max_concurrent_lookups: 16,
max_stored_items: super::store::DEFAULT_MAX_ITEMS,
bootstrap_on_start: true,
bootstrap_timeout: Duration::from_secs(60),
cleanup_interval: Duration::from_secs(300),
save_interval: Duration::from_secs(1800),
persistence_max_age: Duration::from_secs(24 * 60 * 60),
}
}
}
impl DhtEngineConfig {
pub fn local() -> Self {
Self {
port: 0,
bootstrap_on_start: false,
..Default::default()
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum DhtEngineState {
Stopped = 0,
Bootstrapping = 1,
Running = 2,
ShuttingDown = 3,
}
#[derive(Debug, Clone)]
pub struct DhtEngineStats {
pub total_nodes: usize,
pub good_nodes: usize,
pub pending_transactions: usize,
pub questionable_nodes: usize,
pub bad_nodes: usize,
pub cached_nodes: usize,
pub bucket_count: usize,
pub peer_info_hashes: usize,
pub stored_peers: usize,
pub peer_storage_evictions: u64,
pub max_peer_info_hashes: usize,
pub persistence_enabled: bool,
pub persistence_max_age_secs: u64,
pub cleanup_interval_secs: u64,
pub save_interval_secs: u64,
pub state: DhtEngineState,
}
#[derive(Debug, Clone)]
pub struct FindPeersResult {
pub peers: Vec<SocketAddr>,
pub nodes_contacted: usize,
}
pub(super) struct DhtEngineInner {
pub(super) state: DhtEngineState,
}
pub(super) struct DhtEngineContext {
pub(super) inner: Arc<RwLock<DhtEngineInner>>,
pub(super) state_updates: watch::Sender<DhtEngineState>,
pub(super) routing_table_save_lock: Arc<tokio::sync::Mutex<()>>,
pub(super) config: DhtEngineConfig,
pub(super) token_tracker: Arc<std::sync::Mutex<TokenTracker>>,
pub(super) peer_storage: Arc<DhtPeerStorage>,
pub(super) local_peer_lookup: Option<Arc<DhtLocalPeerLookup>>,
pub(super) item_store: DhtItemStore,
pub(super) shutdown_requested: Arc<AtomicBool>,
pub(super) task_context: DhtTaskContext,
}
pub struct DhtEngine {
pub(super) context: Arc<DhtEngineContext>,
pub(super) shutdown_tx: tokio::sync::watch::Sender<bool>,
pub(super) background_tasks: tokio::sync::Mutex<tokio::task::JoinSet<()>>,
pub(super) task_queue: Arc<DhtTaskQueue>,
}
impl DhtEngine {
pub async fn save_state(&self) -> Result<(), String> {
self.context.save_state().await
}
pub async fn evict_nodes(&self) -> (usize, usize) {
self.context.evict_and_replace_nodes().await
}
}