This commit is contained in:
Igor Katson 2021-07-03 15:52:39 +01:00
parent 2061fe56bb
commit 487ff50bd7
5 changed files with 28 additions and 16 deletions

View file

@ -1,7 +1,7 @@
use std::net::SocketAddr;
use futures::{stream::FuturesUnordered, StreamExt};
use log::warn;
use log::debug;
use tokio::sync::mpsc::UnboundedReceiver;
use crate::{buffers::ByteString, peer_info_reader, torrent_metainfo::TorrentMetaV1Info};
@ -50,7 +50,7 @@ pub async fn read_metainfo_from_peer_receiver(
match done {
Some(Ok(info)) => return ReadMetainfoResult::Found { info, seen, rx: addrs },
Some(Err(e)) => {
warn!("error in peer_info_reader::read_metainfo_from_peer: {}", e);
debug!("error in peer_info_reader::read_metainfo_from_peer: {}", e);
},
None => unreachable!()
}

View file

@ -27,12 +27,10 @@ impl Magnet {
}
}
match info_hash {
Some(info_hash) => {
return Ok(Magnet {
info_hash,
trackers,
})
}
Some(info_hash) => Ok(Magnet {
info_hash,
trackers,
}),
None => {
anyhow::bail!("did not find infohash")
}

View file

@ -37,6 +37,7 @@ pub struct TorrentManagerBuilder {
overwrite: bool,
output_folder: PathBuf,
only_files: Option<Vec<usize>>,
peer_id: Option<[u8; 20]>,
force_tracker_interval: Option<Duration>,
spawner: Option<BlockingSpawner>,
}
@ -53,6 +54,7 @@ impl TorrentManagerBuilder {
overwrite: false,
output_folder: output_folder.as_ref().into(),
only_files: None,
peer_id: None,
force_tracker_interval: None,
spawner: None,
}
@ -78,6 +80,11 @@ impl TorrentManagerBuilder {
self
}
pub fn peer_id(&mut self, peer_id: [u8; 20]) -> &mut Self {
self.peer_id = Some(peer_id);
self
}
pub fn start_manager(self) -> anyhow::Result<TorrentManagerHandle> {
TorrentManager::start(
self.info,
@ -86,6 +93,7 @@ impl TorrentManagerBuilder {
self.overwrite,
self.only_files,
self.force_tracker_interval,
self.peer_id,
self.spawner.unwrap_or_else(|| BlockingSpawner::new(true)),
)
}
@ -123,6 +131,7 @@ impl TorrentManagerHandle {
struct TorrentManager {
state: Arc<TorrentState>,
#[allow(dead_code)]
speed_estimator: Arc<SpeedEstimator>,
trackers: Mutex<HashSet<Url>>,
force_tracker_interval: Option<Duration>,
@ -136,13 +145,15 @@ fn make_lengths<ByteBuf: Clone + Deref<Target = [u8]>>(
}
impl TorrentManager {
pub fn start<P: AsRef<Path>>(
#[allow(clippy::too_many_arguments)]
fn start<P: AsRef<Path>>(
info: TorrentMetaV1Info<ByteString>,
info_hash: [u8; 20],
out: P,
overwrite: bool,
only_files: Option<Vec<usize>>,
force_tracker_interval: Option<Duration>,
peer_id: Option<[u8; 20]>,
spawner: BlockingSpawner,
) -> anyhow::Result<TorrentManagerHandle> {
let files = {
@ -180,7 +191,7 @@ impl TorrentManager {
files
};
let peer_id = generate_peer_id();
let peer_id = peer_id.unwrap_or_else(generate_peer_id);
let lengths = make_lengths(&info).context("unable to compute Lengths from torrent")?;
debug!("computed lengths: {:?}", &lengths);

View file

@ -27,7 +27,7 @@ use crate::{
peer_connection::{PeerConnection, PeerConnectionHandler, WriterRequest},
peer_state::{InflightRequest, LivePeerState, PeerState},
spawn_utils::{spawn, BlockingSpawner},
torrent_metainfo::{TorrentMetaV1Info, TorrentMetaV1Owned},
torrent_metainfo::TorrentMetaV1Info,
type_aliases::{PeerHandle, Sha1, BF},
};