- Added new MessageType.BOATSTATE enum to support new message type - Modified EncoderFactory and DecoderFactory to support new enum - Verify that original and decoded messages match #story[1291]main
parent
ccbe3f86ea
commit
a295728e89
@ -0,0 +1,38 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.BoatState;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static network.Utils.ByteConverter.bytesToInt;
|
||||
|
||||
/**
|
||||
* Decoder for {@link BoatState} messages
|
||||
*/
|
||||
public class BoatStateDecoder implements MessageDecoder {
|
||||
/**
|
||||
* Decoded BoatState message
|
||||
*/
|
||||
private BoatState message;
|
||||
|
||||
@Override
|
||||
public AC35Data decode(byte[] encodedMessage) throws InvalidMessageException {
|
||||
byte[] sourceID = Arrays.copyOfRange(encodedMessage, 0, 4);
|
||||
byte boatHealth = encodedMessage[4];
|
||||
|
||||
// Unpack bytes into BoatState
|
||||
this.message = new BoatState(
|
||||
bytesToInt(sourceID),
|
||||
boatHealth
|
||||
);
|
||||
|
||||
// Return BoatState
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public BoatState getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.BoatState;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static network.Utils.ByteConverter.intToBytes;
|
||||
|
||||
/**
|
||||
* Encoder for {@link BoatState} message
|
||||
*/
|
||||
public class BoatStateEncoder implements MessageEncoder {
|
||||
@Override
|
||||
public byte[] encode(AC35Data message) throws InvalidMessageException {
|
||||
// Downcast message
|
||||
BoatState boatState = (BoatState)message;
|
||||
|
||||
//Serialise message
|
||||
byte[] sourceID = intToBytes(boatState.getSourceID());
|
||||
byte boatHealth = (byte)boatState.getBoatHealth();
|
||||
|
||||
// Pack bytes into string
|
||||
ByteBuffer boatStateMessage = ByteBuffer.allocate(5);
|
||||
boatStateMessage.put(sourceID);
|
||||
boatStateMessage.put(boatHealth);
|
||||
|
||||
// Return byte string
|
||||
return boatStateMessage.array();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package network.Messages;
|
||||
|
||||
import network.Messages.Enums.MessageType;
|
||||
|
||||
/**
|
||||
* Represents the information in a BoatState message according to protocol meeting
|
||||
*/
|
||||
public class BoatState extends AC35Data {
|
||||
/**
|
||||
* Source ID of boat described in message
|
||||
*/
|
||||
private int sourceID;
|
||||
/**
|
||||
* Health between 0-100 of boat with above source ID
|
||||
*/
|
||||
private int boatHealth;
|
||||
|
||||
public BoatState(int sourceID, int boatHealth) {
|
||||
super(MessageType.BOATSTATE);
|
||||
this.sourceID = sourceID;
|
||||
this.boatHealth = boatHealth;
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public int getBoatHealth() {
|
||||
return boatHealth;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatState;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* Test for the {@link network.Messages.BoatState} encoder and decoder.
|
||||
*/
|
||||
public class BoatStateDecoderTest {
|
||||
private BoatState originalMessage;
|
||||
private BoatState decodedMessage;
|
||||
|
||||
@Before
|
||||
public void setUp() throws InvalidMessageException {
|
||||
originalMessage = new BoatState(0, 100);
|
||||
|
||||
byte[] encodedMessage = RaceVisionByteEncoder.encode(originalMessage);
|
||||
BoatStateDecoder decoder = new BoatStateDecoder();
|
||||
decoder.decode(encodedMessage);
|
||||
decodedMessage = decoder.getMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodingEqualsOriginal() {
|
||||
assertEquals(originalMessage.getSourceID(), decodedMessage.getSourceID());
|
||||
assertEquals(originalMessage.getBoatHealth(), decodedMessage.getBoatHealth());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue