summaryrefslogtreecommitdiff
path: root/sources/protocols
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/protocols
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/protocols')
-rw-r--r--sources/protocols/BerkelyTimeProtocol.java126
-rw-r--r--sources/protocols/BroadcastSturmProtocol.java2
-rw-r--r--sources/protocols/DummyProtocol.java2
-rw-r--r--sources/protocols/ExternalTimeSyncProtocol.java2
-rw-r--r--sources/protocols/InternalTimeSyncProtocol.java2
-rw-r--r--sources/protocols/PingPongProtocol.java3
-rw-r--r--sources/protocols/RegisteredProtocols.java12
-rw-r--r--sources/protocols/VSProtocol.java23
8 files changed, 151 insertions, 21 deletions
diff --git a/sources/protocols/BerkelyTimeProtocol.java b/sources/protocols/BerkelyTimeProtocol.java
new file mode 100644
index 0000000..ea81cba
--- /dev/null
+++ b/sources/protocols/BerkelyTimeProtocol.java
@@ -0,0 +1,126 @@
+package protocols;
+
+import prefs.VSPrefs;
+import core.VSMessage;
+
+import java.util.HashMap;
+
+public class BerkelyTimeProtocol extends VSProtocol {
+ /* Berkely Server variables */
+
+ /* Integer: Process ID, Long: Local time of the process */
+ private HashMap<Integer,Long> processTimes = new HashMap<Integer,Long>();
+ /* Integer: Process ID, Long: Time of receiving the response from the process */
+ private HashMap<Integer,Long> recvTimes = new HashMap<Integer,Long>();
+ /* Integer: Process ID, Long: Calculated process times (using the RTT) */
+ private HashMap<Integer,Long> realTimesRTT = new HashMap<Integer,Long>();
+ /* Time the request/response has started */
+ private long requestTime;
+
+ /* Berkely Client vairables */
+
+ protected void onInit() {
+ setProtocolClassname(getClass().toString());
+
+ /* Those prefs are editable through the VSProtocol VSEditor GUI. t_min and t_max in milliseconds */
+ setInteger("numProcesses", getNumProcesses()-1);
+ }
+
+ protected void onClientReset() {
+ processTimes.clear();
+ recvTimes.clear();
+ realTimesRTT.clear();
+ }
+
+ protected void onClientStart() {
+ requestTime = process.getTime();
+ VSMessage message = new VSMessage(getProtocolClassname());
+ message.setBoolean("isRequest", true);
+ sendMessage(message);
+ }
+
+ protected void onClientRecv(VSMessage recvMessage) {
+ /* Ignore all protocol messages which are not a response message, e.g. itself */
+ if (!recvMessage.getBoolean("isResponse"))
+ return;
+
+ Integer processID = new Integer(recvMessage.getInteger("processID"));
+ Long time = new Long(recvMessage.getLong("time"));
+
+ processTimes.put(processID, time);
+ recvTimes.put(processID, new Long(process.getTime()));
+
+ /* All processes have comitted the response */
+ if (processTimes.size() == getInteger("numProcesses")) {
+ long avgTime = calculateAverageTime();
+ /* Set the local's process time to the new avg reference time */
+ process.setTime(avgTime);
+ /* Tell all other processes what to do in order to justify their times */
+ sendJustifyRequests(avgTime);
+ /* Start "clean" next time */
+ onClientReset();
+ }
+ }
+
+ /**
+ * Calculate the new average time
+ */
+ private long calculateAverageTime() {
+ long sum = 0;
+ for (Integer processID : processTimes.keySet()) {
+ Long localTime = processTimes.get(processID);
+ Long recvTime = recvTimes.get(processID);
+ long rtt = recvTime.longValue() - requestTime;
+ long realProcessTime = localTime + (long) (rtt / 2);
+ realTimesRTT.put(processID, new Long(realProcessTime));
+ sum += realProcessTime;
+ }
+ /* Include the time of the local process */
+ sum += process.getTime();
+ return (long) sum / (1 + getInteger("numProcesses"));
+ }
+
+ /**
+ * Sends to all clients a value to justify their local clocks
+ */
+ private void sendJustifyRequests(long avgTime) {
+ for (Integer processID : processTimes.keySet()) {
+ long realProcessTime = realTimesRTT.get(processID).longValue();
+ long diff = avgTime - realProcessTime;
+ VSMessage message = new VSMessage(getProtocolClassname());
+ message.setBoolean("isJustify", true);
+ message.setLong("timeDiff", diff);
+ message.setInteger("receiverProcessID", processID);
+ sendMessage(message);
+ }
+ }
+
+ protected void onServerReset() {
+ }
+
+ protected void onServerRecv(VSMessage recvMessage) {
+ if (recvMessage.getBoolean("isRequest")) {
+ VSMessage message = new VSMessage(getProtocolClassname());
+ message.setInteger("processID", process.getProcessID());
+ message.setLong("time", process.getTime());
+ message.setBoolean("isResponse", true);
+ sendMessage(message);
+
+ } else if (recvMessage.getBoolean("isJustify")) {
+ /* Check if it's "my" justify message */
+ if (recvMessage.getInteger("receiverProcessID") != process.getProcessID())
+ return;
+
+ long timeDiff = recvMessage.getLong("timeDiff");
+ long recvTime = process.getTime();
+ long newTime = process.getTime() + timeDiff;
+ logg("Neue Zeit: " + newTime);
+
+ process.setTime(newTime);
+ }
+ }
+
+ public String toString() {
+ return super.toString();
+ }
+}
diff --git a/sources/protocols/BroadcastSturmProtocol.java b/sources/protocols/BroadcastSturmProtocol.java
index 3940e5f..6ce95e8 100644
--- a/sources/protocols/BroadcastSturmProtocol.java
+++ b/sources/protocols/BroadcastSturmProtocol.java
@@ -9,7 +9,7 @@ public class BroadcastSturmProtocol extends VSProtocol {
private ArrayList<VSMessage> sentMessages;
private static int broadcastCount;
- public BroadcastSturmProtocol() {
+ protected void onInit() {
setProtocolClassname(getClass().toString());
sentMessages = new ArrayList<VSMessage>();
}
diff --git a/sources/protocols/DummyProtocol.java b/sources/protocols/DummyProtocol.java
index 8c9009c..9a531e1 100644
--- a/sources/protocols/DummyProtocol.java
+++ b/sources/protocols/DummyProtocol.java
@@ -3,7 +3,7 @@ package protocols;
import core.VSMessage;
public class DummyProtocol extends VSProtocol {
- public DummyProtocol() {
+ protected void onInit() {
setProtocolClassname(getClass().toString());
}
diff --git a/sources/protocols/ExternalTimeSyncProtocol.java b/sources/protocols/ExternalTimeSyncProtocol.java
index 33a60ad..b6ee73f 100644
--- a/sources/protocols/ExternalTimeSyncProtocol.java
+++ b/sources/protocols/ExternalTimeSyncProtocol.java
@@ -7,7 +7,7 @@ public class ExternalTimeSyncProtocol extends VSProtocol {
private long requestTime;
private boolean waitingForResponse;
- public ExternalTimeSyncProtocol() {
+ protected void onInit() {
setProtocolClassname(getClass().toString());
}
diff --git a/sources/protocols/InternalTimeSyncProtocol.java b/sources/protocols/InternalTimeSyncProtocol.java
index 48b57e0..961cf65 100644
--- a/sources/protocols/InternalTimeSyncProtocol.java
+++ b/sources/protocols/InternalTimeSyncProtocol.java
@@ -6,7 +6,7 @@ import core.VSMessage;
public class InternalTimeSyncProtocol extends VSProtocol {
private boolean waitingForResponse;
- public InternalTimeSyncProtocol() {
+ protected void onInit() {
setProtocolClassname(getClass().toString());
/* Those prefs are editable through the VSProtocol VSEditor GUI. t_min and t_max in milliseconds */
diff --git a/sources/protocols/PingPongProtocol.java b/sources/protocols/PingPongProtocol.java
index 6244b9a..ee54a3c 100644
--- a/sources/protocols/PingPongProtocol.java
+++ b/sources/protocols/PingPongProtocol.java
@@ -7,7 +7,7 @@ public class PingPongProtocol extends VSProtocol {
private int clientCounter;
private int serverCounter;
- public PingPongProtocol() {
+ protected void onInit() {
setProtocolClassname(getClass().toString());
}
@@ -16,7 +16,6 @@ public class PingPongProtocol extends VSProtocol {
}
protected void onClientStart() {
-
VSMessage message = new VSMessage(getProtocolClassname());
message.setBoolean("fromClient", true);
message.setInteger("counter", ++clientCounter);
diff --git a/sources/protocols/RegisteredProtocols.java b/sources/protocols/RegisteredProtocols.java
index 542d686..b08bb87 100644
--- a/sources/protocols/RegisteredProtocols.java
+++ b/sources/protocols/RegisteredProtocols.java
@@ -11,7 +11,7 @@ public final class RegisteredProtocols {
private static HashMap<String,String> protocolNames;
private static VSPrefs prefs;
- public static void initialize(VSPrefs prefs_) {
+ public static void init(VSPrefs prefs_) {
prefs = prefs_;
protocolNames = new HashMap<String, String>();
protocolClassnames = new HashMap<String, String>();
@@ -21,6 +21,7 @@ public final class RegisteredProtocols {
registerProtocol("protocols.ExternalTimeSyncProtocol");
registerProtocol("protocols.InternalTimeSyncProtocol");
registerProtocol("protocols.BroadcastSturmProtocol");
+ registerProtocol("protocols.BerkelyTimeProtocol");
}
public static Vector<String> getProtocolNames() {
@@ -43,12 +44,15 @@ public final class RegisteredProtocols {
return protocolNames.get(protocolClassname);
}
- public static VSProtocol getProtocolInstanceByName(String protocolName) {
+ public static VSProtocol getProtocolInstanceByName(String protocolName, VSProcess process) {
final String protocolClassname = protocolClassnames.get(protocolName);
final Object protocolObj = new VSClassLoader().newInstance(protocolClassname);
- if (protocolObj instanceof VSProtocol)
- return (VSProtocol) protocolObj;
+ if (protocolObj instanceof VSProtocol) {
+ VSProtocol protocol = (VSProtocol) protocolObj;
+ protocol.init(process);
+ return protocol;
+ }
return null;
}
diff --git a/sources/protocols/VSProtocol.java b/sources/protocols/VSProtocol.java
index b66f034..4f34195 100644
--- a/sources/protocols/VSProtocol.java
+++ b/sources/protocols/VSProtocol.java
@@ -11,9 +11,12 @@ abstract public class VSProtocol extends VSPrefs implements VSEvent {
private boolean isClient;
protected VSProcess process;
private boolean currentContextIsServer;
- private boolean lamportIncreased;
- public VSProtocol() {
+ public void init(VSProcess process) {
+ this.process = process;
+ this.prefs = process.getPrefs();
+
+ onInit();
}
protected final void setProtocolClassname(String protocolClassname) {
@@ -36,7 +39,8 @@ abstract public class VSProtocol extends VSPrefs implements VSEvent {
}
protected void sendMessage(VSMessage message) {
- process.setLamportTime(process.getLamportTime()+1);
+ process.increaseLamportTime();
+ process.increaseVectorTime();
message.setSendingProcess(process);
process.sendMessage(message);
}
@@ -75,14 +79,6 @@ abstract public class VSProtocol extends VSPrefs implements VSEvent {
this.isClient = isClient;
}
- public final void setVSPrefs(VSPrefs prefs) {
- this.prefs = prefs;
- }
-
- public final void setProcess(VSProcess process) {
- this.process = process;
- }
-
public void reset() {
if (isServer) {
currentContextIsServer = true;
@@ -95,6 +91,7 @@ abstract public class VSProtocol extends VSPrefs implements VSEvent {
}
}
+ abstract protected void onInit();
abstract protected void onClientStart();
abstract protected void onClientReset();
abstract protected void onClientRecv(VSMessage message);
@@ -110,6 +107,10 @@ abstract public class VSProtocol extends VSPrefs implements VSEvent {
return protocol.getID() == getID();
}
+ protected int getNumProcesses() {
+ return process.getSimulationPanel().getNumProcesses();
+ }
+
public String toString() {
String type = new String();