Skip to content
Snippets Groups Projects
Commit 6474bf19 authored by Ramkumar, Ragulan (UG - Comp Sci & Elec Eng)'s avatar Ramkumar, Ragulan (UG - Comp Sci & Elec Eng)
Browse files

Update Server/ClientThread.java

parent b75d836b
No related branches found
No related tags found
No related merge requests found
...@@ -27,58 +27,69 @@ public class ClientThread implements Runnable { ...@@ -27,58 +27,69 @@ public class ClientThread implements Runnable {
while (true) { while (true) {
String message = this.clientInput.readLine(); String message = this.clientInput.readLine();
message = message.toUpperCase(); message = message.toUpperCase();
if (message.equals("HELLO")) { String response_code = message.split("\\s+")[0];
this.UUID = java.util.UUID.randomUUID(); switch (response_code) {
Server.UUIDMap.put(this.UUID.toString(), this); case "HELLO":
send("WELCOME"); this.UUID = java.util.UUID.randomUUID();
send("UUID " + this.UUID); Server.UUIDMap.put(this.UUID.toString(), this);
Server.clientQueue.add(getClientObject()); send("WELCOME");
if (Server.clientQueue.size() == 1) { // only person in queue send("UUID " + this.UUID);
send("NEXT"); // send client straight to resource Server.clientQueue.add(getClientObject());
} else { if (Server.clientQueue.size() == 1) { // only person in queue
// send position and add to queue send("NEXT"); // send client straight to resource
send("POSITION " + getPosition()); } else {
} // send position and add to queue
send(""); send("POSITION " + getPosition());
} else if (message.equals("POSITION")) { }
send("POSITION " + getPosition()); send("");
send(""); break;
} else if (message.startsWith("UUID ")) { case "POSITION":
String UUID = message.substring(5, message.length()).toLowerCase();
if (Server.UUIDMap.containsKey(UUID)) {
this.client = Server.UUIDMap.get(UUID);
send("WELCOME_BACK");
send("POSITION " + getPosition()); send("POSITION " + getPosition());
} else {
send("INCORRECT_UUID");
send("Terminating connection");
send(""); send("");
serverOutput.close(); break;
}
send(""); case "UUID":
} else if (message.equals("GOODBYE")) { String UUID = message.substring(5, message.length()).toLowerCase();
send("GOODBYE"); if (Server.UUIDMap.containsKey(UUID)) {
send(""); this.client = Server.UUIDMap.get(UUID);
Server.clientQueue.remove(getClientObject()); send("WELCOME_BACK");
Server.UUIDMap.remove(getClientObject().getUUID().toString()); send("POSITION " + getPosition());
serverOutput.close(); } else {
} else if (message.equals("DONE")) { send("INCORRECT_UUID");
if (Server.clientQueue.indexOf(getClientObject()) == 0) { send("Terminating connection");
send("Your connection is now being terminated."); send("");
serverOutput.close();
}
send("");
break;
case "GOODBYE":
send("GOODBYE"); send("GOODBYE");
send(""); send("");
removeClientFromServer(getClientObject());
serverOutput.close(); serverOutput.close();
Server.clientQueue.remove(getClientObject()); break;
} else {
send("It's not your turn. To exit the queue send GOODBYE."); case "DONE":
if (Server.clientQueue.indexOf(getClientObject()) == 0) {
send("Your connection is now being terminated.");
send("GOODBYE");
send("");
removeClientFromServer(getClientObject());
} else {
send("It's not your turn. To exit the queue send GOODBYE.");
send("");
}
break;
default:
send("UNKNOWN_COMMAND");
send(""); send("");
}
} else {
send("UNKNOWN_COMMAND");
send("");
} }
} }
} catch (Exception e) { } catch (
Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
serverOutput.close(); serverOutput.close();
...@@ -102,6 +113,11 @@ public class ClientThread implements Runnable { ...@@ -102,6 +113,11 @@ public class ClientThread implements Runnable {
serverOutput.println(s); serverOutput.println(s);
} }
private void removeClientFromServer(ClientThread client) {
Server.clientQueue.remove(client);
Server.UUIDMap.remove(client.getUUID().toString());
}
public String getUUID() { public String getUUID() {
return this.UUID.toString(); return this.UUID.toString();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment