Permalink
Browse files

Finished Lamport Clock Sync

  • Loading branch information...
1 parent d6dc5ea commit 8bf7596a16b6f7f69d31f0cef11e381bdca3d07d @pksunkara committed Sep 16, 2011
Showing with 71 additions and 42 deletions.
  1. +1 −0 LamportClock/Agent.java
  2. +1 −0 LamportClock/Pentagon.java
  3. +54 −42 LamportClock/Ranger.java
  4. +15 −0 LamportClock/report_ranger
View
@@ -34,6 +34,7 @@ public static void main(String[] args) throws Exception {
report("Finishing mission SuperSecret!");
pentagonOutput.writeObject("Agent " + nodeName + " reporting, Mission Accomplished.");
+ pentagonInput.readObject();
pentagonInput.close();
pentagonOutput.close();
@@ -54,6 +54,7 @@ public void gatherClientDetails() throws Exception {
}
public void endClients() throws Exception {
+ out.writeObject("END");
in.close();
out.close();
}
View
@@ -45,18 +45,17 @@ public static void main(String[] args) throws Exception {
serverThread.thread.join();
pentagonOutput.writeObject("Ranger " + nodeName + " reporting, Mission Accomplished.");
+ pentagonInput.readObject();
+
+ for(int i=0; i<2; i++)
+ serverThread.clientList[i].closeStreams();
+ serverThread.server.close();
pentagonInput.close();
pentagonOutput.close();
pentagonSocket.close();
}
- private static void executeCriticalSection() throws Exception {
- report("Initialising mission SuperSecret!");
- report("Hacking the uber super computer!");
- report("Finishing mission SuperSecret!");
- }
-
private static void report(String msg) throws Exception {
fileStream = new FileWriter("report", true);
fileStream.write("Ranger " + nodeName + "" + msg + "\n");
@@ -66,17 +65,33 @@ private static void report(String msg) throws Exception {
private static void requestQueueInsert(Request r) {
Request tmp[] = new Request[3];
+ boolean done = false;
for(int i=0, j=0; i<3; i++) {
- if(requestQueue[i]!=null && ((requestQueue[i].timeStamp<r.timeStamp) || (requestQueue[i].timeStamp==r.timeStamp && requestQueue[i].nodePort<r.nodePort))) {
- tmp[j++] = requestQueue[i];
+ if(!done) {
+ if(requestQueue[i]!=null && ((requestQueue[i].timeStamp<r.timeStamp) || (requestQueue[i].timeStamp==r.timeStamp && requestQueue[i].nodePort<r.nodePort))) {
+ tmp[j++] = requestQueue[i];
+ } else {
+ tmp[j++] = r;
+ done = true;
+ i--;
+ }
} else {
- tmp[j++] = r;
- break;
+ if(requestQueue[i]!=null)
+ tmp[j++] = requestQueue[i];
}
}
requestQueue = tmp;
}
+// private static void printRequestQueue() {
+// System.out.println("RQ:");
+// for(int i=0; i<3; i++) {
+// if(requestQueue[i]!=null) {
+// System.out.println("\t"+i+" "+requestQueue[i].nodePort+" "+requestQueue[i].timeStamp);
+// }
+// }
+// }
+
private static void requestQueueRelease(int n) {
Request tmp[] = new Request[3];
for(int i=0, j=0; i<3; i++) {
@@ -99,46 +114,50 @@ public Request(String timeStamp, String nodePort) {
private static class Clock {
public int timeStamp;
- public int ack;
public Clock() throws Exception {
timeStamp = 0;
- ack = 0;
+ }
+
+ synchronized public void executeCriticalSection() throws Exception {
+ int portNumber = Integer.parseInt(port);
+ while(requestQueue[0].nodePort!=portNumber) {
+ wait();
+ }
+ report("Initialising mission SuperSecret!");
+ report("Hacking the uber super computer!");
+ report("Finishing mission SuperSecret!");
}
synchronized public String event(String msg, ObjectOutputStream out) throws Exception {
String tmp[] = msg.split(" ");
- if(tmp[0]=="request") {
+ if(tmp[0].equals("request")) {
requestQueueInsert(new Request(tmp[1], tmp[2]));
- int newTimeStamp = Integer.parseInt(tmp[1]);
- if(newTimeStamp > timeStamp)
- timeStamp = newTimeStamp;
- timeStamp++;
+ syncTimeStamp(tmp[1]);
out.writeObject("ack " + timeStamp + " " + port);
return "";
- } else if(tmp[0]=="release") {
+ } else if(tmp[0].equals("release")) {
requestQueueRelease(Integer.parseInt(tmp[2]));
- int newTimeStamp = Integer.parseInt(tmp[1]);
- if(newTimeStamp > timeStamp)
- timeStamp = newTimeStamp;
- timeStamp++;
+ syncTimeStamp(tmp[1]);
+ notify();
return "";
- } else if(tmp[0]=="ask") {
+ } else if(tmp[0].equals("ask")) {
requestQueueInsert(new Request(""+timeStamp, port));
timeStamp++;
return "request " + (timeStamp-1) + " " + port;
- } else if(tmp[0]=="ack") {
- int newTimeStamp = Integer.parseInt(tmp[1]);
- if(newTimeStamp > timeStamp)
- timeStamp = newTimeStamp;
- timeStamp++;
- if(++ack==2) {
- timeStamp++;
- return "release " + timeStamp + " " + port;
- }
+ } else if(tmp[0].equals("ack")) {
+ syncTimeStamp(tmp[1]);
+ return "release " + timeStamp + " " + port;
}
return "";
}
+
+ private void syncTimeStamp(String time) {
+ int newTimeStamp = Integer.parseInt(time);
+ if(newTimeStamp > timeStamp)
+ timeStamp = newTimeStamp;
+ timeStamp++;
+ }
}
private static class OtherClient {
@@ -185,7 +204,7 @@ public OtherClientThread(OtherClient client) throws Exception {
public void run() {
try {
- for(int i=0; i<4; i++)
+ for(int i=0; i<2; i++)
clock.event((String)client.in.readObject(), client.out);
closeStreams();
} catch (Exception e) {
@@ -218,26 +237,19 @@ public void run() {
client = server.accept();
clientList[i] = new Client(client);
}
+
String message = clock.event("ask", clientList[0].out);
for(int i=0; i<2; i++)
clientList[i].out.writeObject(message);
for(int i=0; i<2; i++)
message = clock.event((String)clientList[i].in.readObject(), clientList[0].out);
- executeCriticalSection();
+ clock.executeCriticalSection();
requestQueueRelease(Integer.parseInt(port));
for(int i=0; i<2; i++)
clientList[i].out.writeObject(message);
- for(int i=0; i<2; i++)
- clientList[i].closeStreams();
- server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
-
- public void writeObject(String msg) throws Exception {
- for(int i=0; i<2; i++)
- clientList[i].out.writeObject(msg);
- }
}
}
View
@@ -0,0 +1,15 @@
+Pentagon → Ranger alpha ready to proceed.
+Pentagon → Ranger beta ready to proceed.
+Pentagon → Ranger charlie ready to proceed.
+Ranger alpha → Initialising mission SuperSecret!
+Ranger alpha → Hacking the uber super computer!
+Ranger alpha → Finishing mission SuperSecret!
+Ranger charlie → Initialising mission SuperSecret!
+Ranger charlie → Hacking the uber super computer!
+Ranger charlie → Finishing mission SuperSecret!
+Ranger beta → Initialising mission SuperSecret!
+Ranger beta → Hacking the uber super computer!
+Ranger beta → Finishing mission SuperSecret!
+Pentagon → Ranger beta reporting, Mission Accomplished.
+Pentagon → Ranger alpha reporting, Mission Accomplished.
+Pentagon → Ranger charlie reporting, Mission Accomplished.

0 comments on commit 8bf7596

Please sign in to comment.