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
Branches main
No related tags found
No related merge requests found
......@@ -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();
}
......
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