Added BoatActonEncoder.

Updated ControllerClient to use RaceVisionByteEncoder.encode(message). It also logs a warning if the encoding fails. Also removed pointless and out of place encode/decode of the action enum.
Added a BoatActionDecoderTest which tests encoding/decoding a BoatAction Message.
issue #35 #36
#story[1095]
This commit is contained in:
fjc40
2017-08-05 21:53:54 +12:00
parent 16686678a7
commit 31ce9fff94
6 changed files with 168 additions and 14 deletions
@@ -0,0 +1,40 @@
package network.MessageEncoders;
import network.Messages.AC35Data;
import network.Messages.BoatAction;
import java.nio.ByteBuffer;
import static network.Utils.ByteConverter.intToBytes;
/**
* This encoder can encode a {@link BoatAction} message.
*/
public class BoatActionEncoder implements MessageEncoder {
/**
* Constructor.
*/
public BoatActionEncoder() {
}
@Override
public byte[] encode(AC35Data message) {
//Downcast.
BoatAction boatAction = (BoatAction) message;
//Message is 1 byte.
ByteBuffer boatActionMessage = ByteBuffer.allocate(1);
boatActionMessage.put(intToBytes(boatAction.getBoatAction(), 1));
byte [] result = boatActionMessage.array();
return result;
}
}
@@ -33,6 +33,8 @@ public class EncoderFactory {
case JOIN_ACCEPTANCE: return new JoinAcceptanceEncoder();
case BOATACTION: return new BoatActionEncoder();
default: throw new InvalidMessageTypeException("Unrecognised message type: " + type);
}
@@ -341,12 +341,6 @@ public class RaceVisionByteEncoder {
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;
}
/**
@@ -8,6 +8,10 @@ import java.util.Map;
*/
public enum BoatActionEnum {
NOT_A_STATUS(-1),
/**
* Autopilot = auto VMG.
*/
AUTO_PILOT(1),
SAILS_IN(2),
SAILS_OUT(3),
@@ -68,4 +72,4 @@ public enum BoatActionEnum {
}
}
}
}
@@ -1,6 +1,7 @@
package visualiser.gameController;
import network.BinaryMessageEncoder;
import network.Exceptions.InvalidMessageException;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
@@ -12,6 +13,8 @@ import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Basic service for sending key presses to game server
@@ -50,18 +53,20 @@ public class ControllerClient {
BoatActionEnum protocolCode = key.getProtocolCode();
if(protocolCode != BoatActionEnum.NOT_A_STATUS) {
byte[] bytes = new byte[4];
ByteBuffer.wrap(bytes).putInt(protocolCode.getValue());
BoatActionEnum boatActionEnum = BoatActionEnum.fromByte(bytes[3]);
BoatAction boatAction = new BoatAction(protocolCode);
BoatAction boatAction = new BoatAction(boatActionEnum);
byte[] encodedBoatAction = RaceVisionByteEncoder.boatActionMessage(boatAction);
//Encode BoatAction.
byte[] encodedBoatAction = new byte[0];
try {
encodedBoatAction = RaceVisionByteEncoder.encode(boatAction);
} catch (InvalidMessageException e) {
Logger.getGlobal().log(Level.WARNING, "Could not encode BoatAction: " + boatAction, e);
}
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, System.currentTimeMillis(), 0,
(short) encodedBoatAction.length, encodedBoatAction);
System.out.println("Sending out key: " + boatActionEnum);
System.out.println("Sending out key: " + protocolCode);
outputStream.write(binaryMessage.getFullMessage());
}
}