use std::sync::Arc;
use super::{PieceHashValidator, ValidatorKind};
pub struct BtCheckIntegrity {
validator: ValidatorKind,
download_context: Option<Arc<crate::download::DownloadContext>>,
piece_storage: Option<Arc<dyn crate::segment::piece_storage::PieceStorage>>,
}
impl std::fmt::Debug for BtCheckIntegrity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BtCheckIntegrity")
.field("validator", &self.validator)
.field("download_context", &self.download_context.is_some())
.field("piece_storage", &self.piece_storage.is_some())
.finish()
}
}
impl BtCheckIntegrity {
pub fn new(
download_context: Arc<crate::download::DownloadContext>,
piece_storage: Arc<dyn crate::segment::piece_storage::PieceStorage>,
) -> Self {
Self {
validator: ValidatorKind::None,
download_context: Some(download_context),
piece_storage: Some(piece_storage),
}
}
pub fn is_validation_ready(&self) -> bool {
if let Some(ctx) = &self.download_context {
!ctx.get_piece_hashes().is_empty()
} else {
true
}
}
pub fn init_validator(&mut self) {
tracing::trace!("BtCheckIntegrity initializing validator");
if let (Some(ctx), Some(ps)) = (&self.download_context, &self.piece_storage) {
let total_pieces = ctx.get_piece_hashes().len();
if total_pieces == 0 {
tracing::trace!("No piece hashes available — skipping validator creation");
return;
}
let total_length = ctx.get_total_length();
let piece_length = ctx.get_piece_length() as u64;
let mut validator = PieceHashValidator::new(
Arc::clone(ctx),
Arc::clone(ps),
total_pieces,
total_length,
piece_length,
);
validator.init();
self.validator = ValidatorKind::PieceHash(validator);
}
}
pub fn validate_chunk(&mut self) {
self.validator.validate_chunk();
}
pub fn is_finished(&self) -> bool {
self.validator.is_finished()
}
pub fn total_length(&self) -> u64 {
self.validator.total_length()
}
pub fn current_length(&self) -> u64 {
self.validator.current_offset()
}
pub fn on_download_finished(&self) {
tracing::trace!("BtCheckIntegrity::on_download_finished (no-op)");
}
pub fn on_download_incomplete(&self) {
tracing::trace!("BtCheckIntegrity::on_download_incomplete");
}
pub fn cut_trailing_garbage(&self) {
tracing::trace!("BtCheckIntegrity::cut_trailing_garbage");
}
pub fn should_report_incomplete_as_error(&self) -> bool {
false
}
pub fn validator(&self) -> &ValidatorKind {
&self.validator
}
pub fn validator_mut(&mut self) -> &mut ValidatorKind {
&mut self.validator
}
}