You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

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 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 extends Observable implements Runnable {
/**
* Socket to client
*/
private Socket socket;
/**
* 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, RaceLogic rc) {
this.socket = socket;
this.rc = rc;
this.addObserver(rc);
try {
this.inputStream = new DataInputStream(this.socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public BoatActionEnum getAction() {
return action;
}
/**
* Wait for controller key input from client and loop.
*/
@Override
public void run() {
while(true) {
byte[] message = new byte[20];
try {
if (inputStream.available() > 0) {
inputStream.read(message);
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
BoatActionDecoder boatActionDecoder = new BoatActionDecoder();
try {
boatActionDecoder.decode(encodedMessage.getMessageBody());
BoatAction boatAction = boatActionDecoder.getMessage();
action = boatAction.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);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}