Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tracker/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::peer::TorrentPeer;
use crate::protocol::clock::{DefaultClock, TimeNow};
use crate::{PeerId, MAX_SCRAPE_TORRENTS};

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TorrentEntry {
#[serde(skip)]
pub peers: std::collections::BTreeMap<PeerId, TorrentPeer>,
Expand Down
24 changes: 22 additions & 2 deletions src/tracker/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ impl TorrentTracker {

// Adding torrents is not relevant to public trackers.
pub async fn add_torrent_to_whitelist(&self, info_hash: &InfoHash) -> Result<(), database::Error> {
self.database.add_info_hash_to_whitelist(info_hash.clone()).await?;
self.whitelist.write().await.insert(info_hash.clone());
self.add_torrent_to_database_whitelist(info_hash).await?;
self.add_torrent_to_memory_whitelist(info_hash).await;
Ok(())
}

async fn add_torrent_to_database_whitelist(&self, info_hash: &InfoHash) -> Result<(), database::Error> {
self.database.add_info_hash_to_whitelist(*info_hash).await?;
Ok(())
}

pub async fn add_torrent_to_memory_whitelist(&self, info_hash: &InfoHash) -> bool {
self.whitelist.write().await.insert(*info_hash)
}

// Removing torrents is not relevant to public trackers.
pub async fn remove_torrent_from_whitelist(&self, info_hash: &InfoHash) -> Result<(), database::Error> {
self.database.remove_info_hash_from_whitelist(info_hash.clone()).await?;
Expand Down Expand Up @@ -171,6 +180,7 @@ impl TorrentTracker {
Ok(())
}

/// Get all torrent peers for a given torrent filtering out the peer with the client address
pub async fn get_torrent_peers(&self, info_hash: &InfoHash, client_addr: &SocketAddr) -> Vec<TorrentPeer> {
let read_lock = self.torrents.read().await;

Expand All @@ -180,6 +190,16 @@ impl TorrentTracker {
}
}

/// Get all torrent peers for a given torrent
pub async fn get_all_torrent_peers(&self, info_hash: &InfoHash) -> Vec<TorrentPeer> {
let read_lock = self.torrents.read().await;

match read_lock.get(info_hash) {
None => vec![],
Some(entry) => entry.get_peers(None).into_iter().cloned().collect(),
}
}

pub async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &TorrentPeer) -> TorrentStats {
let mut torrents = self.torrents.write().await;

Expand Down
Loading