Permalink
Browse files

Fixed bugs in socket streams

  • Loading branch information...
1 parent 472ffef commit cdbe30e592c7a2089d7077a52874e8bea66cabc3 @pksunkara committed Sep 16, 2011
Showing with 33 additions and 10 deletions.
  1. +0 −1 LamportClock/Agent.java
  2. +0 −1 LamportClock/Pentagon.java
  3. +33 −8 LamportClock/Ranger.java
View
@@ -34,7 +34,6 @@ 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,7 +54,6 @@ public void gatherClientDetails() throws Exception {
}
public void endClients() throws Exception {
- out.writeObject("END");
in.close();
out.close();
}
View
@@ -12,7 +12,7 @@
private static ObjectOutputStream pentagonOutput;
private static OtherClient otherClientList[] = new OtherClient[2];
- private static boolean wait = true;
+ private static OtherClientThread otherClientListThread[] = new OtherClientThread[2];
public static void main(String[] args) throws Exception {
if(args.length!=2) {
@@ -24,27 +24,29 @@ public static void main(String[] args) throws Exception {
serverThread = new ServerThread(args[1]);
pentagonSocket = new Socket("localhost", 5000);
- pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
+ pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
pentagonOutput.writeObject("Ranger " + nodeName + " ready to proceed.");
pentagonOutput.writeObject(args[1]);
for(int i=0; i<2; i++)
otherClientList[i] = new OtherClient();
+ for(int i=0; i<2; i++)
+ otherClientListThread[i] = new OtherClientThread(otherClientList[i]);
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();
+ for(int i=0; i<2; i++)
+ otherClientListThread[i].thread.join();
serverThread.thread.join();
}
@@ -65,8 +67,8 @@ 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());
+ in = new ObjectInputStream(client.getInputStream());
}
}
@@ -82,6 +84,31 @@ public Client(Socket client) throws Exception {
}
}
+ private static class OtherClientThread implements Runnable {
+ public OtherClient client;
+ public Thread thread;
+
+ public OtherClientThread(OtherClient client) throws Exception {
+ this.client = client;
+ thread = new Thread(this);
+ thread.start();
+ }
+
+ public void run() {
+ try {
+ closeStreams();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void closeStreams() throws Exception {
+ client.in.close();
+ client.out.close();
+ client.client.close();
+ }
+ }
+
private static class ClientThread implements Runnable {
public Client client;
public Thread thread;
@@ -94,9 +121,6 @@ public ClientThread(Client client) throws Exception {
public void run() {
try {
- while(wait) {
- Thread.sleep(1000);
- }
closeStreams();
} catch (Exception e) {
e.printStackTrace();
@@ -106,6 +130,7 @@ public void run() {
private void closeStreams() throws Exception {
client.in.close();
client.out.close();
+ client.client.close();
}
}

0 comments on commit cdbe30e

Please sign in to comment.