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