Merge remote-tracking branch 'origin/master' into issue_28_36_decoders

# Conflicts:
#	racevisionGame/src/main/java/mock/app/ConnectionAcceptor.java
#	racevisionGame/src/main/java/mock/app/Event.java
#	racevisionGame/src/main/java/visualiser/gameController/ControllerServer.java
This commit is contained in:
fjc40
2017-08-10 13:12:26 +12:00
21 changed files with 537 additions and 117 deletions
@@ -1,23 +1,23 @@
package visualiser.gameController;
import mock.model.RaceLogic;
import network.BinaryMessageDecoder;
import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.BoatActionDecoder;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
import visualiser.gameController.Keys.ControlKey;
import visualiser.gameController.Keys.KeyFactory;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.Observable;
import java.util.logging.Logger;
/**
* Service for dispatching key press data to race from client
*/
public class ControllerServer implements Runnable {
public class ControllerServer extends Observable implements Runnable {
/**
* Socket to client
*/
@@ -26,13 +26,23 @@ public class ControllerServer implements Runnable {
* Wrapper for input from client
*/
private DataInputStream inputStream;
/**
* Last received boat action
*/
private BoatActionEnum action;
/**
*
*/
private RaceLogic rc;
/**
* Initialise server-side controller with live client socket
* @param socket to client
*/
public ControllerServer(Socket socket) {
public ControllerServer(Socket socket, RaceLogic rc) {
this.socket = socket;
this.rc = rc;
this.addObserver(rc);
try {
this.inputStream = new DataInputStream(this.socket.getInputStream());
} catch (IOException e) {
@@ -40,6 +50,10 @@ public class ControllerServer implements Runnable {
}
}
public BoatActionEnum getAction() {
return action;
}
/**
* Wait for controller key input from client and loop.
*/
@@ -58,7 +72,11 @@ public class ControllerServer implements Runnable {
try {
boatActionDecoder.decode(encodedMessage.getMessageBody());
BoatAction boatAction = boatActionDecoder.getMessage();
System.out.println("Received key: " + boatAction.getBoatAction());
action = boatActionDecoder.getBoatAction();
// Notify observers of most recent action
this.notifyObservers();
this.setChanged();
} catch (InvalidMessageException e) {
Logger.getGlobal().log(Level.WARNING, "Could not decode BoatAction message.", e);
@@ -27,8 +27,8 @@ public class KeyFactory {
keyState.put("SPACE", new VMGKey("VMG"));
keyState.put("SHIFT", new SailsToggleKey("Toggle Sails"));
keyState.put("ENTER", new TackGybeKey("Tack/Gybe"));
keyState.put("PAGE_UP", new UpWindKey("Upwind"));
keyState.put("PAGE_DOWN", new DownWindKey("Downwind"));
keyState.put("UP", new UpWindKey("Upwind"));
keyState.put("DOWN", new DownWindKey("Downwind"));
}
/**