Merging main features into story 6. App now sends and receives key strokes. #story[1007, 1089]
This commit is contained in:
@@ -193,6 +193,10 @@ public class BinaryMessageDecoder {
|
|||||||
AverageWindDecoder awDecoder = new AverageWindDecoder(messageBody);
|
AverageWindDecoder awDecoder = new AverageWindDecoder(messageBody);
|
||||||
return awDecoder.getAverageWind();
|
return awDecoder.getAverageWind();
|
||||||
|
|
||||||
|
case BOATACTION:
|
||||||
|
BoatActionDecoder baDecoder = new BoatActionDecoder(messageBody);
|
||||||
|
return new BoatAction(baDecoder.getBoatAction());
|
||||||
|
|
||||||
default:
|
default:
|
||||||
//System.out.println("Broken Message!");
|
//System.out.println("Broken Message!");
|
||||||
//throw new InvalidMessageException("Broken message! Did not recognise message type: " + headerMessageType + ".");
|
//throw new InvalidMessageException("Broken message! Did not recognise message type: " + headerMessageType + ".");
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -339,4 +339,11 @@ public class RaceVisionByteEncoder {
|
|||||||
return result.array();
|
return result.array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] boatActionMessage(BoatAction boatAction){
|
||||||
|
ByteBuffer boatActionMessage = ByteBuffer.allocate(1);
|
||||||
|
boatActionMessage.put(intToBytes(boatAction.getBoatAction(), 1));
|
||||||
|
byte [] result = boatActionMessage.array();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package visualiser.gameController;
|
package visualiser.gameController;
|
||||||
|
|
||||||
import network.BinaryMessageEncoder;
|
import network.BinaryMessageEncoder;
|
||||||
|
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||||
|
import network.Messages.BoatAction;
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
import network.Messages.Enums.MessageType;
|
import network.Messages.Enums.MessageType;
|
||||||
import visualiser.gameController.Keys.ControlKey;
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
|
||||||
@@ -46,12 +49,18 @@ public class ControllerClient {
|
|||||||
public void sendKey(ControlKey key) throws IOException {
|
public void sendKey(ControlKey key) throws IOException {
|
||||||
int protocolCode = key.getProtocolCode();
|
int protocolCode = key.getProtocolCode();
|
||||||
if(protocolCode > -1) {
|
if(protocolCode > -1) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(4);
|
|
||||||
buffer.putInt(protocolCode);
|
byte[] bytes = new byte[4];
|
||||||
byte[] message = new byte[]{buffer.get(3)};
|
ByteBuffer.wrap(bytes).putInt(key.getProtocolCode());
|
||||||
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, message);
|
|
||||||
//outputStream.write(binaryMessage.getFullMessage());
|
BoatAction boatAction = new BoatAction(BoatActionEnum.fromByte(bytes[3]));
|
||||||
System.out.println("Binary message constructed");
|
|
||||||
|
byte[] encodedBoatAction = RaceVisionByteEncoder.boatActionMessage(boatAction);
|
||||||
|
|
||||||
|
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, System.currentTimeMillis(), 0,
|
||||||
|
(short) encodedBoatAction.length, encodedBoatAction);
|
||||||
|
|
||||||
|
outputStream.write(binaryMessage.getFullMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package visualiser.gameController;
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import network.BinaryMessageDecoder;
|
||||||
|
import network.MessageDecoders.BoatActionDecoder;
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
import visualiser.gameController.Keys.ControlKey;
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
import visualiser.gameController.Keys.KeyFactory;
|
import visualiser.gameController.Keys.KeyFactory;
|
||||||
|
|
||||||
@@ -39,10 +42,15 @@ public class ControllerServer implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while(true) {
|
while(true) {
|
||||||
byte[] key = new byte[16];
|
byte[] message = new byte[20];
|
||||||
try {
|
try {
|
||||||
inputStream.read(key);
|
if (inputStream.available() > 0) {
|
||||||
// TODO - handle messages received
|
inputStream.read(message);
|
||||||
|
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
|
||||||
|
BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody());
|
||||||
|
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction();
|
||||||
|
int key = decodedMessage.getValue();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user