diff --git a/Server/ClientThread.java b/Server/ClientThread.java index 476922b18483207d9a3be05a5a6becfcf8735801..826074f6156c413506b0bcddfe50bae9ec829668 100644 --- a/Server/ClientThread.java +++ b/Server/ClientThread.java @@ -27,58 +27,69 @@ public class ClientThread implements Runnable { while (true) { String message = this.clientInput.readLine(); message = message.toUpperCase(); - if (message.equals("HELLO")) { - this.UUID = java.util.UUID.randomUUID(); - Server.UUIDMap.put(this.UUID.toString(), this); - send("WELCOME"); - send("UUID " + this.UUID); - Server.clientQueue.add(getClientObject()); - if (Server.clientQueue.size() == 1) { // only person in queue - send("NEXT"); // send client straight to resource - } else { - // send position and add to queue - send("POSITION " + getPosition()); - } - send(""); - } else if (message.equals("POSITION")) { - send("POSITION " + getPosition()); - send(""); - } else if (message.startsWith("UUID ")) { - String UUID = message.substring(5, message.length()).toLowerCase(); - if (Server.UUIDMap.containsKey(UUID)) { - this.client = Server.UUIDMap.get(UUID); - send("WELCOME_BACK"); + String response_code = message.split("\\s+")[0]; + switch (response_code) { + case "HELLO": + this.UUID = java.util.UUID.randomUUID(); + Server.UUIDMap.put(this.UUID.toString(), this); + send("WELCOME"); + send("UUID " + this.UUID); + Server.clientQueue.add(getClientObject()); + if (Server.clientQueue.size() == 1) { // only person in queue + send("NEXT"); // send client straight to resource + } else { + // send position and add to queue + send("POSITION " + getPosition()); + } + send(""); + break; + case "POSITION": send("POSITION " + getPosition()); - } else { - send("INCORRECT_UUID"); - send("Terminating connection"); send(""); - serverOutput.close(); - } - send(""); - } else if (message.equals("GOODBYE")) { - send("GOODBYE"); - send(""); - Server.clientQueue.remove(getClientObject()); - Server.UUIDMap.remove(getClientObject().getUUID().toString()); - serverOutput.close(); - } else if (message.equals("DONE")) { - if (Server.clientQueue.indexOf(getClientObject()) == 0) { - send("Your connection is now being terminated."); + break; + + case "UUID": + 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()); + } else { + send("INCORRECT_UUID"); + send("Terminating connection"); + send(""); + serverOutput.close(); + } + send(""); + break; + + case "GOODBYE": send("GOODBYE"); send(""); + removeClientFromServer(getClientObject()); serverOutput.close(); - Server.clientQueue.remove(getClientObject()); - } else { - send("It's not your turn. To exit the queue send GOODBYE."); + break; + + 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(""); - } - } else { - send("UNKNOWN_COMMAND"); - send(""); } } - } catch (Exception e) { + } catch ( + + Exception e) { e.printStackTrace(); } finally { serverOutput.close(); @@ -102,6 +113,11 @@ public class ClientThread implements Runnable { serverOutput.println(s); } + private void removeClientFromServer(ClientThread client) { + Server.clientQueue.remove(client); + Server.UUIDMap.remove(client.getUUID().toString()); + } + public String getUUID() { return this.UUID.toString(); }