Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simple socket connection

  • Loading branch information...
commit 21107c1b6982ce8bf4b66cf42c0881aacf48430d 1 parent 361d32e
@pksunkara authored
View
47 LamportClock/Agent.java
@@ -1,32 +1,41 @@
import java.io.*;
+import java.util.*;
+import java.net.*;
public class Agent {
-
- private static String nodeName = "";
-
+ private static String nodeName;
private static FileWriter fileStream;
+ private static Socket pentagonSocket;
+ private static ObjectOutputStream pentagonOutput;
+ private static ObjectInputStream pentagonInput;
- public static void main(String[] args) {
- if(args.length!=1) {
- System.out.println("Usage: Agent <codename>");
+ public static void main(String[] args) throws Exception {
+ if(args.length!=2) {
+ System.out.println("Usage: Agent <codename> <port>");
System.exit(1);
}
nodeName = args[0];
- report("Initialised mission SuperSecret!");
- report("Hacked the super computer!");
- report("Finished mission SuperSecret!");
- }
+ pentagonSocket = new Socket("localhost", 5000);
+ pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
+ pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
- private static void report(String msg) {
- try {
- fileStream = new FileWriter("report", true);
- fileStream.write("Agent " + nodeName + "" + msg + "\n");
- fileStream.close();
- Thread.sleep(2000);
- } catch (Exception e) {
- e.printStackTrace();
- }
+ pentagonOutput.writeObject("Agent " + nodeName + " in place.");
+ pentagonOutput.writeObject("Agent " + nodeName + " ready to proceed.");
+
+ pentagonInput.close();
+ pentagonOutput.close();
+ pentagonSocket.close();
+
+ 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(nodeName + "" + msg + "\n");
+ fileStream.close();
+ Thread.sleep(2000);
+ }
}
View
59 LamportClock/Pentagon.java
@@ -1,4 +1,61 @@
+import java.io.*;
+import java.util.*;
+import java.net.*;
+
public class Pentagon {
- public static void main(String[] args) {
+ private static ServerSocket serverSocket;
+ private static FileWriter fileStream;
+ private static Socket clientList[] = new Socket[3];
+ private static clientThread clientThreadList[] = new clientThread[3];
+
+ public static void main(String[] args) throws Exception {
+ Socket client;
+
+ serverSocket = new ServerSocket(5000);
+ for(int i=0; i<3; i++) {
+ client = serverSocket.accept();
+ clientList[i] = client;
+ clientThreadList[i] = new clientThread(client);
+ }
+
+ for(int i=0; i<3; i++)
+ clientThreadList[i].thread.join();
+ serverSocket.close();
+ }
+
+ private static void report(String msg) throws Exception {
+ fileStream = new FileWriter("report", true);
+ fileStream.write("Pentagon → " + msg + "\n");
+ fileStream.close();
+ }
+
+ private static class clientThread implements Runnable {
+ private Socket client;
+ public Thread thread;
+ private ObjectInputStream in;
+ private ObjectOutputStream out;
+
+ public clientThread(Socket client) throws Exception {
+ this.client = client;
+ in = new ObjectInputStream(client.getInputStream());
+ out = new ObjectOutputStream(client.getOutputStream());
+ thread = new Thread(this);
+ thread.start();
+ }
+
+ public void run() {
+ try {
+ System.out.println((String)in.readObject());
+ System.out.println((String)in.readObject());
+ closeStreams();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void closeStreams() throws Exception {
+ in.close();
+ out.close();
+ }
}
}
View
21 LamportClock/Ranger.java
@@ -1,16 +1,27 @@
import java.io.*;
+import java.util.*;
+import java.net.*;
public class Ranger {
-
- private static String nodeName = "";
-
+ private static String nodeName;
private static FileWriter fileStream;
public static void main(String[] args) {
- if(args.length!=1) {
- System.out.println("Usage: Ranger <codename>");
+ if(args.length!=2) {
+ System.out.println("Usage: Ranger <codename> <port>");
System.exit(1);
}
nodeName = args[0];
+
+ 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(nodeName + "" + msg + "\n");
+ fileStream.close();
+ Thread.sleep(2000);
}
}
View
18 LamportClock/report
@@ -1,9 +1,9 @@
-Agent alpha → Initialised mission SuperSecret!
-Agent beta → Initialised mission SuperSecret!
-Agent charlie → Initialised mission SuperSecret!
-Agent alpha → Hacked the super computer!
-Agent beta → Hacked the super computer!
-Agent charlie → Hacked the super computer!
-Agent alpha → Finished mission SuperSecret!
-Agent beta → Finished mission SuperSecret!
-Agent charlie → Finished mission SuperSecret!
+alpha → Initialising mission SuperSecret!
+beta → Initialising mission SuperSecret!
+charlie → Initialising mission SuperSecret!
+alpha → Hacking the uber super computer!
+beta → Hacking the uber super computer!
+charlie → Hacking the uber super computer!
+alpha → Finishing mission SuperSecret!
+beta → Finishing mission SuperSecret!
+charlie → Finishing mission SuperSecret!
Please sign in to comment.
Something went wrong with that request. Please try again.