From 6e9e79a02e8f50ed0ba5483879bb1b9bc5004875 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Sat, 9 Oct 2021 16:49:55 +0100 Subject: [PATCH] Move stats printing to main --- crates/librqbit/src/http_api.rs | 3 +- crates/librqbit/src/session.rs | 33 -------------------- crates/rqbit/src/main.rs | 53 ++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 39cf113..b753552 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -209,7 +209,8 @@ impl HttpApi { "GET /torrents": "List torrents (default torrent is 0)", "GET /torrents/{index}": "Torrent details", "GET /torrents/{index}/haves": "The bitfield of have pieces", - "GET /torrents/{index}/stats": "Torrent stats" + "GET /torrents/{index}/stats": "Torrent stats", + "POST /torrents/": "Add a torrent here. magnet: or http:// or a local file." } }); move || json_response(&api_list) diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 7fc3dff..b1ffc8c 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -321,39 +321,6 @@ impl Session { handle.add_peer(peer); } - spawn("Stats printer", { - let handle = handle.clone(); - async move { - loop { - let peer_stats = handle.torrent_state().peer_stats_snapshot(); - let stats = handle.torrent_state().stats_snapshot(); - let speed = handle.speed_estimator(); - let total = stats.total_bytes; - let progress = stats.total_bytes - stats.remaining_bytes; - let downloaded_pct = if stats.remaining_bytes == 0 { - 100f64 - } else { - (progress as f64 / total as f64) * 100f64 - }; - info!( - "Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}", - downloaded_pct, - SF::new(progress), - speed.download_mbps(), - SF::new(stats.fetched_bytes), - SF::new(stats.remaining_bytes), - SF::new(total), - SF::new(stats.uploaded_bytes), - peer_stats.live, - peer_stats.connecting, - peer_stats.queued, - peer_stats.seen, - ); - tokio::time::sleep(Duration::from_secs(1)).await; - } - } - }); - if let Some(mut dht_peer_rx) = dht_peer_rx { spawn("DHT peer adder", { let handle = handle.clone(); diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index 7153126..df48ff4 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -5,8 +5,10 @@ use clap::Clap; use librqbit::{ peer_connection::PeerConnectionOptions, session::{AddTorrentOptions, Session, SessionOptions}, - spawn_utils::BlockingSpawner, + spawn_utils::{spawn, BlockingSpawner}, }; +use log::info; +use size_format::SizeFormatterBinary as SF; #[derive(Debug, Clap)] enum LogLevel { @@ -158,15 +160,52 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()> force_tracker_interval: opts.force_tracker_interval.map(|d| d.0), ..Default::default() }; - if let Some(handle) = session + + let handle = match session .add_torrent(opts.torrent_path, Some(torrent_opts)) .await .context("error adding torrent to session")? { - handle - .wait_until_completed() - .await - .context("error waiting for torrent completion")?; - } + Some(handle) => handle, + None => return Ok(()), + }; + + spawn("Stats printer", { + let handle = handle.clone(); + async move { + loop { + let peer_stats = handle.torrent_state().peer_stats_snapshot(); + let stats = handle.torrent_state().stats_snapshot(); + let speed = handle.speed_estimator(); + let total = stats.total_bytes; + let progress = stats.total_bytes - stats.remaining_bytes; + let downloaded_pct = if stats.remaining_bytes == 0 { + 100f64 + } else { + (progress as f64 / total as f64) * 100f64 + }; + info!( + "Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}", + downloaded_pct, + SF::new(progress), + speed.download_mbps(), + SF::new(stats.fetched_bytes), + SF::new(stats.remaining_bytes), + SF::new(total), + SF::new(stats.uploaded_bytes), + peer_stats.live, + peer_stats.connecting, + peer_stats.queued, + peer_stats.seen, + ); + tokio::time::sleep(Duration::from_secs(1)).await; + } + } + }); + + handle + .wait_until_completed() + .await + .context("error waiting for torrent completion")?; Ok(()) }