1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! Progress tracking, speed monitoring, and ETA calculation.
//!
//! These methods provide progress queries and speed updates for the
//! download, using the lock-free `AtomicProgress` counters where
//! possible to avoid contention on the `RwLock`.
use std::sync::Arc;
use std::sync::atomic::Ordering;
use crate::util::rwlock_ext::RwLockRecover;
impl super::RequestGroup {
/// Attach the manager-owned session counters to this group.
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);
}
}
/// Return the manager-owned session counters, if this group is registered.
pub(crate) fn global_net_stat(
&self,
) -> Option<Arc<crate::request::global_net_stat::GlobalNetStat>> {
self.global_net_stat.recover().clone()
}
// ── Progress Queries ────────────────────────────────────────────────
/// Total file length in bytes.
pub fn total_length(&self) -> u64 {
self.progress.total_length()
}
/// Set the total file length.
pub fn set_total_length(&self, length: u64) {
self.progress.set_total_length(length);
}
/// Validate a remote protocol-reported length against an already known
/// download length, matching aria2's `validateTotalLength`.
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(())
}
/// Completed bytes so far.
pub fn completed_length(&self) -> u64 {
self.progress.completed_length()
}
/// Total uploaded bytes for BT downloads. Non-BT downloads return 0.
/// Mirrors C++ `RequestGroup::getUploadLength()`.
pub fn upload_length(&self) -> u64 {
self.progress.upload_length()
}
/// Update the completed length counter.
pub fn update_completed_length(&self, length: u64) {
self.progress.set_completed_length(length);
}
/// Update progress from the download engine.
pub fn update_progress(&self, completed_length: u64) {
self.progress.set_completed_length(completed_length);
}
/// Download progress as a percentage (0.0 - 100.0).
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
}
}
/// Current download speed in bytes/sec.
pub fn download_speed(&self) -> u64 {
self.progress.download_speed()
}
/// Current upload speed in bytes/sec.
pub fn upload_speed(&self) -> u64 {
self.progress.upload_speed()
}
/// Update both download and upload speed counters.
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);
}
/// Add a segment to the segment list.
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());
}
/// Return a snapshot of the current segments.
pub fn segments(&self) -> Vec<crate::segment::Segment> {
self.segments.recover().clone()
}
/// Time elapsed since download start.
pub fn elapsed_time(&self) -> Option<std::time::Duration> {
let start = *self.start_time.recover();
start.map(|t| t.elapsed())
}
/// Estimated time to completion based on current download speed.
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))
}
}
// ── Atomic Progress Accessors (for session persistence) ─────────────
// These use AtomicU64 for lock-free reads, suitable for frequent polling.
/// Set completed length using atomic store (lock-free).
pub fn set_completed_length(&self, val: u64) {
self.progress.set_completed_length(val);
}
/// Get completed length using atomic load (lock-free).
pub fn get_completed_length(&self) -> u64 {
self.progress.completed_length()
}
/// Set total length using atomic store (lock-free).
pub fn set_total_length_atomic(&self, val: u64) {
self.progress.set_total_length(val);
}
/// Get total length using atomic load (lock-free).
pub fn get_total_length_atomic(&self) -> u64 {
self.progress.total_length()
}
/// Set uploaded length using atomic store (lock-free).
pub fn set_uploaded_length(&self, val: u64) {
self.progress.set_upload_length(val);
}
/// Get uploaded length using atomic load (lock-free).
pub fn get_uploaded_length(&self) -> u64 {
self.progress.upload_length()
}
/// Set download speed cache using atomic store (lock-free).
pub fn set_download_speed_cached(&self, val: u64) {
self.progress.set_download_speed(val);
}
/// Get download speed from cache using atomic load (lock-free).
pub fn get_download_speed_cached(&self) -> u64 {
self.progress.download_speed()
}
/// Set upload speed cache using atomic store (lock-free).
pub fn set_upload_speed_cached(&self, val: u64) {
self.progress.set_upload_speed(val);
}
/// Get upload speed from cache using atomic load (lock-free).
pub fn get_upload_speed_cached(&self) -> u64 {
self.progress.upload_speed()
}
/// Set resume offset for HTTP/FTP range request resumption.
pub fn set_resume_offset(&self, offset: u64) {
self.progress.set_completed_length(offset);
}
// ── BT Bitfield ─────────────────────────────────────────────────────
/// Set BT bitfield (sync, uses std::sync::RwLock).
pub fn set_bt_bitfield(&self, bf: Option<Vec<u8>>) {
*self.bt_bitfield.recover_mut() = bf;
}
/// Get BT bitfield (sync, uses std::sync::RwLock).
pub fn get_bt_bitfield(&self) -> Option<Vec<u8>> {
self.bt_bitfield.recover().clone()
}
// ── BT Peer Snapshots ──────────────────────────────────────────────
/// Replace the current active BitTorrent peer snapshots.
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();
}
// ── BT Metadata ─────────────────────────────────────────────────────
/// Set BT metadata fields (num_pieces, piece_length, info_hash_hex).
/// Called by BtDownloadCommand after parsing TorrentMeta.
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);
}
/// Get number of pieces (lock-free atomic read).
pub fn get_bt_num_pieces(&self) -> u32 {
self.bt_num_pieces.load(Ordering::Relaxed)
}
/// Get piece length (lock-free atomic read).
pub fn get_bt_piece_length(&self) -> u32 {
self.bt_piece_length.load(Ordering::Relaxed)
}
/// Get info hash hex string (blocking read for non-async contexts).
pub fn get_bt_info_hash_hex(&self) -> Option<String> {
self.bt_info_hash_hex.recover().clone()
}
}