Added HeartBeatDecoder. Added HeartBeatEncoder. BinaryMessageDecoder now uses HeartBeatDecoder. MockOutput now logs a warning if a heartBeat cannot be encoded. Added HeartBeatDecoderTest. issue #35 #36 #story[1095]main
parent
c3ed30019c
commit
8ef906472b
@ -0,0 +1,53 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.HeartBeat;
|
||||
|
||||
import static network.Utils.ByteConverter.bytesToLong;
|
||||
|
||||
/**
|
||||
* Decodes {@link network.Messages.HeartBeat} messages.
|
||||
*/
|
||||
public class HeartBeatDecoder {
|
||||
|
||||
/**
|
||||
* The encoded message.
|
||||
*/
|
||||
private byte[] encodedMessage;
|
||||
|
||||
/**
|
||||
* The decoded message.
|
||||
*/
|
||||
private HeartBeat message;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a decoder to decode a given message.
|
||||
* @param encodedMessage The message to decode.
|
||||
*/
|
||||
public HeartBeatDecoder(byte[] encodedMessage) {
|
||||
this.encodedMessage = encodedMessage;
|
||||
|
||||
decode();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes the contained message.
|
||||
*/
|
||||
private void decode() {
|
||||
|
||||
message = new HeartBeat(bytesToLong(encodedMessage));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the decoded message.
|
||||
* @return The decoded message.
|
||||
*/
|
||||
public HeartBeat getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.HeartBeat;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static network.Utils.ByteConverter.intToBytes;
|
||||
import static network.Utils.ByteConverter.longToBytes;
|
||||
|
||||
/**
|
||||
* This encoder can encode a {@link HeartBeat} message.
|
||||
*/
|
||||
public class HeartBeatEncoder implements MessageEncoder {
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public HeartBeatEncoder() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] encode(AC35Data message) {
|
||||
|
||||
//Downcast.
|
||||
HeartBeat heartbeat = (HeartBeat) message;
|
||||
|
||||
//Message is 4 bytes.
|
||||
ByteBuffer heartBeat = ByteBuffer.allocate(4);
|
||||
heartBeat.put(longToBytes(heartbeat.getSequenceNumber(), 4));
|
||||
|
||||
byte[] result = heartBeat.array();
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatAction;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.HeartBeat;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
/**
|
||||
* Test for the HeartBeat encoder and decoder
|
||||
*/
|
||||
public class HeartBeatDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes and decodes a given message.
|
||||
* @param message Message to encode/decode.
|
||||
* @return The decoded message.
|
||||
*/
|
||||
private HeartBeat encodeDecodeMessage(HeartBeat message) throws InvalidMessageException {
|
||||
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
HeartBeatDecoder testDecoder = new HeartBeatDecoder(testEncodedMessage);
|
||||
HeartBeat decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a given sequence number can be encoded and decoded correctly.
|
||||
* @param sequenceNumber The sequenceNumber to use.
|
||||
*/
|
||||
private void heartBeatTest(long sequenceNumber) throws Exception {
|
||||
|
||||
//Prepare message.
|
||||
HeartBeat beforeMessage = new HeartBeat(sequenceNumber);
|
||||
|
||||
|
||||
//Encode/decode it.
|
||||
HeartBeat afterMessage = encodeDecodeMessage(beforeMessage);
|
||||
|
||||
|
||||
//Compare.
|
||||
assertEquals(beforeMessage.getSequenceNumber(), afterMessage.getSequenceNumber());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a sequence number of zero can be encoded and decoded correctly.
|
||||
*/
|
||||
@Test
|
||||
public void heartBeatZeroTest() throws Exception {
|
||||
heartBeatTest(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a sequence number of 1234512 can be encoded and decoded correctly.
|
||||
*/
|
||||
@Test
|
||||
public void heartBeatNonZeroTest() throws Exception {
|
||||
heartBeatTest(1234512);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue