mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
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:
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user