Final version of project

This commit is contained in:
2025mohseni
2026-06-11 02:38:34 +03:30
parent 9e5c715088
commit 5fbe9de273
6 changed files with 267 additions and 264 deletions
+38 -82
View File
@@ -3,105 +3,61 @@ import java.util.List;
public class Main {
public static void main(String[] args) {
System.out.println("=== Simulated Download Manager ===");
DownloadConfig config = ConfigReader.readConfig("src/main/resources/download_config.txt");
// 1. Read config
DownloadConfig config;
try {
config = ConfigReader.readConfig("download_config.txt");
} catch (Exception e) {
System.out.println("Failed to read configuration: " + e.getMessage());
return;
List<ChunkStatus> chunks = new ArrayList<>();
double chunkSize = config.getTotalSizeMB() / config.getChunkCount();
for (int i = 0; i < config.getChunkCount(); i++) {
chunks.add(new ChunkStatus(i + 1, chunkSize));
}
System.out.println("File name: " + config.getFileName());
System.out.println("Total size (MB): " + config.getTotalSizeMB());
System.out.println("Chunk count: " + config.getChunkCount());
System.out.println();
ProgressMonitor monitor = new ProgressMonitor(chunks, config);
// 2. Create chunks
List<ChunkStatus> chunks = ChunkUtils.createChunks(
config.getTotalSizeMB(),
config.getChunkCount()
);
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
monitorThread.setDaemon(true);
monitorThread.start();
// 3. Create worker threads
List<Thread> workerThreads = new ArrayList<>();
for (ChunkStatus chunk : chunks) {
DownloadWorker worker = new DownloadWorker(chunk, config);
Thread workerThread = new Thread(worker, "Worker-" + chunk.getChunkId());
Thread workerThread = new Thread(
new DownloadWorker(chunk, config),
"Worker-" + chunk.getChunkId()
);
workerThreads.add(workerThread);
// TODO:
// Students may print helpful debug information here,
// for example which chunk is assigned to which worker thread.
workerThread.start();
}
// 4. Create and start monitor thread
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
try {
for (Thread thread : workerThreads) {
thread.join();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Main thread interrupted while waiting for workers: " + e.getMessage());
}
// 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.
// 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();
// }
// 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. Print final report
System.out.println();
System.out.println("=== Final Report ===");
int completedChunks = 0;
double downloadedMB = 0.0;
double totalDownloaded = 0;
for (ChunkStatus chunk : chunks) {
downloadedMB += chunk.getDownloadedMB();
if (chunk.isCompleted()) {
completedChunks++;
}
System.out.println(
"Chunk " + chunk.getChunkId()
+ ": " + chunk.getDownloadedMB()
+ "/" + chunk.getChunkSizeMB()
+ " MB"
);
totalDownloaded += chunk.getDownloadedMB();
}
double totalSize = config.getTotalSizeMB();
double progress = (totalDownloaded / totalSize) * 100;
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.printf(
"Progress: %.2f%% (%.2f / %.2f MB)%n",
progress,
totalDownloaded,
totalSize
);
System.out.println("All workers finished.");
}
}