mod operations;
mod types;
#[cfg(test)]
mod tests;
pub use types::{BtObject, BtObjectBuilder};
use crate::engine::bittorrent::dht::config::DhtEngineOptions;
use crate::engine::bittorrent::dht::engine_set::DhtEngineSet;
use crate::engine::bittorrent::peer::blocklist::BtPeerBlocklist;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
pub struct BtRegistry {
pub(crate) pool: HashMap<u64, BtObject>,
pub(crate) info_hash_index: HashMap<String, u64>,
dht_external_ips: HashMap<u64, std::net::IpAddr>,
global_dht_engines: DhtEngineSet,
global_dht_options: Arc<DhtEngineOptions>,
pub(crate) global_dht_storage_config_revision: u64,
dht_engine_start_locks: [std::sync::Arc<tokio::sync::Mutex<()>>; 2],
dht_engines: HashMap<u64, DhtEngineSet>,
tcp_port: u16,
udp_port: u16,
peer_blocklist: BtPeerBlocklist,
}
impl BtRegistry {
pub fn new() -> Self {
Self {
pool: HashMap::new(),
info_hash_index: HashMap::new(),
dht_external_ips: HashMap::new(),
global_dht_engines: DhtEngineSet::default(),
global_dht_options: Arc::new(DhtEngineOptions::default()),
global_dht_storage_config_revision: 0,
dht_engine_start_locks: [
std::sync::Arc::new(tokio::sync::Mutex::new(())),
std::sync::Arc::new(tokio::sync::Mutex::new(())),
],
dht_engines: HashMap::new(),
tcp_port: 0,
udp_port: 0,
peer_blocklist: BtPeerBlocklist::new(),
}
}
pub(crate) fn set_global_dht_options(&mut self, options: Arc<DhtEngineOptions>) {
self.global_dht_options = options;
}
pub(crate) fn global_dht_options(&self) -> Arc<DhtEngineOptions> {
Arc::clone(&self.global_dht_options)
}
}
impl Default for BtRegistry {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for BtRegistry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BtRegistry")
.field("pool_len", &self.pool.len())
.field("info_hash_index_len", &self.info_hash_index.len())
.field("dht_engine_count", &self.global_dht_engines.iter().count())
.field("tcp_port", &self.tcp_port)
.field("udp_port", &self.udp_port)
.field("blocklist_count", &self.peer_blocklist.count())
.finish()
}
}