Develop #1

Open
Z.Gharagozloo.M wants to merge 6 commits from develop into main
3 changed files with 55 additions and 11 deletions
Showing only changes of commit b2bf918030 - Show all commits
+4
View File
@@ -105,4 +105,8 @@ public class ChunkStatus {
completed ? " [Completed]" : ""
);
}
public void setStartTime(long startTime) {
}
}
+6
View File
@@ -67,4 +67,10 @@ public class DownloadConfig {
", maxStepDownloadMB=" + maxStepDownloadMB +
'}';
}
public int getMinDelayMs() {
}
public int getMaxDelayMs() {
}
}
+45 -11
View File
@@ -21,23 +21,57 @@ public class DownloadWorker implements Runnable {
@Override
public void run() {
// TODO: Record the chunk start time in chunkStatus.
long startTime = System.currentTimeMillis();
chunkStatus.setStartTime(startTime);
double downloaded = 0.0;
// TODO: Print a message that this chunk has started downloading.
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" started downloading.");
while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay.
// TODO: Sleep for that delay.
// TODO: Generate a random download amount for this step.
// TODO: Increase downloaded, but do not go beyond chunk size.
// TODO: Save the updated downloaded value into chunkStatus.
// TODO: Optionally print step-by-step progress.
try {
//Generate a random sleep delay between min and max delay.
int minDelay = config.getMinDelayMs();
int maxDelay = config.getMaxDelayMs();
int randomDelay = random.nextInt((maxDelay - minDelay) + 1) + minDelay;
//Sleep for that delay.
Thread.sleep(randomDelay);
//Generate a random download amount for this step.
double minStep = config.getMinStepDelayMs();
double maxStep = config.getMaxStepDelayMs();
double randomStep = (maxStep + minStep)*random.nextDouble() + minStep;
//Increase downloaded, but do not go beyond chunk size.
downloaded += randomStep;
if (downloaded > chunkStatus.getChunkSizeMB()) {
downloaded = chunkStatus.getChunkSizeMB();
}
//Save the updated downloaded value into chunkStatus.
chunkStatus.setDownloadedMB(downloaded);
//print step-by-step progress.
System.out.printf("Chunk #%d: Downloaded %.2f / %.2f MB%n",
chunkStatus.getChunkId(), downloaded, chunkStatus.getChunkSizeMB());
} catch (InterruptedException e) {
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" was interrupted!");
return;
}
}
// TODO: Mark the chunk as completed.
// TODO: Record the chunk end time in chunkStatus.
// TODO: Print a message that this chunk has finished downloading.
//Mark the chunk as completed.
chunkStatus.setCompleted(true);
//Record the chunk end time in chunkStatus.
long endTime = System.currentTimeMillis();
chunkStatus.setEndTimeMs(endTime);
//Print a message that this chunk has finished downloading.
System.out.println("Chunk \"" + chunkStatus.getChunkId() + "\" has finished downloading.");
}
}