Skip to content

Commit

Permalink
refactor: rename field from peers to swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Apr 15, 2024
1 parent 4a567cd commit 922afda
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ pub trait EntryAsync {
/// The tracker keeps one entry like this for every torrent.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Torrent {
/// The swarm: a network of peers that are all trying to download the torrent associated to this entry
pub(crate) peers: PeerList,
/// A network of peers that are all trying to download the torrent associated to this entry
pub(crate) swarm: PeerList,
/// The number of peers that have ever completed downloading the torrent associated to this entry
pub(crate) downloaded: u32,
}
Expand Down
20 changes: 10 additions & 10 deletions packages/torrent-repository/src/entry/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::EntrySingle;
impl Entry for EntrySingle {
#[allow(clippy::cast_possible_truncation)]
fn get_swarm_metadata(&self) -> SwarmMetadata {
let (seeders, leechers) = self.peers.seeders_and_leechers();
let (seeders, leechers) = self.swarm.seeders_and_leechers();

SwarmMetadata {
downloaded: self.downloaded,
Expand All @@ -27,54 +27,54 @@ impl Entry for EntrySingle {
return true;
}

if policy.remove_peerless_torrents && self.peers.is_empty() {
if policy.remove_peerless_torrents && self.swarm.is_empty() {
return false;
}

true
}

fn peers_is_empty(&self) -> bool {
self.peers.is_empty()
self.swarm.is_empty()
}

fn get_peers_len(&self) -> usize {
self.peers.len()
self.swarm.len()
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.peers.get_peers(limit)
self.swarm.get_peers(limit)
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
self.peers.get_peers_for_client(client, limit)
self.swarm.get_peers_for_client(client, limit)
}

fn upsert_peer(&mut self, peer: &peer::Peer) -> bool {
let mut downloaded_stats_updated: bool = false;

match peer::ReadInfo::get_event(peer) {
AnnounceEvent::Stopped => {
drop(self.peers.remove(&peer::ReadInfo::get_id(peer)));
drop(self.swarm.remove(&peer::ReadInfo::get_id(peer)));
}
AnnounceEvent::Completed => {
let previous = self.peers.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer));
let previous = self.swarm.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer));
// Don't count if peer was not previously known and not already completed.
if previous.is_some_and(|p| p.event != AnnounceEvent::Completed) {
self.downloaded += 1;
downloaded_stats_updated = true;
}
}
_ => {
drop(self.peers.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer)));
drop(self.swarm.insert(peer::ReadInfo::get_id(peer), Arc::new(*peer)));
}
}

downloaded_stats_updated
}

fn remove_inactive_peers(&mut self, current_cutoff: DurationSinceUnixEpoch) {
self.peers
self.swarm
.retain(|_, peer| peer::ReadInfo::get_updated(peer) > current_cutoff);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
2 changes: 1 addition & 1 deletion packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
}

let entry = EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *downloaded,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ where
}

let entry = EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: PeerList::default(),
swarm: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down

0 comments on commit 922afda

Please sign in to comment.