Renamed Heartbeat to HeartBeat.
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]
This commit is contained in:
@@ -133,9 +133,8 @@ public class BinaryMessageDecoder {
|
||||
switch(mType) {
|
||||
case HEARTBEAT:
|
||||
//System.out.println("Decoding HeartBeat Message!");
|
||||
//TODO maybe use HeartbeatDecoder.decode(message).
|
||||
//TODO also, decoders for each message type should encapsulate the constructing of the object. E.g., return HeartbeatDecoder.decode(message);.
|
||||
return new Heartbeat(bytesToLong(messageBody));
|
||||
HeartBeatDecoder heartBeatDecoder = new HeartBeatDecoder(messageBody);
|
||||
return heartBeatDecoder.getMessage();
|
||||
|
||||
case RACESTATUS:
|
||||
//System.out.println("Race Status Message");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,8 @@ public class EncoderFactory {
|
||||
|
||||
switch (type) {
|
||||
|
||||
case HEARTBEAT: return new HeartBeatEncoder();
|
||||
|
||||
case BOATLOCATION: return new BoatLocationEncoder();
|
||||
|
||||
case REQUEST_TO_JOIN: return new RequestToJoinEncoder();
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -19,20 +19,7 @@ import java.util.List;
|
||||
*/
|
||||
public class RaceVisionByteEncoder {
|
||||
|
||||
/**
|
||||
* Serializes a heartbeat message.
|
||||
* @param heartbeat Heartbeat message.
|
||||
* @return Serialized message.
|
||||
*/
|
||||
public static byte[] heartBeat(Heartbeat heartbeat) {
|
||||
|
||||
ByteBuffer heartBeat = ByteBuffer.allocate(4);
|
||||
heartBeat.put(longToBytes(heartbeat.getSequenceNumber(), 4));
|
||||
|
||||
byte[] result = heartBeat.array();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a RaceStatus message.
|
||||
|
||||
+2
-2
@@ -6,7 +6,7 @@ import network.Messages.Enums.MessageType;
|
||||
/**
|
||||
* Represents a Heartbeat message.
|
||||
*/
|
||||
public class Heartbeat extends AC35Data {
|
||||
public class HeartBeat extends AC35Data {
|
||||
|
||||
/**
|
||||
* Sequence number of the heartbeat.
|
||||
@@ -17,7 +17,7 @@ public class Heartbeat extends AC35Data {
|
||||
* Ctor.
|
||||
* @param sequenceNumber Sequence number of the heartbeat.
|
||||
*/
|
||||
public Heartbeat(long sequenceNumber) {
|
||||
public HeartBeat(long sequenceNumber) {
|
||||
super(MessageType.HEARTBEAT);
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
}
|
||||
Reference in New Issue
Block a user