diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 5109729..b69bede 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -20,13 +20,13 @@ public class Main { System.out.println("Chunk count: " + config.getChunkCount()); System.out.println(); - // 2. Create chunks + List chunks = ChunkUtils.createChunks( config.getTotalSizeMB(), config.getChunkCount() ); - // 3. Create worker threads + List workerThreads = new ArrayList<>(); for (ChunkStatus chunk : chunks) { @@ -35,12 +35,13 @@ public class Main { workerThreads.add(workerThread); - // TODO: - // Students may print helpful debug information here, - // for example which chunk is assigned to which worker thread. + System.out.println("Assigned Worker-" + chunk.getChunkId() + + " to chunk #" + chunk.getChunkId() + + " (" + chunk.getChunkSizeMB() + " MB)"); } - // 4. Create and start monitor thread + System.out.println(); + ProgressMonitor monitor = new ProgressMonitor(config, chunks); Thread monitorThread = new Thread(monitor, "Progress-Monitor"); @@ -78,17 +79,15 @@ public class Main { completedChunks++; } - System.out.println( - "Chunk " + chunk.getChunkId() - + ": " + chunk.getDownloadedMB() - + "/" + chunk.getChunkSizeMB() - + " MB" - ); + System.out.printf(" Chunk %d: %.2f/%.0f MB | Duration: %d ms%n", + chunk.getChunkId(), chunk.getDownloadedMB(), + chunk.getChunkSizeMB(), chunk.getDownloadDurationMs()); + } System.out.println(); - System.out.println("Completed chunks: " + completedChunks + "/" + chunks.size()); - System.out.println("Downloaded total: " + downloadedMB + "/" + config.getTotalSizeMB() + " MB"); - System.out.println("Simulation finished."); + System.out.println("Completed chunks : " + completedChunks + "/" + chunks.size()); + System.out.printf("Downloaded total : %.2f/%d MB%n", downloadedMB, config.getTotalSizeMB()); + } } diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index b9b982d..7280439 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -16,6 +16,7 @@ public class ProgressMonitor implements Runnable { @Override public void run() { + double previousProgress = 0.0; while (true) { double totalDownloadedMB = 0.0; @@ -34,15 +35,36 @@ public class ProgressMonitor implements Runnable { 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() - ); + + double deltaMB = totalDownloadedMB - previousProgress; + double speedMBps = deltaMB / (monitorDelayMs / 1000.0); + previousProgress = totalDownloadedMB; + + + double remainingMB = totalSizeMB - totalDownloadedMB; + long etaSeconds = (speedMBps > 0) ? (long)(remainingMB / speedMBps) : 0; + + + int barWidth = 30; + int filled = (int)(percent / 100.0 * barWidth); + filled = Math.min(filled, barWidth); + String bar = "=".repeat(filled) + (filled < barWidth ? ">" : "") + " ".repeat(Math.max(0, barWidth - filled - 1)); + + + System.out.printf("\r[%-30s] %5.1f%% | %5.1f/%d MB | Speed: %.2f MB/s | ETA: %ds | Chunks: %d/%d ", + bar, percent, totalDownloadedMB, totalSizeMB, + speedMBps, etaSeconds, + completedChunks, chunks.size()); + +// 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()) { diff --git a/src/main/resources/download_config.txt b/src/main/resources/download_config.txt index f0ff5f2..bfb6dac 100644 --- a/src/main/resources/download_config.txt +++ b/src/main/resources/download_config.txt @@ -1,7 +1,7 @@ fileName=movie.mkv totalSizeMB=120 chunkCount=10 -minStepDelayMs=80 -maxStepDelayMs=200 -minStepDownloadMB=2 -maxStepDownloadMB=6 \ No newline at end of file +minStepDelayMs=200 +maxStepDelayMs=600 +minStepDownloadMB=.1 +maxStepDownloadMB=.4 \ No newline at end of file