Permalink
Browse files

Made some progress in Ranger.java

  • Loading branch information...
1 parent 13aee78 commit 472ffefd0dec282a55304a897e0ccb5f670bd401 @pksunkara committed Sep 15, 2011
Showing with 107 additions and 9 deletions.
  1. +4 −0 LamportClock/Agent.java
  2. +9 −7 LamportClock/Pentagon.java
  3. +94 −2 LamportClock/Ranger.java
View
4 LamportClock/Agent.java
@@ -5,9 +5,11 @@
public class Agent {
private static String nodeName;
private static FileWriter fileStream;
+
private static Socket pentagonSocket;
private static ObjectOutputStream pentagonOutput;
private static ObjectInputStream pentagonInput;
+
private static OtherClient otherClientList[] = new OtherClient[2];
public static void main(String[] args) throws Exception {
@@ -30,7 +32,9 @@ public static void main(String[] args) throws Exception {
report("Initialising mission SuperSecret!");
report("Hacking the uber super computer!");
report("Finishing mission SuperSecret!");
+
pentagonOutput.writeObject("Agent " + nodeName + " reporting, Mission Accomplished.");
+ pentagonInput.readObject();
pentagonInput.close();
pentagonOutput.close();
View
16 LamportClock/Pentagon.java
@@ -21,9 +21,11 @@ public static void main(String[] args) throws Exception {
clientList[i].gatherClientDetails();
for(int i=0; i<3; i++)
clientThreadList[i] = new ClientThread(clientList[i]);
-
for(int i=0; i<3; i++)
clientThreadList[i].thread.join();
+ for(int i=0; i<3; i++)
+ clientList[i].endClients();
+
serverSocket.close();
}
@@ -50,6 +52,12 @@ public void gatherClientDetails() throws Exception {
report((String)in.readObject());
port = (String)in.readObject();
}
+
+ public void endClients() throws Exception {
+ out.writeObject("END");
+ in.close();
+ out.close();
+ }
}
private static class ClientThread implements Runnable {
@@ -68,15 +76,9 @@ public void run() {
if(this!=clientThreadList[i])
client.out.writeObject(clientList[i].host + " " + clientList[i].port);
report((String)client.in.readObject());
- closeStreams();
} catch (Exception e) {
e.printStackTrace();
}
}
-
- private void closeStreams() throws Exception {
- client.in.close();
- client.out.close();
- }
}
}
View
96 LamportClock/Ranger.java
@@ -5,10 +5,14 @@
public class Ranger {
private static String nodeName;
private static FileWriter fileStream;
+ private static ServerThread serverThread;
+
private static Socket pentagonSocket;
- private static ObjectOutputStream pentagonOutput;
private static ObjectInputStream pentagonInput;
+ private static ObjectOutputStream pentagonOutput;
+
private static OtherClient otherClientList[] = new OtherClient[2];
+ private static boolean wait = true;
public static void main(String[] args) throws Exception {
if(args.length!=2) {
@@ -17,9 +21,11 @@ public static void main(String[] args) throws Exception {
}
nodeName = args[0];
+ serverThread = new ServerThread(args[1]);
+
pentagonSocket = new Socket("localhost", 5000);
- pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
+ pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
pentagonOutput.writeObject("Ranger " + nodeName + " ready to proceed.");
pentagonOutput.writeObject(args[1]);
@@ -30,11 +36,16 @@ public static void main(String[] args) throws Exception {
report("Initialising mission SuperSecret!");
report("Hacking the uber super computer!");
report("Finishing mission SuperSecret!");
+
pentagonOutput.writeObject("Ranger " + nodeName + " reporting, Mission Accomplished.");
+ pentagonInput.readObject();
+ wait = false;
pentagonInput.close();
pentagonOutput.close();
pentagonSocket.close();
+
+ serverThread.thread.join();
}
private static void report(String msg) throws Exception {
@@ -46,9 +57,90 @@ private static void report(String msg) throws Exception {
private static class OtherClient {
private String string;
+ private Socket client;
+ private ObjectInputStream in;
+ private ObjectOutputStream out;
public OtherClient() throws Exception {
string = (String)pentagonInput.readObject();
+ String tmp[] = string.split(" ");
+ client = new Socket(tmp[0], Integer.parseInt(tmp[1]));
+ in = new ObjectInputStream(client.getInputStream());
+ out = new ObjectOutputStream(client.getOutputStream());
+ }
+ }
+
+ private static class Client {
+ public Socket client;
+ public ObjectInputStream in;
+ public ObjectOutputStream out;
+
+ public Client(Socket client) throws Exception {
+ this.client = client;
+ in = new ObjectInputStream(client.getInputStream());
+ out = new ObjectOutputStream(client.getOutputStream());
+ }
+ }
+
+ private static class ClientThread implements Runnable {
+ public Client client;
+ public Thread thread;
+
+ public ClientThread(Client client) throws Exception {
+ this.client = client;
+ thread = new Thread(this);
+ thread.start();
+ }
+
+ public void run() {
+ try {
+ while(wait) {
+ Thread.sleep(1000);
+ }
+ closeStreams();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void closeStreams() throws Exception {
+ client.in.close();
+ client.out.close();
+ }
+ }
+
+ private static class ServerThread implements Runnable {
+ private ServerSocket server;
+ public Thread thread;
+ public Client clientList[] = new Client[2];
+ public ClientThread clientListThread[] = new ClientThread[2];
+
+ public ServerThread(String port) throws Exception {
+ server = new ServerSocket(Integer.parseInt(port));
+ thread = new Thread(this);
+ thread.start();
+ }
+
+ public void run() {
+ Socket client;
+ try {
+ for(int i=0; i<2; i++) {
+ client = server.accept();
+ clientList[i] = new Client(client);
+ }
+ for(int i=0; i<2; i++)
+ clientListThread[i] = new ClientThread(clientList[i]);
+ for(int i=0; i<2; i++)
+ clientListThread[i].thread.join();
+ 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);
}
}
}

0 comments on commit 472ffef

Please sign in to comment.