Report size to trackers
This commit is contained in:
parent
5488e1d40f
commit
a6ebecee97
8 changed files with 183 additions and 42 deletions
|
|
@ -47,7 +47,7 @@ pub use session::{
|
|||
SUPPORTED_SCHEMES,
|
||||
};
|
||||
pub use spawn_utils::spawn as librqbit_spawn;
|
||||
pub use torrent_state::{ManagedTorrent, ManagedTorrentState};
|
||||
pub use torrent_state::{ManagedTorrent, ManagedTorrentState, TorrentStats, TorrentStatsState};
|
||||
|
||||
pub use buffers::*;
|
||||
pub use clone_to_owned::CloneToOwned;
|
||||
|
|
|
|||
|
|
@ -783,7 +783,7 @@ impl Session {
|
|||
/// Add a torrent to the session.
|
||||
#[inline(never)]
|
||||
pub fn add_torrent<'a>(
|
||||
&'a self,
|
||||
self: &'a Arc<Self>,
|
||||
add: AddTorrent<'a>,
|
||||
opts: Option<AddTorrentOptions>,
|
||||
) -> BoxFuture<'a, anyhow::Result<AddTorrentResponse>> {
|
||||
|
|
@ -1077,7 +1077,7 @@ impl Session {
|
|||
|
||||
// Get a peer stream from both DHT and trackers.
|
||||
fn make_peer_rx(
|
||||
&self,
|
||||
self: &Arc<Self>,
|
||||
info_hash: Id20,
|
||||
trackers: Vec<String>,
|
||||
announce_port: Option<u16>,
|
||||
|
|
@ -1089,12 +1089,16 @@ impl Session {
|
|||
.as_ref()
|
||||
.map(|dht| dht.get_peers(info_hash, announce_port))
|
||||
.transpose()?;
|
||||
|
||||
let peer_rx_stats = PeerRxTorrentInfo {
|
||||
info_hash,
|
||||
session: self.clone(),
|
||||
};
|
||||
let peer_rx = TrackerComms::start(
|
||||
info_hash,
|
||||
self.peer_id,
|
||||
trackers,
|
||||
// TODO: report actual bytes, not zeroes.
|
||||
Box::new(()),
|
||||
Box::new(peer_rx_stats),
|
||||
force_tracker_interval,
|
||||
announce_port,
|
||||
);
|
||||
|
|
@ -1108,7 +1112,7 @@ impl Session {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
|
||||
pub fn unpause(self: &Arc<Self>, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
|
||||
let peer_rx = self.make_peer_rx(
|
||||
handle.info_hash(),
|
||||
handle.info().trackers.clone().into_iter().collect(),
|
||||
|
|
@ -1124,3 +1128,45 @@ impl Session {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Ad adapter for converting stats into the format that tracker_comms accepts.
|
||||
struct PeerRxTorrentInfo {
|
||||
info_hash: Id20,
|
||||
session: Arc<Session>,
|
||||
}
|
||||
|
||||
impl tracker_comms::TorrentStatsProvider for PeerRxTorrentInfo {
|
||||
fn get(&self) -> tracker_comms::TrackerCommsStats {
|
||||
let mt = self.session.with_torrents(|torrents| {
|
||||
for (_, mt) in torrents {
|
||||
if mt.info_hash() == self.info_hash {
|
||||
return Some(mt.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
});
|
||||
let mt = match mt {
|
||||
Some(mt) => mt,
|
||||
None => {
|
||||
warn!(info_hash=?self.info_hash, "can't find torrent in the session");
|
||||
return Default::default();
|
||||
}
|
||||
};
|
||||
let stats = mt.stats();
|
||||
|
||||
use crate::torrent_state::stats::TorrentStatsState as TS;
|
||||
use tracker_comms::TrackerCommsStatsState as S;
|
||||
|
||||
tracker_comms::TrackerCommsStats {
|
||||
downloaded_bytes: stats.progress_bytes,
|
||||
total_bytes: stats.total_bytes,
|
||||
uploaded_bytes: stats.uploaded_bytes,
|
||||
torrent_state: match stats.state {
|
||||
TS::Initializing => S::Initializing,
|
||||
TS::Live => S::Live,
|
||||
TS::Paused => S::Paused,
|
||||
TS::Error => S::None,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue