Skip to content

Commit 0a4c805

Browse files
committed
refactor: [torrust#1495] add SwarmMetadata to Swarm
- Moved responsability for keeping metadata to the Swarm type. - Number of seeder and leechers is now calculated when the Swarm changes not on-demand. We avoid iterating over the peers to get the number of seeders and leechers. - The number of downloads is also calculate now in the Swarm. It will be removed from the TrackedTorrent.
1 parent 2882705 commit 0a4c805

File tree

2 files changed

+503
-166
lines changed

2 files changed

+503
-166
lines changed

packages/primitives/src/swarm_metadata.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use derive_more::Constructor;
77
/// Swarm metadata dictionary in the scrape response.
88
///
99
/// See [BEP 48: Tracker Protocol Extension: Scrape](https://www.bittorrent.org/beps/bep_0048.html)
10-
#[derive(Copy, Clone, Debug, PartialEq, Default, Constructor)]
10+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Constructor)]
1111
pub struct SwarmMetadata {
1212
/// (i.e `completed`): The number of peers that have ever completed
1313
/// downloading a given torrent.
@@ -27,6 +27,21 @@ impl SwarmMetadata {
2727
pub fn zeroed() -> Self {
2828
Self::default()
2929
}
30+
31+
#[must_use]
32+
pub fn downloads(&self) -> u32 {
33+
self.downloaded
34+
}
35+
36+
#[must_use]
37+
pub fn seeders(&self) -> u32 {
38+
self.complete
39+
}
40+
41+
#[must_use]
42+
pub fn leechers(&self) -> u32 {
43+
self.incomplete
44+
}
3045
}
3146

3247
/// Structure that holds aggregate swarm metadata.

0 commit comments

Comments
 (0)