2/n Add pause/start actions

This commit is contained in:
Igor Katson 2023-11-24 14:29:05 +00:00
parent 0c4844f534
commit b79a21179f
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 19 additions and 16 deletions

View file

@ -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<String> {
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<String> {
let mgr = self.mgr_handle(idx)?;
Ok(mgr.with_chunk_tracker(|chunks| format!("{:?}", chunks.get_have_pieces()))?)
}
}

View file

@ -201,8 +201,11 @@ impl Session {
self.dht.as_ref()
}
pub fn with_torrents<R>(&self, callback: impl Fn(&[ManagedTorrentHandle]) -> R) -> R {
callback(&self.locked.read().torrents)
pub fn with_torrents<R>(
&self,
callback: impl Fn(&mut dyn Iterator<Item = (TorrentId, &ManagedTorrentHandle)>) -> R,
) -> R {
callback(&mut self.locked.read().torrents.iter().enumerate())
}
pub async fn add_torrent(

View file

@ -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<R>(&self, f: impl FnOnce(&ChunkTracker) -> R) -> anyhow::Result<R> {
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<Arc<TorrentStateLive>> {
let g = self.locked.read();
match &g.state {