Skip to main content

fx_torrent/
config.rs

1use std::path::{Path, PathBuf};
2use std::time::Duration;
3
4const DEFAULT_PEER_CLIENT_NAME: &str = "PopcornFX";
5const DEFAULT_PEER_TIMEOUT: Duration = Duration::from_secs(6);
6const DEFAULT_PEER_LOWER_LIMIT: usize = 10;
7const DEFAULT_PEER_UPPER_LIMIT: usize = 200;
8const DEFAULT_PEER_IN_FLIGHT: usize = 25;
9const DEFAULT_PEER_UPLOAD_SLOTS: usize = 50;
10const DEFAULT_MAX_IN_FLIGHT_PIECES: usize = 256;
11const DEFAULT_CHECKING_MEMORY_USAGE: usize = 32 * 1024 * 1024; // 32MB
12
13/// The configuration of a torrent session.
14#[derive(Debug, Clone)]
15pub struct SessionConfig {
16    pub enable_tcp_peer: bool,
17    pub enable_utp_peer: bool,
18    pub torrent: TorrentConfig,
19}
20
21impl SessionConfig {
22    /// Create a new session configuration builder.
23    pub fn builder() -> SessionConfigBuilder {
24        SessionConfigBuilder::builder()
25    }
26
27    /// Returns the client name for the session.
28    pub fn client_name(&self) -> &str {
29        self.torrent.client_name()
30    }
31
32    /// Returns the filepath for the torrent data of the session.
33    pub fn path(&self) -> &Path {
34        self.torrent.path()
35    }
36}
37
38#[derive(Debug, Default)]
39pub struct SessionConfigBuilder {
40    enable_tcp_peer: Option<bool>,
41    enable_utp_peer: Option<bool>,
42    torrent: TorrentConfigBuilder,
43}
44
45impl SessionConfigBuilder {
46    /// Create a new session configuration builder.
47    pub fn builder() -> Self {
48        Self::default()
49    }
50
51    /// Set if TCP peer connections should be enabled for the session.
52    pub fn enable_tcp_peer(&mut self, enable: bool) -> &mut Self {
53        self.enable_tcp_peer = Some(enable);
54        self
55    }
56
57    /// Set if UTP peer connections should be enabled for the session.
58    pub fn enable_utp_peer(&mut self, enable: bool) -> &mut Self {
59        self.enable_utp_peer = Some(enable);
60        self
61    }
62
63    /// Set the client name for the session.
64    pub fn client_name<S: AsRef<str>>(&mut self, name: S) -> &mut Self {
65        self.torrent.client_name(name);
66        self
67    }
68
69    /// Set the filepath for the torrent data of the session.
70    pub fn path(&mut self, path: impl AsRef<Path>) -> &mut Self {
71        self.torrent.path(path);
72        self
73    }
74
75    /// Set the lower limit for the number of peers.
76    pub fn peers_lower_limit(&mut self, limit: usize) -> &mut Self {
77        self.torrent.peers_lower_limit(limit);
78        self
79    }
80
81    /// Set the upper limit for the number of peers.
82    pub fn peers_upper_limit(&mut self, limit: usize) -> &mut Self {
83        self.torrent.peers_upper_limit(limit);
84        self
85    }
86
87    /// Set the max number of peer connections in flight.
88    pub fn peers_in_flight(&mut self, limit: usize) -> &mut Self {
89        self.torrent.peers_in_flight(limit);
90        self
91    }
92
93    /// Set the max number of peer upload slots.
94    pub fn peers_upload_slots(&mut self, slots: usize) -> &mut Self {
95        self.torrent.peers_upload_slots(slots);
96        self
97    }
98
99    /// Set the timeout for peer connections.
100    pub fn peer_connection_timeout(&mut self, timeout: Duration) -> &mut Self {
101        self.torrent.peer_connection_timeout(timeout);
102        self
103    }
104
105    /// Set the maximum number of in flight pieces which can be requested in parallel from peers.
106    pub fn max_in_flight_pieces(&mut self, limit: usize) -> &mut Self {
107        self.torrent.max_in_flight_pieces(limit);
108        self
109    }
110
111    /// Set the maximum memory usage in bytes for checking files.
112    /// Higher number means more memory usage but faster validation.
113    /// Defaults to 32MB.
114    pub fn checking_mem_usage(&mut self, mem_usage: usize) -> &mut Self {
115        self.torrent.checking_mem_usage(mem_usage);
116        self
117    }
118
119    /// Build the session configuration.
120    pub fn build(&mut self) -> SessionConfig {
121        SessionConfig {
122            enable_tcp_peer: self.enable_tcp_peer.take().unwrap_or(true),
123            enable_utp_peer: self.enable_utp_peer.take().unwrap_or(true),
124            torrent: self.torrent.build(),
125        }
126    }
127}
128
129/// The torrent configuration values.
130#[derive(Debug, Clone)]
131pub struct TorrentConfig {
132    client_name: String,
133    path: PathBuf,
134    pub peers_lower_limit: usize,
135    pub peers_upper_limit: usize,
136    pub peers_in_flight: usize,
137    pub peers_upload_slots: usize,
138    pub peer_connection_timeout: Duration,
139    /// The maximum number of in flight pieces which can be requested in parallel from peers.
140    pub max_in_flight_pieces: usize,
141    /// The maximum memory usage in bytes for checking files.
142    /// Higher number means more memory usage but faster validation.
143    pub checking_mem_usage: usize,
144}
145
146impl TorrentConfig {
147    /// Create a new torrent configuration builder.
148    pub fn builder() -> TorrentConfigBuilder {
149        TorrentConfigBuilder::builder()
150    }
151
152    /// Returns the client name of the torrent.
153    pub fn client_name(&self) -> &str {
154        self.client_name.as_str()
155    }
156
157    /// Returns the path of the torrent data.
158    pub fn path(&self) -> &Path {
159        self.path.as_path()
160    }
161
162    /// Set the filepath for the torrent data.
163    pub fn set_path(&mut self, path: impl AsRef<Path>) {
164        self.path = path.as_ref().to_path_buf();
165    }
166}
167
168impl Default for TorrentConfig {
169    fn default() -> Self {
170        Self::builder().build()
171    }
172}
173
174#[derive(Debug, Default)]
175pub struct TorrentConfigBuilder {
176    client_name: Option<String>,
177    path: Option<PathBuf>,
178    peers_lower_limit: Option<usize>,
179    peers_upper_limit: Option<usize>,
180    peers_in_flight: Option<usize>,
181    peers_upload_slots: Option<usize>,
182    peer_connection_timeout: Option<Duration>,
183    max_in_flight_pieces: Option<usize>,
184    checking_mem_usage: Option<usize>,
185}
186
187impl TorrentConfigBuilder {
188    /// Create a new torrent configuration builder.
189    pub fn builder() -> Self {
190        Self::default()
191    }
192
193    /// Set the name of the client.
194    pub fn client_name<S: AsRef<str>>(&mut self, name: S) -> &mut Self {
195        self.client_name = Some(name.as_ref().to_string());
196        self
197    }
198
199    /// Set the torrent data path.
200    /// This is the path where the downloaded data will be stored.
201    pub fn path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
202        self.path = Some(path.as_ref().to_path_buf());
203        self
204    }
205
206    /// Set the lower limit for the number of peers.
207    pub fn peers_lower_limit(&mut self, limit: usize) -> &mut Self {
208        self.peers_lower_limit = Some(limit);
209        self
210    }
211
212    /// Set the upper limit for the number of peers.
213    pub fn peers_upper_limit(&mut self, limit: usize) -> &mut Self {
214        self.peers_upper_limit = Some(limit);
215        self
216    }
217
218    /// Set the max number of peer connections in flight.
219    pub fn peers_in_flight(&mut self, limit: usize) -> &mut Self {
220        self.peers_in_flight = Some(limit);
221        self
222    }
223
224    /// Set the max number of peer upload slots.
225    pub fn peers_upload_slots(&mut self, slots: usize) -> &mut Self {
226        self.peers_upload_slots = Some(slots);
227        self
228    }
229
230    /// Set the timeout for peer connections.
231    pub fn peer_connection_timeout(&mut self, timeout: Duration) -> &mut Self {
232        self.peer_connection_timeout = Some(timeout);
233        self
234    }
235
236    /// Set the maximum number of in flight pieces which can be requested in parallel from peers.
237    pub fn max_in_flight_pieces(&mut self, limit: usize) -> &mut Self {
238        self.max_in_flight_pieces = Some(limit);
239        self
240    }
241
242    /// Set the maximum memory usage in bytes for checking files.
243    /// Higher number means more memory usage but faster validation.
244    /// Defaults to 32MB.
245    pub fn checking_mem_usage(&mut self, mem_usage: usize) -> &mut Self {
246        self.checking_mem_usage = Some(mem_usage);
247        self
248    }
249
250    /// Build the torrent configuration.
251    pub fn build(&mut self) -> TorrentConfig {
252        let client_name = self
253            .client_name
254            .take()
255            .unwrap_or_else(|| DEFAULT_PEER_CLIENT_NAME.to_string());
256        let path = self.path.take().unwrap_or_else(|| PathBuf::new());
257        let peers_lower_limit = self
258            .peers_lower_limit
259            .take()
260            .unwrap_or(DEFAULT_PEER_LOWER_LIMIT);
261        let peers_upper_limit = self
262            .peers_upper_limit
263            .take()
264            .unwrap_or(DEFAULT_PEER_UPPER_LIMIT);
265        let peers_in_flight = self
266            .peers_in_flight
267            .take()
268            .unwrap_or(DEFAULT_PEER_IN_FLIGHT);
269        let peers_upload_slots = self
270            .peers_upload_slots
271            .take()
272            .unwrap_or(DEFAULT_PEER_UPLOAD_SLOTS);
273        let peer_connection_timeout = self
274            .peer_connection_timeout
275            .take()
276            .unwrap_or(DEFAULT_PEER_TIMEOUT);
277        let max_in_flight_pieces = self
278            .max_in_flight_pieces
279            .take()
280            .unwrap_or(DEFAULT_MAX_IN_FLIGHT_PIECES);
281        let checking_mem_usage = self
282            .checking_mem_usage
283            .take()
284            .unwrap_or(DEFAULT_CHECKING_MEMORY_USAGE);
285
286        TorrentConfig {
287            client_name,
288            path,
289            peers_lower_limit,
290            peers_upper_limit,
291            peers_in_flight,
292            peers_upload_slots,
293            peer_connection_timeout,
294            max_in_flight_pieces,
295            checking_mem_usage,
296        }
297    }
298}
299
300#[cfg(test)]
301mod tests {
302    use super::*;
303
304    mod session {
305        use super::*;
306
307        #[test]
308        fn test_default() {
309            let result = SessionConfig::builder().build();
310
311            assert_eq!(DEFAULT_PEER_CLIENT_NAME, result.client_name());
312            assert_eq!(PathBuf::new(), result.path());
313        }
314    }
315
316    mod torrent {
317        use super::*;
318
319        #[test]
320        fn test_default() {
321            let result = TorrentConfig::builder().build();
322
323            assert_eq!(DEFAULT_PEER_CLIENT_NAME, result.client_name);
324            assert_eq!(PathBuf::new(), result.path);
325        }
326    }
327}