|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use torrust_tracker_configuration::TrackerPolicy; |
| 4 | +use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; |
| 5 | +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; |
| 6 | +use torrust_tracker_torrent_repository::entry::{Entry as _, EntryAsync as _, EntrySync as _}; |
| 7 | +use torrust_tracker_torrent_repository::{EntryMutexStd, EntryMutexTokio, EntrySingle}; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub(crate) enum Torrent { |
| 11 | + Single(EntrySingle), |
| 12 | + MutexStd(EntryMutexStd), |
| 13 | + MutexTokio(EntryMutexTokio), |
| 14 | +} |
| 15 | + |
| 16 | +impl Torrent { |
| 17 | + pub(crate) async fn get_stats(&self) -> SwarmMetadata { |
| 18 | + match self { |
| 19 | + Torrent::Single(entry) => entry.get_stats(), |
| 20 | + Torrent::MutexStd(entry) => entry.get_stats(), |
| 21 | + Torrent::MutexTokio(entry) => entry.clone().get_stats().await, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + pub(crate) async fn is_good(&self, policy: &TrackerPolicy) -> bool { |
| 26 | + match self { |
| 27 | + Torrent::Single(entry) => entry.is_good(policy), |
| 28 | + Torrent::MutexStd(entry) => entry.is_good(policy), |
| 29 | + Torrent::MutexTokio(entry) => entry.clone().is_good(policy).await, |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + pub(crate) async fn peers_is_empty(&self) -> bool { |
| 34 | + match self { |
| 35 | + Torrent::Single(entry) => entry.peers_is_empty(), |
| 36 | + Torrent::MutexStd(entry) => entry.peers_is_empty(), |
| 37 | + Torrent::MutexTokio(entry) => entry.clone().peers_is_empty().await, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub(crate) async fn get_peers_len(&self) -> usize { |
| 42 | + match self { |
| 43 | + Torrent::Single(entry) => entry.get_peers_len(), |
| 44 | + Torrent::MutexStd(entry) => entry.get_peers_len(), |
| 45 | + Torrent::MutexTokio(entry) => entry.clone().get_peers_len().await, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub(crate) async fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> { |
| 50 | + match self { |
| 51 | + Torrent::Single(entry) => entry.get_peers(limit), |
| 52 | + Torrent::MutexStd(entry) => entry.get_peers(limit), |
| 53 | + Torrent::MutexTokio(entry) => entry.clone().get_peers(limit).await, |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pub(crate) async fn get_peers_for_peer(&self, client: &peer::Peer, limit: Option<usize>) -> Vec<Arc<peer::Peer>> { |
| 58 | + match self { |
| 59 | + Torrent::Single(entry) => entry.get_peers_for_peer(client, limit), |
| 60 | + Torrent::MutexStd(entry) => entry.get_peers_for_peer(client, limit), |
| 61 | + Torrent::MutexTokio(entry) => entry.clone().get_peers_for_peer(client, limit).await, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub(crate) async fn insert_or_update_peer(&mut self, peer: &peer::Peer) -> bool { |
| 66 | + match self { |
| 67 | + Torrent::Single(entry) => entry.insert_or_update_peer(peer), |
| 68 | + Torrent::MutexStd(entry) => entry.insert_or_update_peer(peer), |
| 69 | + Torrent::MutexTokio(entry) => entry.clone().insert_or_update_peer(peer).await, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub(crate) async fn insert_or_update_peer_and_get_stats(&mut self, peer: &peer::Peer) -> (bool, SwarmMetadata) { |
| 74 | + match self { |
| 75 | + Torrent::Single(entry) => entry.insert_or_update_peer_and_get_stats(peer), |
| 76 | + Torrent::MutexStd(entry) => entry.insert_or_update_peer_and_get_stats(peer), |
| 77 | + Torrent::MutexTokio(entry) => entry.clone().insert_or_update_peer_and_get_stats(peer).await, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + pub(crate) async fn remove_inactive_peers(&mut self, current_cutoff: DurationSinceUnixEpoch) { |
| 82 | + match self { |
| 83 | + Torrent::Single(entry) => entry.remove_inactive_peers(current_cutoff), |
| 84 | + Torrent::MutexStd(entry) => entry.remove_inactive_peers(current_cutoff), |
| 85 | + Torrent::MutexTokio(entry) => entry.clone().remove_inactive_peers(current_cutoff).await, |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments