Add http api for debugging

This commit is contained in:
Igor Katson 2021-06-30 10:14:33 +01:00
parent 1cb7a7bbc6
commit a3e84e4a99
6 changed files with 359 additions and 15 deletions

View file

@ -0,0 +1,13 @@
use std::sync::Arc;
use warp::Filter;
use crate::torrent_state::TorrentState;
// This is just a stub for debugging, nothing useful here.
pub async fn make_and_run_http_api(state: Arc<TorrentState>) -> anyhow::Result<()> {
let dump_haves = warp::path("haves")
.map(move || format!("{:?}", state.locked.read().chunks.get_have_pieces()));
warp::serve(dump_haves).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}

View file

@ -3,6 +3,7 @@ pub mod chunk_tracker;
pub mod clone_to_owned;
pub mod constants;
pub mod file_ops;
pub mod http_api;
pub mod lengths;
pub mod peer_binary_protocol;
pub mod peer_connection;

View file

@ -19,6 +19,7 @@ use size_format::SizeFormatterBinary as SF;
use crate::{
chunk_tracker::ChunkTracker,
file_ops::FileOps,
http_api::make_and_run_http_api,
lengths::Lengths,
spawn_utils::spawn,
torrent_metainfo::TorrentMetaV1Owned,
@ -189,6 +190,7 @@ impl TorrentManager {
spawn("tracker monitor", mgr.clone().task_tracker_monitor());
spawn("stats printer", mgr.clone().stats_printer());
spawn("http api", make_and_run_http_api(mgr.state.clone()));
Ok(mgr.into_handle())
}
@ -265,7 +267,7 @@ impl TorrentManager {
let response = crate::serde_bencode::from_bytes::<CompactTrackerResponse>(&bytes)?;
for peer in response.peers.iter_sockaddrs() {
self.state.add_peer(peer);
self.state.add_peer_if_not_seen(peer);
}
Ok(response.interval)
}

View file

@ -378,11 +378,11 @@ impl TorrentState {
);
}
pub fn add_peer(self: &Arc<Self>, addr: SocketAddr) {
pub fn add_peer_if_not_seen(self: &Arc<Self>, addr: SocketAddr) -> bool {
let (out_tx, out_rx) = channel::<WriterRequest>(1);
let handle = match self.locked.write().peers.add_if_not_seen(addr, out_tx) {
Some(handle) => handle,
None => return,
None => return false,
};
let peer_connection = PeerConnection::new(self.clone());
@ -393,5 +393,6 @@ impl TorrentState {
peer_connection.into_state().drop_peer(handle);
Ok::<_, anyhow::Error>(())
});
true
}
}