108 lines
3.7 KiB
Java
108 lines
3.7 KiB
Java
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
System.out.println("=== Simulated Download Manager ===");
|
|
|
|
// 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;
|
|
}
|
|
|
|
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();
|
|
|
|
// 2. Create chunks
|
|
List<ChunkStatus> chunks = ChunkUtils.createChunks(
|
|
config.getTotalSizeMB(),
|
|
config.getChunkCount()
|
|
);
|
|
|
|
// 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());
|
|
|
|
workerThreads.add(workerThread);
|
|
|
|
// TODO:
|
|
// Students may print helpful debug information here,
|
|
// for example which chunk is assigned to which worker thread.
|
|
}
|
|
|
|
// 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.
|
|
|
|
// 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;
|
|
|
|
for (ChunkStatus chunk : chunks) {
|
|
downloadedMB += chunk.getDownloadedMB();
|
|
|
|
if (chunk.isCompleted()) {
|
|
completedChunks++;
|
|
}
|
|
|
|
System.out.println(
|
|
"Chunk " + chunk.getChunkId()
|
|
+ ": " + chunk.getDownloadedMB()
|
|
+ "/" + chunk.getChunkSizeMB()
|
|
+ " MB"
|
|
);
|
|
}
|
|
|
|
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.");
|
|
}
|
|
}
|