MockOutput now uses LatestMessages for xml messages. Moved xml message encoding into RaceVisionByteEncoder. Removed XMLMessageEncoder. Moved XML message sub types into an enumeration (XMLMessageType). XMLMessage can now be queried for its attributes (like timestamp, length, etc...). VisualiserInput now uses LatestMessages. VisualiserInput now (as it did previously) uses switch statements for checking packet type, instead of if statements. VisualiserRace now uses LatestMessages instead of VisualiserInput.main
parent
8e18ad62ca
commit
f057ad58b7
@ -1,59 +0,0 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static network.Utils.ByteConverter.*;
|
||||
|
||||
|
||||
/**
|
||||
* Encodes a XML file into a message of AC35 format
|
||||
*/
|
||||
public class XMLMessageEncoder {
|
||||
private byte[] messageVersionNumber;
|
||||
private short ackNumber;
|
||||
private long timeStamp;
|
||||
private byte[] xmlMsgSubType;
|
||||
private short sequenceNumber;
|
||||
private short xmlMsgLength;
|
||||
private String xmlMessage;
|
||||
|
||||
public XMLMessageEncoder(short ackNumber, long timeStamp, int xmlMsgSubType, short sequenceNumber, short xmlMsgLength, String xmlMessage) {
|
||||
this.messageVersionNumber = intToBytes(1, 1);
|
||||
this.ackNumber = ackNumber;
|
||||
this.timeStamp = timeStamp;
|
||||
this.xmlMsgSubType = intToBytes(xmlMsgSubType, 1);
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.xmlMsgLength = xmlMsgLength;
|
||||
this.xmlMessage = xmlMessage;
|
||||
}
|
||||
|
||||
public byte[] encode() {
|
||||
byte[] messageBytes = xmlMessage.getBytes();
|
||||
if (messageBytes.length > this.xmlMsgLength) {
|
||||
//System.err.println("Xml message is to big");
|
||||
return null;
|
||||
}
|
||||
ByteBuffer tempOutputByteBuffer = ByteBuffer.allocate(14 + messageBytes.length);
|
||||
|
||||
//ackNumber converted to bytes
|
||||
byte[] ackNumberBytes = shortToBytes(ackNumber, 2);
|
||||
|
||||
//sequenceNumber converted to bytes
|
||||
byte[] sequenceNumberBytes = shortToBytes(sequenceNumber, 2);
|
||||
|
||||
//xmlMsgLength converted to bytes
|
||||
byte[] xmlMsgLengthBytes = shortToBytes(xmlMsgLength, 2);
|
||||
|
||||
|
||||
tempOutputByteBuffer.put(messageVersionNumber);
|
||||
tempOutputByteBuffer.put(ackNumberBytes);
|
||||
tempOutputByteBuffer.put(longToBytes(timeStamp, 6));
|
||||
tempOutputByteBuffer.put(xmlMsgSubType);
|
||||
tempOutputByteBuffer.put(sequenceNumberBytes);
|
||||
tempOutputByteBuffer.put(xmlMsgLengthBytes);
|
||||
tempOutputByteBuffer.put(messageBytes);
|
||||
|
||||
return tempOutputByteBuffer.array();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,87 @@
|
||||
package network.Messages.Enums;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Enumeration that encapsulates the various types of XML messages that can be sent.
|
||||
*/
|
||||
public enum XMLMessageType {
|
||||
|
||||
/**
|
||||
* A regatta.xml message.
|
||||
*/
|
||||
REGATTA(5),
|
||||
|
||||
/**
|
||||
* A race.xml message.
|
||||
*/
|
||||
RACE(6),
|
||||
|
||||
/**
|
||||
* A boats.xml message.
|
||||
*/
|
||||
BOAT(7),
|
||||
|
||||
/**
|
||||
* Used for unrecognised byte values.
|
||||
*/
|
||||
NOT_A_MESSAGE_TYPE(0);
|
||||
|
||||
|
||||
///Primitive value of the enum.
|
||||
private byte value;
|
||||
|
||||
/**
|
||||
* Ctor. Creates a XMLMessageType enum from a given primitive integer value, cast to a byte.
|
||||
* @param value Integer, which is cast to byte, to construct from.
|
||||
*/
|
||||
private XMLMessageType(int value) {
|
||||
this.value = (byte)value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the primitive value of the enum.
|
||||
* @return Primitive value of the enum.
|
||||
*/
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
///Stores a mapping between Byte values and XMLMessageType values.
|
||||
private static final Map<Byte, XMLMessageType> byteToTypeMap = new HashMap<>();
|
||||
|
||||
|
||||
/*
|
||||
Static initialization block. Initializes the byteToTypeMap.
|
||||
*/
|
||||
static {
|
||||
for (XMLMessageType type : XMLMessageType.values()) {
|
||||
byteToTypeMap.put(type.value, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the enumeration value which corresponds to a given byte value.
|
||||
* @param messageTypeByte Byte value to convert to a XMLMessageType value.
|
||||
* @return The XMLMessageType value which corresponds to the given byte value.
|
||||
*/
|
||||
public static XMLMessageType fromByte(byte messageTypeByte) {
|
||||
//Gets the corresponding XMLMessageType from the map.
|
||||
XMLMessageType type = byteToTypeMap.get(messageTypeByte);
|
||||
|
||||
if (type == null) {
|
||||
//If the byte value wasn't found, return the NOTAMESSAGE XMLMessageType.
|
||||
return XMLMessageType.NOT_A_MESSAGE_TYPE;
|
||||
}
|
||||
else {
|
||||
//Otherwise, return the XMLMessageType.
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue