mod choking_interest;
mod constants_and_types;
mod dispatch_message;
mod extension;
mod keepalive_flooding;
mod peer_id;
mod piece_exchange;
mod state_machine;
use std::time::{Duration, Instant};
use crate::engine::bt_peer_connection::BtPeerConn;
use crate::segment::piece::Piece;
use super::piece_provider::PieceProvider;
#[allow(dead_code)]
pub(crate) fn instant_past(secs: u64) -> Instant {
Instant::now()
.checked_sub(Duration::from_secs(secs))
.unwrap_or(Instant::now())
}
#[allow(dead_code)]
pub(crate) fn make_test_conn() -> BtPeerConn {
BtPeerConn::new_stub(&[0u8; 20])
}
#[allow(dead_code)]
pub(crate) struct MockPieceProvider {
pub has_missing: bool,
pub has_missing_unused: bool,
pub is_end_game: bool,
pub entered_end_game: bool,
pub missing_pieces: Vec<Piece>,
pub fast_pieces: Vec<Piece>,
}
#[allow(dead_code)]
impl MockPieceProvider {
pub fn new() -> Self {
Self {
has_missing: true,
has_missing_unused: true,
is_end_game: false,
entered_end_game: false,
missing_pieces: Vec::new(),
fast_pieces: Vec::new(),
}
}
}
impl PieceProvider for MockPieceProvider {
fn has_missing_piece(&self, _peer: &BtPeerConn) -> bool {
self.has_missing
}
fn get_missing_pieces(
&mut self,
count: usize,
_peer: &BtPeerConn,
_target_piece_indexes: &[u32],
_cuid: u64,
) -> Vec<Piece> {
self.missing_pieces
.drain(..count.min(self.missing_pieces.len()))
.collect()
}
fn get_missing_fast_pieces(
&mut self,
count: usize,
_peer: &BtPeerConn,
_target_piece_indexes: &[u32],
_cuid: u64,
) -> Vec<Piece> {
self.fast_pieces
.drain(..count.min(self.fast_pieces.len()))
.collect()
}
fn is_end_game(&self) -> bool {
self.is_end_game
}
fn has_missing_unused_piece(&self) -> bool {
self.has_missing_unused
}
fn enter_end_game(&mut self) {
self.entered_end_game = true;
}
fn update_piece_stats(&mut self, _new_bitfield: &[u8], _old_bitfield: &[u8]) {}
fn update_piece_stat(&mut self, _index: usize, _old: bool, _new: bool) {}
fn get_advertised_piece_indexes_ext(
&self,
_my_cuid: u64,
_last_have_index: u64,
) -> (Vec<usize>, u64) {
(Vec::new(), 0)
}
fn get_bitfield_length_ext(&self) -> usize {
0
}
fn get_bitfield_ext(&self) -> Vec<u8> {
Vec::new()
}
fn all_download_finished_ext(&self) -> bool {
false
}
fn get_completed_length_ext(&self) -> u64 {
0
}
}