diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-20 19:25:10 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-20 19:25:10 +0300 |
| commit | 7ee77637273e2e913cf19f078e9143ae6977f44f (patch) | |
| tree | 2cda0f82a7bf4c6f76e115f57e28de18d744d5e6 /src/main/java/core | |
| parent | 70fc0505b223f7bf17d3671d0532773359cf7858 (diff) | |
Add comprehensive Javadoc documentation for public APIs
- Document core classes: VSTask, VSMessage, VSInternalProcess
- Document event interfaces: VSAbstractEvent, VSCopyableEvent, VSTime
- Document timestamp events: VSTimestampTriggeredEvent, VSLamportTimestampEvent
- Document protocol framework: VSAbstractProtocol methods
- Document VSSimulator and VSRegisteredEvents
- Add detailed method descriptions with parameter and return value docs
- Include usage examples and cross-references between related classes
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/core')
| -rw-r--r-- | src/main/java/core/VSInternalProcess.java | 133 | ||||
| -rw-r--r-- | src/main/java/core/VSMessage.java | 50 | ||||
| -rw-r--r-- | src/main/java/core/VSTask.java | 19 | ||||
| -rw-r--r-- | src/main/java/core/time/VSTime.java | 19 |
4 files changed, 164 insertions, 57 deletions
diff --git a/src/main/java/core/VSInternalProcess.java b/src/main/java/core/VSInternalProcess.java index 5442cd1..ddf378e 100644 --- a/src/main/java/core/VSInternalProcess.java +++ b/src/main/java/core/VSInternalProcess.java @@ -14,9 +14,24 @@ import simulator.VSSimulatorVisualization; import utils.VSPriorityQueue; /** - * The class VSInternalProcess, an object of this class represents a process - * of a simulator. + * Internal process implementation for the distributed systems simulator. + * This class extends {@link VSAbstractProcess} with simulation-specific features + * including: + * <ul> + * <li>Time synchronization with global simulation clock</li> + * <li>Clock variance simulation for realistic distributed behavior</li> + * <li>Visual highlighting and color management</li> + * <li>Vector clock monitoring for timestamp-triggered events</li> + * <li>Process crash simulation</li> + * <li>Protocol and event management</li> + * </ul> + * + * <p>Each process maintains its own local time that drifts from global time + * based on configured clock variance, simulating realistic clock skew in + * distributed systems.</p> * + * @see VSAbstractProcess + * @see VSSimulatorVisualization * @author Paul C. Buetow */ public class VSInternalProcess extends VSAbstractProcess { @@ -39,8 +54,17 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Called from the VSProcessEditor, after finishing editing! This makes - * sure that the VSInternalProcess object is using the up to date prefs! + * Updates this process's configuration from its preference settings. + * Called by {@link simulator.VSProcessEditor} after editing is complete + * to apply any configuration changes. + * + * <p>This method updates:</p> + * <ul> + * <li>Clock variance for time drift simulation</li> + * <li>Local time value</li> + * <li>Process colors</li> + * <li>Random crash parameters</li> + * </ul> */ public synchronized void updateFromPrefs() { setClockVariance(getFloat("process.clock.variance")); @@ -50,8 +74,15 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Called from the VSProcessEditor, before starting editing! This makes - * sure that the editor edits the up to date prefs of the process! + * Saves this process's current state to its preference settings. + * Called by {@link simulator.VSProcessEditor} before editing begins + * to ensure the editor shows current values. + * + * <p>This method saves:</p> + * <ul> + * <li>Current clock variance</li> + * <li>Current local time</li> + * </ul> */ public synchronized void updatePrefs() { setFloat("process.clock.variance", getClockVariance()); @@ -59,12 +90,22 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Syncs the process' time. This method is using the clockOffset and - * clockVariance variables. This method is called repeatedly from the - * VSSimulatorVisualization in order to update the process' local and global - * time values. + * Synchronizes this process's time with the global simulation time. + * This method simulates realistic clock drift using the configured + * clock variance. + * + * <p>The synchronization algorithm:</p> + * <ol> + * <li>Calculates time elapsed since last sync</li> + * <li>Advances local time by elapsed time</li> + * <li>Applies clock drift based on variance</li> + * <li>Accumulates fractional time in clockOffset</li> + * <li>Ensures time never goes negative</li> + * </ol> + * + * <p>Called repeatedly by {@link VSSimulatorVisualization} during simulation.</p> * - * @param globalTime the global time. + * @param globalTime the current global simulation time */ public synchronized void syncTime(final long globalTime) { final long currentGlobalTimestep = globalTime - this.globalTime; @@ -89,7 +130,11 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Highlights the process. + * Activates visual highlighting for this process. + * The process's current color is saved and replaced with the highlight color. + * Use {@link #highlightOff()} to restore the original color. + * + * @see #highlightOff() */ public synchronized void highlightOn() { tmpColor = currentColor; @@ -159,28 +204,47 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Creates a random percentage 0..100 using the process' own pseudo - * random number generator object of the VSRandom class. + * Generates a random percentage value between 0 and 100 (inclusive). + * Uses this process's dedicated random number generator to ensure + * reproducible results for a given random seed. + * + * <p>This method is commonly used for:</p> + * <ul> + * <li>Probabilistic event triggering</li> + * <li>Random crash simulation</li> + * <li>Message loss simulation</li> + * </ul> * - * @return A random percentage 0..100. + * @return a random integer between 0 and 100 inclusive + * @see utils.VSRandom */ public synchronized int getRandomPercentage() { return random.nextInt() % constants.VSConstants.PERCENTAGE_RANGE; } /** - * Adds the clock offset. This method is used by the task manager. The - * clock offset identifies if the local time of the process has changed and - * how much.. + * Adds to this process's clock offset, adjusting its local time drift. + * Used by the task manager when tasks modify process time. + * + * <p>The clock offset accumulates fractional time units that are + * converted to whole time units during {@link #syncTime(long)}.</p> * - * @param add the clock offset to add. + * @param add the amount to add to the clock offset (can be negative) + * @see VSTaskManager#runTasks(long) */ public synchronized void addClockOffset(long add) { this.clockOffset += add; } /** - * The process' state is 'play'. Called by the simulator canvas. + * Transitions this process to the 'playing' state. + * The process resumes normal operation and its color changes + * to indicate running status. + * + * <p>Called by the simulator when starting or resuming simulation.</p> + * + * @see #pause() + * @see #finish() */ public synchronized void play() { isPaused = false; @@ -188,7 +252,14 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * The process' state is 'pause'. Called by the simulator canvas. + * Transitions this process to the 'paused' state. + * The process stops executing and its color changes + * to indicate stopped status. + * + * <p>Called by the simulator when pausing simulation.</p> + * + * @see #play() + * @see #finish() */ public synchronized void pause() { isPaused = true; @@ -196,7 +267,13 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * The process' state is 'Finish'. Called by the simulator canvas. + * Transitions this process to the 'finished' state. + * The process stops executing and returns to its default color. + * + * <p>Called by the simulator when ending simulation.</p> + * + * @see #play() + * @see #pause() */ public synchronized void finish() { isPaused = true; @@ -222,11 +299,15 @@ public class VSInternalProcess extends VSAbstractProcess { } /** - * Checks if the time has been modified. by a task. - * This mehod is needed by the task manager in order to add a clock offset - * to the process object. + * Checks if this process's time has been modified by a task. + * The task manager uses this flag to determine if clock offset + * adjustments are needed after task execution. + * + * <p>This flag is set when tasks directly modify the process's + * local time, requiring synchronization adjustments.</p> * - * @return true, if yes + * @return true if the time has been modified since last check + * @see VSTaskManager#runTasks(long) */ public synchronized boolean timeModified() { return timeModified; diff --git a/src/main/java/core/VSMessage.java b/src/main/java/core/VSMessage.java index a7b48e6..130bd53 100644 --- a/src/main/java/core/VSMessage.java +++ b/src/main/java/core/VSMessage.java @@ -79,64 +79,71 @@ public class VSMessage extends VSPrefs { } /** - * Gets the protocol name of the message. + * Gets the human-readable protocol name associated with this message. + * The name is retrieved from the registered events based on the protocol classname. * - * @return The protocol name of the message. + * @return the localized protocol name, or null if not found */ public String getName() { return VSRegisteredEvents.getNameByClassname(getProtocolClassname()); } /** - * Gets the protocol classname. + * Gets the fully qualified classname of the protocol that created this message. + * This is used to identify which protocol handler should process the message. * - * @return The protocol classname of the message. + * @return the protocol's full classname (e.g., "protocols.implementations.VSPingPongProtocol") */ public String getProtocolClassname() { return protocolClassname; } /** - * Gets the message id. + * Gets the unique identifier for this message. + * Each message is assigned a sequential ID when created. * - * @return The id of the message. + * @return the unique message ID */ public long getMessageID() { return messageID; } /** - * Gets a reference of the sending process. + * Gets the process that sent this message. + * This can be used to identify the sender and access sender information. * - * @return The process which sent this message. + * @return the process that created and sent this message */ public VSAbstractProcess getSendingProcess() { return sendingProcess; } /** - * Gets the lamport time. + * Gets the Lamport timestamp of the sending process at the time this message was sent. + * This is used for logical ordering of events in the distributed system. * - * @return The lamport time of the sending process. + * @return the sender's Lamport time when the message was created */ public long getLamportTime() { return lamportTime; } /** - * Gets the vector time. + * Gets the vector clock of the sending process at the time this message was sent. + * This is used for determining causality between events in the distributed system. * - * @return The vector time of the sending process. + * @return a copy of the sender's vector clock when the message was created */ public VSVectorTime getVectorTime() { return vectorTime; } /** - * Checks if the message has been sent by a server or a client. + * Checks whether this message was sent by a server or client protocol instance. + * This distinction is important for protocols that have different behavior + * for server and client roles. * - * @return true, if the message has been sent by a server. false, if the - * message has been sent by a client. + * @return true if sent by a server protocol instance, false if sent by a client */ public boolean isServerMessage() { return isServerMessage; @@ -160,20 +167,21 @@ public class VSMessage extends VSPrefs { } /** - * Extended string representation of the message object. + * Returns an extended string representation of this message. + * This includes the basic message information plus all stored preferences/data. * - * @return Extended string representation of the message object. + * @return detailed string representation including ID, protocol, and all message data */ public String toStringFull() { return toString() + "; " + super.toString(); } /** - * Compares two messages. + * Compares this message with another message for equality. + * Two messages are considered equal if they have the same message ID. * - * @param message The message to compare with. - * - * @return true, if the messages have the same id. Otherwise false. + * @param message the message to compare with this message + * @return true if both messages have the same ID, false otherwise */ public boolean equals(VSMessage message) { return messageID == message.getMessageID(); diff --git a/src/main/java/core/VSTask.java b/src/main/java/core/VSTask.java index cc43f1c..81b098e 100644 --- a/src/main/java/core/VSTask.java +++ b/src/main/java/core/VSTask.java @@ -257,7 +257,12 @@ public class VSTask implements Comparable<Object>, VSSerializable { } /** - * Runs the task. + * Executes this task by running its associated event. + * This method is called by the task manager when the task's scheduled time arrives. + * If the event requires it, this will also increase the process's timestamps. + * + * @see VSAbstractEvent#onStart() + * @see VSAbstractEvent#shouldIncreaseTimestamps() */ public void run() { if (event.getProcess() == null) @@ -279,18 +284,22 @@ public class VSTask implements Comparable<Object>, VSSerializable { } /** - * Sets the task time. + * Sets the scheduled execution time for this task. + * For global-timed tasks, this is in global simulation time units. + * For local-timed tasks, this is in the process's local time units. * - * @param taskTime the task time + * @param taskTime the time when this task should execute */ public void setTaskTime(long taskTime) { this.taskTime = taskTime; } /** - * Sets the process. + * Sets the process that owns this task. + * This method only updates the process if it differs from the current one. + * It also updates the event's process reference. * - * @param process the process + * @param process the process that will own and execute this task */ public void setProcess(VSInternalProcess process) { /* Only do it if the process differs */ diff --git a/src/main/java/core/time/VSTime.java b/src/main/java/core/time/VSTime.java index d46e0e4..20806d6 100644 --- a/src/main/java/core/time/VSTime.java +++ b/src/main/java/core/time/VSTime.java @@ -1,22 +1,31 @@ package core.time; /** - * This interface is a guidline for general time format classes. + * Interface for time representations in the distributed simulator. + * This interface provides a common contract for different time implementations + * including Lamport logical time and vector clocks. + * + * <p>All time implementations must track the global simulation time + * and provide a string representation for display purposes.</p> * + * @see VSLamportTime + * @see VSVectorTime * @author Paul C. Buetow */ public interface VSTime { /** - * Gets the global time. + * Gets the global simulation time when this time value was recorded. + * This allows correlation between logical time and simulation time. * - * @return The global time + * @return the global simulation time in milliseconds */ public long getGlobalTime(); /** - * Returns a string representation. + * Returns a string representation of this time value. + * The format depends on the specific time implementation. * - * @return The representation of the implementing object as a string + * @return string representation suitable for display or logging */ public String toString(); } |
