1/n Add pause/start actions

This commit is contained in:
Igor Katson 2023-11-24 14:19:39 +00:00
parent afbf2a76b9
commit 0c4844f534
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
4 changed files with 50 additions and 12 deletions

View file

@ -690,7 +690,8 @@ impl Dht {
});
Ok(Dht { state })
}
pub async fn get_peers(
pub fn get_peers(
&self,
info_hash: Id20,
) -> anyhow::Result<impl Stream<Item = SocketAddr> + Unpin> {

View file

@ -107,7 +107,7 @@ mod tests {
let info_hash = Id20::from_str("cf3ea75e2ebbd30e0da6e6e215e2226bf35f2e33").unwrap();
let dht = Dht::new().await.unwrap();
let peer_rx = dht.get_peers(info_hash).await.unwrap();
let peer_rx = dht.get_peers(info_hash).unwrap();
let peer_id = generate_peer_id();
match read_metainfo_from_peer_receiver(peer_id, info_hash, peer_rx, None).await {
ReadMetainfoResult::Found { info, .. } => dbg!(info),

View file

@ -2,7 +2,7 @@ use anyhow::Context;
use axum::body::Bytes;
use axum::extract::{Path, Query, State};
use axum::response::IntoResponse;
use axum::routing::get;
use axum::routing::{get, post};
use buffers::ByteString;
use dht::{Dht, DhtStats};
use http::StatusCode;
@ -116,6 +116,20 @@ impl HttpApi {
state.api_peer_stats(idx, filter).map(axum::Json)
}
async fn torrent_action_pause(
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_pause(idx)
}
async fn torrent_action_start(
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_start(idx)
}
#[allow(unused_mut)]
let mut app = Router::new()
.route("/", get(api_root))
@ -125,7 +139,9 @@ impl HttpApi {
.route("/torrents/:id", get(torrent_details))
.route("/torrents/:id/haves", get(torrent_haves))
.route("/torrents/:id/stats", get(torrent_stats))
.route("/torrents/:id/peer_stats", get(peer_stats));
.route("/torrents/:id/peer_stats", get(peer_stats))
.route("/torrents/:id/pause", post(torrent_action_pause))
.route("/torrents/:id/start", post(torrent_action_start));
#[cfg(feature = "webui")]
{
@ -362,14 +378,14 @@ impl ApiInternal {
TorrentListResponse { torrents: items }
}
fn api_torrent_details(&self, idx: usize) -> Result<TorrentDetailsResponse> {
fn api_torrent_details(&self, idx: TorrentId) -> Result<TorrentDetailsResponse> {
let handle = self.mgr_handle(idx)?;
let info_hash = handle.info().info_hash;
let only_files = handle.only_files();
make_torrent_details(&info_hash, &handle.info().info, only_files.as_deref())
}
fn api_peer_stats(&self, idx: usize, filter: PeerStatsFilter) -> Result<PeerStatsSnapshot> {
fn api_peer_stats(&self, idx: TorrentId, filter: PeerStatsFilter) -> Result<PeerStatsSnapshot> {
let handle = self.mgr_handle(idx)?;
Ok(handle
.live()
@ -377,6 +393,22 @@ impl ApiInternal {
.per_peer_stats_snapshot(filter))
}
fn api_torrent_action_pause(&self, idx: TorrentId) -> Result<()> {
let handle = self.mgr_handle(idx)?;
handle
.pause()
.context("error pausing torrent")
.with_error_status_code(StatusCode::BAD_REQUEST)
}
fn api_torrent_action_start(&self, idx: TorrentId) -> Result<()> {
let handle = self.mgr_handle(idx)?;
self.session
.unpause(&handle)
.context("error unpausing torrent")
.with_error_status_code(StatusCode::BAD_REQUEST)
}
pub async fn api_add_torrent(
&self,
add: AddTorrent<'_>,
@ -436,7 +468,7 @@ impl ApiInternal {
Ok(dht.with_routing_table(|r| r.clone()))
}
fn api_stats(&self, idx: usize) -> Result<StatsResponse> {
fn api_stats(&self, idx: TorrentId) -> Result<StatsResponse> {
let mgr = self.mgr_handle(idx)?;
let live = mgr.live().context("not live")?;
let snapshot = live.stats_snapshot();

View file

@ -224,8 +224,7 @@ impl Session {
.dht
.as_ref()
.context("magnet links without DHT are not supported")?
.get_peers(info_hash)
.await?;
.get_peers(info_hash)?;
let trackers = trackers
.into_iter()
@ -274,7 +273,7 @@ impl Session {
let dht_rx = match self.dht.as_ref() {
Some(dht) => {
debug!("reading peers for {:?} from DHT", torrent.info_hash);
Some(dht.get_peers(torrent.info_hash).await?)
Some(dht.get_peers(torrent.info_hash)?)
}
None => None,
};
@ -430,7 +429,13 @@ impl Session {
self.locked.read().torrents.get(id).cloned()
}
pub fn restart(&self, id: usize) -> anyhow::Result<()> {
todo!()
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
let peer_rx = self
.dht
.as_ref()
.map(|dht| dht.get_peers(handle.info_hash()))
.transpose()?;
handle.start(Default::default(), peer_rx);
return Ok(());
}
}