113 lines
3.0 KiB
Java
113 lines
3.0 KiB
Java
// Educational simplification:
|
|
// each worker writes only to its own ChunkStatus,
|
|
// and the monitor only reads chunk states.
|
|
// volatile is used here to make progress updates more visible across threads.
|
|
|
|
/**
|
|
* Represents the state and download progress of a single file chunk.
|
|
* Each worker thread updates its own ChunkStatus, while the monitor thread reads it.
|
|
*/
|
|
public class ChunkStatus {
|
|
private final int chunkId;
|
|
private final double chunkSizeMB;
|
|
private volatile double downloadedMB;
|
|
private volatile boolean completed;
|
|
private volatile long startTimeMs;
|
|
private volatile long endTimeMs;
|
|
|
|
/**
|
|
* Initializes a chunk with its unique ID and total allocated size.
|
|
* Progress-related fields are initialized to default values.
|
|
*/
|
|
public ChunkStatus(int chunkId, double chunkSizeMB) {
|
|
this.chunkId = chunkId;
|
|
this.chunkSizeMB = chunkSizeMB;
|
|
this.downloadedMB = 0.0;
|
|
this.completed = false;
|
|
this.startTimeMs = 0;
|
|
this.endTimeMs = 0;
|
|
}
|
|
|
|
// Getters and Setters
|
|
public int getChunkId() {
|
|
return chunkId;
|
|
}
|
|
|
|
public double getChunkSizeMB() {
|
|
return chunkSizeMB;
|
|
}
|
|
|
|
public double getDownloadedMB() {
|
|
return downloadedMB;
|
|
}
|
|
|
|
public void setDownloadedMB(double downloadedMB) {
|
|
// Guard to prevent downloaded size exceeding actual chunk size
|
|
if (downloadedMB >= this.chunkSizeMB) {
|
|
this.downloadedMB = this.chunkSizeMB;
|
|
} else {
|
|
this.downloadedMB = downloadedMB;
|
|
}
|
|
}
|
|
|
|
public boolean isCompleted() {
|
|
return completed;
|
|
}
|
|
|
|
public void setCompleted(boolean completed) {
|
|
this.completed = completed;
|
|
}
|
|
|
|
public long getStartTimeMs() {
|
|
return startTimeMs;
|
|
}
|
|
|
|
public void setStartTimeMs(long startTimeMs) {
|
|
this.startTimeMs = startTimeMs;
|
|
}
|
|
|
|
public long getEndTimeMs() {
|
|
return endTimeMs;
|
|
}
|
|
|
|
public void setEndTimeMs(long endTimeMs) {
|
|
this.endTimeMs = endTimeMs;
|
|
}
|
|
|
|
/**
|
|
* Helper method to calculate the duration of this specific chunk's download.
|
|
* Returns 0 if the chunk hasn't started or finished yet.
|
|
*/
|
|
public long getDownloadDurationMs() {
|
|
if (startTimeMs > 0 && endTimeMs > startTimeMs) {
|
|
return endTimeMs - startTimeMs;
|
|
} else if (startTimeMs > 0 && !completed) {
|
|
return System.currentTimeMillis() - startTimeMs;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Helper method to calculate the download percentage of this chunk.
|
|
*/
|
|
public double getProgressPercentage() {
|
|
if (chunkSizeMB == 0) return 100.0;
|
|
return (downloadedMB / chunkSizeMB) * 100.0;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("Chunk #%d: %.1f/%.1f MB (%.1f%%)%s",
|
|
chunkId,
|
|
downloadedMB,
|
|
chunkSizeMB,
|
|
getProgressPercentage(),
|
|
completed ? " [Completed]" : ""
|
|
);
|
|
}
|
|
|
|
public void setStartTime(long startTime) {
|
|
|
|
}
|
|
}
|