1/n Add pause/start actions
This commit is contained in:
parent
afbf2a76b9
commit
0c4844f534
4 changed files with 50 additions and 12 deletions
|
|
@ -690,7 +690,8 @@ impl Dht {
|
||||||
});
|
});
|
||||||
Ok(Dht { state })
|
Ok(Dht { state })
|
||||||
}
|
}
|
||||||
pub async fn get_peers(
|
|
||||||
|
pub fn get_peers(
|
||||||
&self,
|
&self,
|
||||||
info_hash: Id20,
|
info_hash: Id20,
|
||||||
) -> anyhow::Result<impl Stream<Item = SocketAddr> + Unpin> {
|
) -> anyhow::Result<impl Stream<Item = SocketAddr> + Unpin> {
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ mod tests {
|
||||||
|
|
||||||
let info_hash = Id20::from_str("cf3ea75e2ebbd30e0da6e6e215e2226bf35f2e33").unwrap();
|
let info_hash = Id20::from_str("cf3ea75e2ebbd30e0da6e6e215e2226bf35f2e33").unwrap();
|
||||||
let dht = Dht::new().await.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();
|
let peer_id = generate_peer_id();
|
||||||
match read_metainfo_from_peer_receiver(peer_id, info_hash, peer_rx, None).await {
|
match read_metainfo_from_peer_receiver(peer_id, info_hash, peer_rx, None).await {
|
||||||
ReadMetainfoResult::Found { info, .. } => dbg!(info),
|
ReadMetainfoResult::Found { info, .. } => dbg!(info),
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use anyhow::Context;
|
||||||
use axum::body::Bytes;
|
use axum::body::Bytes;
|
||||||
use axum::extract::{Path, Query, State};
|
use axum::extract::{Path, Query, State};
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use axum::routing::get;
|
use axum::routing::{get, post};
|
||||||
use buffers::ByteString;
|
use buffers::ByteString;
|
||||||
use dht::{Dht, DhtStats};
|
use dht::{Dht, DhtStats};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
|
@ -116,6 +116,20 @@ impl HttpApi {
|
||||||
state.api_peer_stats(idx, filter).map(axum::Json)
|
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)]
|
#[allow(unused_mut)]
|
||||||
let mut app = Router::new()
|
let mut app = Router::new()
|
||||||
.route("/", get(api_root))
|
.route("/", get(api_root))
|
||||||
|
|
@ -125,7 +139,9 @@ impl HttpApi {
|
||||||
.route("/torrents/:id", get(torrent_details))
|
.route("/torrents/:id", get(torrent_details))
|
||||||
.route("/torrents/:id/haves", get(torrent_haves))
|
.route("/torrents/:id/haves", get(torrent_haves))
|
||||||
.route("/torrents/:id/stats", get(torrent_stats))
|
.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")]
|
#[cfg(feature = "webui")]
|
||||||
{
|
{
|
||||||
|
|
@ -362,14 +378,14 @@ impl ApiInternal {
|
||||||
TorrentListResponse { torrents: items }
|
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 handle = self.mgr_handle(idx)?;
|
||||||
let info_hash = handle.info().info_hash;
|
let info_hash = handle.info().info_hash;
|
||||||
let only_files = handle.only_files();
|
let only_files = handle.only_files();
|
||||||
make_torrent_details(&info_hash, &handle.info().info, only_files.as_deref())
|
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)?;
|
let handle = self.mgr_handle(idx)?;
|
||||||
Ok(handle
|
Ok(handle
|
||||||
.live()
|
.live()
|
||||||
|
|
@ -377,6 +393,22 @@ impl ApiInternal {
|
||||||
.per_peer_stats_snapshot(filter))
|
.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(
|
pub async fn api_add_torrent(
|
||||||
&self,
|
&self,
|
||||||
add: AddTorrent<'_>,
|
add: AddTorrent<'_>,
|
||||||
|
|
@ -436,7 +468,7 @@ impl ApiInternal {
|
||||||
Ok(dht.with_routing_table(|r| r.clone()))
|
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 mgr = self.mgr_handle(idx)?;
|
||||||
let live = mgr.live().context("not live")?;
|
let live = mgr.live().context("not live")?;
|
||||||
let snapshot = live.stats_snapshot();
|
let snapshot = live.stats_snapshot();
|
||||||
|
|
|
||||||
|
|
@ -224,8 +224,7 @@ impl Session {
|
||||||
.dht
|
.dht
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.context("magnet links without DHT are not supported")?
|
.context("magnet links without DHT are not supported")?
|
||||||
.get_peers(info_hash)
|
.get_peers(info_hash)?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
let trackers = trackers
|
let trackers = trackers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
@ -274,7 +273,7 @@ impl Session {
|
||||||
let dht_rx = match self.dht.as_ref() {
|
let dht_rx = match self.dht.as_ref() {
|
||||||
Some(dht) => {
|
Some(dht) => {
|
||||||
debug!("reading peers for {:?} from DHT", torrent.info_hash);
|
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,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
@ -430,7 +429,13 @@ impl Session {
|
||||||
self.locked.read().torrents.get(id).cloned()
|
self.locked.read().torrents.get(id).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restart(&self, id: usize) -> anyhow::Result<()> {
|
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
|
||||||
todo!()
|
let peer_rx = self
|
||||||
|
.dht
|
||||||
|
.as_ref()
|
||||||
|
.map(|dht| dht.get_peers(handle.info_hash()))
|
||||||
|
.transpose()?;
|
||||||
|
handle.start(Default::default(), peer_rx);
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue