use std::any::Any;
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use super::super::net_stat::NetStat;
use super::super::types::{ContextAttributeType, Signature};
use crate::download::file_entry::FileEntry;
pub struct DownloadContext {
pub(super) signature: Option<Signature>,
pub(super) owner_request_group_id: Option<u64>,
pub(super) attrs: HashMap<ContextAttributeType, Box<dyn Any + Send + Sync>>,
pub(super) file_entries: Vec<FileEntry>,
pub(super) piece_hashes: Vec<String>,
pub(super) net_stat: NetStat,
pub(super) download_stop_time: Option<std::time::Instant>,
pub(super) piece_hash_type: String,
pub(super) digest: String,
pub(super) hash_type: String,
pub(super) base_path: String,
pub(super) piece_length: u32,
pub(super) checksum_verified: AtomicBool,
pub(super) knows_total_length: bool,
pub(super) accept_metalink: bool,
pub(super) info_hash: String,
}
impl std::fmt::Debug for DownloadContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DownloadContext")
.field("owner_request_group_id", &self.owner_request_group_id)
.field("attrs_count", &self.attrs.len())
.field("file_entries", &self.file_entries.len())
.field("piece_hashes_count", &self.piece_hashes.len())
.field("net_stat", &self.net_stat)
.field("piece_hash_type", &self.piece_hash_type)
.field("base_path", &self.base_path)
.field("piece_length", &self.piece_length)
.field(
"checksum_verified",
&self
.checksum_verified
.load(std::sync::atomic::Ordering::Acquire),
)
.field("knows_total_length", &self.knows_total_length)
.field("accept_metalink", &self.accept_metalink)
.field("info_hash", &self.info_hash)
.finish()
}
}
pub(super) static EMPTY_STRING: &str = "";
impl DownloadContext {
pub fn new_default() -> Self {
Self {
signature: None,
owner_request_group_id: None,
attrs: HashMap::new(),
file_entries: Vec::new(),
piece_hashes: Vec::new(),
net_stat: NetStat::default(),
download_stop_time: None,
piece_hash_type: String::new(),
digest: String::new(),
hash_type: String::new(),
base_path: String::new(),
piece_length: 0,
checksum_verified: AtomicBool::new(false),
knows_total_length: true,
accept_metalink: true,
info_hash: String::new(),
}
}
pub fn new(piece_length: u32, total_length: u64, path: String) -> Self {
let file_entry = FileEntry::new(path, total_length, 0, Vec::new());
Self {
piece_length,
file_entries: vec![file_entry],
signature: None,
owner_request_group_id: None,
attrs: HashMap::new(),
piece_hashes: Vec::new(),
net_stat: NetStat::default(),
download_stop_time: None,
piece_hash_type: String::new(),
digest: String::new(),
hash_type: String::new(),
base_path: String::new(),
checksum_verified: AtomicBool::new(false),
knows_total_length: true,
accept_metalink: true,
info_hash: String::new(),
}
}
}
impl Default for DownloadContext {
fn default() -> Self {
Self::new_default()
}
}