use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio_util::sync::CancellationToken;
use crate::engine::bt_choke_manager::BtSeederStateChoke;
use crate::engine::bt_piece_downloader::FileBackedPieceProvider;
use crate::engine::bt_tracker_comm::TrackerAnnouncer;
use crate::engine::bt_upload_session::{
BtSeedingConfig, BtUploadConnection, BtUploadSession, PieceDataProvider,
};
use crate::engine::choking_algorithm::ChokingAlgorithm;
use crate::engine::peer_stats::PeerStats;
use super::{BtSeedManager, CHOKE_ROUND_INTERVAL_SECS, SeedExitCondition};
impl BtSeedManager {
pub fn new(
connections: Vec<aria2_protocol::bittorrent::peer::connection::PeerConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
) -> Self {
Self::build(
[0u8; 20],
connections
.into_iter()
.map(|connection| BtUploadConnection::Plain(Box::new(connection)))
.collect(),
piece_provider,
config,
exit_condition,
total_downloaded,
None,
CancellationToken::new(),
None,
None,
[0u8; 20],
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_with_info_hash(
info_hash: [u8; 20],
connections: Vec<aria2_protocol::bittorrent::peer::connection::PeerConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
) -> Self {
Self::build(
info_hash,
connections
.into_iter()
.map(|connection| BtUploadConnection::Plain(Box::new(connection)))
.collect(),
piece_provider,
config,
exit_condition,
total_downloaded,
None,
CancellationToken::new(),
None,
None,
[0u8; 20],
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_with_choking_algo(
connections: Vec<aria2_protocol::bittorrent::peer::connection::PeerConnection>,
piece_provider: Arc<FileBackedPieceProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
choking_algo: Option<ChokingAlgorithm>,
) -> Self {
Self::build(
[0u8; 20],
connections
.into_iter()
.map(|connection| BtUploadConnection::Plain(Box::new(connection)))
.collect(),
piece_provider,
config,
exit_condition,
total_downloaded,
choking_algo,
CancellationToken::new(),
None,
None,
[0u8; 20],
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_with_announcer(
info_hash: [u8; 20],
connections: Vec<aria2_protocol::bittorrent::peer::connection::PeerConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
choking_algo: Option<ChokingAlgorithm>,
announcer: Option<TrackerAnnouncer>,
peer_id: [u8; 20],
) -> Self {
Self::build(
info_hash,
connections
.into_iter()
.map(|connection| BtUploadConnection::Plain(Box::new(connection)))
.collect(),
piece_provider,
config,
exit_condition,
total_downloaded,
choking_algo,
CancellationToken::new(),
None,
announcer,
peer_id,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_with_cancel_token(
info_hash: [u8; 20],
connections: Vec<aria2_protocol::bittorrent::peer::connection::PeerConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
cancel_token: CancellationToken,
) -> Self {
Self::build(
info_hash,
connections
.into_iter()
.map(|connection| BtUploadConnection::Plain(Box::new(connection)))
.collect(),
piece_provider,
config,
exit_condition,
total_downloaded,
None,
cancel_token,
None,
None,
[0u8; 20],
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn new_with_transports(
info_hash: [u8; 20],
connections: Vec<BtUploadConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
choking_algo: Option<ChokingAlgorithm>,
announcer: Option<TrackerAnnouncer>,
peer_id: [u8; 20],
incoming_peers: Option<
tokio::sync::mpsc::Receiver<crate::engine::bt_peer_listener::IncomingPeer>,
>,
) -> Self {
Self::build(
info_hash,
connections,
piece_provider,
config,
exit_condition,
total_downloaded,
choking_algo,
CancellationToken::new(),
incoming_peers,
announcer,
peer_id,
)
}
#[allow(clippy::too_many_arguments)]
fn build(
info_hash: [u8; 20],
connections: Vec<BtUploadConnection>,
piece_provider: Arc<dyn PieceDataProvider>,
config: BtSeedingConfig,
exit_condition: SeedExitCondition,
total_downloaded: u64,
choking_algo: Option<ChokingAlgorithm>,
cancel_token: CancellationToken,
incoming_peers: Option<
tokio::sync::mpsc::Receiver<crate::engine::bt_peer_listener::IncomingPeer>,
>,
announcer: Option<TrackerAnnouncer>,
peer_id: [u8; 20],
) -> Self {
let upload_sessions: Vec<BtUploadSession> = connections
.into_iter()
.map(|conn| {
let mut session = BtUploadSession::new_with_connection(conn, &config);
session.configure_message_validator(
piece_provider.num_pieces(),
piece_provider.piece_length(),
);
session
})
.collect();
let peer_stats: Vec<PeerStats> = upload_sessions
.iter()
.map(|session| {
let addr = session
.endpoint()
.and_then(|(ip, port)| format!("{ip}:{port}").parse().ok())
.unwrap_or_else(|| "0.0.0.0:0".parse().expect("valid unspecified address"));
PeerStats::new([0u8; 20], addr)
})
.collect();
let seeder_choke = BtSeederStateChoke::with_slots(config.max_peers_to_unchoke);
Self {
info_hash,
upload_sessions,
peer_stats,
piece_provider: Some(piece_provider),
config,
exit_condition,
total_downloaded,
total_uploaded: 0,
seeding_start_time: Instant::now(),
is_active: true,
seeder_choke,
choking_algo,
cancel_token,
peer_storage: None,
halt_requested: false,
last_choke_time: Instant::now() - Duration::from_secs(CHOKE_ROUND_INTERVAL_SECS),
announcer,
peer_id,
incoming_peers,
upload_progress: None,
connection_state: None,
peer_snapshot_store: None,
}
}
pub fn with_peer_storage(
mut self,
peer_storage: std::sync::Arc<
std::sync::Mutex<crate::engine::bt_peer_storage::DefaultPeerStorage>,
>,
) -> Self {
self.peer_storage = Some(peer_storage);
self
}
}