commit
402df18b71
@ -0,0 +1,20 @@
|
|||||||
|
package network.MessageDecoders;
|
||||||
|
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class BoatActionDecoder {
|
||||||
|
byte byteBoatAction;
|
||||||
|
BoatActionEnum boatAction;
|
||||||
|
|
||||||
|
public BoatActionDecoder(byte[] encodedBoatAction) {
|
||||||
|
byteBoatAction = encodedBoatAction[0];
|
||||||
|
|
||||||
|
boatAction = BoatActionEnum.fromByte(byteBoatAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BoatActionEnum getBoatAction() {
|
||||||
|
return boatAction;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package network.Messages;
|
||||||
|
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by David on 10/07/2017.
|
||||||
|
*/
|
||||||
|
public class BoatAction extends AC35Data {
|
||||||
|
|
||||||
|
private byte boatAction;
|
||||||
|
|
||||||
|
public BoatAction(BoatActionEnum boatAction){
|
||||||
|
super(MessageType.BOATACTION);
|
||||||
|
this.boatAction = boatAction.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getBoatAction() {
|
||||||
|
return boatAction;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,71 @@
|
|||||||
|
package network.Messages.Enums;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boat actions
|
||||||
|
*/
|
||||||
|
public enum BoatActionEnum {
|
||||||
|
NOT_A_STATUS(-1),
|
||||||
|
AUTO_PILOT(1),
|
||||||
|
SAILS_IN(2),
|
||||||
|
SAILS_OUT(3),
|
||||||
|
TACK_GYBE(4),
|
||||||
|
UPWIND(5),
|
||||||
|
DOWNWIND(6);
|
||||||
|
|
||||||
|
private byte value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctor. Creates a BoatActionEnum from a given primitive integer value, cast to a byte.
|
||||||
|
* @param value Integer, which is cast to byte, to construct from.
|
||||||
|
*/
|
||||||
|
private BoatActionEnum(int value) {
|
||||||
|
this.value = (byte) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the primitive value of the enum.
|
||||||
|
* @return Primitive value of the enum.
|
||||||
|
*/
|
||||||
|
public byte getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores a mapping between Byte values and BoatActionEnum values.
|
||||||
|
*/
|
||||||
|
private static final Map<Byte, BoatActionEnum> byteToStatusMap = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Static initialization block. Initializes the byteToStatusMap.
|
||||||
|
*/
|
||||||
|
static {
|
||||||
|
for (BoatActionEnum type : BoatActionEnum.values()) {
|
||||||
|
BoatActionEnum.byteToStatusMap.put(type.value, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the enumeration value which corresponds to a given byte value.
|
||||||
|
* @param boatActionEnum Byte value to convert to a BoatActionEnum value.
|
||||||
|
* @return The BoatActionEnum value which corresponds to the given byte value.
|
||||||
|
*/
|
||||||
|
public static BoatActionEnum fromByte(byte boatActionEnum) {
|
||||||
|
//Gets the corresponding MessageType from the map.
|
||||||
|
BoatActionEnum type = BoatActionEnum.byteToStatusMap.get(boatActionEnum);
|
||||||
|
|
||||||
|
if (type == null) {
|
||||||
|
//If the byte value wasn't found, return the NOT_A_STATUS boatActionEnum.
|
||||||
|
return BoatActionEnum.NOT_A_STATUS;
|
||||||
|
} else {
|
||||||
|
//Otherwise, return the boatActionEnum.
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import network.BinaryMessageEncoder;
|
||||||
|
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||||
|
import network.Messages.BoatAction;
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic service for sending key presses to game server
|
||||||
|
*/
|
||||||
|
public class ControllerClient {
|
||||||
|
/**
|
||||||
|
* Socket to server
|
||||||
|
*/
|
||||||
|
Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output stream wrapper for socket to server
|
||||||
|
*/
|
||||||
|
DataOutputStream outputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise controller client with live socket.
|
||||||
|
* @param socket to server
|
||||||
|
*/
|
||||||
|
public ControllerClient(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.outputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a keypress to server
|
||||||
|
* @param key to send
|
||||||
|
* @throws IOException if socket write fails
|
||||||
|
*/
|
||||||
|
public void sendKey(ControlKey key) throws IOException {
|
||||||
|
int protocolCode = key.getProtocolCode();
|
||||||
|
if(protocolCode > -1) {
|
||||||
|
|
||||||
|
byte[] bytes = new byte[4];
|
||||||
|
ByteBuffer.wrap(bytes).putInt(key.getProtocolCode());
|
||||||
|
BoatActionEnum boatActionEnum = BoatActionEnum.fromByte(bytes[3]);
|
||||||
|
|
||||||
|
BoatAction boatAction = new BoatAction(boatActionEnum);
|
||||||
|
|
||||||
|
byte[] encodedBoatAction = RaceVisionByteEncoder.boatActionMessage(boatAction);
|
||||||
|
|
||||||
|
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, System.currentTimeMillis(), 0,
|
||||||
|
(short) encodedBoatAction.length, encodedBoatAction);
|
||||||
|
|
||||||
|
System.out.println("Sending out key: " + boatActionEnum);
|
||||||
|
outputStream.write(binaryMessage.getFullMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import network.BinaryMessageDecoder;
|
||||||
|
import network.MessageDecoders.BoatActionDecoder;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for dispatching key press data to race from client
|
||||||
|
*/
|
||||||
|
public class ControllerServer implements Runnable {
|
||||||
|
/**
|
||||||
|
* Socket to client
|
||||||
|
*/
|
||||||
|
private Socket socket;
|
||||||
|
/**
|
||||||
|
* Wrapper for input from client
|
||||||
|
*/
|
||||||
|
private DataInputStream inputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise server-side controller with live client socket
|
||||||
|
* @param socket to client
|
||||||
|
*/
|
||||||
|
public ControllerServer(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
try {
|
||||||
|
this.inputStream = new DataInputStream(this.socket.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(encodedMessage.getMessageBody());
|
||||||
|
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction();
|
||||||
|
System.out.println("Received key: " + decodedMessage);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,36 +1,51 @@
|
|||||||
package visualiser.gameController.Keys;
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
import javafx.scene.input.KeyCode;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory for creating Keys, these could be predefined in the future.
|
* Factory for creating Keys, these could be predefined in the future.
|
||||||
*/
|
*/
|
||||||
public class KeyFactory {
|
public class KeyFactory {
|
||||||
|
/**
|
||||||
|
* Retrieve command given key
|
||||||
|
*/
|
||||||
|
private Map<String, ControlKey> keyState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance to enforce consistent key state
|
||||||
|
*/
|
||||||
|
private static KeyFactory theFactory = new KeyFactory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton constructor for key state, set up initial state of each action.
|
||||||
|
*/
|
||||||
|
private KeyFactory() {
|
||||||
|
this.keyState = new HashMap<>();
|
||||||
|
keyState.put("Z", new ZoomInKey("Zoom In"));
|
||||||
|
keyState.put("X", new ZoomOutKey("Zoom Out"));
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get singleton instance of KeyFactory to interact with key state
|
||||||
|
* @return automatically constructed KeyFactory
|
||||||
|
*/
|
||||||
|
public static KeyFactory getFactory() {
|
||||||
|
return theFactory;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Control Key in charge of a key press
|
* Get the Control Key in charge of a key press
|
||||||
* @param key key pressed (String value of KeyCode)
|
* @param key key pressed (String value of KeyCode)
|
||||||
* @return the Control Key behaviour of the key pressed.
|
* @return the Control Key behaviour of the key pressed.
|
||||||
*/
|
*/
|
||||||
public static ControlKey getKey(String key){
|
public ControlKey getKey(String key){
|
||||||
switch(key){
|
return keyState.get(key);
|
||||||
case "Z":
|
|
||||||
return new ZoomInKey("Z", KeyCode.Z);
|
|
||||||
case "X":
|
|
||||||
return new ZoomOutKey("X", KeyCode.X);
|
|
||||||
case "SPACE":
|
|
||||||
return new VMGKey("SPACE", KeyCode.SPACE);
|
|
||||||
case "SHIFT":
|
|
||||||
return new SailsToggleKey("SHIFT", KeyCode.SHIFT);
|
|
||||||
case "ENTER":
|
|
||||||
return new TackGybeKey("ENTER", KeyCode.ENTER);
|
|
||||||
case "PAGE_UP":
|
|
||||||
return new UpWindKey("PAGE_UP", KeyCode.PAGE_UP);
|
|
||||||
case "PAGE_DOWN":
|
|
||||||
return new DownWindKey("PAGE_DOWN", KeyCode.PAGE_DOWN);
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
|
||||||
|
<BoatConfig>
|
||||||
|
<Boats>
|
||||||
|
|
||||||
|
<!--Mark Boats-->
|
||||||
|
<Boat Type="Mark" BoatName="PRO" SourceID="101" >
|
||||||
|
<GPSposition X= "-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="PIN" SourceID="102" >
|
||||||
|
<GPSposition X= "-64.855242" Y="32.293771" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="Marker1" SourceID="103" >
|
||||||
|
<GPSposition X= "-64.843983" Y="32.293039" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="WGL" SourceID="104" >
|
||||||
|
<GPSposition X= "-64.850045" Y="32.28468" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="WGR" SourceID="105" >
|
||||||
|
<GPSposition X= "-64.847591" Y="32.280164" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="LGL" SourceID="106" >
|
||||||
|
<GPSposition X= "-64.835249" Y="32.309693" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="LGR" SourceID="107" >
|
||||||
|
<GPSposition X= "-64.831785" Y="32.308046" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="FL" SourceID="108" >
|
||||||
|
<GPSposition X= "-64.839291" Y="32.317379" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="FR" SourceID="109" >
|
||||||
|
<GPSposition X= "-64.83626" Y="32.317257" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
|
||||||
|
<!--Participants-->
|
||||||
|
<Boat BoatName="Emirates Team New Zealand" HullNum="RG01" ShapeID="0" ShortName="NZL" SourceID="126" StoweName="NZL" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Land Rover BAR" HullNum="RG01" ShapeID="0" ShortName="GBR" SourceID="122" StoweName="GBR" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="SoftBank Team Japan" HullNum="RG01" ShapeID="0" ShortName="JPN" SourceID="123" StoweName="JPN" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Groupama Team France" HullNum="RG01" ShapeID="0" ShortName="FRA" SourceID="124" StoweName="FRA" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Artemis Racing" HullNum="RG01" ShapeID="0" ShortName="SWE" SourceID="125" StoweName="SWE" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Emirates Team New Zealand" HullNum="RG01" ShapeID="0" ShortName="NZL" SourceID="126" StoweName="NZL" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
</Boats>
|
||||||
|
</BoatConfig>
|
||||||
Loading…
Reference in new issue