63 lines
1.8 KiB
Java
63 lines
1.8 KiB
Java
import java.util.List;
|
|
|
|
public class ProgressMonitor implements Runnable {
|
|
|
|
private final String fileName;
|
|
private final int totalSizeMB;
|
|
private final List<ChunkStatus> chunks;
|
|
private final long monitorDelayMs;
|
|
|
|
public ProgressMonitor(DownloadConfig config, List<ChunkStatus> chunks) {
|
|
this.fileName = config.getFileName();
|
|
this.totalSizeMB = config.getTotalSizeMB();
|
|
this.chunks = chunks;
|
|
this.monitorDelayMs = 500;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
System.out.println("[Monitor] Started monitoring progress...");
|
|
|
|
while (true) {
|
|
double totalDownloadedMB = 0.0;
|
|
int completedChunks = 0;
|
|
|
|
for (ChunkStatus chunk : chunks) {
|
|
totalDownloadedMB += chunk.getDownloadedMB();
|
|
|
|
if (chunk.isCompleted()) {
|
|
completedChunks++;
|
|
}
|
|
}
|
|
|
|
double percent = 0.0;
|
|
if (totalSizeMB > 0) {
|
|
percent = (totalDownloadedMB * 100.0) / totalSizeMB;
|
|
}
|
|
|
|
System.out.printf(
|
|
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n",
|
|
fileName,
|
|
totalDownloadedMB,
|
|
(double) totalSizeMB,
|
|
percent,
|
|
completedChunks,
|
|
chunks.size()
|
|
);
|
|
|
|
if (completedChunks == chunks.size())
|
|
{
|
|
System.out.println("[Monitor] All chunks downloaded successfully!");
|
|
break;
|
|
}
|
|
|
|
try {
|
|
Thread.sleep(monitorDelayMs);
|
|
} catch (InterruptedException e) {
|
|
System.out.println("Progress monitor interrupted.");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|