From b79a21179f9681b16b7286d2bbf5ba0f0c29bf90 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Fri, 24 Nov 2023 14:29:05 +0000 Subject: [PATCH] 2/n Add pause/start actions --- crates/librqbit/src/http_api.rs | 18 ++++-------------- crates/librqbit/src/session.rs | 7 +++++-- crates/librqbit/src/torrent_state/mod.rs | 10 ++++++++++ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 0f56493..179e2c6 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -4,12 +4,11 @@ use axum::extract::{Path, Query, State}; use axum::response::IntoResponse; use axum::routing::{get, post}; use buffers::ByteString; -use dht::{Dht, DhtStats}; +use dht::DhtStats; use http::StatusCode; use itertools::Itertools; use librqbit_core::id20::Id20; use librqbit_core::torrent_metainfo::TorrentMetaV1Info; -use parking_lot::RwLock; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use std::sync::Arc; @@ -367,8 +366,6 @@ impl ApiInternal { fn api_torrent_list(&self) -> TorrentListResponse { let items = self.session.with_torrents(|torrents| { torrents - .iter() - .enumerate() .map(|(id, mgr)| TorrentListResponseItem { id, info_hash: mgr.info().info_hash.as_string(), @@ -488,16 +485,9 @@ impl ApiInternal { }) } - fn api_dump_haves(&self, _idx: usize) -> Result { - Err(anyhow::anyhow!("not implemented").into()) - // let mgr = self.mgr_handle(idx)?; - // Ok(format!( - // "{:?}", - // mgr.live().conetext() - // .lock_read("api_dump_haves") - // .chunks - // .get_have_pieces(), - // )) + fn api_dump_haves(&self, idx: usize) -> Result { + let mgr = self.mgr_handle(idx)?; + Ok(mgr.with_chunk_tracker(|chunks| format!("{:?}", chunks.get_have_pieces()))?) } } diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index fe7c0ec..2809973 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -201,8 +201,11 @@ impl Session { self.dht.as_ref() } - pub fn with_torrents(&self, callback: impl Fn(&[ManagedTorrentHandle]) -> R) -> R { - callback(&self.locked.read().torrents) + pub fn with_torrents( + &self, + callback: impl Fn(&mut dyn Iterator) -> R, + ) -> R { + callback(&mut self.locked.read().torrents.iter().enumerate()) } pub async fn add_torrent( diff --git a/crates/librqbit/src/torrent_state/mod.rs b/crates/librqbit/src/torrent_state/mod.rs index 9de0d9a..1451da4 100644 --- a/crates/librqbit/src/torrent_state/mod.rs +++ b/crates/librqbit/src/torrent_state/mod.rs @@ -23,6 +23,7 @@ use tokio_stream::StreamExt; use tracing::trace_span; use url::Url; +use crate::chunk_tracker::ChunkTracker; use crate::spawn_utils::spawn; use crate::spawn_utils::BlockingSpawner; @@ -98,6 +99,15 @@ impl ManagedTorrent { f(&self.locked.read().state) } + pub fn with_chunk_tracker(&self, f: impl FnOnce(&ChunkTracker) -> R) -> anyhow::Result { + let g = self.locked.read(); + match &g.state { + ManagedTorrentState::Paused(p) => Ok(f(&p.chunk_tracker)), + ManagedTorrentState::Live(l) => Ok(f(&l.lock_read("chunk_tracker").chunks)), + _ => bail!("no chunk tracker, torrent neither paused nor live"), + } + } + pub fn live(&self) -> Option> { let g = self.locked.read(); match &g.state {