implement DownloadWorker.java
This commit is contained in:
@@ -22,22 +22,53 @@ public class DownloadWorker implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO: Record the chunk start time in chunkStatus.
|
// TODO: Record the chunk start time in chunkStatus.
|
||||||
|
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
||||||
|
|
||||||
double downloaded = 0.0;
|
double downloaded = 0.0;
|
||||||
|
|
||||||
// TODO: Print a message that this chunk has started downloading.
|
// TODO: Print a message that this chunk has started downloading.
|
||||||
|
System.out.println("Starting download for Chunk ID: " + chunkStatus.getChunkId());
|
||||||
|
|
||||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||||
|
|
||||||
// TODO: Generate a random sleep delay between min and max delay.
|
// TODO: Generate a random sleep delay between min and max delay.
|
||||||
|
long minDelay = config.getMinStepDelayMs();
|
||||||
|
long maxDelay = config.getMaxStepDelayMs();
|
||||||
|
long sleepTime = minDelay + (long) ((maxDelay - minDelay) * random.nextDouble());
|
||||||
|
|
||||||
// TODO: Sleep for that delay.
|
// TODO: Sleep for that delay.
|
||||||
|
try {
|
||||||
|
Thread.sleep(sleepTime);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Generate a random download amount for this step.
|
// TODO: Generate a random download amount for this step.
|
||||||
|
double minStep = config.getMinStepDownloadMB();
|
||||||
|
double maxStep = config.getMaxStepDownloadMB();
|
||||||
|
double downloadAmount = minStep + ((maxStep - minStep) * random.nextDouble());
|
||||||
|
|
||||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
// TODO: Increase downloaded, but do not go beyond chunk size.
|
||||||
|
if(downloaded + downloadAmount >= chunkStatus.getChunkSizeMB())
|
||||||
|
downloaded = chunkStatus.getChunkSizeMB();
|
||||||
|
else
|
||||||
|
downloaded += downloadAmount;
|
||||||
|
|
||||||
// TODO: Save the updated downloaded value into chunkStatus.
|
// TODO: Save the updated downloaded value into chunkStatus.
|
||||||
|
chunkStatus.setDownloadedMB(downloaded);
|
||||||
|
|
||||||
// TODO: Optionally print step-by-step progress.
|
// TODO: Optionally print step-by-step progress.
|
||||||
|
System.out.println("Chunk ID: " + chunkStatus.getChunkId() + " | downloaded: " + downloaded);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Mark the chunk as completed.
|
// TODO: Mark the chunk as completed.
|
||||||
|
chunkStatus.setCompleted(true);
|
||||||
|
|
||||||
// TODO: Record the chunk end time in chunkStatus.
|
// TODO: Record the chunk end time in chunkStatus.
|
||||||
|
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||||
|
|
||||||
// TODO: Print a message that this chunk has finished downloading.
|
// TODO: Print a message that this chunk has finished downloading.
|
||||||
|
System.out.println("Chunk ID: " + chunkStatus.getChunkId() + " | COMPLETED!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user