Report size to trackers

This commit is contained in:
Igor Katson 2024-03-01 07:54:27 +00:00
parent 5488e1d40f
commit a6ebecee97
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
8 changed files with 183 additions and 42 deletions

View file

@ -42,7 +42,7 @@ use crate::type_aliases::PeerStream;
use initializing::TorrentStateInitializing;
use self::paused::TorrentStatePaused;
use self::stats::TorrentStats;
pub use self::stats::{TorrentStats, TorrentStatsState};
pub enum ManagedTorrentState {
Initializing(Arc<TorrentStateInitializing>),
@ -351,11 +351,13 @@ impl ManagedTorrent {
/// Get stats.
pub fn stats(&self) -> TorrentStats {
use stats::TorrentStatsState as S;
let mut resp = TorrentStats {
total_bytes: self.info().lengths.total_length(),
state: "",
state: S::Error,
error: None,
progress_bytes: 0,
uploaded_bytes: 0,
finished: false,
live: None,
};
@ -363,17 +365,17 @@ impl ManagedTorrent {
self.with_state(|s| {
match s {
ManagedTorrentState::Initializing(i) => {
resp.state = "initializing";
resp.state = S::Initializing;
resp.progress_bytes = i.checked_bytes.load(Ordering::Relaxed);
}
ManagedTorrentState::Paused(p) => {
resp.state = "paused";
resp.state = S::Paused;
resp.total_bytes = p.chunk_tracker.get_total_selected_bytes();
resp.progress_bytes = resp.total_bytes - p.needed_bytes;
resp.finished = resp.progress_bytes == resp.total_bytes;
}
ManagedTorrentState::Live(l) => {
resp.state = "live";
resp.state = S::Live;
let live_stats = LiveStats::from(l.as_ref());
let total = l.get_total_selected_bytes();
let remaining = l.get_left_to_download_bytes();
@ -382,14 +384,15 @@ impl ManagedTorrent {
resp.progress_bytes = progress;
resp.total_bytes = total;
resp.finished = remaining == 0;
resp.uploaded_bytes = l.get_uploaded_bytes();
resp.live = Some(live_stats);
}
ManagedTorrentState::Error(e) => {
resp.state = "error";
resp.state = S::Error;
resp.error = Some(format!("{:?}", e))
}
ManagedTorrentState::None => {
resp.state = "error";
resp.state = S::Error;
resp.error = Some("bug: torrent in broken \"None\" state".to_string());
}
}

View file

@ -43,11 +43,35 @@ impl From<&TorrentStateLive> for LiveStats {
}
}
#[derive(Clone, Copy, Serialize, Debug)]
pub enum TorrentStatsState {
#[serde(rename = "initializing")]
Initializing,
#[serde(rename = "live")]
Live,
#[serde(rename = "paused")]
Paused,
#[serde(rename = "error")]
Error,
}
impl std::fmt::Display for TorrentStatsState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TorrentStatsState::Initializing => f.write_str("initializing"),
TorrentStatsState::Live => f.write_str("live"),
TorrentStatsState::Paused => f.write_str("paused"),
TorrentStatsState::Error => f.write_str("error"),
}
}
}
#[derive(Serialize, Debug)]
pub struct TorrentStats {
pub state: &'static str,
pub state: TorrentStatsState,
pub error: Option<String>,
pub progress_bytes: u64,
pub uploaded_bytes: u64,
pub total_bytes: u64,
pub finished: bool,
pub live: Option<LiveStats>,