use dht_rpc::DhtConfig;
pub const DEFAULT_MAX_PARALLEL: usize = 3;
pub const DEFAULT_MAX_PEERS: usize = 64;
#[derive(Debug)]
pub struct SwarmConfig {
pub max_parallel: usize,
pub max_peers: usize,
pub auto_connect: bool,
pub auto_retry: bool,
pub dht_config: DhtConfig,
}
impl Default for SwarmConfig {
fn default() -> Self {
Self {
max_parallel: DEFAULT_MAX_PARALLEL,
max_peers: DEFAULT_MAX_PEERS,
auto_connect: true,
auto_retry: true,
dht_config: DhtConfig::default(),
}
}
}
impl SwarmConfig {
pub fn new(dht_config: DhtConfig) -> Self {
Self {
dht_config,
..Default::default()
}
}
pub fn max_parallel(mut self, n: usize) -> Self {
self.max_parallel = n;
self
}
pub fn max_peers(mut self, n: usize) -> Self {
self.max_peers = n;
self
}
pub fn auto_connect(mut self, enabled: bool) -> Self {
self.auto_connect = enabled;
self
}
pub fn auto_retry(mut self, enabled: bool) -> Self {
self.auto_retry = enabled;
self
}
pub fn add_bootstrap_node(mut self, addr: std::net::SocketAddr) -> Self {
self.dht_config = self.dht_config.add_bootstrap_node(addr);
self
}
}
impl From<DhtConfig> for SwarmConfig {
fn from(dht_config: DhtConfig) -> Self {
Self::new(dht_config)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = SwarmConfig::default();
assert_eq!(config.max_parallel, DEFAULT_MAX_PARALLEL);
assert_eq!(config.max_peers, DEFAULT_MAX_PEERS);
assert!(config.auto_connect);
assert!(config.auto_retry);
}
#[test]
fn test_builder_pattern() {
let config = SwarmConfig::default()
.max_parallel(5)
.max_peers(100)
.auto_connect(false);
assert_eq!(config.max_parallel, 5);
assert_eq!(config.max_peers, 100);
assert!(!config.auto_connect);
}
}