Fix unpause

This commit is contained in:
Igor Katson 2024-02-17 21:13:57 +00:00
parent 95769cca6a
commit 1582d16cc5
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 25 additions and 11 deletions

View file

@ -43,7 +43,7 @@ use crate::{
torrent_state::{ torrent_state::{
ManagedTorrentBuilder, ManagedTorrentHandle, ManagedTorrentState, TorrentStateLive, ManagedTorrentBuilder, ManagedTorrentHandle, ManagedTorrentState, TorrentStateLive,
}, },
tracker_comms::{TorrentStatsForTrackerDummy, TrackerComms}, tracker_comms::{self, TorrentStatsForTrackerDummy, TrackerComms},
type_aliases::PeerStream, type_aliases::PeerStream,
}; };
@ -768,7 +768,7 @@ impl Session {
self.tcp_listen_port self.tcp_listen_port
}; };
let (info_hash, info, peer_rx, initial_peers) = match add { let (info_hash, info, trackers, peer_rx, initial_peers) = match add {
AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => { AddTorrent::Url(magnet) if magnet.starts_with("magnet:") => {
let magnet = let magnet =
Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?; Magnet::parse(&magnet).context("provided path is not a valid magnet URL")?;
@ -785,7 +785,7 @@ impl Session {
let tracker_peer_rx = TrackerComms::start( let tracker_peer_rx = TrackerComms::start(
info_hash, info_hash,
self.peer_id, self.peer_id,
magnet.trackers, magnet.trackers.clone(),
Box::new(TorrentStatsForTrackerDummy {}), Box::new(TorrentStatsForTrackerDummy {}),
opts.force_tracker_interval, opts.force_tracker_interval,
self.cancellation_token().clone(), self.cancellation_token().clone(),
@ -816,6 +816,7 @@ impl Session {
( (
info_hash, info_hash,
info, info,
magnet.trackers,
if opts.paused || opts.list_only { if opts.paused || opts.list_only {
None None
} else { } else {
@ -856,7 +857,7 @@ impl Session {
Ok(url) => Some(url.to_owned()), Ok(url) => Some(url.to_owned()),
Err(_) => { Err(_) => {
warn!("cannot parse tracker url as utf-8, ignoring"); warn!("cannot parse tracker url as utf-8, ignoring");
return None; None
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -864,7 +865,7 @@ impl Session {
let tracker_peer_rx = TrackerComms::start( let tracker_peer_rx = TrackerComms::start(
torrent.info_hash, torrent.info_hash,
self.peer_id, self.peer_id,
trackers, trackers.clone(),
Box::new(TorrentStatsForTrackerDummy {}), Box::new(TorrentStatsForTrackerDummy {}),
opts.force_tracker_interval, opts.force_tracker_interval,
self.cancellation_token().clone(), self.cancellation_token().clone(),
@ -876,6 +877,7 @@ impl Session {
( (
torrent.info_hash, torrent.info_hash,
torrent.info, torrent.info,
trackers,
peer_rx, peer_rx,
opts.initial_peers opts.initial_peers
.clone() .clone()
@ -889,6 +891,7 @@ impl Session {
self.main_torrent_info( self.main_torrent_info(
info_hash, info_hash,
info, info,
trackers,
peer_rx, peer_rx,
initial_peers.into_iter().collect(), initial_peers.into_iter().collect(),
opts, opts,
@ -901,6 +904,7 @@ impl Session {
&self, &self,
info_hash: Id20, info_hash: Id20,
info: TorrentMetaV1Info<ByteString>, info: TorrentMetaV1Info<ByteString>,
trackers: Vec<String>,
peer_rx: Option<PeerStream>, peer_rx: Option<PeerStream>,
initial_peers: Vec<SocketAddr>, initial_peers: Vec<SocketAddr>,
opts: AddTorrentOptions, opts: AddTorrentOptions,
@ -988,6 +992,7 @@ impl Session {
.overwrite(opts.overwrite) .overwrite(opts.overwrite)
.spawner(self.spawner) .spawner(self.spawner)
.cancellation_token(self.cancellation_token.child_token()) .cancellation_token(self.cancellation_token.child_token())
.trackers(trackers)
.peer_id(self.peer_id); .peer_id(self.peer_id);
if let Some(only_files) = only_files { if let Some(only_files) = only_files {
@ -1074,13 +1079,22 @@ impl Session {
} }
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> { pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
let peer_rx = self let dht_rx = self
.dht .dht
.as_ref() .as_ref()
.map(|dht| dht.get_peers(handle.info_hash(), self.tcp_listen_port)) .map(|dht| dht.get_peers(handle.info_hash(), self.tcp_listen_port))
.transpose()?; .transpose()?;
todo!(); let trackers = handle.info().trackers.clone();
let peer_rx = None; let peer_rx = TrackerComms::start(
handle.info.info_hash,
handle.info.peer_id,
trackers.into_iter().collect(),
Box::new(tracker_comms::TorrentStatsForTrackerDummy {}),
None,
self.cancellation_token.clone(),
self.tcp_listen_port,
);
let peer_rx = merge_peer_rx(dht_rx, peer_rx);
handle.start(Default::default(), peer_rx, false)?; handle.start(Default::default(), peer_rx, false)?;
Ok(()) Ok(())
} }

View file

@ -84,7 +84,7 @@ pub struct ManagedTorrentInfo {
pub info_hash: Id20, pub info_hash: Id20,
pub out_dir: PathBuf, pub out_dir: PathBuf,
pub(crate) spawner: BlockingSpawner, pub(crate) spawner: BlockingSpawner,
pub trackers: HashSet<Url>, pub trackers: HashSet<String>,
pub peer_id: Id20, pub peer_id: Id20,
pub lengths: Lengths, pub lengths: Lengths,
pub span: tracing::Span, pub span: tracing::Span,
@ -422,7 +422,7 @@ pub struct ManagedTorrentBuilder {
peer_connect_timeout: Option<Duration>, peer_connect_timeout: Option<Duration>,
peer_read_write_timeout: Option<Duration>, peer_read_write_timeout: Option<Duration>,
only_files: Option<Vec<usize>>, only_files: Option<Vec<usize>>,
trackers: Vec<Url>, trackers: Vec<String>,
peer_id: Option<Id20>, peer_id: Option<Id20>,
overwrite: bool, overwrite: bool,
spawner: Option<BlockingSpawner>, spawner: Option<BlockingSpawner>,
@ -461,7 +461,7 @@ impl ManagedTorrentBuilder {
self self
} }
pub fn trackers(&mut self, trackers: Vec<Url>) -> &mut Self { pub fn trackers(&mut self, trackers: Vec<String>) -> &mut Self {
self.trackers = trackers; self.trackers = trackers;
self self
} }