use tracing::{debug, info, warn};
use crate::config::PiecePriorityRule;
use crate::constants;
use crate::download::file_entry::FileEntry;
pub const ENDGAME_THRESHOLD: u32 = constants::BT_ENDGAME_THRESHOLD as u32;
pub fn prioritized_piece_indices(
rules: &[PiecePriorityRule],
file_entries: &[FileEntry],
piece_length: u64,
) -> Result<Vec<u32>, String> {
if piece_length == 0 {
return Err("piece length must be greater than zero".to_string());
}
let mut indices = Vec::new();
for rule in rules {
for entry in file_entries {
let length = entry.length();
if length == 0 {
continue;
}
let end_offset = entry
.offset()
.checked_add(length)
.ok_or_else(|| "file offset exceeds u64::MAX".to_string())?;
let size = match rule {
PiecePriorityRule::Head { size } | PiecePriorityRule::Tail { size } => {
(*size).min(length)
}
};
if size == 0 {
continue;
}
let (first_piece, last_piece) = match rule {
PiecePriorityRule::Head { .. } => (
entry.offset() / piece_length,
(entry.offset() + size - 1) / piece_length,
),
PiecePriorityRule::Tail { .. } => (
(end_offset - size) / piece_length,
(end_offset - 1) / piece_length,
),
};
indices.extend(first_piece..=last_piece);
}
}
indices.sort_unstable();
indices.dedup();
indices
.into_iter()
.map(|index| {
u32::try_from(index)
.map_err(|_| format!("piece index {} exceeds the BitTorrent index range", index))
})
.collect()
}
pub fn allowed_piece_indices(
context: &crate::download::DownloadContext,
piece_length: u64,
num_pieces: u32,
) -> Option<Vec<u32>> {
let entries = context.get_file_entries();
if entries.len() <= 1 || entries.iter().all(|entry| entry.is_requested()) {
return None;
}
let mut allowed = vec![false; num_pieces as usize];
if piece_length == 0 || allowed.is_empty() {
return Some(Vec::new());
}
for entry in entries.iter().filter(|entry| entry.is_requested()) {
if entry.length() == 0 {
continue;
}
let start = usize::try_from(entry.offset() / piece_length).unwrap_or(allowed.len());
let end_offset = entry.offset().saturating_add(entry.length());
let end = end_offset
.saturating_sub(1)
.checked_div(piece_length)
.and_then(|index| usize::try_from(index).ok())
.unwrap_or(allowed.len().saturating_sub(1));
let start = start.min(allowed.len());
let end = end.min(allowed.len().saturating_sub(1));
if start <= end {
allowed[start..=end].fill(true);
}
}
Some(
allowed
.into_iter()
.enumerate()
.filter_map(|(index, allowed)| allowed.then_some(index as u32))
.collect(),
)
}
#[derive(Debug, Clone)]
pub struct PieceSelectorConfig {
pub endgame_threshold: u32,
pub prefer_rarest: bool,
pub strict_priority: bool,
}
impl Default for PieceSelectorConfig {
fn default() -> Self {
Self {
endgame_threshold: ENDGAME_THRESHOLD,
prefer_rarest: true,
strict_priority: false,
}
}
}
pub struct PieceSelectionResult {
pub piece_index: Option<usize>,
pub is_endgame: bool,
pub remaining_count: usize,
}
pub struct BtPieceSelector {
config: PieceSelectorConfig,
num_pieces: u32,
}
impl BtPieceSelector {
pub fn new(num_pieces: u32) -> Self {
Self {
config: PieceSelectorConfig::default(),
num_pieces,
}
}
pub fn with_config(num_pieces: u32, config: PieceSelectorConfig) -> Self {
Self { config, num_pieces }
}
pub fn select_next_piece(
&self,
piece_picker: &mut crate::engine::bittorrent::piece::PiecePicker,
remaining: usize,
) -> PieceSelectionResult {
let is_endgame = remaining > 0 && remaining <= self.config.endgame_threshold as usize;
if is_endgame && !piece_picker.endgame_candidates().is_empty() {
warn!("[BT] === ENDGAME MODE === ({} pieces remaining)", remaining);
}
let next_piece_idx = if is_endgame {
piece_picker.pick_next()
} else {
piece_picker.pick_next_without_endgame()
}
.map(|v| v as usize);
debug!(
"[BT] Selected piece: {:?} (endgame={}, remaining={})",
next_piece_idx, is_endgame, remaining
);
PieceSelectionResult {
piece_index: next_piece_idx,
is_endgame,
remaining_count: remaining,
}
}
pub fn select_next_piece_for_peer(
&self,
piece_picker: &mut crate::engine::bittorrent::piece::PiecePicker,
peer_conn: &crate::engine::bittorrent::peer::connection::BtPeerConn,
peer_bitfield: &[u8],
is_choked: bool,
remaining: usize,
) -> PieceSelectionResult {
if is_choked && !peer_conn.peer_allowed_fast_set().is_empty() {
debug!(
"[BT] Peer is choked but has {} allowed fast pieces, checking...",
peer_conn.peer_allowed_fast_set().len()
);
for &fast_idx in peer_conn.peer_allowed_fast_set() {
if piece_picker.is_allowed(fast_idx)
&& let Some(info) = piece_picker.get_piece_info(fast_idx)
&& !info.is_completed
&& !info.in_progress
&& Self::is_bitfield_set(peer_bitfield, fast_idx)
{
info!("[BT] Using AllowedFast piece {} despite choke", fast_idx);
return PieceSelectionResult {
piece_index: Some(fast_idx as usize),
is_endgame: false,
remaining_count: remaining,
};
}
}
debug!("[BT] No suitable AllowedFast piece found, falling back");
}
self.select_next_piece(piece_picker, remaining)
}
fn is_bitfield_set(bitfield: &[u8], piece_index: u32) -> bool {
let byte_idx = (piece_index as usize) / 8;
let bit_idx = 7 - ((piece_index as usize) % 8);
if byte_idx >= bitfield.len() {
return false;
}
(bitfield[byte_idx] & (1 << bit_idx)) != 0
}
pub fn calculate_piece_length(
&self,
piece_index: usize,
piece_length: u32,
total_size: u64,
) -> u32 {
if piece_index == self.num_pieces as usize - 1
&& !total_size.is_multiple_of(piece_length as u64)
{
(total_size % piece_length as u64) as u32
} else {
piece_length
}
}
pub fn calculate_num_blocks(piece_length: u32, block_size: u32) -> u32 {
piece_length.div_ceil(block_size)
}
pub fn initialize_frequencies(
&self,
piece_picker: &mut crate::engine::bittorrent::piece::PiecePicker,
peer_tracker: &crate::engine::bittorrent::piece::PeerBitfieldTracker,
) {
piece_picker.set_frequencies_from_peers(&peer_tracker.piece_frequencies());
info!(
"[BT] Piece selection initialized: {} pieces, {} peers tracked",
self.num_pieces,
peer_tracker.peer_count()
);
}
pub fn is_complete(piece_picker: &crate::engine::bittorrent::piece::PiecePicker) -> bool {
piece_picker.is_complete()
}
pub fn num_pieces(&self) -> u32 {
self.num_pieces
}
}
pub fn build_bitfield_from_completed<F>(num_pieces: u32, is_completed: F) -> Vec<u8>
where
F: Fn(u32) -> bool,
{
let bf_len = (num_pieces as usize).div_ceil(8);
let mut bitfield = vec![0u8; bf_len];
for i in 0..num_pieces {
if is_completed(i) {
let byte_idx = (i as usize) / 8;
let bit_idx = 7 - ((i as usize) % 8); if byte_idx < bitfield.len() {
bitfield[byte_idx] |= 1 << bit_idx;
}
}
}
bitfield
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::PiecePriorityRule;
use crate::download::file_entry::FileEntry;
#[test]
fn test_endgame_threshold_constant() {
assert_eq!(ENDGAME_THRESHOLD, 20);
}
#[test]
fn test_piece_selector_config_default() {
let config = PieceSelectorConfig::default();
assert_eq!(config.endgame_threshold, 20);
assert!(config.prefer_rarest);
assert!(!config.strict_priority);
}
#[test]
fn test_calculate_piece_length_normal() {
let selector = BtPieceSelector::new(100);
let length = selector.calculate_piece_length(50, 256 * 1024, 100 * 256 * 1024);
assert_eq!(length, 256 * 1024); }
#[test]
fn test_calculate_piece_length_last_piece_shorter() {
let selector = BtPieceSelector::new(10);
let total_size = 10 * 1024u64 - 100;
let length = selector.calculate_piece_length(9, 1024, total_size);
assert_eq!(length, 924); }
#[test]
fn test_calculate_num_blocks_exact() {
let blocks = BtPieceSelector::calculate_num_blocks(16384, 16384);
assert_eq!(blocks, 1); }
#[test]
fn test_calculate_num_blocks_partial() {
let blocks = BtPieceSelector::calculate_num_blocks(20000, 16384);
assert_eq!(blocks, 2); }
#[test]
fn test_build_bitfield_all_complete() {
let bf = build_bitfield_from_completed(16, |_| true);
assert_eq!(bf.len(), 2); assert_eq!(bf[0], 0xFF); assert_eq!(bf[1], 0xFF);
}
#[test]
fn test_build_bitfield_none_complete() {
let bf = build_bitfield_from_completed(8, |_| false);
assert_eq!(bf.len(), 1); assert_eq!(bf[0], 0x00); }
#[test]
fn test_build_bitfield_mixed() {
let bf = build_bitfield_from_completed(8, |i| i % 2 == 0);
assert_eq!(bf.len(), 1);
assert_eq!(bf[0], 0xAA); }
#[test]
fn test_piece_selection_result_default() {
let result = PieceSelectionResult {
piece_index: None,
is_endgame: false,
remaining_count: 100,
};
assert!(result.piece_index.is_none());
assert!(!result.is_endgame);
assert_eq!(result.remaining_count, 100);
}
#[test]
fn test_prioritized_piece_indices_match_head_and_tail_file_ranges() {
let entries = vec![FileEntry::new("a".into(), 4096, 0, Vec::new())];
assert_eq!(
prioritized_piece_indices(&[PiecePriorityRule::Head { size: 1025 }], &entries, 1024,)
.unwrap(),
vec![0, 1]
);
assert_eq!(
prioritized_piece_indices(&[PiecePriorityRule::Tail { size: 1025 }], &entries, 1024,)
.unwrap(),
vec![2, 3]
);
}
#[test]
fn test_prioritized_piece_indices_deduplicates_file_boundaries() {
let entries = vec![
FileEntry::new("a".into(), 1500, 0, Vec::new()),
FileEntry::new("b".into(), 1500, 1500, Vec::new()),
];
assert_eq!(
prioritized_piece_indices(
&[
PiecePriorityRule::Head { size: 1024 },
PiecePriorityRule::Tail { size: 1024 },
],
&entries,
1024,
)
.unwrap(),
vec![0, 1, 2]
);
}
#[test]
fn test_prioritized_piece_indices_rejects_zero_piece_length() {
let entries = vec![FileEntry::new("a".into(), 1, 0, Vec::new())];
assert!(prioritized_piece_indices(&[], &entries, 0).is_err());
}
#[test]
fn test_allowed_piece_indices_follow_requested_files() {
let mut context = crate::download::DownloadContext::new_default();
context.set_file_entries(vec![
FileEntry::new("a".into(), 4, 0, Vec::new()),
FileEntry::new("b".into(), 4, 4, Vec::new()),
FileEntry::new("c".into(), 4, 8, Vec::new()),
]);
context.set_file_filter(vec![2]);
assert_eq!(allowed_piece_indices(&context, 4, 3), Some(vec![1]));
}
#[test]
fn test_allowed_piece_indices_include_piece_spanning_file_boundary() {
let mut context = crate::download::DownloadContext::new_default();
context.set_file_entries(vec![
FileEntry::new("a".into(), 3, 0, Vec::new()),
FileEntry::new("b".into(), 3, 3, Vec::new()),
]);
context.set_file_filter(vec![2]);
assert_eq!(allowed_piece_indices(&context, 4, 2), Some(vec![0, 1]));
}
}