Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished Agent team (No Synchronisation)

  • Loading branch information...
commit 843bd970e83da59b0d82ec0903755728df95fea0 1 parent 21107c1
@pksunkara authored
View
25 LamportClock/Agent.java
@@ -9,6 +9,8 @@
private static ObjectOutputStream pentagonOutput;
private static ObjectInputStream pentagonInput;
+ private static OtherClient otherClientList[] = new OtherClient[2];
+
public static void main(String[] args) throws Exception {
if(args.length!=2) {
System.out.println("Usage: Agent <codename> <port>");
@@ -20,22 +22,35 @@ public static void main(String[] args) throws Exception {
pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
- pentagonOutput.writeObject("Agent " + nodeName + " in place.");
pentagonOutput.writeObject("Agent " + nodeName + " ready to proceed.");
+ pentagonOutput.writeObject(args[1]);
- pentagonInput.close();
- pentagonOutput.close();
- pentagonSocket.close();
+ for(int i=0; i<2; i++)
+ otherClientList[i] = new OtherClient();
report("Initialising mission SuperSecret!");
report("Hacking the uber super computer!");
report("Finishing mission SuperSecret!");
+
+ pentagonOutput.writeObject("Agent " + nodeName + " reporting, Mission Accomplished.");
+
+ pentagonInput.close();
+ pentagonOutput.close();
+ pentagonSocket.close();
}
private static void report(String msg) throws Exception {
fileStream = new FileWriter("report", true);
- fileStream.write(nodeName + "" + msg + "\n");
+ fileStream.write("Agent " + nodeName + "" + msg + "\n");
fileStream.close();
Thread.sleep(2000);
}
+
+ private static class OtherClient {
+ private String string;
+
+ public OtherClient() throws Exception {
+ string = (String)pentagonInput.readObject();
+ }
+ }
}
View
49 LamportClock/Pentagon.java
@@ -5,8 +5,8 @@
public class Pentagon {
private static ServerSocket serverSocket;
private static FileWriter fileStream;
- private static Socket clientList[] = new Socket[3];
- private static clientThread clientThreadList[] = new clientThread[3];
+ private static Client clientList[] = new Client[3];
+ private static ClientThread clientThreadList[] = new ClientThread[3];
public static void main(String[] args) throws Exception {
Socket client;
@@ -14,11 +14,15 @@ public static void main(String[] args) throws Exception {
serverSocket = new ServerSocket(5000);
for(int i=0; i<3; i++) {
client = serverSocket.accept();
- clientList[i] = client;
- clientThreadList[i] = new clientThread(client);
+ clientList[i] = new Client(client);
}
for(int i=0; i<3; i++)
+ 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();
serverSocket.close();
}
@@ -29,24 +33,41 @@ private static void report(String msg) throws Exception {
fileStream.close();
}
- private static class clientThread implements Runnable {
- private Socket client;
- public Thread thread;
- private ObjectInputStream in;
- private ObjectOutputStream out;
+ private static class Client {
+ public Socket client;
+ public String port, host;
+ public ObjectInputStream in;
+ public ObjectOutputStream out;
- public clientThread(Socket client) throws Exception {
+ public Client(Socket client) throws Exception {
this.client = client;
+ host = client.getInetAddress().getHostName();
in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(client.getOutputStream());
+ }
+
+ public void gatherClientDetails() throws Exception {
+ report((String)in.readObject());
+ port = (String)in.readObject();
+ }
+ }
+
+ 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 {
- System.out.println((String)in.readObject());
- System.out.println((String)in.readObject());
+ for(int i=0; i<3; i++)
+ if(this!=clientThreadList[i])
+ client.out.writeObject(clientList[i].host + " " + clientList[i].port);
+ report((String)client.in.readObject());
closeStreams();
} catch (Exception e) {
e.printStackTrace();
@@ -54,8 +75,8 @@ public void run() {
}
private void closeStreams() throws Exception {
- in.close();
- out.close();
+ client.in.close();
+ client.out.close();
}
}
}
View
2  LamportClock/Ranger.java
@@ -6,7 +6,7 @@
private static String nodeName;
private static FileWriter fileStream;
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
if(args.length!=2) {
System.out.println("Usage: Ranger <codename> <port>");
System.exit(1);
View
15 LamportClock/report_agent
@@ -0,0 +1,15 @@
+Pentagon → Agent alpha ready to proceed.
+Pentagon → Agent beta ready to proceed.
+Pentagon → Agent charlie ready to proceed.
+Agent beta → Initialising mission SuperSecret!
+Agent charlie → Initialising mission SuperSecret!
+Agent alpha → Initialising mission SuperSecret!
+Agent beta → Hacking the uber super computer!
+Agent charlie → Hacking the uber super computer!
+Agent alpha → Hacking the uber super computer!
+Agent beta → Finishing mission SuperSecret!
+Agent charlie → Finishing mission SuperSecret!
+Agent alpha → Finishing mission SuperSecret!
+Pentagon → Agent beta reporting, Mission Accomplished.
+Pentagon → Agent charlie reporting, Mission Accomplished.
+Pentagon → Agent alpha reporting, Mission Accomplished.
Please sign in to comment.
Something went wrong with that request. Please try again.