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:
@@ -7,12 +7,9 @@ import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.*;
|
||||
import network.Messages.Enums.MessageType;
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -101,24 +98,25 @@ public class MockOutput implements Runnable
|
||||
* Generates the next heartbeat message and returns it. Increments the heartbeat sequence number.
|
||||
* @return The next heartbeat message.
|
||||
*/
|
||||
private Heartbeat createHeartbeatMessage() {
|
||||
private HeartBeat createHeartbeatMessage() {
|
||||
|
||||
//Create the heartbeat message.
|
||||
Heartbeat heartbeat = new Heartbeat(this.heartbeatSequenceNum);
|
||||
HeartBeat heartBeat = new HeartBeat(this.heartbeatSequenceNum);
|
||||
heartbeatSequenceNum++;
|
||||
|
||||
return heartbeat;
|
||||
return heartBeat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a heartbeat message into a packet to be sent, and returns the byte array.
|
||||
* @param heartbeat The heartbeat message to serialize.
|
||||
* @param heartBeat The heartbeat message to serialize.
|
||||
* @return Byte array containing the next heartbeat message.
|
||||
* @throws InvalidMessageException Thrown if the message cannot be encoded.
|
||||
*/
|
||||
private byte[] parseHeartbeat(Heartbeat heartbeat) {
|
||||
private byte[] parseHeartbeat(HeartBeat heartBeat) throws InvalidMessageException {
|
||||
|
||||
//Serializes the heartbeat message.
|
||||
byte[] heartbeatMessage = RaceVisionByteEncoder.heartBeat(heartbeat);
|
||||
byte[] heartbeatMessage = RaceVisionByteEncoder.encode(heartBeat);
|
||||
|
||||
//Places the serialized message in a packet.
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(
|
||||
@@ -213,7 +211,15 @@ public class MockOutput implements Runnable
|
||||
public void sendHeartBeat() throws IOException {
|
||||
//Sends a heartbeat every so often.
|
||||
if (timeSinceHeartbeat() >= heartbeatPeriod) {
|
||||
outToVisualiser.write(parseHeartbeat(createHeartbeatMessage()));
|
||||
|
||||
HeartBeat heartBeat = createHeartbeatMessage();
|
||||
|
||||
try {
|
||||
outToVisualiser.write(parseHeartbeat(heartBeat));
|
||||
} catch (InvalidMessageException e) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Could not encode HeartBeat: " + heartBeat, e);
|
||||
}
|
||||
|
||||
lastHeartbeatTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user