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]
main
fjc40 8 years ago
parent 16686678a7
commit 31ce9fff94

@ -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());
}
}

@ -0,0 +1,109 @@
package network.MessageDecoders;
import network.Exceptions.InvalidMessageException;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.RequestToJoin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Test for the BoatAction encoder and decoder
*/
public class BoatActionDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private BoatAction encodeDecodeMessage(BoatAction message) throws InvalidMessageException {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
//Decode.
BoatActionDecoder testDecoder = new BoatActionDecoder(testEncodedMessage);
BoatActionEnum decodedBoatAction = testDecoder.getBoatAction();
BoatAction decodedMessage = new BoatAction(decodedBoatAction);
return decodedMessage;
}
/**
* Tests if a specific boat action type message can be encoded and decoded correctly.
* @param type The type of boat action.
*/
private void boatActionTypeTest(BoatActionEnum type) throws Exception {
//Prepare message.
BoatAction beforeMessage = new BoatAction(type);
//Encode/decode it.
BoatAction afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getBoatAction(), afterMessage.getBoatAction());
}
/**
* Tests if an autopilot message can be encoded and decoded correctly.
*/
@Test
public void autoPilotTest() throws Exception {
boatActionTypeTest(BoatActionEnum.AUTO_PILOT);
}
/**
* Tests if a sails in message can be encoded and decoded correctly.
*/
@Test
public void sailsInTest() throws Exception {
boatActionTypeTest(BoatActionEnum.SAILS_IN);
}
/**
* Tests if a sails out message can be encoded and decoded correctly.
*/
@Test
public void sailsOutTest() throws Exception {
boatActionTypeTest(BoatActionEnum.SAILS_OUT);
}
/**
* Tests if a tack/gybe message can be encoded and decoded correctly.
*/
@Test
public void tackGybeTest() throws Exception {
boatActionTypeTest(BoatActionEnum.TACK_GYBE);
}
/**
* Tests if an upwind message can be encoded and decoded correctly.
*/
@Test
public void upwindTest() throws Exception {
boatActionTypeTest(BoatActionEnum.UPWIND);
}
/**
* Tests if a downwind message can be encoded and decoded correctly.
*/
@Test
public void downwindTest() throws Exception {
boatActionTypeTest(BoatActionEnum.DOWNWIND);
}
}
Loading…
Cancel
Save