Skip to content
Snippets Groups Projects
Commit 3113e1df authored by ik1g19's avatar ik1g19
Browse files

connections close correctly

parent 90bb8a1f
Branches
No related tags found
No related merge requests found
......@@ -117,22 +117,29 @@ public class Controller extends Server {
file.setStoreAcksQuota(r);
send("STORE_TO " + ports, client);
}
else if (command.equals("STORE_ACK")) {
String filename = args[1];
DStoreFile file = fileIndex.get(filename);
file.storeAck();
Boolean fileStored = false;
while (!fileStored) {
if (file.ackCheck()) {
file.setStoreInProgress(false);
file.setStoreComplete(true);
fileStored = true;
threadIDOutput("Store of file " + filename + " complete");
send("STORE_COMPLETE", client);
}
}
}
else if (command.equals("STORE_ACK")) {
String filename = args[1];
DStoreFile file = fileIndex.get(filename);
file.storeAck();
}
else if (command.equals("LOAD")) {
String filename = args[1];
......
......@@ -104,6 +104,9 @@ public class DStore extends Server {
String response = readSocket(client);
if (response.equals("ACK")) threadIDOutput("Successfully joined Controller");
closeConnection(client);
}
else if (command.equals("STORE")) {
......@@ -125,6 +128,8 @@ public class DStore extends Server {
if (controller != null) {
send("STORE_ACK " + filename, controller);
} else threadIDErr("Unable to connect to Controller");
closeConnection(controller);
}
else if (command.equals("LOAD_DATA")) {
......
......@@ -21,9 +21,13 @@ public abstract class Server {
public void run() {
try {
threadIDOutput("Connection Accepted");
Boolean closed = false;
while (!closed) {
String request = readSocket(client);
handleRequest(request, client);
client.close();
if (request == null) closed = true;
else handleRequest(request, client);
}
closeConnection(client);
} catch (Exception e) {
threadIDErr("Exception thrown: " + e.getMessage());
}
......@@ -98,4 +102,10 @@ public abstract class Server {
System.err.println("Thread ID: " + Thread.currentThread().getId() + " " + output);
}
protected void closeConnection(Socket client) {
try {client.close(); threadIDOutput("Connection Closed");} catch (IOException e) {threadIDErr(e.getMessage());}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment