diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-17 14:55:16 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-17 14:55:16 +0000 |
| commit | 271949bf140359dd97cbee9ef927ee9280c9f31f (patch) | |
| tree | 31cd0e175401d2041e23128402c235eb6d2e3117 /sources/core/VSProcess.java | |
| parent | 5a0924146201bc577ca170952a21a42464ac7c71 (diff) | |
Vectortimestamps work
Better representation of Lamporttimestamps
BerkelyTimeProtocol
M trunk/ROADMAP
M trunk/sources/prefs/VSPrefs.java
M trunk/sources/prefs/VSDefaultPrefs.java
M trunk/sources/simulator/VSMain.java
M trunk/sources/simulator/VSSimulation.java
M trunk/sources/simulator/VSSimulationPanel.java
M trunk/sources/utils/VSFrame.java
M trunk/sources/utils/VSClassLoader.java
M trunk/sources/utils/VSInfoArea.java
M trunk/sources/protocols/BroadcastSturmProtocol.java
M trunk/sources/protocols/ExternalTimeSyncProtocol.java
M trunk/sources/protocols/RegisteredProtocols.java
M trunk/sources/protocols/PingPongProtocol.java
M trunk/sources/protocols/InternalTimeSyncProtocol.java
M trunk/sources/protocols/VSProtocol.java
M trunk/sources/protocols/DummyProtocol.java
A trunk/sources/protocols/BerkelyTimeProtocol.java
M trunk/sources/core/VSLamport.java
M trunk/sources/core/VSProcess.java
A trunk/sources/core/VSTime.java
A trunk/sources/core/VSVectorTime.java
M trunk/sources/core/VSTask.java
M trunk/sources/core/VSMessage.java
M trunk/sources/editors/VSEditor.java
M trunk/sources/editors/VSProtocolEditor.java
M trunk/sources/editors/VSSimulationEditor.java
M trunk/sources/editors/VSEditorFrame.java
M trunk/sources/editors/VSProcessEditor.java
Diffstat (limited to 'sources/core/VSProcess.java')
| -rw-r--r-- | sources/core/VSProcess.java | 152 |
1 files changed, 115 insertions, 37 deletions
diff --git a/sources/core/VSProcess.java b/sources/core/VSProcess.java index bb65959..c9002ca 100644 --- a/sources/core/VSProcess.java +++ b/sources/core/VSProcess.java @@ -24,15 +24,15 @@ public final class VSProcess extends VSPrefs { private boolean timeModified; private double clockOffset; private float clockVariance; - private int num; private int processID; private long globalTime; private long localTime; private static int processCounter; private boolean isCrashed; private long lamportTime; - private ArrayList<VSLamport> lamportTimestamps; - private ArrayList<Integer> vectorTimestamps; + private ArrayList<VSLamport> lamportTimeHistory; + private VSVectorTime vectorTime; + private ArrayList<VSVectorTime> vectorTimeHistory; /* This array contains all Integer prefs of the process which should show * up in the prefs menu! All keys which dont start with "sim." only show @@ -79,16 +79,13 @@ public final class VSProcess extends VSPrefs { private static final String DEFAULT_STRING_VALUE_KEYS[] = { }; - public VSProcess(VSPrefs prefs, VSSimulationPanel simulationPanel, VSLogging logging, int num) { + public VSProcess(VSPrefs prefs, VSSimulationPanel simulationPanel, VSLogging logging) { this.prefs = prefs; this.simulationPanel = simulationPanel; this.logging = logging; - this.num = num; - this.random = new VSRandom(processID+processCounter); - this.lamportTimestamps = new ArrayList<VSLamport>(); - this.vectorTimestamps = new ArrayList<Integer>(); - setLamportTime(0); + random = new VSRandom(processID+processCounter); + initTimeFormats(); setObject("protocols", new ArrayList<VSProtocol>()); isPaused = true; @@ -110,6 +107,31 @@ public final class VSProcess extends VSPrefs { createRandomCrashTask(); } + private void initTimeFormats() { + lamportTime = 0; + lamportTimeHistory = new ArrayList<VSLamport>(); + + vectorTime = new VSVectorTime(0); + vectorTimeHistory = new ArrayList<VSVectorTime>(); + + final int numProcesses = simulationPanel.getNumProcesses(); + for (int i = 0; i < numProcesses; ++i) + vectorTime.add(new Long(0)); + } + + private void resetTimeFormats() { + lamportTime = 0; + lamportTimeHistory.clear(); + + vectorTime = new VSVectorTime(0); + vectorTimeHistory.clear(); + + final int numProcesses = simulationPanel.getNumProcesses(); + for (int i = numProcesses; i > 0; --i) + vectorTime.add(new Long(0)); + } + + /** * Called from the VSProcessEditor, after finishing editing! */ @@ -178,9 +200,6 @@ public final class VSProcess extends VSPrefs { localTime = 0; globalTime = 0; clockOffset = 0; - lamportTime = 0; - lamportTimestamps.clear(); - vectorTimestamps.clear(); if (objectExists("protocols.registered")) { Object protocolsObj = getObject("protocols.registered"); @@ -193,6 +212,7 @@ public final class VSProcess extends VSPrefs { setCurrentColor(getColor("process.default")); createRandomCrashTask(); + resetTimeFormats(); } private void createRandomCrashTask() { @@ -277,10 +297,6 @@ public final class VSProcess extends VSPrefs { return crashedColor; } - public int getNum() { - return num; - } - public synchronized boolean timeModified() { return timeModified; } @@ -362,40 +378,83 @@ public final class VSProcess extends VSPrefs { return isPaused; } + public void increaseLamportTime() { + setLamportTime(getLamportTime()+1); + } + + public void updateLamportTime(long time) { + final long lamportTime = getLamportTime() + 1; + + if (time > lamportTime) + setLamportTime(time); + else + setLamportTime(lamportTime); + } + public synchronized long getLamportTime() { return lamportTime; } public synchronized void setLamportTime(long lamportTime) { this.lamportTime = lamportTime; - lamportTimestamps.add(new VSLamport(globalTime, lamportTime)); + lamportTimeHistory.add(new VSLamport(globalTime, lamportTime)); } - public synchronized VSLamport[] getLamportArray() { - final int size = lamportTimestamps.size(); - final VSLamport[] arr = new VSLamport[size]; + public synchronized VSTime[] getLamportTimeArray() { + final int size = lamportTimeHistory.size(); + final VSTime[] arr = new VSLamport[size]; for (int i = 0; i < size; ++i) - arr[i] = lamportTimestamps.get(i); + arr[i] = (VSTime) lamportTimeHistory.get(i); return arr; } - public synchronized int[] getVectorTime() { - final int size = vectorTimestamps.size(); - final int[] arr = new int[size]; + public synchronized void increaseVectorTime() { + vectorTime.set(processID-1, new Long(vectorTime.get(processID-1).longValue()+1)); + vectorTime.setGlobalTime(globalTime); + vectorTimeHistory.add(vectorTime.getCopy()); + } + + public synchronized void updateVectorTime(VSVectorTime vectorTimeUpdate) { + final int size = vectorTime.size(); + + for (int i = 0; i < size; ++i) { + if (i == processID-1) + vectorTime.set(i, new Long(vectorTime.get(i).longValue()+1)); + else if (vectorTimeUpdate.get(i) > vectorTime.get(i)) + vectorTime.set(i, vectorTimeUpdate.get(i)); + } + + vectorTime.setGlobalTime(globalTime); + vectorTimeHistory.add(vectorTime.getCopy()); + } + + public synchronized VSVectorTime getVectorTime() { + return vectorTime; + } + + public synchronized VSTime[] getVectorTimeArray() { + final int size = vectorTimeHistory.size(); + final VSTime[] arr = new VSTime[size]; for (int i = 0; i < size; ++i) - arr[i] = vectorTimestamps.get(i); + arr[i] = (VSTime) vectorTimeHistory.get(i); return arr; } - public void sendMessage(VSMessage message) { - logg(prefs.getString("lang.message.sent") + "; " - + prefs.getString("lang.protocol") + ": " + message.getProtocolName() + "; " - + prefs.getString("lang.message") + " " + message.toStringFull()); + StringBuffer buffer = new StringBuffer(); + buffer.append(prefs.getString("lang.message.sent")); + buffer.append("; "); + buffer.append(prefs.getString("lang.protocol")); + buffer.append(": " + message.getProtocolName()); + buffer.append("; "); + buffer.append(prefs.getString("lang.message")); + buffer.append(" "); + buffer.append(message.toStringFull()); + logg(buffer.toString()); simulationPanel.sendMessage(message); } @@ -412,20 +471,39 @@ public final class VSProcess extends VSPrefs { } public String toString() { - return - prefs.getString("lang.process.id") + ": " - + getProcessID() + "; " - + prefs.getString("lang.process.time.local") + ": " - + VSTools.getTimeString(getTime()) - + "; Lamport: " + lamportTime; + StringBuffer buffer = new StringBuffer(); + buffer.append(prefs.getString("lang.process.id")); + buffer.append(": "); + buffer.append(getProcessID()); + buffer.append("; "); + buffer.append(prefs.getString("lang.process.time.local")); + buffer.append(": "); + buffer.append(VSTools.getTimeString(getTime())); + buffer.append("; "); + buffer.append(prefs.getString("lang.time.lamport")); + buffer.append(": "); + buffer.append(lamportTime); + buffer.append("; "); + buffer.append(prefs.getString("lang.time.vector")); + buffer.append(": "); + buffer.append(vectorTime); + return buffer.toString(); } public String toStringFull() { - return toString() + "; paused: " + isPaused + "; crashed: " + isCrashed + "; crashTask: " + randomCrashTask; + StringBuffer buffer = new StringBuffer(); + buffer.append(toString()); + buffer.append("; paused: "); + buffer.append(isPaused); + buffer.append("; crashed: "); + buffer.append(isCrashed); + buffer.append("; crashTask: "); + buffer.append(randomCrashTask); + return buffer.toString(); } public boolean equals(VSProcess process) { - return process.getNum() == getNum(); + return process.getProcessID() == processID; } public VSSimulationPanel getSimulationPanel() { |
