Simplify the trait for tracker comms
This commit is contained in:
parent
b30ee13388
commit
70c59834ba
2 changed files with 28 additions and 28 deletions
|
|
@ -41,7 +41,7 @@ use crate::{
|
||||||
torrent_state::{
|
torrent_state::{
|
||||||
ManagedTorrentBuilder, ManagedTorrentHandle, ManagedTorrentState, TorrentStateLive,
|
ManagedTorrentBuilder, ManagedTorrentHandle, ManagedTorrentState, TorrentStateLive,
|
||||||
},
|
},
|
||||||
tracker_comms::{self, TrackerComms},
|
tracker_comms::TrackerComms,
|
||||||
type_aliases::PeerStream,
|
type_aliases::PeerStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1093,7 +1093,7 @@ impl Session {
|
||||||
self.peer_id,
|
self.peer_id,
|
||||||
trackers,
|
trackers,
|
||||||
// TODO: report actual bytes, not zeroes.
|
// TODO: report actual bytes, not zeroes.
|
||||||
Box::new(tracker_comms::TorrentStatsForTrackerDummy {}),
|
Box::new(()),
|
||||||
force_tracker_interval,
|
force_tracker_interval,
|
||||||
cancel,
|
cancel,
|
||||||
announce_port,
|
announce_port,
|
||||||
|
|
|
||||||
|
|
@ -20,21 +20,24 @@ use librqbit_core::hash_id::Id20;
|
||||||
pub struct TrackerComms {
|
pub struct TrackerComms {
|
||||||
info_hash: Id20,
|
info_hash: Id20,
|
||||||
peer_id: Id20,
|
peer_id: Id20,
|
||||||
stats: Box<dyn TorrentStatsForTracker>,
|
stats: Box<dyn TorrentStatsProvider>,
|
||||||
force_tracker_interval: Option<Duration>,
|
force_tracker_interval: Option<Duration>,
|
||||||
cancellation_token: CancellationToken,
|
cancellation_token: CancellationToken,
|
||||||
tx: Sender,
|
tx: Sender,
|
||||||
tcp_listen_port: Option<u16>,
|
tcp_listen_port: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TorrentStatsForTracker: Send + Sync {
|
#[derive(Default)]
|
||||||
fn get_uploaded_bytes(&self) -> u64;
|
pub struct TrackerCommsStats {
|
||||||
fn get_downloaded_bytes(&self) -> u64;
|
pub uploaded_bytes: u64,
|
||||||
fn get_total_bytes(&self) -> u64;
|
pub downloaded_bytes: u64,
|
||||||
|
pub total_bytes: u64,
|
||||||
|
}
|
||||||
|
|
||||||
fn get_left_to_download_bytes(&self) -> u64 {
|
impl TrackerCommsStats {
|
||||||
let total = self.get_total_bytes();
|
pub fn get_left_to_download_bytes(&self) -> u64 {
|
||||||
let down = self.get_downloaded_bytes();
|
let total = self.total_bytes;
|
||||||
|
let down = self.downloaded_bytes;
|
||||||
if total >= down {
|
if total >= down {
|
||||||
return total - down;
|
return total - down;
|
||||||
}
|
}
|
||||||
|
|
@ -42,18 +45,13 @@ pub trait TorrentStatsForTracker: Send + Sync {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TorrentStatsForTrackerDummy {}
|
pub trait TorrentStatsProvider: Send + Sync {
|
||||||
impl TorrentStatsForTracker for TorrentStatsForTrackerDummy {
|
fn get(&self) -> TrackerCommsStats;
|
||||||
fn get_uploaded_bytes(&self) -> u64 {
|
}
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_downloaded_bytes(&self) -> u64 {
|
impl TorrentStatsProvider for () {
|
||||||
0
|
fn get(&self) -> TrackerCommsStats {
|
||||||
}
|
Default::default()
|
||||||
|
|
||||||
fn get_total_bytes(&self) -> u64 {
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,7 +62,7 @@ impl TrackerComms {
|
||||||
info_hash: Id20,
|
info_hash: Id20,
|
||||||
peer_id: Id20,
|
peer_id: Id20,
|
||||||
trackers: Vec<String>,
|
trackers: Vec<String>,
|
||||||
stats: Box<dyn TorrentStatsForTracker>,
|
stats: Box<dyn TorrentStatsProvider>,
|
||||||
force_interval: Option<Duration>,
|
force_interval: Option<Duration>,
|
||||||
cancellation_token: CancellationToken,
|
cancellation_token: CancellationToken,
|
||||||
tcp_listen_port: Option<u16>,
|
tcp_listen_port: Option<u16>,
|
||||||
|
|
@ -131,13 +129,14 @@ impl TrackerComms {
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let mut event = Some(tracker_comms_http::TrackerRequestEvent::Started);
|
let mut event = Some(tracker_comms_http::TrackerRequestEvent::Started);
|
||||||
loop {
|
loop {
|
||||||
|
let stats = self.stats.get();
|
||||||
let request = tracker_comms_http::TrackerRequest {
|
let request = tracker_comms_http::TrackerRequest {
|
||||||
info_hash: self.info_hash,
|
info_hash: self.info_hash,
|
||||||
peer_id: self.peer_id,
|
peer_id: self.peer_id,
|
||||||
port: 6778,
|
port: 6778,
|
||||||
uploaded: self.stats.get_uploaded_bytes(),
|
uploaded: stats.uploaded_bytes,
|
||||||
downloaded: self.stats.get_downloaded_bytes(),
|
downloaded: stats.downloaded_bytes,
|
||||||
left: self.stats.get_left_to_download_bytes(),
|
left: stats.get_left_to_download_bytes(),
|
||||||
compact: true,
|
compact: true,
|
||||||
no_peer_id: false,
|
no_peer_id: false,
|
||||||
event,
|
event,
|
||||||
|
|
@ -212,12 +211,13 @@ impl TrackerComms {
|
||||||
tokio::time::sleep(i).await;
|
tokio::time::sleep(i).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let stats = self.stats.get();
|
||||||
let request = AnnounceFields {
|
let request = AnnounceFields {
|
||||||
info_hash: self.info_hash,
|
info_hash: self.info_hash,
|
||||||
peer_id: self.peer_id,
|
peer_id: self.peer_id,
|
||||||
downloaded: self.stats.get_downloaded_bytes(),
|
downloaded: stats.downloaded_bytes,
|
||||||
left: self.stats.get_left_to_download_bytes(),
|
left: stats.get_left_to_download_bytes(),
|
||||||
uploaded: self.stats.get_uploaded_bytes(),
|
uploaded: stats.uploaded_bytes,
|
||||||
event: EVENT_NONE,
|
event: EVENT_NONE,
|
||||||
key: 0, // whatever that is?
|
key: 0, // whatever that is?
|
||||||
port: self.tcp_listen_port.unwrap_or(0),
|
port: self.tcp_listen_port.unwrap_or(0),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue