diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-15 23:08:33 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-15 23:08:33 +0000 |
| commit | d4c1ddcc90c1e2e8660598fc36b3772d2bff6816 (patch) | |
| tree | 28a0afc255e42f92adbca0d102e785301bc43a58 /sources/protocols/InternalTimeSyncProtocol.java | |
| parent | 61599471a5978c1521b9c89c044ac2ce9a88c398 (diff) | |
1 Moved the stuff to trunk!
Diffstat (limited to 'sources/protocols/InternalTimeSyncProtocol.java')
| -rw-r--r-- | sources/protocols/InternalTimeSyncProtocol.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/sources/protocols/InternalTimeSyncProtocol.java b/sources/protocols/InternalTimeSyncProtocol.java new file mode 100644 index 0000000..48b57e0 --- /dev/null +++ b/sources/protocols/InternalTimeSyncProtocol.java @@ -0,0 +1,69 @@ +package protocols; + +import prefs.VSPrefs; +import core.VSMessage; + +public class InternalTimeSyncProtocol extends VSProtocol { + private boolean waitingForResponse; + + public InternalTimeSyncProtocol() { + setProtocolClassname(getClass().toString()); + + /* Those prefs are editable through the VSProtocol VSEditor GUI. t_min and t_max in milliseconds */ + setLong("t_min", 1000); + setLong("t_max", 5000); + } + + protected void onClientReset() { + } + + protected void onClientStart() { + waitingForResponse = true; + + /* Multicast message to all processes */ + VSMessage message = new VSMessage(getProtocolClassname()); + message.setBoolean("isClientRequest", true); + sendMessage(message); + } + + protected void onClientRecv(VSMessage recvMessage) { + /* Ignore all protocol messages which are not a response message, e.g. itself */ + if (!recvMessage.getBoolean("isServerResponse")) + return; + + if (waitingForResponse) + waitingForResponse = false; + else + return; + + long tMax = getLong("t_max"); + long tMin = getLong("t_min"); + long serverTime = recvMessage.getLong("time"); + long newTime = serverTime + (long) ((tMax + tMin) / 2 ); + + logg("Server Zeit: " + serverTime + "; (t_min,t_max): (" + tMin + "," + tMax + + "); Alte Zeit: " + process.getTime() + "; Neue Zeit: " + newTime + + "; Offset: " + (process.getTime() - newTime)); + + process.setTime(newTime); + } + + protected void onServerReset() { + } + + protected void onServerRecv(VSMessage recvMessage) { + /* Ignore all protocol messages which are not a request message, e.g. itself */ + if (!recvMessage.getBoolean("isClientRequest")) + return; + + /* Multicast message to all processes */ + VSMessage message = new VSMessage(getProtocolClassname()); + message.setLong("time", process.getTime()); + message.setBoolean("isServerResponse", true); + sendMessage(message); + } + + public String toString() { + return super.toString(); + } +} |
