summaryrefslogtreecommitdiff
path: root/sources/core
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-05-17 14:55:16 +0000
committerPaul Buetow <paul@buetow.org>2008-05-17 14:55:16 +0000
commit271949bf140359dd97cbee9ef927ee9280c9f31f (patch)
tree31cd0e175401d2041e23128402c235eb6d2e3117 /sources/core
parent5a0924146201bc577ca170952a21a42464ac7c71 (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')
-rw-r--r--sources/core/VSLamport.java16
-rw-r--r--sources/core/VSMessage.java6
-rw-r--r--sources/core/VSProcess.java152
-rw-r--r--sources/core/VSTask.java13
-rw-r--r--sources/core/VSTime.java6
-rw-r--r--sources/core/VSVectorTime.java52
6 files changed, 191 insertions, 54 deletions
diff --git a/sources/core/VSLamport.java b/sources/core/VSLamport.java
index 157aaae..4c4707f 100644
--- a/sources/core/VSLamport.java
+++ b/sources/core/VSLamport.java
@@ -1,19 +1,23 @@
package core;
-public class VSLamport {
- private long time;
+public class VSLamport implements VSTime {
+ private long globalTime;
private long lamportTime;
- public VSLamport(long time, long lamportTime) {
- this.time = time;
+ public VSLamport(long globalTime, long lamportTime) {
+ this.globalTime = globalTime;
this.lamportTime = lamportTime;
}
- public long getTime() {
- return time;
+ public long getGlobalTime() {
+ return globalTime;
}
public long getLamportTime() {
return lamportTime;
}
+
+ public String toString() {
+ return "(" + lamportTime + ")";
+ }
}
diff --git a/sources/core/VSMessage.java b/sources/core/VSMessage.java
index b3c1d31..466fc4d 100644
--- a/sources/core/VSMessage.java
+++ b/sources/core/VSMessage.java
@@ -10,6 +10,7 @@ public class VSMessage extends VSPrefs implements VSEvent {
private long messageID;
private static long messageCounter;
private long lamportTime;
+ private VSVectorTime vectorTime;
public VSMessage(String protocolClassname) {
this.protocolClassname = protocolClassname;
@@ -31,6 +32,7 @@ public class VSMessage extends VSPrefs implements VSEvent {
public void setSendingProcess(VSProcess sendingProcess) {
this.sendingProcess = sendingProcess;
lamportTime = sendingProcess.getLamportTime();
+ vectorTime = sendingProcess.getVectorTime().getCopy();
}
public VSProcess getSendingProcess() {
@@ -41,6 +43,10 @@ public class VSMessage extends VSPrefs implements VSEvent {
return lamportTime;
}
+ public VSVectorTime getVectorTime() {
+ return vectorTime;
+ }
+
public String toString() {
return "ID: " + messageID;
}
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() {
diff --git a/sources/core/VSTask.java b/sources/core/VSTask.java
index 43b0265..26b2485 100644
--- a/sources/core/VSTask.java
+++ b/sources/core/VSTask.java
@@ -84,13 +84,8 @@ public class VSTask implements Comparable {
final String protocolName = message.getProtocolName();
final String protocolClassname = message.getProtocolClassname();
- final long recvLamportTime = message.getLamportTime() + 1;
- final long lamportTime = process.getLamportTime() + 1;
-
- if (recvLamportTime > lamportTime)
- process.setLamportTime(recvLamportTime);
- else
- process.setLamportTime(lamportTime);
+ process.updateLamportTime(message.getLamportTime()+1);
+ process.updateVectorTime(message.getVectorTime());
Object protocolObj;
@@ -120,10 +115,6 @@ public class VSTask implements Comparable {
private void onProcessEventStart() {
final VSProcessEvent processEvent = (VSProcessEvent) event;
processEvent.onStart(process);
- /*
- if (process.isCrashed())
- process.setLamportTime(process.getLamportTime()-1);
- */
}
public long getTaskTime() {
diff --git a/sources/core/VSTime.java b/sources/core/VSTime.java
new file mode 100644
index 0000000..f35c0dd
--- /dev/null
+++ b/sources/core/VSTime.java
@@ -0,0 +1,6 @@
+package core;
+
+public interface VSTime {
+ public long getGlobalTime();
+ public String toString();
+}
diff --git a/sources/core/VSVectorTime.java b/sources/core/VSVectorTime.java
new file mode 100644
index 0000000..70a62a2
--- /dev/null
+++ b/sources/core/VSVectorTime.java
@@ -0,0 +1,52 @@
+package core;
+
+import java.util.ArrayList;
+
+public class VSVectorTime extends ArrayList<Long> implements VSTime {
+ /* Only needed for painting in the painting panel */
+ private long globalTime;
+
+ public VSVectorTime(long globalTime) {
+ this.globalTime = globalTime;
+ }
+
+ public long[] toLongArray() {
+ final int size = super.size();
+ final long[] arr = new long[size];
+
+ for (int i = 0; i < size; ++i)
+ arr[i] = super.get(i).longValue();
+
+ return arr;
+ }
+
+ public void setGlobalTime(long globalTime) {
+ this.globalTime = globalTime;
+ }
+
+ public long getGlobalTime() {
+ return globalTime;
+ }
+
+ public VSVectorTime getCopy() {
+ final VSVectorTime vectorTime = new VSVectorTime(globalTime);
+ final int size = super.size();
+
+ for (int i = 0; i < size; ++i)
+ vectorTime.add(super.get(i));
+
+ return vectorTime;
+ }
+
+ public String toString() {
+ final int size = super.size();
+ final StringBuffer buffer = new StringBuffer();
+ buffer.append("(");
+
+ for (int i = 0; i < size-1; ++i)
+ buffer.append(super.get(i)+",");
+ buffer.append(super.get(size-1)+")");
+
+ return buffer.toString();
+ }
+}