From ec73b93e38241c3c5eae524363b069bbd78feb56 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Wed, 3 Jun 2026 17:56:46 +0330 Subject: [PATCH] complete code --- .idea/misc.xml | 2 +- src/main/java/DownloadConfig.java | 1 + src/main/java/DownloadWorker.java | 62 ++++++++++++++++++++++++------ src/main/java/Main.java | 59 +++++++++++++--------------- src/main/java/ProgressMonitor.java | 19 ++++----- 5 files changed, 88 insertions(+), 55 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fdc35ea..0c04b52 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/DownloadConfig.java b/src/main/java/DownloadConfig.java index dc1ba3a..d95cf13 100644 --- a/src/main/java/DownloadConfig.java +++ b/src/main/java/DownloadConfig.java @@ -14,6 +14,7 @@ public class DownloadConfig { /** * Constructs a new DownloadConfig with specified simulation parameters. */ + public DownloadConfig(String fileName, int totalSizeMB, int chunkCount, int minStepDelayMs, int maxStepDelayMs, double minStepDownloadMB, double maxStepDownloadMB) { diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..a1abb67 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -21,23 +21,63 @@ public class DownloadWorker implements Runnable { @Override public void run() { - // TODO: Record the chunk start time in chunkStatus. + + + long start_time = System.currentTimeMillis(); + chunkStatus.setStartTimeMs(start_time); + + double downloaded = 0.0; - // TODO: Print a message that this chunk has started downloading. + System.out.println("The chunk number: " + chunkStatus.getChunkId() + " start" ); + 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. + + int delay = random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1) + + config.getMinStepDelayMs(); + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + + + double downloadValue = (random.nextDouble()*(config.getMaxStepDownloadMB() - config.getMinStepDownloadMB())) + + config.getMinStepDownloadMB(); + + + + if(downloadValue + chunkStatus.getDownloadedMB() > chunkStatus.getChunkSizeMB()){ + downloadValue = chunkStatus.getChunkSizeMB() - chunkStatus.getDownloadedMB(); + } + + + chunkStatus.setDownloadedMB(downloadValue + chunkStatus.getDownloadedMB()); + downloaded = chunkStatus.getDownloadedMB(); + + + // you can comment next line to get better output + System.out.println( "The chunk number " + chunkStatus.getChunkId() + + "download :"+ downloadValue+"(MB)" + chunkStatus.getProgressPercentage()); + } - // TODO: Mark the chunk as completed. - // TODO: Record the chunk end time in chunkStatus. - // TODO: Print a message that this chunk has finished downloading. + chunkStatus.setCompleted(true); + + + + long end_time = System.currentTimeMillis(); + chunkStatus.setEndTimeMs(end_time); + + + System.out.println("\u001B[33m" +"The chunk number" +chunkStatus.getChunkId() + + "Finished the download. Time:" + +( chunkStatus.getEndTimeMs() - chunkStatus.getStartTimeMs() )+ "(ms). Speed =" + + (chunkStatus.getChunkSizeMB()/( chunkStatus.getEndTimeMs() - chunkStatus.getStartTimeMs() ) *1000) + +" Mb/s"+ "\u001B[0m" ); + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..9094882 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -34,50 +34,45 @@ 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("Chunk number " + chunk.getChunkId() + + "assigend to thread number "+ workerThread.getName() ); } + System.out.println("------------------------------"); + System.out.println("Total Threads: " + workerThreads.size()); + // 4. Create and start monitor thread ProgressMonitor monitor = new ProgressMonitor(config, chunks); Thread monitorThread = new Thread(monitor, "Progress-Monitor"); - // TODO: - // Start the monitor thread before starting the workers - // so that progress can be displayed while downloading happens. - // - // Example idea: - // monitorThread.start(); - // 5. Start worker threads - // TODO: - // Start each worker thread in workerThreads. - // Use a loop and call start() on each thread. + // 5. start threads + monitorThread.start(); + for(Thread th:workerThreads){ + th.start(); + } + // 6. Wait for workers to finish - // TODO: - // Wait for all worker threads to complete by calling join(). - // This should be done inside a try-catch block for InterruptedException. - // - // Hint: - // for (Thread thread : workerThreads) { - // thread.join(); - // } + for(Thread th : workerThreads){ + try { + th.join(); + } catch (InterruptedException e) { + System.out.println("Main thread interrupted"); + } + } - // TODO: - // After all workers finish, the monitor thread may also need to stop. - // Depending on how ProgressMonitor is implemented, students may: - // - wait for it to finish on its own, or - // - add a stopping mechanism in ProgressMonitor later. - // - // If your monitor finishes automatically, you may join it here. - // NOTE: - // this final report may show 0 progress because no worker has actually run yet. - // Until students complete the thread start/join TODOs above, + // 7. Wait for monitor to finish + try { + monitorThread.join(); + } catch (InterruptedException e) { + System.out.println(" Main thread interrupted"); + } - // 7. Print final report + + + // 8. Print final report System.out.println(); System.out.println("=== Final Report ==="); diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..a220b10 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -16,15 +16,6 @@ public class ProgressMonitor implements Runnable { @Override public void run() { - // TODO: - // Repeatedly check chunk progress until all chunks are completed. - // In each loop: - // 1. Read the downloaded size from every chunk - // 2. Add all downloaded amounts to totalDownloadedMB - // 3. Count completed chunks - // 4. Print a progress message - // 5. If completedChunks == chunks.size(), print a final monitor message and stop - // 6. Otherwise sleep for monitorDelayMs and continue while (true) { @@ -44,6 +35,7 @@ public class ProgressMonitor implements Runnable { percent = (totalDownloadedMB * 100.0) / totalSizeMB; } + System.out.println("\u001B[34m"); System.out.printf( "Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n", fileName, @@ -53,10 +45,14 @@ public class ProgressMonitor implements Runnable { completedChunks, chunks.size() ); + System.out.println("\u001B[0m"); - // TODO: - // If all chunks are completed, print a final message and exit the loop + + if(completedChunks == chunks.size()){ + System.out.println("ALL CHUNKS FINISHED"); + return; + } try { Thread.sleep(monitorDelayMs); @@ -65,5 +61,6 @@ public class ProgressMonitor implements Runnable { return; } } + } }