use std::sync::Arc;
use std::sync::atomic::Ordering;
use crate::util::rwlock_ext::RwLockRecover;
impl super::RequestGroup {
pub(crate) fn set_global_net_stat(
&mut self,
stats: Arc<crate::request::global_net_stat::GlobalNetStat>,
) {
*self.global_net_stat.recover_mut() = Some(Arc::clone(&stats));
if let Some(context) = self.download_context.recover().as_ref() {
context.set_global_net_stat(stats);
}
}
pub(crate) fn global_net_stat(
&self,
) -> Option<Arc<crate::request::global_net_stat::GlobalNetStat>> {
self.global_net_stat.recover().clone()
}
pub fn total_length(&self) -> u64 {
self.progress.total_length()
}
pub fn set_total_length(&self, length: u64) {
self.progress.set_total_length(length);
}
pub fn validate_total_length(
&self,
expected_total_length: u64,
actual_total_length: u64,
) -> crate::error::Result<()> {
if expected_total_length != 0 && expected_total_length != actual_total_length {
return Err(crate::error::Aria2Error::FtpProtocol(format!(
"FTP remote size mismatch: expected {}, got {}",
expected_total_length, actual_total_length
)));
}
Ok(())
}
pub fn completed_length(&self) -> u64 {
self.progress.completed_length()
}
pub fn upload_length(&self) -> u64 {
self.progress.upload_length()
}
pub fn update_completed_length(&self, length: u64) {
self.progress.set_completed_length(length);
}
pub fn update_progress(&self, completed_length: u64) {
self.progress.set_completed_length(completed_length);
}
pub(crate) fn record_network_activity(&self) {
self.progress.record_network_activity();
}
pub(crate) fn last_network_activity(&self) -> std::time::Instant {
self.progress.last_network_activity()
}
pub fn progress(&self) -> f64 {
let total = self.progress.total_length();
let completed = self.progress.completed_length();
if total == 0 {
0.0
} else {
(completed as f64 / total as f64) * 100.0
}
}
pub fn download_speed(&self) -> u64 {
self.progress.download_speed()
}
pub fn upload_speed(&self) -> u64 {
self.progress.upload_speed()
}
pub fn update_speed(&self, dl_speed: u64, ul_speed: u64) {
self.progress.set_download_speed(dl_speed);
self.progress.set_upload_speed(ul_speed);
}
pub fn add_segment(&mut self, segment: crate::segment::Segment) {
let mut segments = self.segments.recover_mut();
segments.push(segment);
tracing::debug!("Adding segment, current segments: {}", segments.len());
}
pub fn segments(&self) -> Vec<crate::segment::Segment> {
self.segments.recover().clone()
}
pub fn elapsed_time(&self) -> Option<std::time::Duration> {
let start = *self.start_time.recover();
let end = *self.end_time.recover();
start.map(|t| {
end.map(|end| end.duration_since(t))
.unwrap_or_else(|| t.elapsed())
})
}
pub fn eta(&self) -> Option<std::time::Duration> {
let speed = self.progress.download_speed();
let total = self.progress.total_length();
let completed = self.progress.completed_length();
let remaining = total.saturating_sub(completed);
if speed == 0 || remaining == 0 {
None
} else {
Some(std::time::Duration::from_secs(remaining / speed))
}
}
pub fn set_completed_length(&self, val: u64) {
self.progress.set_completed_length(val);
}
pub fn get_completed_length(&self) -> u64 {
self.progress.completed_length()
}
pub fn set_total_length_atomic(&self, val: u64) {
self.progress.set_total_length(val);
}
pub fn get_total_length_atomic(&self) -> u64 {
self.progress.total_length()
}
pub fn set_uploaded_length(&self, val: u64) {
self.progress.set_upload_length(val);
}
pub fn get_uploaded_length(&self) -> u64 {
self.progress.upload_length()
}
pub fn set_download_speed_cached(&self, val: u64) {
self.progress.set_download_speed(val);
}
pub fn get_download_speed_cached(&self) -> u64 {
self.progress.download_speed()
}
pub fn set_upload_speed_cached(&self, val: u64) {
self.progress.set_upload_speed(val);
}
pub fn get_upload_speed_cached(&self) -> u64 {
self.progress.upload_speed()
}
pub fn set_resume_offset(&self, offset: u64) {
self.progress.set_completed_length(offset);
}
pub fn set_bt_bitfield(&self, bf: Option<Vec<u8>>) {
*self.bt_bitfield.recover_mut() = bf;
}
pub fn get_bt_bitfield(&self) -> Option<Vec<u8>> {
self.bt_bitfield.recover().clone()
}
pub fn set_bt_peer_snapshots(&self, snapshots: Vec<super::BtPeerSnapshot>) {
*self.bt_peer_snapshots.recover_mut() = snapshots;
}
pub fn bt_peer_snapshots(&self) -> Vec<super::BtPeerSnapshot> {
self.bt_peer_snapshots.recover().clone()
}
pub fn clear_bt_peer_snapshots(&self) {
self.bt_peer_snapshots.recover_mut().clear();
self.connection_state.set_bt(0);
}
#[cfg(feature = "bittorrent")]
pub(crate) fn bt_peer_snapshot_store(
&self,
) -> std::sync::Arc<std::sync::RwLock<Vec<super::BtPeerSnapshot>>> {
std::sync::Arc::clone(&self.bt_peer_snapshots)
}
pub fn set_bt_metadata(&self, num_pieces: u32, piece_length: u32, info_hash_hex: String) {
self.bt_num_pieces.store(num_pieces, Ordering::Relaxed);
self.bt_piece_length.store(piece_length, Ordering::Relaxed);
*self.bt_info_hash_hex.recover_mut() = Some(info_hash_hex);
}
pub fn get_bt_num_pieces(&self) -> u32 {
self.bt_num_pieces.load(Ordering::Relaxed)
}
pub fn get_bt_piece_length(&self) -> u32 {
self.bt_piece_length.load(Ordering::Relaxed)
}
pub fn get_bt_info_hash_hex(&self) -> Option<String> {
self.bt_info_hash_hex.recover().clone()
}
}