use std::path::{Path, PathBuf};
use std::time::Duration;
const DEFAULT_PEER_CLIENT_NAME: &str = "PopcornFX";
const DEFAULT_PEER_TIMEOUT: Duration = Duration::from_secs(6);
const DEFAULT_PEER_LOWER_LIMIT: usize = 10;
const DEFAULT_PEER_UPPER_LIMIT: usize = 200;
const DEFAULT_PEER_IN_FLIGHT: usize = 25;
const DEFAULT_PEER_UPLOAD_SLOTS: usize = 50;
const DEFAULT_MAX_IN_FLIGHT_PIECES: usize = 256;
const DEFAULT_CHECKING_MEMORY_USAGE: usize = 32 * 1024 * 1024;
#[derive(Debug, Clone)]
pub struct SessionConfig {
pub enable_tcp_peer: bool,
pub enable_utp_peer: bool,
pub torrent: TorrentConfig,
}
impl SessionConfig {
pub fn builder() -> SessionConfigBuilder {
SessionConfigBuilder::builder()
}
pub fn client_name(&self) -> &str {
self.torrent.client_name()
}
pub fn path(&self) -> &Path {
self.torrent.path()
}
}
#[derive(Debug, Default)]
pub struct SessionConfigBuilder {
enable_tcp_peer: Option<bool>,
enable_utp_peer: Option<bool>,
torrent: TorrentConfigBuilder,
}
impl SessionConfigBuilder {
pub fn builder() -> Self {
Self::default()
}
pub fn enable_tcp_peer(&mut self, enable: bool) -> &mut Self {
self.enable_tcp_peer = Some(enable);
self
}
pub fn enable_utp_peer(&mut self, enable: bool) -> &mut Self {
self.enable_utp_peer = Some(enable);
self
}
pub fn client_name<S: AsRef<str>>(&mut self, name: S) -> &mut Self {
self.torrent.client_name(name);
self
}
pub fn path(&mut self, path: impl AsRef<Path>) -> &mut Self {
self.torrent.path(path);
self
}
pub fn peers_lower_limit(&mut self, limit: usize) -> &mut Self {
self.torrent.peers_lower_limit(limit);
self
}
pub fn peers_upper_limit(&mut self, limit: usize) -> &mut Self {
self.torrent.peers_upper_limit(limit);
self
}
pub fn peers_in_flight(&mut self, limit: usize) -> &mut Self {
self.torrent.peers_in_flight(limit);
self
}
pub fn peers_upload_slots(&mut self, slots: usize) -> &mut Self {
self.torrent.peers_upload_slots(slots);
self
}
pub fn peer_connection_timeout(&mut self, timeout: Duration) -> &mut Self {
self.torrent.peer_connection_timeout(timeout);
self
}
pub fn max_in_flight_pieces(&mut self, limit: usize) -> &mut Self {
self.torrent.max_in_flight_pieces(limit);
self
}
pub fn checking_mem_usage(&mut self, mem_usage: usize) -> &mut Self {
self.torrent.checking_mem_usage(mem_usage);
self
}
pub fn build(&mut self) -> SessionConfig {
SessionConfig {
enable_tcp_peer: self.enable_tcp_peer.take().unwrap_or(true),
enable_utp_peer: self.enable_utp_peer.take().unwrap_or(true),
torrent: self.torrent.build(),
}
}
}
#[derive(Debug, Clone)]
pub struct TorrentConfig {
client_name: String,
path: PathBuf,
pub peers_lower_limit: usize,
pub peers_upper_limit: usize,
pub peers_in_flight: usize,
pub peers_upload_slots: usize,
pub peer_connection_timeout: Duration,
pub max_in_flight_pieces: usize,
pub checking_mem_usage: usize,
}
impl TorrentConfig {
pub fn builder() -> TorrentConfigBuilder {
TorrentConfigBuilder::builder()
}
pub fn client_name(&self) -> &str {
self.client_name.as_str()
}
pub fn path(&self) -> &Path {
self.path.as_path()
}
pub fn set_path(&mut self, path: impl AsRef<Path>) {
self.path = path.as_ref().to_path_buf();
}
}
impl Default for TorrentConfig {
fn default() -> Self {
Self::builder().build()
}
}
#[derive(Debug, Default)]
pub struct TorrentConfigBuilder {
client_name: Option<String>,
path: Option<PathBuf>,
peers_lower_limit: Option<usize>,
peers_upper_limit: Option<usize>,
peers_in_flight: Option<usize>,
peers_upload_slots: Option<usize>,
peer_connection_timeout: Option<Duration>,
max_in_flight_pieces: Option<usize>,
checking_mem_usage: Option<usize>,
}
impl TorrentConfigBuilder {
pub fn builder() -> Self {
Self::default()
}
pub fn client_name<S: AsRef<str>>(&mut self, name: S) -> &mut Self {
self.client_name = Some(name.as_ref().to_string());
self
}
pub fn path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.path = Some(path.as_ref().to_path_buf());
self
}
pub fn peers_lower_limit(&mut self, limit: usize) -> &mut Self {
self.peers_lower_limit = Some(limit);
self
}
pub fn peers_upper_limit(&mut self, limit: usize) -> &mut Self {
self.peers_upper_limit = Some(limit);
self
}
pub fn peers_in_flight(&mut self, limit: usize) -> &mut Self {
self.peers_in_flight = Some(limit);
self
}
pub fn peers_upload_slots(&mut self, slots: usize) -> &mut Self {
self.peers_upload_slots = Some(slots);
self
}
pub fn peer_connection_timeout(&mut self, timeout: Duration) -> &mut Self {
self.peer_connection_timeout = Some(timeout);
self
}
pub fn max_in_flight_pieces(&mut self, limit: usize) -> &mut Self {
self.max_in_flight_pieces = Some(limit);
self
}
pub fn checking_mem_usage(&mut self, mem_usage: usize) -> &mut Self {
self.checking_mem_usage = Some(mem_usage);
self
}
pub fn build(&mut self) -> TorrentConfig {
let client_name = self
.client_name
.take()
.unwrap_or_else(|| DEFAULT_PEER_CLIENT_NAME.to_string());
let path = self.path.take().unwrap_or_else(|| PathBuf::new());
let peers_lower_limit = self
.peers_lower_limit
.take()
.unwrap_or(DEFAULT_PEER_LOWER_LIMIT);
let peers_upper_limit = self
.peers_upper_limit
.take()
.unwrap_or(DEFAULT_PEER_UPPER_LIMIT);
let peers_in_flight = self
.peers_in_flight
.take()
.unwrap_or(DEFAULT_PEER_IN_FLIGHT);
let peers_upload_slots = self
.peers_upload_slots
.take()
.unwrap_or(DEFAULT_PEER_UPLOAD_SLOTS);
let peer_connection_timeout = self
.peer_connection_timeout
.take()
.unwrap_or(DEFAULT_PEER_TIMEOUT);
let max_in_flight_pieces = self
.max_in_flight_pieces
.take()
.unwrap_or(DEFAULT_MAX_IN_FLIGHT_PIECES);
let checking_mem_usage = self
.checking_mem_usage
.take()
.unwrap_or(DEFAULT_CHECKING_MEMORY_USAGE);
TorrentConfig {
client_name,
path,
peers_lower_limit,
peers_upper_limit,
peers_in_flight,
peers_upload_slots,
peer_connection_timeout,
max_in_flight_pieces,
checking_mem_usage,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
mod session {
use super::*;
#[test]
fn test_default() {
let result = SessionConfig::builder().build();
assert_eq!(DEFAULT_PEER_CLIENT_NAME, result.client_name());
assert_eq!(PathBuf::new(), result.path());
}
}
mod torrent {
use super::*;
#[test]
fn test_default() {
let result = TorrentConfig::builder().build();
assert_eq!(DEFAULT_PEER_CLIENT_NAME, result.client_name);
assert_eq!(PathBuf::new(), result.path);
}
}
}