rqbit/crates/librqbit/src/http_api.rs

223 lines
6.1 KiB
Rust
Raw Normal View History

2021-07-08 00:09:00 +01:00
use std::net::SocketAddr;
2021-06-30 10:14:33 +01:00
use std::sync::Arc;
2021-07-08 00:09:00 +01:00
use parking_lot::RwLock;
2021-07-08 23:03:58 +01:00
use serde::Serialize;
2021-06-30 10:31:30 +01:00
use std::io::Write;
2021-07-08 23:03:58 +01:00
use std::time::{Duration, Instant};
2021-06-30 10:14:33 +01:00
use warp::Filter;
2021-07-08 00:09:00 +01:00
use crate::torrent_manager::TorrentManagerHandle;
2021-07-08 23:03:58 +01:00
use crate::torrent_state::StatsSnapshot;
// enum Response<T, B> {
// NotFound(usize),
// OkJson(T),
// Ok(B),
// }
// impl<T, B> warp::Reply for Response<T, B>
// where T: Serialize + Send,
// B: Into<warp::hyper::Body> + Send
// {
// fn into_response(self) -> warp::reply::Response
// {
// match self {
// Response::NotFound(idx) => {
// let mut response = warp::reply::Response::new(warp::hyper::Body::from(format!(
// "torrent {} not found",
// idx
// )));
// *response.status_mut() = warp::http::StatusCode::NOT_FOUND;
// response
// }
// Response::OkJson(body) => {
// match serde_json::to_vec_pretty(&body) {
// Ok(body) => {
// let mut response = warp::reply::Response::new(warp::hyper::Body::from(body));
// response.headers_mut().insert("content-type", warp::http::HeaderValue::from_static("application/json"));
// response
// }
// Err(e) => {
// todo!()
// }
// }
// },
// Response::Ok(body) => warp::reply::Response::new(body.into()),
// }
// }
// }
2021-07-08 00:09:00 +01:00
2021-07-08 23:03:58 +01:00
struct Inner {
startup_time: Instant,
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,
2021-07-08 00:09:00 +01:00
}
2021-07-08 23:03:58 +01:00
impl Inner {
fn new() -> Self {
Self {
startup_time: Instant::now(),
torrent_managers: RwLock::new(Vec::new()),
2021-07-08 00:09:00 +01:00
}
}
}
2021-07-08 23:03:58 +01:00
#[derive(Serialize)]
struct Speed {
mbps: f64,
human_readable: String,
}
impl Speed {
fn new(mbps: f64) -> Self {
Self {
mbps,
human_readable: format!("{:.2}Mbps", mbps),
}
}
}
impl From<f64> for Speed {
fn from(mbps: f64) -> Self {
Self::new(mbps)
}
}
#[derive(Serialize)]
struct TorrentListResponseItem {
id: usize,
info_hash: String,
}
#[derive(Serialize)]
struct TorrentListResponse {
torrents: Vec<TorrentListResponseItem>,
}
#[derive(Serialize)]
struct StatsResponse {
snapshot: StatsSnapshot,
average_piece_download_time: Option<Duration>,
download_speed: Speed,
all_time_download_speed: Speed,
time_remaining: Option<Duration>,
2021-07-08 00:09:00 +01:00
}
impl Inner {
fn mgr_handle(&self, idx: usize) -> Option<TorrentManagerHandle> {
self.torrent_managers.read().get(idx).cloned()
}
2021-07-08 23:03:58 +01:00
fn api_torrent_list(&self) -> TorrentListResponse {
TorrentListResponse {
torrents: self
.torrent_managers
.read()
.iter()
.enumerate()
.map(|(id, mgr)| TorrentListResponseItem {
id,
info_hash: hex::encode(mgr.torrent_state().info_hash()),
})
.collect(),
}
}
fn api_stats(&self, idx: usize) -> Option<StatsResponse> {
let mgr = self.mgr_handle(idx)?;
let snapshot = mgr.torrent_state().stats_snapshot();
let estimator = mgr.speed_estimator();
// Poor mans download speed computation
let elapsed = self.startup_time.elapsed();
let downloaded_bytes = snapshot.downloaded_and_checked_bytes;
let downloaded_mb = downloaded_bytes as f64 / 1024f64 / 1024f64;
Some(StatsResponse {
average_piece_download_time: snapshot.average_piece_download_time(),
snapshot,
all_time_download_speed: (downloaded_mb / elapsed.as_secs_f64()).into(),
download_speed: estimator.download_mbps().into(),
time_remaining: estimator.time_remaining(),
})
}
fn api_dump_haves(&self, idx: usize) -> Option<String> {
let mgr = self.mgr_handle(idx)?;
Some(format!(
"{:?}",
mgr.torrent_state().lock_read().chunks.get_have_pieces(),
))
}
2021-07-08 00:09:00 +01:00
}
2021-07-08 23:03:58 +01:00
#[derive(Clone)]
2021-07-08 00:09:00 +01:00
pub struct HttpApi {
inner: Arc<Inner>,
}
2021-07-08 23:03:58 +01:00
fn json_response<T: Serialize>(v: T) -> warp::reply::Response {
let body = serde_json::to_string_pretty(&v).unwrap();
let mut response = warp::reply::Response::new(body.into());
response.headers_mut().insert(
"content-type",
warp::http::HeaderValue::from_static("application/json"),
);
response
}
fn not_found_response(idx: usize) -> warp::reply::Response {
let mut response = warp::reply::Response::new(format!("torrent {} not found", idx).into());
*response.status_mut() = warp::http::StatusCode::NOT_FOUND;
response
}
fn json_or_404<T: Serialize>(idx: usize, v: Option<T>) -> warp::reply::Response {
match v {
Some(v) => json_response(v),
None => not_found_response(idx),
}
}
2021-07-08 00:09:00 +01:00
impl HttpApi {
pub fn new() -> Self {
2021-07-08 23:03:58 +01:00
Self {
inner: Arc::new(Inner::new()),
}
2021-07-08 00:09:00 +01:00
}
pub fn add_mgr(&self, handle: TorrentManagerHandle) -> usize {
let mut g = self.inner.torrent_managers.write();
let idx = g.len();
g.push(handle);
idx
}
// TODO: this is all for debugging, not even JSON.
// After using this for a bit, not a big fan of warp.
pub async fn make_http_api_and_run(self, addr: SocketAddr) -> anyhow::Result<()> {
let inner = self.inner;
let list = warp::path::end().map({
let inner = inner.clone();
2021-07-08 23:03:58 +01:00
move || json_response(inner.api_torrent_list())
2021-07-08 00:09:00 +01:00
});
let dump_haves = warp::path!(usize / "haves").map({
let inner = inner.clone();
2021-07-08 23:03:58 +01:00
move |idx| json_or_404(idx, inner.api_dump_haves(idx))
2021-07-08 00:09:00 +01:00
});
let dump_stats = warp::path!(usize / "stats").map({
let inner = inner.clone();
2021-07-08 23:03:58 +01:00
move |idx| json_or_404(idx, inner.api_stats(idx))
2021-07-08 00:09:00 +01:00
});
2021-06-30 10:31:30 +01:00
2021-07-08 00:09:00 +01:00
let router = list.or(dump_haves).or(dump_stats);
2021-06-30 10:31:30 +01:00
2021-07-08 00:09:00 +01:00
warp::serve(router).run(addr).await;
Ok(())
}
2021-06-30 10:14:33 +01:00
}