Documented client packet parser, client state, client state querying runnable, client to server thread and replaced e.printStackTrace() with client log messages.

This commit is contained in:
Zhi You Tan
2017-07-26 16:01:41 +12:00
parent 2bfa6cb038
commit af9f1417f1
4 changed files with 61 additions and 37 deletions
@@ -15,7 +15,8 @@ import seng302.server.messages.BoatActionMessage;
import seng302.server.messages.Message;
/**
* Created by kre39 on 13/07/17.
* A class describing a single connection to a Server for the purposes of sending and receiving on
* its own thread.
*/
public class ClientToServerThread implements Runnable {
@@ -30,8 +31,19 @@ public class ClientToServerThread implements Runnable {
private OutputStream os;
private Boolean updateClient = true;
private ByteArrayOutputStream crcBuffer;
private ByteArrayOutputStream crcBuffer;
/**
* Constructor for ClientToServerThread which takes in ipAddress and portNumber and attempts to
* connect to the specified ipAddress and port.
*
* Upon successful socket connection, threeWayHandshake will be preformed and the instance will
* be put on a thread and run immediately.
*
* @param ipAddress a string of ip address to be connected to
* @param portNumber an integer port number
* @throws Exception SocketConnection if fail to connect to ip address and port number combination
*/
public ClientToServerThread(String ipAddress, Integer portNumber) throws Exception{
socket = new Socket(ipAddress, portNumber);
is = socket.getInputStream();
@@ -40,7 +52,7 @@ public class ClientToServerThread implements Runnable {
Integer allocatedID = threeWayHandshake();
if (allocatedID != null) {
ourID = allocatedID;
clientLog("Successful handshake. Allocated ID: " + ourID, 1);
clientLog("Successful handshake. Allocated ID: " + ourID, 0);
ClientState.setClientSourceId(String.valueOf(ourID));
} else {
clientLog("Unsuccessful handshake", 1);
@@ -50,31 +62,30 @@ public class ClientToServerThread implements Runnable {
thread = new Thread(this);
thread.start();
}
/**
* Prints out log message and time happened.
* Only perform task if log level is below LOG_LEVEL variable.
*
* @param message a string of message to be printed out
* @param logLevel an int for log level
*/
static void clientLog(String message, int logLevel){
if(logLevel <= LOG_LEVEL){
System.out.println("[CLIENT " + LocalDateTime.now().toLocalTime().toString() + "] " + message);
}
}
/**
* Perform the thread loop. Will exit loop if ClientState connected to host variable is false.
*/
public void run() {
int sync1;
int sync2;
// TODO: 14/07/17 wmu16 - Work out how to fix this while loop
while(ClientState.isConnectedToHost()) {
try {
//Perform a write if it is time to as delegated by the MainServerThread
if (updateClient) {
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
// try {
// GameState.outputState(os);
// } catch (IOException e) {
// System.out.println("IO error in server thread upon writing to output stream");
// }
updateClient = false;
}
crcBuffer = new ByteArrayOutputStream();
sync1 = readByte();
sync2 = readByte();
@@ -101,7 +112,7 @@ public class ClientToServerThread implements Runnable {
}
} catch (Exception e) {
closeSocket();
e.printStackTrace();
clientLog("Disconnected from server", 1);
return;
}
}
@@ -111,7 +122,7 @@ public class ClientToServerThread implements Runnable {
/**
* Listens for an allocated sourceID and returns it to the server if recieved
* Listens for an allocated sourceID and returns it to the server if received
* @return the sourceID allocated to us by the server
*/
private Integer threeWayHandshake() {
@@ -120,14 +131,15 @@ public class ClientToServerThread implements Runnable {
try {
ourSourceID = is.read();
} catch (IOException e) {
e.printStackTrace();
clientLog("Three way handshake failed", 1);
}
if (ourSourceID != null) {
try {
os.write(ourSourceID);
return ourSourceID;
} catch (IOException e) {
e.printStackTrace();
clientLog("Three way handshake failed", 1);
return null;
}
}
@@ -143,8 +155,7 @@ public class ClientToServerThread implements Runnable {
try {
os.write(boatActionMessage.getBuffer());
} catch (IOException e) {
clientLog("COULD NOT WRITE TO SERVER", 0);
e.printStackTrace();
clientLog("Could not write to server", 1);
}
}
@@ -153,7 +164,7 @@ public class ClientToServerThread implements Runnable {
try {
socket.close();
} catch (IOException e) {
clientLog("Failed to close the socket", 0);
clientLog("Failed to close the socket", 1);
}
}
@@ -164,7 +175,7 @@ public class ClientToServerThread implements Runnable {
currentByte = is.read();
crcBuffer.write(currentByte);
} catch (IOException e) {
e.printStackTrace();
clientLog("Read byte failed", 1);
}
if (currentByte == -1){
throw new Exception();