forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswarm_metadata.rs
More file actions
72 lines (59 loc) · 1.92 KB
/
swarm_metadata.rs
File metadata and controls
72 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::ops::AddAssign;
use derive_more::Constructor;
/// Swarm statistics for one torrent.
///
/// Swarm metadata dictionary in the scrape response.
///
/// See [BEP 48: Tracker Protocol Extension: Scrape](https://www.bittorrent.org/beps/bep_0048.html)
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Constructor)]
pub struct SwarmMetadata {
/// (i.e `completed`): The number of peers that have ever completed
/// downloading a given torrent.
pub downloaded: u32,
/// (i.e `seeders`): The number of active peers that have completed
/// downloading (seeders) a given torrent.
pub complete: u32,
/// (i.e `leechers`): The number of active peers that have not completed
/// downloading (leechers) a given torrent.
pub incomplete: u32,
}
impl SwarmMetadata {
#[must_use]
pub fn zeroed() -> Self {
Self::default()
}
#[must_use]
pub fn downloads(&self) -> u32 {
self.downloaded
}
#[must_use]
pub fn seeders(&self) -> u32 {
self.complete
}
#[must_use]
pub fn leechers(&self) -> u32 {
self.incomplete
}
}
/// Structure that holds aggregate swarm metadata.
///
/// Metrics are aggregate values for all active torrents/swarms.
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct AggregateActiveSwarmMetadata {
/// Total number of peers that have ever completed downloading.
pub total_downloaded: u64,
/// Total number of seeders.
pub total_complete: u64,
/// Total number of leechers.
pub total_incomplete: u64,
/// Total number of torrents.
pub total_torrents: u64,
}
impl AddAssign for AggregateActiveSwarmMetadata {
fn add_assign(&mut self, rhs: Self) {
self.total_complete += rhs.total_complete;
self.total_downloaded += rhs.total_downloaded;
self.total_incomplete += rhs.total_incomplete;
self.total_torrents += rhs.total_torrents;
}
}