Three way handshake implemented client and server side and functioning

Server generates a new Id for connections (Size of connections + 1)
Client now stores an id allocated to it by the server
The id is currently being saved in the clientToServer thread client side

tags: #story[987] #implement
This commit is contained in:
William Muir
2017-07-22 17:44:37 +12:00
parent 3ec930491f
commit 2869d139a3
3 changed files with 88 additions and 26 deletions
@@ -17,12 +17,15 @@ import seng302.server.messages.Message;
*/
public class ClientToServerThread implements Runnable {
private static final int LOG_LEVEL = 1;
private Thread thread;
private Integer ourID;
private Socket socket;
private InputStream is;
private OutputStream os;
private static final int LOG_LEVEL = 1;
private Boolean updateClient = true;
private ByteArrayOutputStream crcBuffer;
@@ -36,6 +39,16 @@ public class ClientToServerThread implements Runnable {
e.printStackTrace();
}
Integer allocatedID = threeWayHandshake();
if (allocatedID != null) {
ourID = allocatedID;
clientLog("Successful handshake. Allocated ID: " + ourID, 1);
} else {
clientLog("Unsuccessful handhsake", 1);
closeSocket();
return;
}
thread = new Thread(this);
thread.start();
@@ -96,6 +109,33 @@ public class ClientToServerThread implements Runnable {
}
/**
* Listens for an allocated sourceID and returns it to the server if recieved
* @return the sourceID allocated to us by the server
*/
private Integer threeWayHandshake() {
Integer ourSourceID = null;
while (true) {
try {
ourSourceID = is.read();
} catch (IOException e) {
e.printStackTrace();
}
if (ourSourceID != null) {
try {
os.write(ourSourceID);
return ourSourceID;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
/**
* Send the post-start race course information
*/