56 lines
1.7 KiB
Java
56 lines
1.7 KiB
Java
import java.util.Random;
|
|
|
|
public class DownloadWorker implements Runnable {
|
|
|
|
private final ChunkStatus chunkStatus;
|
|
private final DownloadConfig config;
|
|
private final Random random;
|
|
|
|
public DownloadWorker(ChunkStatus chunkStatus, DownloadConfig config) {
|
|
this.chunkStatus = chunkStatus;
|
|
this.config = config;
|
|
this.random = new Random();
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
|
|
|
double downloaded = 0;
|
|
|
|
System.out.println("Chunk " + chunkStatus.getChunkId() + " started.");
|
|
|
|
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
|
|
|
int delay = random.nextInt(
|
|
config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1
|
|
) + config.getMinStepDelayMs();
|
|
|
|
try {
|
|
Thread.sleep(delay);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
double amount = random.nextDouble() *
|
|
(config.getMaxStepDownloadMB() - config.getMinStepDownloadMB())
|
|
+ config.getMinStepDownloadMB();
|
|
|
|
downloaded = downloaded + amount;
|
|
|
|
if (downloaded > chunkStatus.getChunkSizeMB()) {
|
|
downloaded = chunkStatus.getChunkSizeMB();
|
|
}
|
|
|
|
chunkStatus.setDownloadedMB(downloaded);
|
|
|
|
System.out.println("Chunk " + chunkStatus.getChunkId()
|
|
+ ": " + downloaded + "/" + chunkStatus.getChunkSizeMB() + " MB");
|
|
}
|
|
|
|
chunkStatus.setCompleted(true);
|
|
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
|
|
|
System.out.println("Chunk " + chunkStatus.getChunkId() + " finished.");
|
|
}
|
|
} |