Merge branch 'splitIntoTwoModules' of https://eng-git.canterbury.ac.nz/seng302-2017/team-7 into isolatingMock
commit
a9f98b48b2
@ -0,0 +1,87 @@
|
||||
package seng302.Networking;
|
||||
|
||||
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMesageEncoder {
|
||||
private byte[] fullMessage;
|
||||
private byte[] header;
|
||||
private byte[] message;
|
||||
//private byte[] crc;
|
||||
|
||||
private byte headerSync1 = (byte)0x47;
|
||||
private byte headerSync2 = (byte)0x83;
|
||||
private byte headerMessageType;
|
||||
private long headerTimeStamp;
|
||||
private int headerSourceID;
|
||||
private short headerMessageLength;
|
||||
|
||||
public BinaryMesageEncoder(MessageType headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message){
|
||||
//set the header
|
||||
this.headerMessageType = headerMessageType.getValue();
|
||||
this.headerTimeStamp = headerTimeStamp;
|
||||
this.headerSourceID = headerSourceID;
|
||||
this.headerMessageLength = headerMessageLength;
|
||||
|
||||
ByteBuffer tempHeaderByteBuffer = ByteBuffer.allocate(15);
|
||||
tempHeaderByteBuffer.put(this.headerSync1);
|
||||
tempHeaderByteBuffer.put(this.headerSync2);
|
||||
tempHeaderByteBuffer.put(this.headerMessageType);
|
||||
tempHeaderByteBuffer.put(longToSixBytes(this.headerTimeStamp));
|
||||
tempHeaderByteBuffer.putInt(this.headerSourceID);
|
||||
tempHeaderByteBuffer.put(shortToBytes(this.headerMessageLength));
|
||||
|
||||
|
||||
this.header = tempHeaderByteBuffer.array();
|
||||
|
||||
//set the message
|
||||
this.message = message;
|
||||
|
||||
//set full message
|
||||
ByteBuffer tempMessageByteBuffer = ByteBuffer.allocate(19+this.headerMessageLength);
|
||||
tempMessageByteBuffer.put(this.header);
|
||||
tempMessageByteBuffer.put(this.message);
|
||||
|
||||
CRC32 crc = new CRC32();
|
||||
crc.reset();
|
||||
byte[] messageAndHeader = new byte[this.header.length + this.message.length];
|
||||
System.arraycopy(this.header, 0, messageAndHeader, 0, this.header.length);
|
||||
System.arraycopy(this.message, 0, messageAndHeader, this.header.length, this.message.length);
|
||||
crc.update(messageAndHeader);
|
||||
//System.out.println(Arrays.toString(messageAndHeader));
|
||||
|
||||
tempMessageByteBuffer.put(intToBytes((int) crc.getValue()));
|
||||
this.fullMessage = tempMessageByteBuffer.array();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private byte[] longToSixBytes(long x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(x);
|
||||
return Arrays.copyOfRange(buffer.array(), 2, 8);
|
||||
}
|
||||
|
||||
private byte[] shortToBytes(short x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
||||
buffer.putShort(x);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
private byte[] intToBytes(int x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||
buffer.putInt(x);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
public byte[] getFullMessage() {
|
||||
return fullMessage;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMessageDecoder {
|
||||
private byte[] fullMessage;
|
||||
private byte[] header;
|
||||
private byte[] message;
|
||||
private byte[] crc;
|
||||
|
||||
private byte headerSync1;
|
||||
private byte headerSync2;
|
||||
private byte headerMessageType;
|
||||
private byte[] headerTimeStamp;
|
||||
private byte[] headerSourceID;
|
||||
private byte[] headerMessageLength;
|
||||
|
||||
|
||||
public BinaryMessageDecoder(byte[] fullMessage) {
|
||||
this.fullMessage = fullMessage;
|
||||
}
|
||||
|
||||
public void decode() throws IndexOutOfBoundsException{
|
||||
//get the header
|
||||
this.header = Arrays.copyOfRange(this.fullMessage, 0, 15);
|
||||
|
||||
this.headerSync1 = this.header[0];
|
||||
this.headerSync2 = this.header[1];
|
||||
this.headerMessageType = this.header[2];
|
||||
this.headerTimeStamp = Arrays.copyOfRange(this.header, 3, 9);
|
||||
this.headerSourceID = Arrays.copyOfRange(this.header, 9, 13);
|
||||
this.headerMessageLength = Arrays.copyOfRange(this.header, 13, 15);
|
||||
|
||||
//get message
|
||||
this.message = Arrays.copyOfRange(this.fullMessage, 15, this.fullMessage.length - 4);
|
||||
|
||||
//get crc
|
||||
this.crc = Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - 4, fullMessage.length);
|
||||
|
||||
//run through the checks
|
||||
if (this.message.length != bytesToShort(this.headerMessageLength)){
|
||||
System.err.println("message length in header does not equal the message length");
|
||||
System.err.println("message length in header: " + bytesToInt(this.headerMessageLength));
|
||||
System.err.println("message length: " + this.message.length);
|
||||
}else if(this.headerSync1 != 0x47){
|
||||
System.err.println("Sync byte 1 is wrong");
|
||||
}else if(this.headerSync2 !=(byte) 0x83){
|
||||
System.err.println("Sync byte 2 is wrong");
|
||||
}else if(false){
|
||||
//todo check crc
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return bytesToLong(this.headerTimeStamp);
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return bytesToInt(this.headerSourceID);
|
||||
}
|
||||
|
||||
public short getMessageLength() {
|
||||
return bytesToShort(this.headerMessageLength);
|
||||
}
|
||||
|
||||
public int getMessageType(){
|
||||
return (int) this.headerMessageType;
|
||||
}
|
||||
|
||||
public byte[] getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class AverageWindDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte[] byteTime;
|
||||
byte[] byteRawPeriod;
|
||||
byte[] byteRawSpeed;
|
||||
byte[] bytePeriod2;
|
||||
byte[] byteSpeed2;
|
||||
byte[] bytePeriod3;
|
||||
byte[] byteSpeed3;
|
||||
byte[] bytePeriod4;
|
||||
byte[] byteSpeed4;
|
||||
|
||||
public AverageWindDecoder(byte[] encodedAverageWind) {
|
||||
messageVersionNumber = encodedAverageWind[0];
|
||||
byteTime = Arrays.copyOfRange(encodedAverageWind, 1, 7);
|
||||
byteRawPeriod = Arrays.copyOfRange(encodedAverageWind, 7, 9);
|
||||
byteRawSpeed = Arrays.copyOfRange(encodedAverageWind, 9, 11);
|
||||
bytePeriod2 = Arrays.copyOfRange(encodedAverageWind, 11, 13);
|
||||
byteSpeed2 = Arrays.copyOfRange(encodedAverageWind, 13, 15);
|
||||
bytePeriod3 = Arrays.copyOfRange(encodedAverageWind, 15, 17);
|
||||
byteSpeed3 = Arrays.copyOfRange(encodedAverageWind, 17, 19);
|
||||
bytePeriod4 = Arrays.copyOfRange(encodedAverageWind, 19, 21);
|
||||
byteSpeed4 = Arrays.copyOfRange(encodedAverageWind, 21, 23);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BoatLocationDecoder {
|
||||
private byte messageVersionNumber;
|
||||
private byte[] time;
|
||||
private byte[] sourceID;
|
||||
private byte[] seqNum;
|
||||
private byte deviceType;
|
||||
private byte[] latitude;
|
||||
private byte[] longitude;
|
||||
private byte[] altitude;
|
||||
private byte[] heading;
|
||||
private byte[] pitch;
|
||||
private byte[] roll;
|
||||
private byte[] boatSpeed;
|
||||
private byte[] cog;
|
||||
private byte[] sog;
|
||||
private byte[] apparentWindSpeed;
|
||||
private byte[] apparentWindAngle;
|
||||
private byte[] trueWindSpeed;
|
||||
private byte[] trueWindDirection;
|
||||
private byte[] trueWindAngle;
|
||||
private byte[] currentDrift;
|
||||
private byte[] currentSet;
|
||||
private byte[] rudderAngle;
|
||||
|
||||
private BoatLocationMessage message;
|
||||
|
||||
public BoatLocationDecoder(byte[] encodedBoatLocation) {
|
||||
messageVersionNumber = encodedBoatLocation[0];
|
||||
time = Arrays.copyOfRange(encodedBoatLocation, 1, 7);
|
||||
sourceID = Arrays.copyOfRange(encodedBoatLocation, 7, 11);
|
||||
seqNum = Arrays.copyOfRange(encodedBoatLocation, 11, 15);
|
||||
deviceType = encodedBoatLocation[15];
|
||||
latitude = Arrays.copyOfRange(encodedBoatLocation, 16, 20);
|
||||
longitude = Arrays.copyOfRange(encodedBoatLocation,20, 24);
|
||||
altitude = Arrays.copyOfRange(encodedBoatLocation, 24, 28);
|
||||
heading = Arrays.copyOfRange(encodedBoatLocation,28, 30);
|
||||
pitch =Arrays.copyOfRange(encodedBoatLocation,30,32);
|
||||
roll = Arrays.copyOfRange(encodedBoatLocation,32,34);
|
||||
boatSpeed = Arrays.copyOfRange(encodedBoatLocation,34,36);
|
||||
cog = Arrays.copyOfRange(encodedBoatLocation,36,38);
|
||||
sog = Arrays.copyOfRange(encodedBoatLocation,38, 40);
|
||||
apparentWindSpeed = Arrays.copyOfRange(encodedBoatLocation, 40, 42);
|
||||
apparentWindAngle = Arrays.copyOfRange(encodedBoatLocation, 42, 44);
|
||||
trueWindSpeed = Arrays.copyOfRange(encodedBoatLocation,44, 46);
|
||||
trueWindDirection = Arrays.copyOfRange(encodedBoatLocation, 46, 48);
|
||||
trueWindAngle = Arrays.copyOfRange(encodedBoatLocation, 48, 50);
|
||||
currentDrift = Arrays.copyOfRange(encodedBoatLocation,50,52);
|
||||
currentSet = Arrays.copyOfRange(encodedBoatLocation,52, 54);
|
||||
rudderAngle = Arrays.copyOfRange(encodedBoatLocation,54, 56);
|
||||
|
||||
message = new BoatLocationMessage(messageVersionNumber, bytesToLong(time),
|
||||
bytesToInt(sourceID), bytesToInt(seqNum),
|
||||
deviceType, bytesToInt(latitude),
|
||||
bytesToInt(longitude), bytesToInt(altitude),
|
||||
twoByteToInt(heading), bytesToShort(pitch),
|
||||
bytesToShort(roll), twoByteToInt(boatSpeed),
|
||||
twoByteToInt(cog), twoByteToInt(sog),
|
||||
twoByteToInt(apparentWindSpeed), bytesToShort(apparentWindAngle),
|
||||
twoByteToInt(trueWindSpeed), bytesToShort(trueWindDirection),
|
||||
bytesToShort(trueWindAngle), twoByteToInt(currentDrift),
|
||||
twoByteToInt(currentSet), bytesToShort(rudderAngle)
|
||||
);
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong);
|
||||
// byteBuffer.put(bytesLong[0]);
|
||||
// byteBuffer.put(bytesLong[1]);
|
||||
// byteBuffer.put(bytesLong[2]);
|
||||
// byteBuffer.put(bytesLong[3]);
|
||||
// byteBuffer.put(bytesLong[4]);
|
||||
// byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public BoatLocationMessage getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class CourseWindDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte byteWindID;
|
||||
byte loopCount;
|
||||
ArrayList<CourseWind> loopMessages = new ArrayList();
|
||||
|
||||
public CourseWindDecoder(byte[] encodedCourseWind) {
|
||||
final int lengthInBytesOfMessages = 20;
|
||||
|
||||
messageVersionNumber = encodedCourseWind[0];
|
||||
byteWindID = encodedCourseWind[1];
|
||||
loopCount = encodedCourseWind[2];
|
||||
byte[] loopMessagesBytes = Arrays.copyOfRange(encodedCourseWind, 3, lengthInBytesOfMessages*loopCount+3);
|
||||
int messageLoopIndex = 0;
|
||||
|
||||
for (int i=0; i < loopCount; i++) {
|
||||
byte[] messageBytes = Arrays.copyOfRange(loopMessagesBytes, messageLoopIndex, messageLoopIndex+20);
|
||||
ArrayList test = new ArrayList();
|
||||
byte[] windId = Arrays.copyOfRange(messageBytes, 0, 1);
|
||||
byte[] time = Arrays.copyOfRange(messageBytes, 1, 7);
|
||||
byte[] raceID = Arrays.copyOfRange(messageBytes, 7, 11);
|
||||
byte[] windDirection = Arrays.copyOfRange(messageBytes, 11, 13);
|
||||
byte[] windSpeed = Arrays.copyOfRange(messageBytes, 13, 15);
|
||||
byte[] bestUpwindAngle = Arrays.copyOfRange(messageBytes, 15, 17);
|
||||
byte[] bestDownwindAngle = Arrays.copyOfRange(messageBytes, 17, 19);
|
||||
byte[] flags = Arrays.copyOfRange(messageBytes, 19, 20);
|
||||
|
||||
CourseWind message = new CourseWind(windId[0], bytesToLong(time),
|
||||
bytesToInt(raceID), twoByteToInt(windDirection),
|
||||
twoByteToInt(windSpeed), twoByteToInt(bestUpwindAngle),
|
||||
twoByteToInt(bestDownwindAngle), flags[0]);
|
||||
|
||||
loopMessages.add(message);
|
||||
messageLoopIndex += 20;
|
||||
}
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public ArrayList<CourseWind> getLoopMessages() {
|
||||
return loopMessages;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class MarkRoundingDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte[] byteTime;
|
||||
byte[] byteAck;
|
||||
byte[] byteRaceID;
|
||||
byte[] byteSourceID;
|
||||
byte byteBoatStatus;
|
||||
byte byteRoundingSide;
|
||||
byte byteMarkType;
|
||||
byte byteMarkID;
|
||||
|
||||
public MarkRoundingDecoder(byte[] encodedMarkRounding) {
|
||||
messageVersionNumber = encodedMarkRounding[0];
|
||||
byteTime = Arrays.copyOfRange(encodedMarkRounding, 1, 7);
|
||||
byteAck = Arrays.copyOfRange(encodedMarkRounding, 7, 9);
|
||||
byteRaceID = Arrays.copyOfRange(encodedMarkRounding, 9, 13);
|
||||
byteSourceID = Arrays.copyOfRange(encodedMarkRounding, 13, 18);
|
||||
byteBoatStatus = encodedMarkRounding[18];
|
||||
byteRoundingSide = encodedMarkRounding[19];
|
||||
byteMarkType = encodedMarkRounding[20];
|
||||
byteMarkID = encodedMarkRounding[21];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class RaceStartStatusDecoder {
|
||||
private byte messageVersion;
|
||||
private byte[] timestamp;
|
||||
private byte[] ackNumber;
|
||||
private byte[] raceStartTime;
|
||||
private byte[] raceIdentifier;
|
||||
private byte notificationType;
|
||||
|
||||
private long time;
|
||||
private short ack;
|
||||
private long startTime;
|
||||
private int raceID;
|
||||
private char notification;
|
||||
|
||||
|
||||
public RaceStartStatusDecoder(byte[] encodedRaceStartStatus) {
|
||||
messageVersion = encodedRaceStartStatus[0];
|
||||
timestamp = Arrays.copyOfRange(encodedRaceStartStatus, 1, 7);
|
||||
ackNumber = Arrays.copyOfRange(encodedRaceStartStatus, 7, 9);
|
||||
raceStartTime = Arrays.copyOfRange(encodedRaceStartStatus, 9, 15);
|
||||
raceIdentifier = Arrays.copyOfRange(encodedRaceStartStatus, 15, 19);
|
||||
notificationType = encodedRaceStartStatus[19];
|
||||
|
||||
time = bytesToLong(timestamp);
|
||||
ack = bytesToShort(ackNumber);
|
||||
startTime = bytesToLong(raceStartTime);
|
||||
raceID = bytesToInt(raceIdentifier);
|
||||
notification = byteToChar(notificationType);
|
||||
}
|
||||
|
||||
private char byteToChar(byte bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
char num = byteBuffer.getChar(0);
|
||||
return num;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getMessageVersion() {
|
||||
return messageVersion;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public short getAck() {
|
||||
return ack;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public int getRaceID() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public char getNotification() {
|
||||
return notification;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,150 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.BoatStatus;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class RaceStatusDecoder {
|
||||
private byte versionNum;
|
||||
private byte[] timeBytes;
|
||||
private byte[] raceID;
|
||||
private byte raceStatus;
|
||||
private byte[] expectedStart;
|
||||
private byte[] raceWind;
|
||||
private byte[] windSpeed;
|
||||
private byte numBoats;
|
||||
private byte bytesRaceType;
|
||||
private byte[] boatsBytes;
|
||||
|
||||
private long time;
|
||||
private int race;
|
||||
private int raceState;
|
||||
private long startTime;
|
||||
private short raceWindDir;
|
||||
private short raceWindSpeed;
|
||||
private int numberOfBoats;
|
||||
private int raceType;
|
||||
private ArrayList<BoatStatus> boats = new ArrayList<>();
|
||||
|
||||
|
||||
public RaceStatusDecoder(byte[] encodedRaceStatus){
|
||||
versionNum = encodedRaceStatus[0];
|
||||
timeBytes = Arrays.copyOfRange(encodedRaceStatus, 1, 7);
|
||||
raceID = Arrays.copyOfRange(encodedRaceStatus, 7, 11);
|
||||
raceStatus = encodedRaceStatus[11];
|
||||
expectedStart = Arrays.copyOfRange(encodedRaceStatus, 12, 18);
|
||||
raceWind = Arrays.copyOfRange(encodedRaceStatus, 18, 20);
|
||||
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 20, 22);
|
||||
numBoats = encodedRaceStatus[22];
|
||||
bytesRaceType = encodedRaceStatus[23];
|
||||
boatsBytes = Arrays.copyOfRange(encodedRaceStatus, 24, 25+20*this.numBoats);
|
||||
|
||||
time = bytesToLong(timeBytes);
|
||||
race = bytesToInt(raceID);
|
||||
raceState = byteToInt(raceStatus);
|
||||
startTime = bytesToLong(expectedStart);
|
||||
raceWindDir = bytesToShort(raceWind);
|
||||
raceWindSpeed = bytesToShort(windSpeed);
|
||||
numberOfBoats = byteToInt(numBoats);
|
||||
|
||||
int boatLoopIndex = 0;
|
||||
|
||||
for (int i=0; i < numberOfBoats; i++) {
|
||||
byte[] boatBytes = Arrays.copyOfRange(boatsBytes, boatLoopIndex, boatLoopIndex+20);
|
||||
|
||||
byte[] sourceID = Arrays.copyOfRange(boatBytes, 1, 5);
|
||||
byte boatStatus = boatBytes[5];
|
||||
byte legNumber = boatBytes[6];
|
||||
byte numPenaltiesAwarded = boatBytes[7];
|
||||
byte numPenaltiesServed = boatBytes[8];
|
||||
byte[] estTimeAtNextMark = Arrays.copyOfRange(boatBytes, 9, 15);
|
||||
byte[] estTimeAtFinish = Arrays.copyOfRange(boatBytes, 15, 20);
|
||||
|
||||
BoatStatus boat = new BoatStatus(bytesToInt(sourceID),boatStatus,
|
||||
legNumber, numPenaltiesAwarded, numPenaltiesServed,
|
||||
bytesToLong(estTimeAtNextMark), bytesToLong(estTimeAtFinish));
|
||||
|
||||
boats.add(boat);
|
||||
boatLoopIndex += 20;
|
||||
}
|
||||
}
|
||||
|
||||
private int byteToInt(byte bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int intVal = byteBuffer.getInt(0);
|
||||
return intVal;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte) 0);
|
||||
byteBuffer.put((byte) 0);
|
||||
byteBuffer.put(bytesLong);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getVersionNum() {
|
||||
return versionNum;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public int getRace() {
|
||||
return race;
|
||||
}
|
||||
|
||||
public int getRaceState() {
|
||||
return raceState;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public short getRaceWindDir() {
|
||||
return raceWindDir;
|
||||
}
|
||||
|
||||
public short getRaceWindSpeed() {
|
||||
return raceWindSpeed;
|
||||
}
|
||||
|
||||
public int getNumberOfBoats() {
|
||||
return numberOfBoats;
|
||||
}
|
||||
|
||||
public int getRaceType() {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public ArrayList<BoatStatus> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
*/
|
||||
public class XMLMessageDecoder {
|
||||
private byte messageVersionNumber;
|
||||
private short ackNumber;
|
||||
private long timeStamp;
|
||||
private byte xmlMsgSubType;
|
||||
private short sequenceNumber;
|
||||
private short xmlMsgLength;
|
||||
private String xmlMessage;
|
||||
|
||||
private byte[] bytes;
|
||||
|
||||
public XMLMessageDecoder(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public void decode(){
|
||||
byte[] ackNumberBytes = Arrays.copyOfRange(bytes, 1, 3);
|
||||
byte[] timeStampBytes = Arrays.copyOfRange(bytes, 3, 9);
|
||||
byte[] sequenceNumberBytes = Arrays.copyOfRange(bytes, 10, 12);
|
||||
byte[] xmlMsgLengthBytes = Arrays.copyOfRange(bytes, 12, 14);
|
||||
byte[] xmlMessagebytes = Arrays.copyOfRange(bytes, 14, bytes.length);
|
||||
|
||||
this.xmlMsgSubType = bytes[9];
|
||||
this.messageVersionNumber = bytes[0];
|
||||
this.ackNumber = bytesToShort(ackNumberBytes);
|
||||
|
||||
this.timeStamp = bytesToLong(timeStampBytes);
|
||||
|
||||
this.sequenceNumber = bytesToShort(sequenceNumberBytes);
|
||||
this.xmlMsgLength = bytesToShort(xmlMsgLengthBytes);
|
||||
this.xmlMessage = new String(xmlMessagebytes);
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
||||
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
byteBuffer.put(bytesShort[0]);
|
||||
byteBuffer.put(bytesShort[1]);
|
||||
short shortVal = byteBuffer.getShort(0);
|
||||
return shortVal;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
|
||||
// System.out.println("====decode====");
|
||||
// for (byte i:byteBuffer.array()
|
||||
// ) {
|
||||
// System.out.println(i);
|
||||
// }
|
||||
// System.out.println("====decode====");
|
||||
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getMessageVersionNumber() {
|
||||
return messageVersionNumber;
|
||||
}
|
||||
|
||||
public short getAckNumber() {
|
||||
return ackNumber;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public byte getXmlMsgSubType() {
|
||||
return xmlMsgSubType;
|
||||
}
|
||||
|
||||
public short getSequenceNumber() {
|
||||
return sequenceNumber;
|
||||
}
|
||||
|
||||
public short getXmlMsgLength() {
|
||||
return xmlMsgLength;
|
||||
}
|
||||
|
||||
public String getXmlMessage() {
|
||||
return xmlMessage;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,347 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
import seng302.Networking.Utils.RaceMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceVisionByteEncoder {
|
||||
|
||||
public byte[] heartBeat(int seq){
|
||||
ByteBuffer heartBeat = ByteBuffer.allocate(4);
|
||||
heartBeat.putInt(seq);
|
||||
byte [] result = heartBeat.array();
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] raceStatus(long time, int race, int raceState, long startTime, short raceWindDir, short raceWindSpeed, int raceType, ArrayList<BoatInRace> boats){
|
||||
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20*boats.size());
|
||||
//Version Number 1 bytes
|
||||
byte versionNum = 0b10; //this changes with the pdf. (2)
|
||||
byte[] timeBytes = convert(time, 6);//time (6 bytes)
|
||||
byte[] raceID = ByteBuffer.allocate(4).putInt(race).array();//race identifier incase multiple races are going at once.
|
||||
byte[] raceStatus = convert(raceState, 1);//race status 0 - 10
|
||||
byte[] expectedStart = convert(startTime, 6);//number of milliseconds from Jan 1, 1970 for when the data is valid
|
||||
byte[] raceWind = ByteBuffer.allocate(2).putShort(raceWindDir).array();//North = 0x0000 East = 0x4000 South = 0x8000
|
||||
byte[] windSpeed = ByteBuffer.allocate(2).putShort(raceWindSpeed).array();//mm/sec
|
||||
byte[] numBoats = convert(boats.size(), 1);
|
||||
byte[] bytesRaceType = convert(raceType, 1);//1 match race, 2 fleet race
|
||||
|
||||
raceStatusMessage.put(versionNum);
|
||||
raceStatusMessage.put(timeBytes);
|
||||
raceStatusMessage.put(raceID);
|
||||
raceStatusMessage.put(raceStatus);
|
||||
raceStatusMessage.put(expectedStart);
|
||||
raceStatusMessage.put(raceWind);
|
||||
raceStatusMessage.put(windSpeed);
|
||||
raceStatusMessage.put(numBoats);
|
||||
raceStatusMessage.put(bytesRaceType);
|
||||
|
||||
for (int i = 0; i < boats.size(); i++){
|
||||
int sourceID = 0; //TODO use boats source id.
|
||||
byte[] legNum = convert(boats.get(i).getCurrentLeg().getLegNumber(), 1);
|
||||
byte[] numPenalties = convert(0, 1); //TODO use boats in race penalties class
|
||||
byte[] numPenaltiesServed = convert(0, 1);//TODO use boats in race penalites served.
|
||||
byte[] estNextMarkTime = convert((long)0, 6);//TODO use boats estimated time to next mark.
|
||||
byte[] estFinishTime = convert((long) 0, 6);//TODO use boats estimated time to the finish.
|
||||
|
||||
raceStatusMessage.putInt(sourceID);
|
||||
raceStatusMessage.put(legNum);
|
||||
raceStatusMessage.put(numPenalties);
|
||||
raceStatusMessage.put(numPenaltiesServed);
|
||||
raceStatusMessage.put(estNextMarkTime);
|
||||
raceStatusMessage.put(estFinishTime);
|
||||
}
|
||||
|
||||
return raceStatusMessage.array();
|
||||
}
|
||||
|
||||
public byte[] displayTextMessage(RaceMessage[] message){
|
||||
//ByteBuffer result = ByteBuffer.allocate(4 + numLines * 32);
|
||||
int messageVersionNumber = 0b1;//version number
|
||||
short ackNum = 0;//no clue what this does just a placeholder for 2 bytes.
|
||||
byte[] messLines = convert(message.length, 1);
|
||||
|
||||
// result.putInt(messageVersionNumber);
|
||||
// result.putShort(ackNum);
|
||||
// result.put(messLines);
|
||||
|
||||
ArrayList<byte[]> messages = new ArrayList<byte[]>();
|
||||
int size = 4;
|
||||
|
||||
for (int i = 0; i < message.length; i ++){
|
||||
int messageLen = message[i].getMessageText().getBytes().length;
|
||||
byte[] messageAsBytes = message[i].getMessageText().getBytes();
|
||||
if (messageLen < 30){
|
||||
messageLen = 30;
|
||||
}
|
||||
ByteBuffer mess = ByteBuffer.allocate(2 + messageLen);
|
||||
mess.put(convert(message[i].getLineNumber(), 1));
|
||||
mess.put(convert(messageLen, 1));
|
||||
for (int j = 0; j < messageLen; j ++){
|
||||
mess.put(messageAsBytes[j]);
|
||||
}
|
||||
messages.add(mess.array());
|
||||
size += 2 + messageLen;
|
||||
}
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(size);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.putShort(ackNum);
|
||||
result.put(messLines);
|
||||
|
||||
for(byte[] mess: messages){
|
||||
result.put(mess);
|
||||
}
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] raceStartStatus(long time, short ack, long startTime, int raceID, char notification){
|
||||
int messageVersion = 0b1;
|
||||
byte[] timestamp = convert(time, 6);
|
||||
byte[] ackNumber = convert(ack, 2);
|
||||
byte[] raceStartTime = convert(startTime, 6);
|
||||
int raceIdentifier = raceID;
|
||||
byte[] notificationType = convert(notification, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(20);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(timestamp);
|
||||
result.put(ackNumber);
|
||||
result.put(raceStartTime);
|
||||
result.putInt(raceIdentifier);
|
||||
result.put(notificationType);
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] yachtEventCode(long time, short acknowledgeNumber, int raceID, int destSourceID, int incidentID,
|
||||
int eventID){
|
||||
int messageVersion = 0b10;
|
||||
byte[] encodeTime = convert(time, 6);
|
||||
short ackNum = acknowledgeNumber;
|
||||
int raceUID = raceID;//TODO chekc if this is an into for a 4 char string.
|
||||
int destSource = destSourceID;
|
||||
int incident = incidentID;
|
||||
byte[] event = convert(eventID, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(22);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(encodeTime);
|
||||
result.putShort(ackNum);
|
||||
result.putInt(raceUID);
|
||||
result.putInt(destSource);
|
||||
result.putInt(incident);
|
||||
result.put(event);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] chatterText(int messageType, String message){
|
||||
int messageVersion = 0b1;
|
||||
byte[] type = convert(messageType, 1);
|
||||
byte[] length = convert(message.length(), 1);
|
||||
byte[] text = convert(message, length[0]);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(3 + text.length);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(type);
|
||||
result.put(length);
|
||||
result.put(text);
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] boatLocation(BoatLocationMessage boatLocationMessage){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] time = convert(boatLocationMessage.getTime(), 6);
|
||||
byte[] sourceID = convert(boatLocationMessage.getSourceID(), 4);
|
||||
byte[] seqNum = convert(boatLocationMessage.getSequenceNumber(), 4);
|
||||
byte deviceType = boatLocationMessage.getDeviceType();
|
||||
byte[] latitude = convert(boatLocationMessage.getLatitude(), 4);
|
||||
byte[] longitude = convert(boatLocationMessage.getLongitude(), 4);
|
||||
byte[] altitude = convert(boatLocationMessage.getAltitude(), 4);
|
||||
byte[] heading = convert(boatLocationMessage.getHeading(), 2);
|
||||
byte[] pitch = convert(boatLocationMessage.getPitch(), 2);
|
||||
byte[] roll = convert(boatLocationMessage.getRoll(), 2);
|
||||
byte[] boatSpeed = convert(boatLocationMessage.getBoatSpeed(), 2);
|
||||
byte[] cog = convert(boatLocationMessage.getBoatCOG(), 2);
|
||||
byte[] sog = convert(boatLocationMessage.getBoatSOG(), 2);
|
||||
byte[] apparentWindSpeed = convert(boatLocationMessage.getApparentWindSpeed(), 2);
|
||||
byte[] apparentWindAngle = convert(boatLocationMessage.getApparentWindAngle(), 2);
|
||||
byte[] trueWindSpeed = convert(boatLocationMessage.getTrueWindSpeed(), 2);
|
||||
byte[] trueWindDirection = convert(boatLocationMessage.getTrueWindDirection(), 2);
|
||||
byte[] trueWindAngle = convert(boatLocationMessage.getTrueWindAngle(), 2);
|
||||
byte[] currentDrift = convert(boatLocationMessage.getCurrentDrift(), 2);
|
||||
byte[] currentSet = convert(boatLocationMessage.getCurrentSet(), 2);
|
||||
byte[] rudderAngle = convert(boatLocationMessage.getRudderAngle(), 2);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(56);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(time);
|
||||
result.put(sourceID);
|
||||
result.put(seqNum);
|
||||
result.put(deviceType);
|
||||
result.put(latitude);
|
||||
result.put(longitude);
|
||||
result.put(altitude);
|
||||
result.put(heading);
|
||||
result.put(pitch);
|
||||
result.put(roll);
|
||||
result.put(boatSpeed);
|
||||
result.put(cog);
|
||||
result.put(sog);
|
||||
result.put(apparentWindSpeed);
|
||||
result.put(apparentWindAngle);
|
||||
result.put(trueWindSpeed);
|
||||
result.put(trueWindDirection);
|
||||
result.put(trueWindAngle);
|
||||
result.put(currentDrift);
|
||||
result.put(currentSet);
|
||||
result.put(rudderAngle);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] markRounding(int time, int ackNumber, int raceID, int sourceID, int boatStatus, int roundingSide, int markType, int markID){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] byteTime = convert(time, 6);
|
||||
byte[] byteAck = convert(ackNumber, 2);
|
||||
byte[] byteRaceID = convert(raceID, 4);
|
||||
byte[] byteSourceID = convert(sourceID, 4);
|
||||
byte[] byteBoatStatus = convert(boatStatus, 1);
|
||||
byte[] byteRoundingSide = convert(roundingSide, 1);
|
||||
byte[] byteMarkType = convert(markType, 1);
|
||||
byte[] byteMarkID = convert(markID, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(21);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteTime);
|
||||
result.put(byteAck);
|
||||
result.put(byteRaceID);
|
||||
result.put(byteSourceID);
|
||||
result.put(byteBoatStatus);
|
||||
result.put(byteRoundingSide);
|
||||
result.put(byteMarkType);
|
||||
result.put(byteMarkID);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] courseWind(byte windID, ArrayList<CourseWind> courseWinds){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte byteWindID = windID;
|
||||
byte[] loopcount = convert(courseWinds.size(), 1);
|
||||
ByteBuffer result = ByteBuffer.allocate(3 + 20 * courseWinds.size());
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteWindID);
|
||||
result.put(loopcount);
|
||||
for (CourseWind wind: courseWinds){
|
||||
result.put(convert(wind.getID(), 1));
|
||||
result.put(convert(wind.getTime(), 6));
|
||||
result.put(convert(wind.getRaceID(), 4));
|
||||
result.put(convert(wind.getWindDirection(), 2));
|
||||
result.put(convert(wind.getWindSpeed(), 2));
|
||||
result.put(convert(wind.getBestUpwindAngle(), 2));
|
||||
result.put(convert(wind.getBestDownwindAngle(), 2));
|
||||
result.put(convert(wind.getFlags(), 1));
|
||||
}
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] averageWind(int time, int rawPeriod, int rawSampleSpeed, int period2, int speed2, int period3, int speed3, int period4, int speed4){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] byteTime = convert(time,6);
|
||||
byte[] byteRawPeriod = convert(rawPeriod, 2);
|
||||
byte[] byteRawSpeed = convert(rawSampleSpeed, 2);
|
||||
byte[] bytePeriod2 = convert(period2, 2);
|
||||
byte[] byteSpeed2 = convert(speed2, 2);
|
||||
byte[] bytePeriod3 = convert(period3, 2);
|
||||
byte[] byteSpeed3 = convert(speed3, 2);
|
||||
byte[] bytePeriod4 = convert(period4, 2);
|
||||
byte[] byteSpeed4 = convert(speed4, 2);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(23);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteTime);
|
||||
result.put(byteRawPeriod);
|
||||
result.put(byteRawSpeed);
|
||||
result.put(bytePeriod2);
|
||||
result.put(byteSpeed2);
|
||||
result.put(bytePeriod3);
|
||||
result.put(byteSpeed3);
|
||||
result.put(bytePeriod4);
|
||||
result.put(byteSpeed4);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] convert(String s, int size){
|
||||
byte[] m = s.getBytes(Charset.forName("UTF-8"));
|
||||
int length = m.length;
|
||||
byte[] result;
|
||||
if (length > 255){
|
||||
length = 255;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
result = Arrays.copyOfRange(m, 0, length + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] convert(int n, int size){
|
||||
byte[] result;
|
||||
if (size > 4){
|
||||
result = new byte[4];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.putInt(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 4 - size, 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] convert(long n, int size){
|
||||
byte[] result;
|
||||
if (size > 8){
|
||||
result = new byte[8];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.putLong(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 8 - size, 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public byte[] convert(short n, int size){
|
||||
byte[] result;
|
||||
if (size > 2){
|
||||
result = new byte[2];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
||||
byteBuffer.putShort(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 2 - size, 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 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(byte messageVersionNumber, short ackNumber, long timeStamp, byte xmlMsgSubType, short sequenceNumber, short xmlMsgLenght, String xmlMessage) {
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
this.ackNumber = ackNumber;
|
||||
this.timeStamp = timeStamp;
|
||||
this.xmlMsgSubType = xmlMsgSubType;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.xmlMsgLength = xmlMsgLenght;
|
||||
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 = new byte[2];
|
||||
ackNumberBytes[0] = (byte) (ackNumber & 0xff);
|
||||
ackNumberBytes[1] = (byte) ((ackNumber >> 8) & 0xff);
|
||||
|
||||
//sequenceNumber converted to bytes
|
||||
byte[] sequenceNumberBytes = new byte[2];
|
||||
sequenceNumberBytes[0] = (byte) (sequenceNumber & 0xff);
|
||||
sequenceNumberBytes[1] = (byte) ((sequenceNumber >> 8) & 0xff);
|
||||
|
||||
//xmlMsgLength converted to bytes
|
||||
byte[] xmlMsgLengthBytes = new byte[2];
|
||||
xmlMsgLengthBytes[0] = (byte) (xmlMsgLength & 0xff);
|
||||
xmlMsgLengthBytes[1] = (byte) ((xmlMsgLength >> 8) & 0xff);
|
||||
|
||||
|
||||
tempOutputByteBuffer.put(messageVersionNumber);
|
||||
tempOutputByteBuffer.put(ackNumberBytes);
|
||||
tempOutputByteBuffer.put(longToSixBytes(timeStamp));
|
||||
tempOutputByteBuffer.put(xmlMsgSubType);
|
||||
tempOutputByteBuffer.put(sequenceNumberBytes);
|
||||
tempOutputByteBuffer.put(xmlMsgLengthBytes);
|
||||
tempOutputByteBuffer.put(messageBytes);
|
||||
|
||||
return tempOutputByteBuffer.array();
|
||||
}
|
||||
|
||||
private byte[] longToSixBytes(long x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(x);
|
||||
|
||||
// System.out.println("====encode====");
|
||||
// for (byte i:buffer.array()
|
||||
// ) {
|
||||
// System.out.println(i);
|
||||
// }
|
||||
// System.out.println("====encode====");
|
||||
|
||||
return Arrays.copyOfRange(buffer.array(), 2, 8);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,357 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by f123 on 21-Apr-17.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents the information in a boat location message (AC streaming spec: 4.9).
|
||||
*/
|
||||
public class BoatLocationMessage
|
||||
{
|
||||
///Version number of the message - is always 1.
|
||||
private byte messageVersionNumber = 1;
|
||||
|
||||
///Time of the event - milliseconds since jan 1 1970. Proper type is 6 byte int.
|
||||
private long time;
|
||||
|
||||
///Source ID of the boat.
|
||||
private int sourceID;
|
||||
|
||||
///Sequence number of the message.
|
||||
private int sequenceNumber;
|
||||
|
||||
///Device type of the message (physical source of the message).
|
||||
private byte deviceType;
|
||||
|
||||
///Latitude of the boat.
|
||||
private int latitude;
|
||||
|
||||
///Longitude of the boat.
|
||||
private int longitude;
|
||||
|
||||
///Altitude of the boat.
|
||||
private int altitude;
|
||||
|
||||
///Heading of the boat. Clockwise, 0 = north. Proper type is unsigned 2 byte int.
|
||||
private int heading;
|
||||
|
||||
///Pitch of the boat.
|
||||
private short pitch;
|
||||
|
||||
///Roll of the boat.
|
||||
private short roll;
|
||||
|
||||
///Speed of the boat. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int boatSpeed;
|
||||
|
||||
///Course over ground (COG) of the boat. Proper type is unsigned 2 byte int.
|
||||
private int boatCOG;
|
||||
|
||||
///Speed over ground (SOG) of the boat. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int boatSOG;
|
||||
|
||||
///Apparent wind speed at time of event. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int apparentWindSpeed;
|
||||
|
||||
///Apparent wind angle at time of the event. Wind over starboard = positive.
|
||||
private short apparentWindAngle;
|
||||
|
||||
///True wind speed. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int trueWindSpeed;
|
||||
|
||||
private short trueWindDirection;
|
||||
|
||||
///True wind angle. Clockwise compass direction, 0 = north.
|
||||
private short trueWindAngle;
|
||||
|
||||
///Current drift. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int currentDrift;
|
||||
|
||||
///Current set. Proper type is unsigned 2 byte int. Clockwise compass direction, 0 = north.
|
||||
private int currentSet;
|
||||
|
||||
///Rudder angle. Positive is rudder set to turn yacht to port.
|
||||
private short rudderAngle;
|
||||
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
*/
|
||||
public BoatLocationMessage()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Ctor, with all parameters.
|
||||
* @param messageVersionNumber
|
||||
* @param time
|
||||
* @param sourceID
|
||||
* @param sequenceNumber
|
||||
* @param deviceType
|
||||
* @param latitude
|
||||
* @param longitude
|
||||
* @param altitude
|
||||
* @param heading
|
||||
* @param pitch
|
||||
* @param roll
|
||||
* @param boatSpeed
|
||||
* @param boatCOG
|
||||
* @param boatSOG
|
||||
* @param apparentWindSpeed
|
||||
* @param apparentWindAngle
|
||||
* @param trueWindSpeed
|
||||
* @param trueWindDirection
|
||||
* @param trueWindAngle
|
||||
* @param currentDrift
|
||||
* @param currentSet
|
||||
* @param rudderAngle
|
||||
*/
|
||||
public BoatLocationMessage(byte messageVersionNumber, long time, int sourceID, int sequenceNumber, byte deviceType, int latitude, int longitude, int altitude, int heading, short pitch, short roll, int boatSpeed, int boatCOG, int boatSOG, int apparentWindSpeed, short apparentWindAngle, int trueWindSpeed, short trueWindDirection, short trueWindAngle, int currentDrift, int currentSet, short rudderAngle)
|
||||
{
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
this.time = time;
|
||||
this.sourceID = sourceID;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.deviceType = deviceType;
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
this.altitude = altitude;
|
||||
this.heading = heading;
|
||||
this.pitch = pitch;
|
||||
this.roll = roll;
|
||||
this.boatSpeed = boatSpeed;
|
||||
this.boatCOG = boatCOG;
|
||||
this.boatSOG = boatSOG;
|
||||
this.apparentWindSpeed = apparentWindSpeed;
|
||||
this.apparentWindAngle = apparentWindAngle;
|
||||
this.trueWindSpeed = trueWindSpeed;
|
||||
this.trueWindDirection = trueWindDirection;
|
||||
this.trueWindAngle = trueWindAngle;
|
||||
this.currentDrift = currentDrift;
|
||||
this.currentSet = currentSet;
|
||||
this.rudderAngle = rudderAngle;
|
||||
}
|
||||
|
||||
|
||||
//Getters and setters for message properties.
|
||||
|
||||
|
||||
public byte getMessageVersionNumber()
|
||||
{
|
||||
return messageVersionNumber;
|
||||
}
|
||||
|
||||
public void setMessageVersionNumber(byte messageVersionNumber)
|
||||
{
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
}
|
||||
|
||||
public long getTime()
|
||||
{
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time)
|
||||
{
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public int getSourceID()
|
||||
{
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public void setSourceID(int sourceID)
|
||||
{
|
||||
this.sourceID = sourceID;
|
||||
}
|
||||
|
||||
public int getSequenceNumber()
|
||||
{
|
||||
return sequenceNumber;
|
||||
}
|
||||
|
||||
public void setSequenceNumber(int sequenceNumber)
|
||||
{
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
}
|
||||
|
||||
public byte getDeviceType()
|
||||
{
|
||||
return deviceType;
|
||||
}
|
||||
|
||||
public void setDeviceType(byte deviceType)
|
||||
{
|
||||
this.deviceType = deviceType;
|
||||
}
|
||||
|
||||
public int getLatitude()
|
||||
{
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(int latitude)
|
||||
{
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public int getLongitude()
|
||||
{
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(int longitude)
|
||||
{
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public int getAltitude()
|
||||
{
|
||||
return altitude;
|
||||
}
|
||||
|
||||
public void setAltitude(int altitude)
|
||||
{
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
public int getHeading()
|
||||
{
|
||||
return heading;
|
||||
}
|
||||
|
||||
public void setHeading(int heading)
|
||||
{
|
||||
this.heading = heading;
|
||||
}
|
||||
|
||||
public short getPitch()
|
||||
{
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public void setPitch(short pitch)
|
||||
{
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public short getRoll()
|
||||
{
|
||||
return roll;
|
||||
}
|
||||
|
||||
public void setRoll(short roll)
|
||||
{
|
||||
this.roll = roll;
|
||||
}
|
||||
|
||||
public int getBoatSpeed()
|
||||
{
|
||||
return boatSpeed;
|
||||
}
|
||||
|
||||
public void setBoatSpeed(int boatSpeed)
|
||||
{
|
||||
this.boatSpeed = boatSpeed;
|
||||
}
|
||||
|
||||
public int getBoatCOG()
|
||||
{
|
||||
return boatCOG;
|
||||
}
|
||||
|
||||
public void setBoatCOG(int boatCOG)
|
||||
{
|
||||
this.boatCOG = boatCOG;
|
||||
}
|
||||
|
||||
public int getBoatSOG()
|
||||
{
|
||||
return boatSOG;
|
||||
}
|
||||
|
||||
public void setBoatSOG(int boatSOG)
|
||||
{
|
||||
this.boatSOG = boatSOG;
|
||||
}
|
||||
|
||||
public int getApparentWindSpeed()
|
||||
{
|
||||
return apparentWindSpeed;
|
||||
}
|
||||
|
||||
public void setApparentWindSpeed(int apparentWindSpeed)
|
||||
{
|
||||
this.apparentWindSpeed = apparentWindSpeed;
|
||||
}
|
||||
|
||||
public short getApparentWindAngle()
|
||||
{
|
||||
return apparentWindAngle;
|
||||
}
|
||||
|
||||
public void setApparentWindAngle(short apparentWindAngle)
|
||||
{
|
||||
this.apparentWindAngle = apparentWindAngle;
|
||||
}
|
||||
|
||||
public int getTrueWindSpeed()
|
||||
{
|
||||
return trueWindSpeed;
|
||||
}
|
||||
|
||||
public void setTrueWindSpeed(int trueWindSpeed)
|
||||
{
|
||||
this.trueWindSpeed = trueWindSpeed;
|
||||
}
|
||||
|
||||
public short getTrueWindDirection() {
|
||||
return trueWindDirection;
|
||||
}
|
||||
|
||||
public void setTrueWindDirection(short trueWindDirection) {
|
||||
this.trueWindDirection = trueWindDirection;
|
||||
}
|
||||
|
||||
public short getTrueWindAngle()
|
||||
{
|
||||
return trueWindAngle;
|
||||
}
|
||||
|
||||
public void setTrueWindAngle(short trueWindAngle)
|
||||
{
|
||||
this.trueWindAngle = trueWindAngle;
|
||||
}
|
||||
|
||||
public int getCurrentDrift()
|
||||
{
|
||||
return currentDrift;
|
||||
}
|
||||
|
||||
public void setCurrentDrift(int currentDrift)
|
||||
{
|
||||
this.currentDrift = currentDrift;
|
||||
}
|
||||
|
||||
public int getCurrentSet()
|
||||
{
|
||||
return currentSet;
|
||||
}
|
||||
|
||||
public void setCurrentSet(int currentSet)
|
||||
{
|
||||
this.currentSet = currentSet;
|
||||
}
|
||||
|
||||
public short getRudderAngle()
|
||||
{
|
||||
return rudderAngle;
|
||||
}
|
||||
|
||||
public void setRudderAngle(short rudderAngle)
|
||||
{
|
||||
this.rudderAngle = rudderAngle;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class BoatStatus {
|
||||
private int sourceID;
|
||||
private byte boatStatus;
|
||||
private byte legNumber;
|
||||
private byte numPenaltiesAwarded;
|
||||
private byte numPenaltiesServed;
|
||||
private long estTimeAtNextMark;
|
||||
private long estTimeAtFinish;
|
||||
|
||||
public BoatStatus(int sourceID, byte boatStatus, byte legNumber, byte numPenaltiesAwarded, byte numPenaltiesServed, long estTimeAtNextMark, long estTimeAtFinish) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatStatus = boatStatus;
|
||||
this.legNumber = legNumber;
|
||||
this.numPenaltiesAwarded = numPenaltiesAwarded;
|
||||
this.numPenaltiesServed = numPenaltiesServed;
|
||||
this.estTimeAtNextMark = estTimeAtNextMark;
|
||||
this.estTimeAtFinish = estTimeAtFinish;
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public byte getBoatStatus() {
|
||||
return boatStatus;
|
||||
}
|
||||
|
||||
public byte getLegNumber() {
|
||||
return legNumber;
|
||||
}
|
||||
|
||||
public byte getNumPenaltiesAwarded() {
|
||||
return numPenaltiesAwarded;
|
||||
}
|
||||
|
||||
public byte getNumPenaltiesServed() {
|
||||
return numPenaltiesServed;
|
||||
}
|
||||
|
||||
public long getEstTimeAtNextMark() {
|
||||
return estTimeAtNextMark;
|
||||
}
|
||||
|
||||
public long getEstTimeAtFinish() {
|
||||
return estTimeAtFinish;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 21/04/17.
|
||||
*/
|
||||
public class CourseWind {
|
||||
|
||||
private int ID, raceID, windDirection, windSpeed, bestUpwindAngle, bestDownwindAngle, flags;
|
||||
private long time;
|
||||
|
||||
public CourseWind(int ID, long time, int raceID, int windDirection, int windSpeed, int bestUpwindAngle, int bestDownwindAngle,
|
||||
int flags){
|
||||
this.ID = ID;
|
||||
this.time = time;
|
||||
this.raceID = raceID;
|
||||
this.windDirection = windDirection;
|
||||
this.windSpeed = windSpeed;
|
||||
this.bestUpwindAngle = bestUpwindAngle;
|
||||
this.bestDownwindAngle = bestDownwindAngle;
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public int getRaceID() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public int getWindDirection() {
|
||||
return windDirection;
|
||||
}
|
||||
|
||||
public int getWindSpeed() {
|
||||
return windSpeed;
|
||||
}
|
||||
|
||||
public int getBestUpwindAngle() {
|
||||
return bestUpwindAngle;
|
||||
}
|
||||
|
||||
public int getBestDownwindAngle() {
|
||||
return bestDownwindAngle;
|
||||
}
|
||||
|
||||
public int getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public enum MessageType {
|
||||
HEARTBEAT(1), RACESTATUS(12), DISPLAYTEXTMESSAGE(20),
|
||||
XMLMESSAGE(26), RACESTARTSTATUS(27), YACHTEVENTCODE(29), YACHTACTIONCODE(31),
|
||||
CHATTERTEXT(36), BOATLOCATION(37), MARKROUNDING(38), COURSEWIND(44), AVGWIND(47);
|
||||
|
||||
private byte value;
|
||||
|
||||
private MessageType(int value) { this.value = (byte)value; }
|
||||
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceMessage {
|
||||
|
||||
private int lineNumber;
|
||||
private String messageText;
|
||||
|
||||
public RaceMessage(int lineNumber, String messageText){
|
||||
this.lineNumber = lineNumber;
|
||||
this.messageText = messageText;
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.BinaryMesageEncoder;
|
||||
import seng302.Networking.BinaryMessageDecoder;
|
||||
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMessageDecoderTest {
|
||||
@Test
|
||||
public void decodeTest(){
|
||||
try{
|
||||
StringBuilder xmlString;
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedMessage = testEncoder.encode();
|
||||
|
||||
BinaryMesageEncoder testMessage = new BinaryMesageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
|
||||
|
||||
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
|
||||
testDecoder.decode();
|
||||
|
||||
//message length
|
||||
Assert.assertEquals((short) encodedMessage.length,testDecoder.getMessageLength());
|
||||
//time stamp
|
||||
Assert.assertEquals(time,testDecoder.getTimeStamp());
|
||||
//source ID
|
||||
Assert.assertEquals((short) 1, testDecoder.getSourceID());
|
||||
//message type
|
||||
Assert.assertEquals(26, testDecoder.getMessageType());
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(testDecoder.getMessage());
|
||||
decoderXML.decode();
|
||||
|
||||
//tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
|
||||
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.BoatLocationDecoder;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class BoatLocationDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
BoatLocationMessage testMessage = new BoatLocationMessage((byte)1, time, (byte)2,
|
||||
3, (byte) 1, 180, -180, 4, (short)5,
|
||||
(short)6, (short)7, 8, 9, 10, 11,
|
||||
(short) 12, 13,(short) 14 ,(short) 15,
|
||||
16, 17, (short) 18);
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
byte [] testEncodedMessage = raceVisionByteEncoder.boatLocation(testMessage);
|
||||
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
|
||||
BoatLocationMessage decodedTest = testDecoder.getMessage();
|
||||
|
||||
Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber());
|
||||
Assert.assertEquals(testMessage.getTime(), decodedTest.getTime());
|
||||
Assert.assertEquals(testMessage.getSequenceNumber(), decodedTest.getSequenceNumber());
|
||||
Assert.assertEquals(testMessage.getDeviceType(), decodedTest.getDeviceType());
|
||||
Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude());
|
||||
Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude());
|
||||
Assert.assertEquals(testMessage.getAltitude(), decodedTest.getAltitude());
|
||||
Assert.assertEquals(testMessage.getHeading(), decodedTest.getHeading());
|
||||
Assert.assertEquals(testMessage.getPitch(), decodedTest.getPitch());
|
||||
Assert.assertEquals(testMessage.getRoll(), decodedTest.getRoll());
|
||||
Assert.assertEquals(testMessage.getBoatSpeed(), decodedTest.getBoatSpeed());
|
||||
|
||||
Assert.assertEquals(testMessage.getBoatCOG(), decodedTest.getBoatCOG());
|
||||
Assert.assertEquals(testMessage.getBoatSOG(), decodedTest.getBoatSOG());
|
||||
Assert.assertEquals(testMessage.getApparentWindSpeed(), decodedTest.getApparentWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindSpeed(), decodedTest.getTrueWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindDirection(), decodedTest.getTrueWindDirection());
|
||||
Assert.assertEquals(testMessage.getTrueWindAngle(), decodedTest.getTrueWindAngle());
|
||||
Assert.assertEquals(testMessage.getCurrentDrift(), decodedTest.getCurrentDrift());
|
||||
Assert.assertEquals(testMessage.getCurrentSet(), decodedTest.getCurrentSet());
|
||||
Assert.assertEquals(testMessage.getRudderAngle(), decodedTest.getRudderAngle());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.CourseWindDecoder;
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class CourseWindDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
CourseWind testCourseWind1 = new CourseWind(1, time, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind2 = new CourseWind(2, time2, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
ArrayList<CourseWind> testCourseWinds = new ArrayList<CourseWind>();
|
||||
testCourseWinds.add(testCourseWind1);
|
||||
testCourseWinds.add(testCourseWind2);
|
||||
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
|
||||
byte[] testEncodedCourseWind = raceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
|
||||
|
||||
CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind);
|
||||
|
||||
ArrayList<CourseWind> testDecodedCourseWinds = testDecoder.getLoopMessages();
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(0).getID(), testDecodedCourseWinds.get(0).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getTime(), testDecodedCourseWinds.get(0).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getRaceID(), testDecodedCourseWinds.get(0).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindDirection(), testDecodedCourseWinds.get(0).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindSpeed(), testDecodedCourseWinds.get(0).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestUpwindAngle(), testDecodedCourseWinds.get(0).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestDownwindAngle(), testDecodedCourseWinds.get(0).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getFlags(), testDecodedCourseWinds.get(0).getFlags());
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(1).getID(), testDecodedCourseWinds.get(1).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getTime(), testDecodedCourseWinds.get(1).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getRaceID(), testDecodedCourseWinds.get(1).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindDirection(), testDecodedCourseWinds.get(1).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindSpeed(), testDecodedCourseWinds.get(1).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestUpwindAngle(), testDecodedCourseWinds.get(1).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestDownwindAngle(), testDecodedCourseWinds.get(1).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getFlags(), testDecodedCourseWinds.get(1).getFlags());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.RaceStartStatusDecoder;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class RaceStartStatusDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
long time2 = System.currentTimeMillis();
|
||||
byte[] encodedRaceStartStatus = raceVisionByteEncoder.raceStartStatus(time, (short)1,
|
||||
time2, 2, (char)3);
|
||||
|
||||
RaceStartStatusDecoder testDecoder = new RaceStartStatusDecoder(encodedRaceStartStatus);
|
||||
|
||||
Assert.assertEquals(0b1, testDecoder.getMessageVersion());
|
||||
Assert.assertEquals(time, testDecoder.getTime());
|
||||
Assert.assertEquals(1, testDecoder.getAck());
|
||||
Assert.assertEquals(time2, testDecoder.getStartTime());
|
||||
Assert.assertEquals(2, testDecoder.getRaceID());
|
||||
Assert.assertEquals((char)3, testDecoder.getNotification());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import javafx.scene.paint.Color;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Networking.MessageDecoders.RaceStatusDecoder;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class RaceStatusDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
BoatInRace boat1 = new BoatInRace("Test", 1, Color.ALICEBLUE, "tt", 1);
|
||||
Leg testLeg = new Leg("test leg", 1);
|
||||
boat1.setCurrentLeg(testLeg);
|
||||
BoatInRace boat2 = new BoatInRace("Test2", 1, Color.ALICEBLUE, "tt", 1);
|
||||
boat2.setCurrentLeg(testLeg);
|
||||
ArrayList boats = new ArrayList();
|
||||
boats.add(boat1);
|
||||
boats.add(boat2);
|
||||
long time2 = System.currentTimeMillis();
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
|
||||
byte[] encodedRaceStatus = raceVisionByteEncoder.raceStatus(time, 1, 2, time2,
|
||||
(short)2, (short)3,4, boats);
|
||||
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);
|
||||
|
||||
Assert.assertEquals(0b10, decoderTest.getVersionNum());
|
||||
Assert.assertEquals(time, decoderTest.getTime());
|
||||
Assert.assertEquals(1, decoderTest.getRace());
|
||||
Assert.assertEquals(2, decoderTest.getRaceState());
|
||||
Assert.assertEquals(time2, decoderTest.getStartTime());
|
||||
Assert.assertEquals((short)2, decoderTest.getRaceWindDir());
|
||||
Assert.assertEquals((short)3, decoderTest.getRaceWindSpeed());
|
||||
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getBoatStatus());
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getLegNumber());
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getNumPenaltiesAwarded());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
*/
|
||||
public class XMLMessageDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
try{
|
||||
StringBuilder xmlString;
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(encodedXML);
|
||||
decoderXML.decode();
|
||||
|
||||
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
|
||||
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 19/04/17.
|
||||
*/
|
||||
public class XMLMessageEncoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
StringBuilder xmlString;
|
||||
try{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
//1 + 2 + 6 + 1 + 2 + 2 + 374
|
||||
Assert.assertEquals(388, encodedXML.length);
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByteArrayNullTest(){
|
||||
StringBuilder xmlString;
|
||||
try{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)1, xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
Assert.assertEquals(null, encodedXML);
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<BoatConfig>
|
||||
<Modified>2017-04-19T15:49:40+1200</Modified>
|
||||
<Version>1</Version>
|
||||
<Settings>
|
||||
<RaceBoatType Type="AC45" />
|
||||
<BoatDimension BoatLength="14.019" HullLength="13.449" />
|
||||
<ZoneSize MarkZoneSize="40.347" CourseZoneSize="40.347" />
|
||||
<ZoneLimits Limit1="200" Limit2="100" Limit3="40.347" Limit4="0" Limit5="-100" />
|
||||
</Settings>
|
||||
<BoatShapes>
|
||||
<BoatShape ShapeID="0">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-2.659" />
|
||||
<Vtx Seq="2" Y="18.359" X="-2.659" />
|
||||
<Vtx Seq="3" Y="18.359" X="2.659" />
|
||||
<Vtx Seq="4" Y="0" X="2.659" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="1">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.278" />
|
||||
<Vtx Seq="2" Y="8.876" X="-1.278" />
|
||||
<Vtx Seq="3" Y="8.876" X="1.278" />
|
||||
<Vtx Seq="4" Y="0" X="1.278" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="2">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.1" />
|
||||
<Vtx Seq="2" Y="8.3" X="-1.1" />
|
||||
<Vtx Seq="3" Y="8.3" X="1.1" />
|
||||
<Vtx Seq="4" Y="0" X="1.1" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="3">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-0.75" />
|
||||
<Vtx Seq="2" Y="3" X="-0.75" />
|
||||
<Vtx Seq="3" Y="3" X="0.75" />
|
||||
<Vtx Seq="4" Y="0" X="0.75" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="4">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-3.46" />
|
||||
<Vtx Seq="2" Y="13.449" X="-3.46" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="13.449" X="3.46" />
|
||||
<Vtx Seq="5" Y="0" X="3.46" />
|
||||
</Vertices>
|
||||
<Catamaran>
|
||||
<Vtx Seq="1" Y="1.769" X="-2.752" />
|
||||
<Vtx Seq="2" Y="0" X="-2.813" />
|
||||
<Vtx Seq="3" Y="0" X="-3.34" />
|
||||
<Vtx Seq="4" Y="5.351" X="-3.46" />
|
||||
<Vtx Seq="5" Y="10.544" X="-3.387" />
|
||||
<Vtx Seq="6" Y="13.449" X="-3.075" />
|
||||
<Vtx Seq="7" Y="10.851" X="-2.793" />
|
||||
<Vtx Seq="8" Y="6.669" X="-2.699" />
|
||||
<Vtx Seq="9" Y="6.669" X="2.699" />
|
||||
<Vtx Seq="10" Y="10.851" X="2.793" />
|
||||
<Vtx Seq="11" Y="13.449" X="3.075" />
|
||||
<Vtx Seq="12" Y="10.544" X="3.387" />
|
||||
<Vtx Seq="13" Y="5.351" X="3.46" />
|
||||
<Vtx Seq="14" Y="0" X="3.34" />
|
||||
<Vtx Seq="15" Y="0" X="2.813" />
|
||||
<Vtx Seq="16" Y="1.769" X="2.752" />
|
||||
</Catamaran>
|
||||
<Bowsprit>
|
||||
<Vtx Seq="1" Y="6.669" X="-0.2" />
|
||||
<Vtx Seq="2" Y="11.377" X="-0.2" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="11.377" X="0.2" />
|
||||
<Vtx Seq="5" Y="6.669" X="0.2" />
|
||||
</Bowsprit>
|
||||
<Trampoline>
|
||||
<Vtx Seq="1" Y="2" X="-2.699" />
|
||||
<Vtx Seq="2" Y="6.438" X="-2.699" />
|
||||
<Vtx Seq="3" Y="6.438" X="2.699" />
|
||||
<Vtx Seq="4" Y="2" X="2.699" />
|
||||
</Trampoline>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="5" />
|
||||
</BoatShapes>
|
||||
<Boats>>
|
||||
<Boat Type="Yacht" SourceID="101" ShapeID="4" HullNum="AC4501" ShortName="USA"
|
||||
BoatName="ORACLE TEAM USA" Country="USA">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="102" ShapeID="4" HullNum="AC4502" ShortName="GBR"
|
||||
BoatName="Land Rover BAR" Country="United Kingdom">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="103" ShapeID="4" HullNum="AC4503" ShortName="JPN"
|
||||
BoatName="SoftBank Team Japan" Country="Japan">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="104" ShapeID="4" HullNum="AC4504" ShortName="FRA"
|
||||
BoatName="Groupama Team France" Country="France">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="105" ShapeID="4" HullNum="AC4505" ShortName="SWE"
|
||||
BoatName="Artemis Racing" Country="Sweden">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="106" ShapeID="4" HullNum="AC4506" ShortName="NZL"
|
||||
BoatName="Emirates Team New Zealand" Country="New Zealand">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
</Boats>
|
||||
</BoatConfig>
|
||||
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Race>
|
||||
<RaceID>17041901</RaceID>
|
||||
<RaceType>Fleet</RaceType>
|
||||
<CreationTimeDate>2017-04-19T15:30:00+1200</CreationTimeDate >
|
||||
<RaceStartTime Time="2019-06-01T13:30:00-0400" Postpone="false" />
|
||||
<Participants>
|
||||
<Yacht SourceID="001" Entry="Port" />
|
||||
<Yacht SourceID="002" Entry="Port" />
|
||||
<Yacht SourceID="003" Entry="Port" />
|
||||
<Yacht SourceID="004" Entry="Port" />
|
||||
<Yacht SourceID="005" Entry="Port" />
|
||||
<Yacht SourceID="006" Entry="Port" />
|
||||
</Participants>
|
||||
<Course>
|
||||
<CompoundMark CompoundMarkID="1" Name="StartLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.296577" TargetLng="-64.854304" SourceID="101" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.293771" TargetLng="-64.855242" SourceID="102" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="2" Name="M1">
|
||||
<Mark Name="M1" TargetLat="32.293039" TargetLng="-64.843983" SourceID="103" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="3" Name="WindwardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.284680" TargetLng="-64.850045" SourceID="104" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.280164" TargetLng="-64.847591" SourceID="105" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="4" Name="LeewardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.309693" TargetLng="-64.835249" SourceID="106" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.308046" TargetLng="-64.831785" SourceID="107" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="5" Name="FinishLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.317379" TargetLng="-64.839291" SourceID="108" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.317257" TargetLng="-64.836260" SourceID="109" />
|
||||
</CompoundMark>
|
||||
</Course>
|
||||
<CompoundMarkSequence>
|
||||
<Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" />
|
||||
<Corner SeqID="2" CompoundMarkID="2" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="4" CompoundMarkID="4" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="5" CompoundMarkID="5" Rounding="SP" ZoneSize="3"/>
|
||||
</CompoundMarkSequence>
|
||||
<CourseLimit>
|
||||
<Limit SeqID="1" Lat="32.313922" Lon="-64.837168"/>
|
||||
<Limit SeqID="2" Lat="32.317379" Lon="-64.839291"/>
|
||||
<Limit SeqID="3" Lat="32.317911" Lon="-64.836996"/>
|
||||
<Limit SeqID="4" Lat="32.317257" Lon="-64.836260"/>
|
||||
<Limit SeqID="5" Lat="32.304273" Lon="-64.822834"/>
|
||||
|
||||
<Limit SeqID="6" Lat="32.279097" Lon="-64.841545"/>
|
||||
<Limit SeqID="7" Lat="32.279604" Lon="-64.849871"/>
|
||||
<Limit SeqID="8" Lat="32.289545" Lon="-64.854162"/>
|
||||
<Limit SeqID="9" Lat="32.290198" Lon="-64.858711"/>
|
||||
<Limit SeqID="10" Lat="32.297164" Lon="-64.856394"/>
|
||||
<Limit SeqID="11" Lat="32.296148" Lon="-64.849184"/>
|
||||
</CourseLimit>
|
||||
</Race>
|
||||
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<RegattaConfig>
|
||||
<RegattaID>1</RegattaID>
|
||||
<RegattaName>Seng302 Mock Test</RegattaName>
|
||||
<CourseName>Bermuda AC35</CourseName>
|
||||
<CentralLatitude>-32.296577</CentralLatitude>
|
||||
<CentralLongitude>64.854304</CentralLongitude>
|
||||
<CentralAltitude>0.00</CentralAltitude>
|
||||
<UtcOffset>-4</UtcOffset>
|
||||
<MagneticVariation>-14.78</MagneticVariation>
|
||||
</RegattaConfig>
|
||||
@ -0,0 +1,87 @@
|
||||
package seng302.Networking;
|
||||
|
||||
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMesageEncoder {
|
||||
private byte[] fullMessage;
|
||||
private byte[] header;
|
||||
private byte[] message;
|
||||
//private byte[] crc;
|
||||
|
||||
private byte headerSync1 = (byte)0x47;
|
||||
private byte headerSync2 = (byte)0x83;
|
||||
private byte headerMessageType;
|
||||
private long headerTimeStamp;
|
||||
private int headerSourceID;
|
||||
private short headerMessageLength;
|
||||
|
||||
public BinaryMesageEncoder(MessageType headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message){
|
||||
//set the header
|
||||
this.headerMessageType = headerMessageType.getValue();
|
||||
this.headerTimeStamp = headerTimeStamp;
|
||||
this.headerSourceID = headerSourceID;
|
||||
this.headerMessageLength = headerMessageLength;
|
||||
|
||||
ByteBuffer tempHeaderByteBuffer = ByteBuffer.allocate(15);
|
||||
tempHeaderByteBuffer.put(this.headerSync1);
|
||||
tempHeaderByteBuffer.put(this.headerSync2);
|
||||
tempHeaderByteBuffer.put(this.headerMessageType);
|
||||
tempHeaderByteBuffer.put(longToSixBytes(this.headerTimeStamp));
|
||||
tempHeaderByteBuffer.putInt(this.headerSourceID);
|
||||
tempHeaderByteBuffer.put(shortToBytes(this.headerMessageLength));
|
||||
|
||||
|
||||
this.header = tempHeaderByteBuffer.array();
|
||||
|
||||
//set the message
|
||||
this.message = message;
|
||||
|
||||
//set full message
|
||||
ByteBuffer tempMessageByteBuffer = ByteBuffer.allocate(19+this.headerMessageLength);
|
||||
tempMessageByteBuffer.put(this.header);
|
||||
tempMessageByteBuffer.put(this.message);
|
||||
|
||||
CRC32 crc = new CRC32();
|
||||
crc.reset();
|
||||
byte[] messageAndHeader = new byte[this.header.length + this.message.length];
|
||||
System.arraycopy(this.header, 0, messageAndHeader, 0, this.header.length);
|
||||
System.arraycopy(this.message, 0, messageAndHeader, this.header.length, this.message.length);
|
||||
crc.update(messageAndHeader);
|
||||
//System.out.println(Arrays.toString(messageAndHeader));
|
||||
|
||||
tempMessageByteBuffer.put(intToBytes((int) crc.getValue()));
|
||||
this.fullMessage = tempMessageByteBuffer.array();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private byte[] longToSixBytes(long x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(x);
|
||||
return Arrays.copyOfRange(buffer.array(), 2, 8);
|
||||
}
|
||||
|
||||
private byte[] shortToBytes(short x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
||||
buffer.putShort(x);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
private byte[] intToBytes(int x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||
buffer.putInt(x);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
public byte[] getFullMessage() {
|
||||
return fullMessage;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMessageDecoder {
|
||||
private byte[] fullMessage;
|
||||
private byte[] header;
|
||||
private byte[] message;
|
||||
private byte[] crc;
|
||||
|
||||
private byte headerSync1;
|
||||
private byte headerSync2;
|
||||
private byte headerMessageType;
|
||||
private byte[] headerTimeStamp;
|
||||
private byte[] headerSourceID;
|
||||
private byte[] headerMessageLength;
|
||||
|
||||
|
||||
public BinaryMessageDecoder(byte[] fullMessage) {
|
||||
this.fullMessage = fullMessage;
|
||||
}
|
||||
|
||||
public void decode() throws IndexOutOfBoundsException{
|
||||
//get the header
|
||||
this.header = Arrays.copyOfRange(this.fullMessage, 0, 15);
|
||||
|
||||
this.headerSync1 = this.header[0];
|
||||
this.headerSync2 = this.header[1];
|
||||
this.headerMessageType = this.header[2];
|
||||
this.headerTimeStamp = Arrays.copyOfRange(this.header, 3, 9);
|
||||
this.headerSourceID = Arrays.copyOfRange(this.header, 9, 13);
|
||||
this.headerMessageLength = Arrays.copyOfRange(this.header, 13, 15);
|
||||
|
||||
//get message
|
||||
this.message = Arrays.copyOfRange(this.fullMessage, 15, this.fullMessage.length - 4);
|
||||
|
||||
//get crc
|
||||
this.crc = Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - 4, fullMessage.length);
|
||||
|
||||
//run through the checks
|
||||
if (this.message.length != bytesToShort(this.headerMessageLength)){
|
||||
System.err.println("message length in header does not equal the message length");
|
||||
System.err.println("message length in header: " + bytesToInt(this.headerMessageLength));
|
||||
System.err.println("message length: " + this.message.length);
|
||||
}else if(this.headerSync1 != 0x47){
|
||||
System.err.println("Sync byte 1 is wrong");
|
||||
}else if(this.headerSync2 !=(byte) 0x83){
|
||||
System.err.println("Sync byte 2 is wrong");
|
||||
}else if(false){
|
||||
//todo check crc
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return bytesToLong(this.headerTimeStamp);
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return bytesToInt(this.headerSourceID);
|
||||
}
|
||||
|
||||
public short getMessageLength() {
|
||||
return bytesToShort(this.headerMessageLength);
|
||||
}
|
||||
|
||||
public int getMessageType(){
|
||||
return (int) this.headerMessageType;
|
||||
}
|
||||
|
||||
public byte[] getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class AverageWindDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte[] byteTime;
|
||||
byte[] byteRawPeriod;
|
||||
byte[] byteRawSpeed;
|
||||
byte[] bytePeriod2;
|
||||
byte[] byteSpeed2;
|
||||
byte[] bytePeriod3;
|
||||
byte[] byteSpeed3;
|
||||
byte[] bytePeriod4;
|
||||
byte[] byteSpeed4;
|
||||
|
||||
public AverageWindDecoder(byte[] encodedAverageWind) {
|
||||
messageVersionNumber = encodedAverageWind[0];
|
||||
byteTime = Arrays.copyOfRange(encodedAverageWind, 1, 7);
|
||||
byteRawPeriod = Arrays.copyOfRange(encodedAverageWind, 7, 9);
|
||||
byteRawSpeed = Arrays.copyOfRange(encodedAverageWind, 9, 11);
|
||||
bytePeriod2 = Arrays.copyOfRange(encodedAverageWind, 11, 13);
|
||||
byteSpeed2 = Arrays.copyOfRange(encodedAverageWind, 13, 15);
|
||||
bytePeriod3 = Arrays.copyOfRange(encodedAverageWind, 15, 17);
|
||||
byteSpeed3 = Arrays.copyOfRange(encodedAverageWind, 17, 19);
|
||||
bytePeriod4 = Arrays.copyOfRange(encodedAverageWind, 19, 21);
|
||||
byteSpeed4 = Arrays.copyOfRange(encodedAverageWind, 21, 23);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BoatLocationDecoder {
|
||||
private byte messageVersionNumber;
|
||||
private byte[] time;
|
||||
private byte[] sourceID;
|
||||
private byte[] seqNum;
|
||||
private byte deviceType;
|
||||
private byte[] latitude;
|
||||
private byte[] longitude;
|
||||
private byte[] altitude;
|
||||
private byte[] heading;
|
||||
private byte[] pitch;
|
||||
private byte[] roll;
|
||||
private byte[] boatSpeed;
|
||||
private byte[] cog;
|
||||
private byte[] sog;
|
||||
private byte[] apparentWindSpeed;
|
||||
private byte[] apparentWindAngle;
|
||||
private byte[] trueWindSpeed;
|
||||
private byte[] trueWindDirection;
|
||||
private byte[] trueWindAngle;
|
||||
private byte[] currentDrift;
|
||||
private byte[] currentSet;
|
||||
private byte[] rudderAngle;
|
||||
|
||||
private BoatLocationMessage message;
|
||||
|
||||
public BoatLocationDecoder(byte[] encodedBoatLocation) {
|
||||
messageVersionNumber = encodedBoatLocation[0];
|
||||
time = Arrays.copyOfRange(encodedBoatLocation, 1, 7);
|
||||
sourceID = Arrays.copyOfRange(encodedBoatLocation, 7, 11);
|
||||
seqNum = Arrays.copyOfRange(encodedBoatLocation, 11, 15);
|
||||
deviceType = encodedBoatLocation[15];
|
||||
latitude = Arrays.copyOfRange(encodedBoatLocation, 16, 20);
|
||||
longitude = Arrays.copyOfRange(encodedBoatLocation,20, 24);
|
||||
altitude = Arrays.copyOfRange(encodedBoatLocation, 24, 28);
|
||||
heading = Arrays.copyOfRange(encodedBoatLocation,28, 30);
|
||||
pitch =Arrays.copyOfRange(encodedBoatLocation,30,32);
|
||||
roll = Arrays.copyOfRange(encodedBoatLocation,32,34);
|
||||
boatSpeed = Arrays.copyOfRange(encodedBoatLocation,34,36);
|
||||
cog = Arrays.copyOfRange(encodedBoatLocation,36,38);
|
||||
sog = Arrays.copyOfRange(encodedBoatLocation,38, 40);
|
||||
apparentWindSpeed = Arrays.copyOfRange(encodedBoatLocation, 40, 42);
|
||||
apparentWindAngle = Arrays.copyOfRange(encodedBoatLocation, 42, 44);
|
||||
trueWindSpeed = Arrays.copyOfRange(encodedBoatLocation,44, 46);
|
||||
trueWindDirection = Arrays.copyOfRange(encodedBoatLocation, 46, 48);
|
||||
trueWindAngle = Arrays.copyOfRange(encodedBoatLocation, 48, 50);
|
||||
currentDrift = Arrays.copyOfRange(encodedBoatLocation,50,52);
|
||||
currentSet = Arrays.copyOfRange(encodedBoatLocation,52, 54);
|
||||
rudderAngle = Arrays.copyOfRange(encodedBoatLocation,54, 56);
|
||||
|
||||
message = new BoatLocationMessage(messageVersionNumber, bytesToLong(time),
|
||||
bytesToInt(sourceID), bytesToInt(seqNum),
|
||||
deviceType, bytesToInt(latitude),
|
||||
bytesToInt(longitude), bytesToInt(altitude),
|
||||
twoByteToInt(heading), bytesToShort(pitch),
|
||||
bytesToShort(roll), twoByteToInt(boatSpeed),
|
||||
twoByteToInt(cog), twoByteToInt(sog),
|
||||
twoByteToInt(apparentWindSpeed), bytesToShort(apparentWindAngle),
|
||||
twoByteToInt(trueWindSpeed), bytesToShort(trueWindDirection),
|
||||
bytesToShort(trueWindAngle), twoByteToInt(currentDrift),
|
||||
twoByteToInt(currentSet), bytesToShort(rudderAngle)
|
||||
);
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong);
|
||||
// byteBuffer.put(bytesLong[0]);
|
||||
// byteBuffer.put(bytesLong[1]);
|
||||
// byteBuffer.put(bytesLong[2]);
|
||||
// byteBuffer.put(bytesLong[3]);
|
||||
// byteBuffer.put(bytesLong[4]);
|
||||
// byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public BoatLocationMessage getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class CourseWindDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte byteWindID;
|
||||
byte loopCount;
|
||||
ArrayList<CourseWind> loopMessages = new ArrayList();
|
||||
|
||||
public CourseWindDecoder(byte[] encodedCourseWind) {
|
||||
final int lengthInBytesOfMessages = 20;
|
||||
|
||||
messageVersionNumber = encodedCourseWind[0];
|
||||
byteWindID = encodedCourseWind[1];
|
||||
loopCount = encodedCourseWind[2];
|
||||
byte[] loopMessagesBytes = Arrays.copyOfRange(encodedCourseWind, 3, lengthInBytesOfMessages*loopCount+3);
|
||||
int messageLoopIndex = 0;
|
||||
|
||||
for (int i=0; i < loopCount; i++) {
|
||||
byte[] messageBytes = Arrays.copyOfRange(loopMessagesBytes, messageLoopIndex, messageLoopIndex+20);
|
||||
ArrayList test = new ArrayList();
|
||||
byte[] windId = Arrays.copyOfRange(messageBytes, 0, 1);
|
||||
byte[] time = Arrays.copyOfRange(messageBytes, 1, 7);
|
||||
byte[] raceID = Arrays.copyOfRange(messageBytes, 7, 11);
|
||||
byte[] windDirection = Arrays.copyOfRange(messageBytes, 11, 13);
|
||||
byte[] windSpeed = Arrays.copyOfRange(messageBytes, 13, 15);
|
||||
byte[] bestUpwindAngle = Arrays.copyOfRange(messageBytes, 15, 17);
|
||||
byte[] bestDownwindAngle = Arrays.copyOfRange(messageBytes, 17, 19);
|
||||
byte[] flags = Arrays.copyOfRange(messageBytes, 19, 20);
|
||||
|
||||
CourseWind message = new CourseWind(windId[0], bytesToLong(time),
|
||||
bytesToInt(raceID), twoByteToInt(windDirection),
|
||||
twoByteToInt(windSpeed), twoByteToInt(bestUpwindAngle),
|
||||
twoByteToInt(bestDownwindAngle), flags[0]);
|
||||
|
||||
loopMessages.add(message);
|
||||
messageLoopIndex += 20;
|
||||
}
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public ArrayList<CourseWind> getLoopMessages() {
|
||||
return loopMessages;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class MarkRoundingDecoder {
|
||||
byte messageVersionNumber;
|
||||
byte[] byteTime;
|
||||
byte[] byteAck;
|
||||
byte[] byteRaceID;
|
||||
byte[] byteSourceID;
|
||||
byte byteBoatStatus;
|
||||
byte byteRoundingSide;
|
||||
byte byteMarkType;
|
||||
byte byteMarkID;
|
||||
|
||||
public MarkRoundingDecoder(byte[] encodedMarkRounding) {
|
||||
messageVersionNumber = encodedMarkRounding[0];
|
||||
byteTime = Arrays.copyOfRange(encodedMarkRounding, 1, 7);
|
||||
byteAck = Arrays.copyOfRange(encodedMarkRounding, 7, 9);
|
||||
byteRaceID = Arrays.copyOfRange(encodedMarkRounding, 9, 13);
|
||||
byteSourceID = Arrays.copyOfRange(encodedMarkRounding, 13, 18);
|
||||
byteBoatStatus = encodedMarkRounding[18];
|
||||
byteRoundingSide = encodedMarkRounding[19];
|
||||
byteMarkType = encodedMarkRounding[20];
|
||||
byteMarkID = encodedMarkRounding[21];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class RaceStartStatusDecoder {
|
||||
private byte messageVersion;
|
||||
private byte[] timestamp;
|
||||
private byte[] ackNumber;
|
||||
private byte[] raceStartTime;
|
||||
private byte[] raceIdentifier;
|
||||
private byte notificationType;
|
||||
|
||||
private long time;
|
||||
private short ack;
|
||||
private long startTime;
|
||||
private int raceID;
|
||||
private char notification;
|
||||
|
||||
|
||||
public RaceStartStatusDecoder(byte[] encodedRaceStartStatus) {
|
||||
messageVersion = encodedRaceStartStatus[0];
|
||||
timestamp = Arrays.copyOfRange(encodedRaceStartStatus, 1, 7);
|
||||
ackNumber = Arrays.copyOfRange(encodedRaceStartStatus, 7, 9);
|
||||
raceStartTime = Arrays.copyOfRange(encodedRaceStartStatus, 9, 15);
|
||||
raceIdentifier = Arrays.copyOfRange(encodedRaceStartStatus, 15, 19);
|
||||
notificationType = encodedRaceStartStatus[19];
|
||||
|
||||
time = bytesToLong(timestamp);
|
||||
ack = bytesToShort(ackNumber);
|
||||
startTime = bytesToLong(raceStartTime);
|
||||
raceID = bytesToInt(raceIdentifier);
|
||||
notification = byteToChar(notificationType);
|
||||
}
|
||||
|
||||
private char byteToChar(byte bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
char num = byteBuffer.getChar(0);
|
||||
return num;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getMessageVersion() {
|
||||
return messageVersion;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public short getAck() {
|
||||
return ack;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public int getRaceID() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public char getNotification() {
|
||||
return notification;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,150 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.BoatStatus;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class RaceStatusDecoder {
|
||||
private byte versionNum;
|
||||
private byte[] timeBytes;
|
||||
private byte[] raceID;
|
||||
private byte raceStatus;
|
||||
private byte[] expectedStart;
|
||||
private byte[] raceWind;
|
||||
private byte[] windSpeed;
|
||||
private byte numBoats;
|
||||
private byte bytesRaceType;
|
||||
private byte[] boatsBytes;
|
||||
|
||||
private long time;
|
||||
private int race;
|
||||
private int raceState;
|
||||
private long startTime;
|
||||
private short raceWindDir;
|
||||
private short raceWindSpeed;
|
||||
private int numberOfBoats;
|
||||
private int raceType;
|
||||
private ArrayList<BoatStatus> boats = new ArrayList<>();
|
||||
|
||||
|
||||
public RaceStatusDecoder(byte[] encodedRaceStatus){
|
||||
versionNum = encodedRaceStatus[0];
|
||||
timeBytes = Arrays.copyOfRange(encodedRaceStatus, 1, 7);
|
||||
raceID = Arrays.copyOfRange(encodedRaceStatus, 7, 11);
|
||||
raceStatus = encodedRaceStatus[11];
|
||||
expectedStart = Arrays.copyOfRange(encodedRaceStatus, 12, 18);
|
||||
raceWind = Arrays.copyOfRange(encodedRaceStatus, 18, 20);
|
||||
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 20, 22);
|
||||
numBoats = encodedRaceStatus[22];
|
||||
bytesRaceType = encodedRaceStatus[23];
|
||||
boatsBytes = Arrays.copyOfRange(encodedRaceStatus, 24, 25+20*this.numBoats);
|
||||
|
||||
time = bytesToLong(timeBytes);
|
||||
race = bytesToInt(raceID);
|
||||
raceState = byteToInt(raceStatus);
|
||||
startTime = bytesToLong(expectedStart);
|
||||
raceWindDir = bytesToShort(raceWind);
|
||||
raceWindSpeed = bytesToShort(windSpeed);
|
||||
numberOfBoats = byteToInt(numBoats);
|
||||
|
||||
int boatLoopIndex = 0;
|
||||
|
||||
for (int i=0; i < numberOfBoats; i++) {
|
||||
byte[] boatBytes = Arrays.copyOfRange(boatsBytes, boatLoopIndex, boatLoopIndex+20);
|
||||
|
||||
byte[] sourceID = Arrays.copyOfRange(boatBytes, 1, 5);
|
||||
byte boatStatus = boatBytes[5];
|
||||
byte legNumber = boatBytes[6];
|
||||
byte numPenaltiesAwarded = boatBytes[7];
|
||||
byte numPenaltiesServed = boatBytes[8];
|
||||
byte[] estTimeAtNextMark = Arrays.copyOfRange(boatBytes, 9, 15);
|
||||
byte[] estTimeAtFinish = Arrays.copyOfRange(boatBytes, 15, 20);
|
||||
|
||||
BoatStatus boat = new BoatStatus(bytesToInt(sourceID),boatStatus,
|
||||
legNumber, numPenaltiesAwarded, numPenaltiesServed,
|
||||
bytesToLong(estTimeAtNextMark), bytesToLong(estTimeAtFinish));
|
||||
|
||||
boats.add(boat);
|
||||
boatLoopIndex += 20;
|
||||
}
|
||||
}
|
||||
|
||||
private int byteToInt(byte bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int intVal = byteBuffer.getInt(0);
|
||||
return intVal;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte) 0);
|
||||
byteBuffer.put((byte) 0);
|
||||
byteBuffer.put(bytesLong);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getVersionNum() {
|
||||
return versionNum;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public int getRace() {
|
||||
return race;
|
||||
}
|
||||
|
||||
public int getRaceState() {
|
||||
return raceState;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public short getRaceWindDir() {
|
||||
return raceWindDir;
|
||||
}
|
||||
|
||||
public short getRaceWindSpeed() {
|
||||
return raceWindSpeed;
|
||||
}
|
||||
|
||||
public int getNumberOfBoats() {
|
||||
return numberOfBoats;
|
||||
}
|
||||
|
||||
public int getRaceType() {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public ArrayList<BoatStatus> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
*/
|
||||
public class XMLMessageDecoder {
|
||||
private byte messageVersionNumber;
|
||||
private short ackNumber;
|
||||
private long timeStamp;
|
||||
private byte xmlMsgSubType;
|
||||
private short sequenceNumber;
|
||||
private short xmlMsgLength;
|
||||
private String xmlMessage;
|
||||
|
||||
private byte[] bytes;
|
||||
|
||||
public XMLMessageDecoder(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public void decode(){
|
||||
byte[] ackNumberBytes = Arrays.copyOfRange(bytes, 1, 3);
|
||||
byte[] timeStampBytes = Arrays.copyOfRange(bytes, 3, 9);
|
||||
byte[] sequenceNumberBytes = Arrays.copyOfRange(bytes, 10, 12);
|
||||
byte[] xmlMsgLengthBytes = Arrays.copyOfRange(bytes, 12, 14);
|
||||
byte[] xmlMessagebytes = Arrays.copyOfRange(bytes, 14, bytes.length);
|
||||
|
||||
this.xmlMsgSubType = bytes[9];
|
||||
this.messageVersionNumber = bytes[0];
|
||||
this.ackNumber = bytesToShort(ackNumberBytes);
|
||||
|
||||
this.timeStamp = bytesToLong(timeStampBytes);
|
||||
|
||||
this.sequenceNumber = bytesToShort(sequenceNumberBytes);
|
||||
this.xmlMsgLength = bytesToShort(xmlMsgLengthBytes);
|
||||
this.xmlMessage = new String(xmlMessagebytes);
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
||||
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
byteBuffer.put(bytesShort[0]);
|
||||
byteBuffer.put(bytesShort[1]);
|
||||
short shortVal = byteBuffer.getShort(0);
|
||||
return shortVal;
|
||||
}
|
||||
|
||||
private long bytesToLong(byte[] bytesLong){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesLong[0]);
|
||||
byteBuffer.put(bytesLong[1]);
|
||||
byteBuffer.put(bytesLong[2]);
|
||||
byteBuffer.put(bytesLong[3]);
|
||||
byteBuffer.put(bytesLong[4]);
|
||||
byteBuffer.put(bytesLong[5]);
|
||||
|
||||
// System.out.println("====decode====");
|
||||
// for (byte i:byteBuffer.array()
|
||||
// ) {
|
||||
// System.out.println(i);
|
||||
// }
|
||||
// System.out.println("====decode====");
|
||||
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getMessageVersionNumber() {
|
||||
return messageVersionNumber;
|
||||
}
|
||||
|
||||
public short getAckNumber() {
|
||||
return ackNumber;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public byte getXmlMsgSubType() {
|
||||
return xmlMsgSubType;
|
||||
}
|
||||
|
||||
public short getSequenceNumber() {
|
||||
return sequenceNumber;
|
||||
}
|
||||
|
||||
public short getXmlMsgLength() {
|
||||
return xmlMsgLength;
|
||||
}
|
||||
|
||||
public String getXmlMessage() {
|
||||
return xmlMessage;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,347 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
import seng302.Networking.Utils.RaceMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceVisionByteEncoder {
|
||||
|
||||
public byte[] heartBeat(int seq){
|
||||
ByteBuffer heartBeat = ByteBuffer.allocate(4);
|
||||
heartBeat.putInt(seq);
|
||||
byte [] result = heartBeat.array();
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] raceStatus(long time, int race, int raceState, long startTime, short raceWindDir, short raceWindSpeed, int raceType, ArrayList<BoatInRace> boats){
|
||||
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20*boats.size());
|
||||
//Version Number 1 bytes
|
||||
byte versionNum = 0b10; //this changes with the pdf. (2)
|
||||
byte[] timeBytes = convert(time, 6);//time (6 bytes)
|
||||
byte[] raceID = ByteBuffer.allocate(4).putInt(race).array();//race identifier incase multiple races are going at once.
|
||||
byte[] raceStatus = convert(raceState, 1);//race status 0 - 10
|
||||
byte[] expectedStart = convert(startTime, 6);//number of milliseconds from Jan 1, 1970 for when the data is valid
|
||||
byte[] raceWind = ByteBuffer.allocate(2).putShort(raceWindDir).array();//North = 0x0000 East = 0x4000 South = 0x8000
|
||||
byte[] windSpeed = ByteBuffer.allocate(2).putShort(raceWindSpeed).array();//mm/sec
|
||||
byte[] numBoats = convert(boats.size(), 1);
|
||||
byte[] bytesRaceType = convert(raceType, 1);//1 match race, 2 fleet race
|
||||
|
||||
raceStatusMessage.put(versionNum);
|
||||
raceStatusMessage.put(timeBytes);
|
||||
raceStatusMessage.put(raceID);
|
||||
raceStatusMessage.put(raceStatus);
|
||||
raceStatusMessage.put(expectedStart);
|
||||
raceStatusMessage.put(raceWind);
|
||||
raceStatusMessage.put(windSpeed);
|
||||
raceStatusMessage.put(numBoats);
|
||||
raceStatusMessage.put(bytesRaceType);
|
||||
|
||||
for (int i = 0; i < boats.size(); i++){
|
||||
int sourceID = 0; //TODO use boats source id.
|
||||
byte[] legNum = convert(boats.get(i).getCurrentLeg().getLegNumber(), 1);
|
||||
byte[] numPenalties = convert(0, 1); //TODO use boats in race penalties class
|
||||
byte[] numPenaltiesServed = convert(0, 1);//TODO use boats in race penalites served.
|
||||
byte[] estNextMarkTime = convert((long)0, 6);//TODO use boats estimated time to next mark.
|
||||
byte[] estFinishTime = convert((long) 0, 6);//TODO use boats estimated time to the finish.
|
||||
|
||||
raceStatusMessage.putInt(sourceID);
|
||||
raceStatusMessage.put(legNum);
|
||||
raceStatusMessage.put(numPenalties);
|
||||
raceStatusMessage.put(numPenaltiesServed);
|
||||
raceStatusMessage.put(estNextMarkTime);
|
||||
raceStatusMessage.put(estFinishTime);
|
||||
}
|
||||
|
||||
return raceStatusMessage.array();
|
||||
}
|
||||
|
||||
public byte[] displayTextMessage(RaceMessage[] message){
|
||||
//ByteBuffer result = ByteBuffer.allocate(4 + numLines * 32);
|
||||
int messageVersionNumber = 0b1;//version number
|
||||
short ackNum = 0;//no clue what this does just a placeholder for 2 bytes.
|
||||
byte[] messLines = convert(message.length, 1);
|
||||
|
||||
// result.putInt(messageVersionNumber);
|
||||
// result.putShort(ackNum);
|
||||
// result.put(messLines);
|
||||
|
||||
ArrayList<byte[]> messages = new ArrayList<byte[]>();
|
||||
int size = 4;
|
||||
|
||||
for (int i = 0; i < message.length; i ++){
|
||||
int messageLen = message[i].getMessageText().getBytes().length;
|
||||
byte[] messageAsBytes = message[i].getMessageText().getBytes();
|
||||
if (messageLen < 30){
|
||||
messageLen = 30;
|
||||
}
|
||||
ByteBuffer mess = ByteBuffer.allocate(2 + messageLen);
|
||||
mess.put(convert(message[i].getLineNumber(), 1));
|
||||
mess.put(convert(messageLen, 1));
|
||||
for (int j = 0; j < messageLen; j ++){
|
||||
mess.put(messageAsBytes[j]);
|
||||
}
|
||||
messages.add(mess.array());
|
||||
size += 2 + messageLen;
|
||||
}
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(size);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.putShort(ackNum);
|
||||
result.put(messLines);
|
||||
|
||||
for(byte[] mess: messages){
|
||||
result.put(mess);
|
||||
}
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] raceStartStatus(long time, short ack, long startTime, int raceID, char notification){
|
||||
int messageVersion = 0b1;
|
||||
byte[] timestamp = convert(time, 6);
|
||||
byte[] ackNumber = convert(ack, 2);
|
||||
byte[] raceStartTime = convert(startTime, 6);
|
||||
int raceIdentifier = raceID;
|
||||
byte[] notificationType = convert(notification, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(20);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(timestamp);
|
||||
result.put(ackNumber);
|
||||
result.put(raceStartTime);
|
||||
result.putInt(raceIdentifier);
|
||||
result.put(notificationType);
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] yachtEventCode(long time, short acknowledgeNumber, int raceID, int destSourceID, int incidentID,
|
||||
int eventID){
|
||||
int messageVersion = 0b10;
|
||||
byte[] encodeTime = convert(time, 6);
|
||||
short ackNum = acknowledgeNumber;
|
||||
int raceUID = raceID;//TODO chekc if this is an into for a 4 char string.
|
||||
int destSource = destSourceID;
|
||||
int incident = incidentID;
|
||||
byte[] event = convert(eventID, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(22);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(encodeTime);
|
||||
result.putShort(ackNum);
|
||||
result.putInt(raceUID);
|
||||
result.putInt(destSource);
|
||||
result.putInt(incident);
|
||||
result.put(event);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] chatterText(int messageType, String message){
|
||||
int messageVersion = 0b1;
|
||||
byte[] type = convert(messageType, 1);
|
||||
byte[] length = convert(message.length(), 1);
|
||||
byte[] text = convert(message, length[0]);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(3 + text.length);
|
||||
result.put(convert(messageVersion, 1));
|
||||
result.put(type);
|
||||
result.put(length);
|
||||
result.put(text);
|
||||
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] boatLocation(BoatLocationMessage boatLocationMessage){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] time = convert(boatLocationMessage.getTime(), 6);
|
||||
byte[] sourceID = convert(boatLocationMessage.getSourceID(), 4);
|
||||
byte[] seqNum = convert(boatLocationMessage.getSequenceNumber(), 4);
|
||||
byte deviceType = boatLocationMessage.getDeviceType();
|
||||
byte[] latitude = convert(boatLocationMessage.getLatitude(), 4);
|
||||
byte[] longitude = convert(boatLocationMessage.getLongitude(), 4);
|
||||
byte[] altitude = convert(boatLocationMessage.getAltitude(), 4);
|
||||
byte[] heading = convert(boatLocationMessage.getHeading(), 2);
|
||||
byte[] pitch = convert(boatLocationMessage.getPitch(), 2);
|
||||
byte[] roll = convert(boatLocationMessage.getRoll(), 2);
|
||||
byte[] boatSpeed = convert(boatLocationMessage.getBoatSpeed(), 2);
|
||||
byte[] cog = convert(boatLocationMessage.getBoatCOG(), 2);
|
||||
byte[] sog = convert(boatLocationMessage.getBoatSOG(), 2);
|
||||
byte[] apparentWindSpeed = convert(boatLocationMessage.getApparentWindSpeed(), 2);
|
||||
byte[] apparentWindAngle = convert(boatLocationMessage.getApparentWindAngle(), 2);
|
||||
byte[] trueWindSpeed = convert(boatLocationMessage.getTrueWindSpeed(), 2);
|
||||
byte[] trueWindDirection = convert(boatLocationMessage.getTrueWindDirection(), 2);
|
||||
byte[] trueWindAngle = convert(boatLocationMessage.getTrueWindAngle(), 2);
|
||||
byte[] currentDrift = convert(boatLocationMessage.getCurrentDrift(), 2);
|
||||
byte[] currentSet = convert(boatLocationMessage.getCurrentSet(), 2);
|
||||
byte[] rudderAngle = convert(boatLocationMessage.getRudderAngle(), 2);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(56);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(time);
|
||||
result.put(sourceID);
|
||||
result.put(seqNum);
|
||||
result.put(deviceType);
|
||||
result.put(latitude);
|
||||
result.put(longitude);
|
||||
result.put(altitude);
|
||||
result.put(heading);
|
||||
result.put(pitch);
|
||||
result.put(roll);
|
||||
result.put(boatSpeed);
|
||||
result.put(cog);
|
||||
result.put(sog);
|
||||
result.put(apparentWindSpeed);
|
||||
result.put(apparentWindAngle);
|
||||
result.put(trueWindSpeed);
|
||||
result.put(trueWindDirection);
|
||||
result.put(trueWindAngle);
|
||||
result.put(currentDrift);
|
||||
result.put(currentSet);
|
||||
result.put(rudderAngle);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] markRounding(int time, int ackNumber, int raceID, int sourceID, int boatStatus, int roundingSide, int markType, int markID){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] byteTime = convert(time, 6);
|
||||
byte[] byteAck = convert(ackNumber, 2);
|
||||
byte[] byteRaceID = convert(raceID, 4);
|
||||
byte[] byteSourceID = convert(sourceID, 4);
|
||||
byte[] byteBoatStatus = convert(boatStatus, 1);
|
||||
byte[] byteRoundingSide = convert(roundingSide, 1);
|
||||
byte[] byteMarkType = convert(markType, 1);
|
||||
byte[] byteMarkID = convert(markID, 1);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(21);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteTime);
|
||||
result.put(byteAck);
|
||||
result.put(byteRaceID);
|
||||
result.put(byteSourceID);
|
||||
result.put(byteBoatStatus);
|
||||
result.put(byteRoundingSide);
|
||||
result.put(byteMarkType);
|
||||
result.put(byteMarkID);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] courseWind(byte windID, ArrayList<CourseWind> courseWinds){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte byteWindID = windID;
|
||||
byte[] loopcount = convert(courseWinds.size(), 1);
|
||||
ByteBuffer result = ByteBuffer.allocate(3 + 20 * courseWinds.size());
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteWindID);
|
||||
result.put(loopcount);
|
||||
for (CourseWind wind: courseWinds){
|
||||
result.put(convert(wind.getID(), 1));
|
||||
result.put(convert(wind.getTime(), 6));
|
||||
result.put(convert(wind.getRaceID(), 4));
|
||||
result.put(convert(wind.getWindDirection(), 2));
|
||||
result.put(convert(wind.getWindSpeed(), 2));
|
||||
result.put(convert(wind.getBestUpwindAngle(), 2));
|
||||
result.put(convert(wind.getBestDownwindAngle(), 2));
|
||||
result.put(convert(wind.getFlags(), 1));
|
||||
}
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] averageWind(int time, int rawPeriod, int rawSampleSpeed, int period2, int speed2, int period3, int speed3, int period4, int speed4){
|
||||
int messageVersionNumber = 0b1;
|
||||
byte[] byteTime = convert(time,6);
|
||||
byte[] byteRawPeriod = convert(rawPeriod, 2);
|
||||
byte[] byteRawSpeed = convert(rawSampleSpeed, 2);
|
||||
byte[] bytePeriod2 = convert(period2, 2);
|
||||
byte[] byteSpeed2 = convert(speed2, 2);
|
||||
byte[] bytePeriod3 = convert(period3, 2);
|
||||
byte[] byteSpeed3 = convert(speed3, 2);
|
||||
byte[] bytePeriod4 = convert(period4, 2);
|
||||
byte[] byteSpeed4 = convert(speed4, 2);
|
||||
|
||||
ByteBuffer result = ByteBuffer.allocate(23);
|
||||
result.put(convert(messageVersionNumber, 1));
|
||||
result.put(byteTime);
|
||||
result.put(byteRawPeriod);
|
||||
result.put(byteRawSpeed);
|
||||
result.put(bytePeriod2);
|
||||
result.put(byteSpeed2);
|
||||
result.put(bytePeriod3);
|
||||
result.put(byteSpeed3);
|
||||
result.put(bytePeriod4);
|
||||
result.put(byteSpeed4);
|
||||
return result.array();
|
||||
}
|
||||
|
||||
public byte[] convert(String s, int size){
|
||||
byte[] m = s.getBytes(Charset.forName("UTF-8"));
|
||||
int length = m.length;
|
||||
byte[] result;
|
||||
if (length > 255){
|
||||
length = 255;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
result = Arrays.copyOfRange(m, 0, length + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] convert(int n, int size){
|
||||
byte[] result;
|
||||
if (size > 4){
|
||||
result = new byte[4];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.putInt(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 4 - size, 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] convert(long n, int size){
|
||||
byte[] result;
|
||||
if (size > 8){
|
||||
result = new byte[8];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
||||
byteBuffer.putLong(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 8 - size, 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public byte[] convert(short n, int size){
|
||||
byte[] result;
|
||||
if (size > 2){
|
||||
result = new byte[2];
|
||||
return result;
|
||||
} else if (size < 1){
|
||||
result = new byte[0];
|
||||
return result;
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
||||
byteBuffer.putShort(n);
|
||||
byte[] bytes = byteBuffer.array();
|
||||
result = Arrays.copyOfRange(bytes, 2 - size, 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 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(byte messageVersionNumber, short ackNumber, long timeStamp, byte xmlMsgSubType, short sequenceNumber, short xmlMsgLenght, String xmlMessage) {
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
this.ackNumber = ackNumber;
|
||||
this.timeStamp = timeStamp;
|
||||
this.xmlMsgSubType = xmlMsgSubType;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.xmlMsgLength = xmlMsgLenght;
|
||||
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 = new byte[2];
|
||||
ackNumberBytes[0] = (byte) (ackNumber & 0xff);
|
||||
ackNumberBytes[1] = (byte) ((ackNumber >> 8) & 0xff);
|
||||
|
||||
//sequenceNumber converted to bytes
|
||||
byte[] sequenceNumberBytes = new byte[2];
|
||||
sequenceNumberBytes[0] = (byte) (sequenceNumber & 0xff);
|
||||
sequenceNumberBytes[1] = (byte) ((sequenceNumber >> 8) & 0xff);
|
||||
|
||||
//xmlMsgLength converted to bytes
|
||||
byte[] xmlMsgLengthBytes = new byte[2];
|
||||
xmlMsgLengthBytes[0] = (byte) (xmlMsgLength & 0xff);
|
||||
xmlMsgLengthBytes[1] = (byte) ((xmlMsgLength >> 8) & 0xff);
|
||||
|
||||
|
||||
tempOutputByteBuffer.put(messageVersionNumber);
|
||||
tempOutputByteBuffer.put(ackNumberBytes);
|
||||
tempOutputByteBuffer.put(longToSixBytes(timeStamp));
|
||||
tempOutputByteBuffer.put(xmlMsgSubType);
|
||||
tempOutputByteBuffer.put(sequenceNumberBytes);
|
||||
tempOutputByteBuffer.put(xmlMsgLengthBytes);
|
||||
tempOutputByteBuffer.put(messageBytes);
|
||||
|
||||
return tempOutputByteBuffer.array();
|
||||
}
|
||||
|
||||
private byte[] longToSixBytes(long x) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(x);
|
||||
|
||||
// System.out.println("====encode====");
|
||||
// for (byte i:buffer.array()
|
||||
// ) {
|
||||
// System.out.println(i);
|
||||
// }
|
||||
// System.out.println("====encode====");
|
||||
|
||||
return Arrays.copyOfRange(buffer.array(), 2, 8);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,357 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by f123 on 21-Apr-17.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents the information in a boat location message (AC streaming spec: 4.9).
|
||||
*/
|
||||
public class BoatLocationMessage
|
||||
{
|
||||
///Version number of the message - is always 1.
|
||||
private byte messageVersionNumber = 1;
|
||||
|
||||
///Time of the event - milliseconds since jan 1 1970. Proper type is 6 byte int.
|
||||
private long time;
|
||||
|
||||
///Source ID of the boat.
|
||||
private int sourceID;
|
||||
|
||||
///Sequence number of the message.
|
||||
private int sequenceNumber;
|
||||
|
||||
///Device type of the message (physical source of the message).
|
||||
private byte deviceType;
|
||||
|
||||
///Latitude of the boat.
|
||||
private int latitude;
|
||||
|
||||
///Longitude of the boat.
|
||||
private int longitude;
|
||||
|
||||
///Altitude of the boat.
|
||||
private int altitude;
|
||||
|
||||
///Heading of the boat. Clockwise, 0 = north. Proper type is unsigned 2 byte int.
|
||||
private int heading;
|
||||
|
||||
///Pitch of the boat.
|
||||
private short pitch;
|
||||
|
||||
///Roll of the boat.
|
||||
private short roll;
|
||||
|
||||
///Speed of the boat. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int boatSpeed;
|
||||
|
||||
///Course over ground (COG) of the boat. Proper type is unsigned 2 byte int.
|
||||
private int boatCOG;
|
||||
|
||||
///Speed over ground (SOG) of the boat. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int boatSOG;
|
||||
|
||||
///Apparent wind speed at time of event. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int apparentWindSpeed;
|
||||
|
||||
///Apparent wind angle at time of the event. Wind over starboard = positive.
|
||||
private short apparentWindAngle;
|
||||
|
||||
///True wind speed. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int trueWindSpeed;
|
||||
|
||||
private short trueWindDirection;
|
||||
|
||||
///True wind angle. Clockwise compass direction, 0 = north.
|
||||
private short trueWindAngle;
|
||||
|
||||
///Current drift. Proper type is unsigned 2 byte int. millimeters per second.
|
||||
private int currentDrift;
|
||||
|
||||
///Current set. Proper type is unsigned 2 byte int. Clockwise compass direction, 0 = north.
|
||||
private int currentSet;
|
||||
|
||||
///Rudder angle. Positive is rudder set to turn yacht to port.
|
||||
private short rudderAngle;
|
||||
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
*/
|
||||
public BoatLocationMessage()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Ctor, with all parameters.
|
||||
* @param messageVersionNumber
|
||||
* @param time
|
||||
* @param sourceID
|
||||
* @param sequenceNumber
|
||||
* @param deviceType
|
||||
* @param latitude
|
||||
* @param longitude
|
||||
* @param altitude
|
||||
* @param heading
|
||||
* @param pitch
|
||||
* @param roll
|
||||
* @param boatSpeed
|
||||
* @param boatCOG
|
||||
* @param boatSOG
|
||||
* @param apparentWindSpeed
|
||||
* @param apparentWindAngle
|
||||
* @param trueWindSpeed
|
||||
* @param trueWindDirection
|
||||
* @param trueWindAngle
|
||||
* @param currentDrift
|
||||
* @param currentSet
|
||||
* @param rudderAngle
|
||||
*/
|
||||
public BoatLocationMessage(byte messageVersionNumber, long time, int sourceID, int sequenceNumber, byte deviceType, int latitude, int longitude, int altitude, int heading, short pitch, short roll, int boatSpeed, int boatCOG, int boatSOG, int apparentWindSpeed, short apparentWindAngle, int trueWindSpeed, short trueWindDirection, short trueWindAngle, int currentDrift, int currentSet, short rudderAngle)
|
||||
{
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
this.time = time;
|
||||
this.sourceID = sourceID;
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
this.deviceType = deviceType;
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
this.altitude = altitude;
|
||||
this.heading = heading;
|
||||
this.pitch = pitch;
|
||||
this.roll = roll;
|
||||
this.boatSpeed = boatSpeed;
|
||||
this.boatCOG = boatCOG;
|
||||
this.boatSOG = boatSOG;
|
||||
this.apparentWindSpeed = apparentWindSpeed;
|
||||
this.apparentWindAngle = apparentWindAngle;
|
||||
this.trueWindSpeed = trueWindSpeed;
|
||||
this.trueWindDirection = trueWindDirection;
|
||||
this.trueWindAngle = trueWindAngle;
|
||||
this.currentDrift = currentDrift;
|
||||
this.currentSet = currentSet;
|
||||
this.rudderAngle = rudderAngle;
|
||||
}
|
||||
|
||||
|
||||
//Getters and setters for message properties.
|
||||
|
||||
|
||||
public byte getMessageVersionNumber()
|
||||
{
|
||||
return messageVersionNumber;
|
||||
}
|
||||
|
||||
public void setMessageVersionNumber(byte messageVersionNumber)
|
||||
{
|
||||
this.messageVersionNumber = messageVersionNumber;
|
||||
}
|
||||
|
||||
public long getTime()
|
||||
{
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time)
|
||||
{
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public int getSourceID()
|
||||
{
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public void setSourceID(int sourceID)
|
||||
{
|
||||
this.sourceID = sourceID;
|
||||
}
|
||||
|
||||
public int getSequenceNumber()
|
||||
{
|
||||
return sequenceNumber;
|
||||
}
|
||||
|
||||
public void setSequenceNumber(int sequenceNumber)
|
||||
{
|
||||
this.sequenceNumber = sequenceNumber;
|
||||
}
|
||||
|
||||
public byte getDeviceType()
|
||||
{
|
||||
return deviceType;
|
||||
}
|
||||
|
||||
public void setDeviceType(byte deviceType)
|
||||
{
|
||||
this.deviceType = deviceType;
|
||||
}
|
||||
|
||||
public int getLatitude()
|
||||
{
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(int latitude)
|
||||
{
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public int getLongitude()
|
||||
{
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(int longitude)
|
||||
{
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public int getAltitude()
|
||||
{
|
||||
return altitude;
|
||||
}
|
||||
|
||||
public void setAltitude(int altitude)
|
||||
{
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
public int getHeading()
|
||||
{
|
||||
return heading;
|
||||
}
|
||||
|
||||
public void setHeading(int heading)
|
||||
{
|
||||
this.heading = heading;
|
||||
}
|
||||
|
||||
public short getPitch()
|
||||
{
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public void setPitch(short pitch)
|
||||
{
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public short getRoll()
|
||||
{
|
||||
return roll;
|
||||
}
|
||||
|
||||
public void setRoll(short roll)
|
||||
{
|
||||
this.roll = roll;
|
||||
}
|
||||
|
||||
public int getBoatSpeed()
|
||||
{
|
||||
return boatSpeed;
|
||||
}
|
||||
|
||||
public void setBoatSpeed(int boatSpeed)
|
||||
{
|
||||
this.boatSpeed = boatSpeed;
|
||||
}
|
||||
|
||||
public int getBoatCOG()
|
||||
{
|
||||
return boatCOG;
|
||||
}
|
||||
|
||||
public void setBoatCOG(int boatCOG)
|
||||
{
|
||||
this.boatCOG = boatCOG;
|
||||
}
|
||||
|
||||
public int getBoatSOG()
|
||||
{
|
||||
return boatSOG;
|
||||
}
|
||||
|
||||
public void setBoatSOG(int boatSOG)
|
||||
{
|
||||
this.boatSOG = boatSOG;
|
||||
}
|
||||
|
||||
public int getApparentWindSpeed()
|
||||
{
|
||||
return apparentWindSpeed;
|
||||
}
|
||||
|
||||
public void setApparentWindSpeed(int apparentWindSpeed)
|
||||
{
|
||||
this.apparentWindSpeed = apparentWindSpeed;
|
||||
}
|
||||
|
||||
public short getApparentWindAngle()
|
||||
{
|
||||
return apparentWindAngle;
|
||||
}
|
||||
|
||||
public void setApparentWindAngle(short apparentWindAngle)
|
||||
{
|
||||
this.apparentWindAngle = apparentWindAngle;
|
||||
}
|
||||
|
||||
public int getTrueWindSpeed()
|
||||
{
|
||||
return trueWindSpeed;
|
||||
}
|
||||
|
||||
public void setTrueWindSpeed(int trueWindSpeed)
|
||||
{
|
||||
this.trueWindSpeed = trueWindSpeed;
|
||||
}
|
||||
|
||||
public short getTrueWindDirection() {
|
||||
return trueWindDirection;
|
||||
}
|
||||
|
||||
public void setTrueWindDirection(short trueWindDirection) {
|
||||
this.trueWindDirection = trueWindDirection;
|
||||
}
|
||||
|
||||
public short getTrueWindAngle()
|
||||
{
|
||||
return trueWindAngle;
|
||||
}
|
||||
|
||||
public void setTrueWindAngle(short trueWindAngle)
|
||||
{
|
||||
this.trueWindAngle = trueWindAngle;
|
||||
}
|
||||
|
||||
public int getCurrentDrift()
|
||||
{
|
||||
return currentDrift;
|
||||
}
|
||||
|
||||
public void setCurrentDrift(int currentDrift)
|
||||
{
|
||||
this.currentDrift = currentDrift;
|
||||
}
|
||||
|
||||
public int getCurrentSet()
|
||||
{
|
||||
return currentSet;
|
||||
}
|
||||
|
||||
public void setCurrentSet(int currentSet)
|
||||
{
|
||||
this.currentSet = currentSet;
|
||||
}
|
||||
|
||||
public short getRudderAngle()
|
||||
{
|
||||
return rudderAngle;
|
||||
}
|
||||
|
||||
public void setRudderAngle(short rudderAngle)
|
||||
{
|
||||
this.rudderAngle = rudderAngle;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class BoatStatus {
|
||||
private int sourceID;
|
||||
private byte boatStatus;
|
||||
private byte legNumber;
|
||||
private byte numPenaltiesAwarded;
|
||||
private byte numPenaltiesServed;
|
||||
private long estTimeAtNextMark;
|
||||
private long estTimeAtFinish;
|
||||
|
||||
public BoatStatus(int sourceID, byte boatStatus, byte legNumber, byte numPenaltiesAwarded, byte numPenaltiesServed, long estTimeAtNextMark, long estTimeAtFinish) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatStatus = boatStatus;
|
||||
this.legNumber = legNumber;
|
||||
this.numPenaltiesAwarded = numPenaltiesAwarded;
|
||||
this.numPenaltiesServed = numPenaltiesServed;
|
||||
this.estTimeAtNextMark = estTimeAtNextMark;
|
||||
this.estTimeAtFinish = estTimeAtFinish;
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public byte getBoatStatus() {
|
||||
return boatStatus;
|
||||
}
|
||||
|
||||
public byte getLegNumber() {
|
||||
return legNumber;
|
||||
}
|
||||
|
||||
public byte getNumPenaltiesAwarded() {
|
||||
return numPenaltiesAwarded;
|
||||
}
|
||||
|
||||
public byte getNumPenaltiesServed() {
|
||||
return numPenaltiesServed;
|
||||
}
|
||||
|
||||
public long getEstTimeAtNextMark() {
|
||||
return estTimeAtNextMark;
|
||||
}
|
||||
|
||||
public long getEstTimeAtFinish() {
|
||||
return estTimeAtFinish;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 21/04/17.
|
||||
*/
|
||||
public class CourseWind {
|
||||
|
||||
private int ID, raceID, windDirection, windSpeed, bestUpwindAngle, bestDownwindAngle, flags;
|
||||
private long time;
|
||||
|
||||
public CourseWind(int ID, long time, int raceID, int windDirection, int windSpeed, int bestUpwindAngle, int bestDownwindAngle,
|
||||
int flags){
|
||||
this.ID = ID;
|
||||
this.time = time;
|
||||
this.raceID = raceID;
|
||||
this.windDirection = windDirection;
|
||||
this.windSpeed = windSpeed;
|
||||
this.bestUpwindAngle = bestUpwindAngle;
|
||||
this.bestDownwindAngle = bestDownwindAngle;
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public int getRaceID() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public int getWindDirection() {
|
||||
return windDirection;
|
||||
}
|
||||
|
||||
public int getWindSpeed() {
|
||||
return windSpeed;
|
||||
}
|
||||
|
||||
public int getBestUpwindAngle() {
|
||||
return bestUpwindAngle;
|
||||
}
|
||||
|
||||
public int getBestDownwindAngle() {
|
||||
return bestDownwindAngle;
|
||||
}
|
||||
|
||||
public int getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public enum MessageType {
|
||||
HEARTBEAT(1), RACESTATUS(12), DISPLAYTEXTMESSAGE(20),
|
||||
XMLMESSAGE(26), RACESTARTSTATUS(27), YACHTEVENTCODE(29), YACHTACTIONCODE(31),
|
||||
CHATTERTEXT(36), BOATLOCATION(37), MARKROUNDING(38), COURSEWIND(44), AVGWIND(47);
|
||||
|
||||
private byte value;
|
||||
|
||||
private MessageType(int value) { this.value = (byte)value; }
|
||||
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
*/
|
||||
public class RaceMessage {
|
||||
|
||||
private int lineNumber;
|
||||
private String messageText;
|
||||
|
||||
public RaceMessage(int lineNumber, String messageText){
|
||||
this.lineNumber = lineNumber;
|
||||
this.messageText = messageText;
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<BoatConfig>
|
||||
<Modified>2017-04-19T15:49:40+1200</Modified>
|
||||
<Version>1</Version>
|
||||
<Settings>
|
||||
<RaceBoatType Type="AC45" />
|
||||
<BoatDimension BoatLength="14.019" HullLength="13.449" />
|
||||
<ZoneSize MarkZoneSize="40.347" CourseZoneSize="40.347" />
|
||||
<ZoneLimits Limit1="200" Limit2="100" Limit3="40.347" Limit4="0" Limit5="-100" />
|
||||
</Settings>
|
||||
<BoatShapes>
|
||||
<BoatShape ShapeID="0">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-2.659" />
|
||||
<Vtx Seq="2" Y="18.359" X="-2.659" />
|
||||
<Vtx Seq="3" Y="18.359" X="2.659" />
|
||||
<Vtx Seq="4" Y="0" X="2.659" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="1">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.278" />
|
||||
<Vtx Seq="2" Y="8.876" X="-1.278" />
|
||||
<Vtx Seq="3" Y="8.876" X="1.278" />
|
||||
<Vtx Seq="4" Y="0" X="1.278" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="2">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.1" />
|
||||
<Vtx Seq="2" Y="8.3" X="-1.1" />
|
||||
<Vtx Seq="3" Y="8.3" X="1.1" />
|
||||
<Vtx Seq="4" Y="0" X="1.1" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="3">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-0.75" />
|
||||
<Vtx Seq="2" Y="3" X="-0.75" />
|
||||
<Vtx Seq="3" Y="3" X="0.75" />
|
||||
<Vtx Seq="4" Y="0" X="0.75" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="4">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-3.46" />
|
||||
<Vtx Seq="2" Y="13.449" X="-3.46" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="13.449" X="3.46" />
|
||||
<Vtx Seq="5" Y="0" X="3.46" />
|
||||
</Vertices>
|
||||
<Catamaran>
|
||||
<Vtx Seq="1" Y="1.769" X="-2.752" />
|
||||
<Vtx Seq="2" Y="0" X="-2.813" />
|
||||
<Vtx Seq="3" Y="0" X="-3.34" />
|
||||
<Vtx Seq="4" Y="5.351" X="-3.46" />
|
||||
<Vtx Seq="5" Y="10.544" X="-3.387" />
|
||||
<Vtx Seq="6" Y="13.449" X="-3.075" />
|
||||
<Vtx Seq="7" Y="10.851" X="-2.793" />
|
||||
<Vtx Seq="8" Y="6.669" X="-2.699" />
|
||||
<Vtx Seq="9" Y="6.669" X="2.699" />
|
||||
<Vtx Seq="10" Y="10.851" X="2.793" />
|
||||
<Vtx Seq="11" Y="13.449" X="3.075" />
|
||||
<Vtx Seq="12" Y="10.544" X="3.387" />
|
||||
<Vtx Seq="13" Y="5.351" X="3.46" />
|
||||
<Vtx Seq="14" Y="0" X="3.34" />
|
||||
<Vtx Seq="15" Y="0" X="2.813" />
|
||||
<Vtx Seq="16" Y="1.769" X="2.752" />
|
||||
</Catamaran>
|
||||
<Bowsprit>
|
||||
<Vtx Seq="1" Y="6.669" X="-0.2" />
|
||||
<Vtx Seq="2" Y="11.377" X="-0.2" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="11.377" X="0.2" />
|
||||
<Vtx Seq="5" Y="6.669" X="0.2" />
|
||||
</Bowsprit>
|
||||
<Trampoline>
|
||||
<Vtx Seq="1" Y="2" X="-2.699" />
|
||||
<Vtx Seq="2" Y="6.438" X="-2.699" />
|
||||
<Vtx Seq="3" Y="6.438" X="2.699" />
|
||||
<Vtx Seq="4" Y="2" X="2.699" />
|
||||
</Trampoline>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="5" />
|
||||
</BoatShapes>
|
||||
<Boats>>
|
||||
<Boat Type="Yacht" SourceID="101" ShapeID="4" HullNum="AC4501" ShortName="USA"
|
||||
BoatName="ORACLE TEAM USA" Country="USA">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="102" ShapeID="4" HullNum="AC4502" ShortName="GBR"
|
||||
BoatName="Land Rover BAR" Country="United Kingdom">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="103" ShapeID="4" HullNum="AC4503" ShortName="JPN"
|
||||
BoatName="SoftBank Team Japan" Country="Japan">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="104" ShapeID="4" HullNum="AC4504" ShortName="FRA"
|
||||
BoatName="Groupama Team France" Country="France">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="105" ShapeID="4" HullNum="AC4505" ShortName="SWE"
|
||||
BoatName="Artemis Racing" Country="Sweden">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="106" ShapeID="4" HullNum="AC4506" ShortName="NZL"
|
||||
BoatName="Emirates Team New Zealand" Country="New Zealand">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
</Boats>
|
||||
</BoatConfig>
|
||||
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Race>
|
||||
<RaceID>17041901</RaceID>
|
||||
<RaceType>Fleet</RaceType>
|
||||
<CreationTimeDate>2017-04-19T15:30:00+1200</CreationTimeDate >
|
||||
<RaceStartTime Time="2019-06-01T13:30:00-0400" Postpone="false" />
|
||||
<Participants>
|
||||
<Yacht SourceID="001" Entry="Port" />
|
||||
<Yacht SourceID="002" Entry="Port" />
|
||||
<Yacht SourceID="003" Entry="Port" />
|
||||
<Yacht SourceID="004" Entry="Port" />
|
||||
<Yacht SourceID="005" Entry="Port" />
|
||||
<Yacht SourceID="006" Entry="Port" />
|
||||
</Participants>
|
||||
<Course>
|
||||
<CompoundMark CompoundMarkID="1" Name="StartLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.296577" TargetLng="-64.854304" SourceID="101" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.293771" TargetLng="-64.855242" SourceID="102" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="2" Name="M1">
|
||||
<Mark Name="M1" TargetLat="32.293039" TargetLng="-64.843983" SourceID="103" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="3" Name="WindwardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.284680" TargetLng="-64.850045" SourceID="104" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.280164" TargetLng="-64.847591" SourceID="105" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="4" Name="LeewardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.309693" TargetLng="-64.835249" SourceID="106" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.308046" TargetLng="-64.831785" SourceID="107" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="5" Name="FinishLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.317379" TargetLng="-64.839291" SourceID="108" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.317257" TargetLng="-64.836260" SourceID="109" />
|
||||
</CompoundMark>
|
||||
</Course>
|
||||
<CompoundMarkSequence>
|
||||
<Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" />
|
||||
<Corner SeqID="2" CompoundMarkID="2" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="4" CompoundMarkID="4" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="5" CompoundMarkID="5" Rounding="SP" ZoneSize="3"/>
|
||||
</CompoundMarkSequence>
|
||||
<CourseLimit>
|
||||
<Limit SeqID="1" Lat="32.313922" Lon="-64.837168"/>
|
||||
<Limit SeqID="2" Lat="32.317379" Lon="-64.839291"/>
|
||||
<Limit SeqID="3" Lat="32.317911" Lon="-64.836996"/>
|
||||
<Limit SeqID="4" Lat="32.317257" Lon="-64.836260"/>
|
||||
<Limit SeqID="5" Lat="32.304273" Lon="-64.822834"/>
|
||||
|
||||
<Limit SeqID="6" Lat="32.279097" Lon="-64.841545"/>
|
||||
<Limit SeqID="7" Lat="32.279604" Lon="-64.849871"/>
|
||||
<Limit SeqID="8" Lat="32.289545" Lon="-64.854162"/>
|
||||
<Limit SeqID="9" Lat="32.290198" Lon="-64.858711"/>
|
||||
<Limit SeqID="10" Lat="32.297164" Lon="-64.856394"/>
|
||||
<Limit SeqID="11" Lat="32.296148" Lon="-64.849184"/>
|
||||
</CourseLimit>
|
||||
</Race>
|
||||
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<RegattaConfig>
|
||||
<RegattaID>1</RegattaID>
|
||||
<RegattaName>Seng302 Mock Test</RegattaName>
|
||||
<CourseName>Bermuda AC35</CourseName>
|
||||
<CentralLatitude>-32.296577</CentralLatitude>
|
||||
<CentralLongitude>64.854304</CentralLongitude>
|
||||
<CentralAltitude>0.00</CentralAltitude>
|
||||
<UtcOffset>-4</UtcOffset>
|
||||
<MagneticVariation>-14.78</MagneticVariation>
|
||||
</RegattaConfig>
|
||||
@ -0,0 +1,65 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.BinaryMesageEncoder;
|
||||
import seng302.Networking.BinaryMessageDecoder;
|
||||
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
public class BinaryMessageDecoderTest {
|
||||
@Test
|
||||
public void decodeTest(){
|
||||
try{
|
||||
StringBuilder xmlString;
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedMessage = testEncoder.encode();
|
||||
|
||||
BinaryMesageEncoder testMessage = new BinaryMesageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
|
||||
|
||||
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
|
||||
testDecoder.decode();
|
||||
|
||||
//message length
|
||||
Assert.assertEquals((short) encodedMessage.length,testDecoder.getMessageLength());
|
||||
//time stamp
|
||||
Assert.assertEquals(time,testDecoder.getTimeStamp());
|
||||
//source ID
|
||||
Assert.assertEquals((short) 1, testDecoder.getSourceID());
|
||||
//message type
|
||||
Assert.assertEquals(26, testDecoder.getMessageType());
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(testDecoder.getMessage());
|
||||
decoderXML.decode();
|
||||
|
||||
//tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
|
||||
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.BoatLocationDecoder;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class BoatLocationDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
BoatLocationMessage testMessage = new BoatLocationMessage((byte)1, time, (byte)2,
|
||||
3, (byte) 1, 180, -180, 4, (short)5,
|
||||
(short)6, (short)7, 8, 9, 10, 11,
|
||||
(short) 12, 13,(short) 14 ,(short) 15,
|
||||
16, 17, (short) 18);
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
byte [] testEncodedMessage = raceVisionByteEncoder.boatLocation(testMessage);
|
||||
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
|
||||
BoatLocationMessage decodedTest = testDecoder.getMessage();
|
||||
|
||||
Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber());
|
||||
Assert.assertEquals(testMessage.getTime(), decodedTest.getTime());
|
||||
Assert.assertEquals(testMessage.getSequenceNumber(), decodedTest.getSequenceNumber());
|
||||
Assert.assertEquals(testMessage.getDeviceType(), decodedTest.getDeviceType());
|
||||
Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude());
|
||||
Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude());
|
||||
Assert.assertEquals(testMessage.getAltitude(), decodedTest.getAltitude());
|
||||
Assert.assertEquals(testMessage.getHeading(), decodedTest.getHeading());
|
||||
Assert.assertEquals(testMessage.getPitch(), decodedTest.getPitch());
|
||||
Assert.assertEquals(testMessage.getRoll(), decodedTest.getRoll());
|
||||
Assert.assertEquals(testMessage.getBoatSpeed(), decodedTest.getBoatSpeed());
|
||||
|
||||
Assert.assertEquals(testMessage.getBoatCOG(), decodedTest.getBoatCOG());
|
||||
Assert.assertEquals(testMessage.getBoatSOG(), decodedTest.getBoatSOG());
|
||||
Assert.assertEquals(testMessage.getApparentWindSpeed(), decodedTest.getApparentWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindSpeed(), decodedTest.getTrueWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindDirection(), decodedTest.getTrueWindDirection());
|
||||
Assert.assertEquals(testMessage.getTrueWindAngle(), decodedTest.getTrueWindAngle());
|
||||
Assert.assertEquals(testMessage.getCurrentDrift(), decodedTest.getCurrentDrift());
|
||||
Assert.assertEquals(testMessage.getCurrentSet(), decodedTest.getCurrentSet());
|
||||
Assert.assertEquals(testMessage.getRudderAngle(), decodedTest.getRudderAngle());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.CourseWindDecoder;
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class CourseWindDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
CourseWind testCourseWind1 = new CourseWind(1, time, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind2 = new CourseWind(2, time2, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
ArrayList<CourseWind> testCourseWinds = new ArrayList<CourseWind>();
|
||||
testCourseWinds.add(testCourseWind1);
|
||||
testCourseWinds.add(testCourseWind2);
|
||||
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
|
||||
byte[] testEncodedCourseWind = raceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
|
||||
|
||||
CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind);
|
||||
|
||||
ArrayList<CourseWind> testDecodedCourseWinds = testDecoder.getLoopMessages();
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(0).getID(), testDecodedCourseWinds.get(0).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getTime(), testDecodedCourseWinds.get(0).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getRaceID(), testDecodedCourseWinds.get(0).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindDirection(), testDecodedCourseWinds.get(0).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindSpeed(), testDecodedCourseWinds.get(0).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestUpwindAngle(), testDecodedCourseWinds.get(0).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestDownwindAngle(), testDecodedCourseWinds.get(0).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getFlags(), testDecodedCourseWinds.get(0).getFlags());
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(1).getID(), testDecodedCourseWinds.get(1).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getTime(), testDecodedCourseWinds.get(1).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getRaceID(), testDecodedCourseWinds.get(1).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindDirection(), testDecodedCourseWinds.get(1).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindSpeed(), testDecodedCourseWinds.get(1).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestUpwindAngle(), testDecodedCourseWinds.get(1).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestDownwindAngle(), testDecodedCourseWinds.get(1).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getFlags(), testDecodedCourseWinds.get(1).getFlags());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.RaceStartStatusDecoder;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class RaceStartStatusDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
long time2 = System.currentTimeMillis();
|
||||
byte[] encodedRaceStartStatus = raceVisionByteEncoder.raceStartStatus(time, (short)1,
|
||||
time2, 2, (char)3);
|
||||
|
||||
RaceStartStatusDecoder testDecoder = new RaceStartStatusDecoder(encodedRaceStartStatus);
|
||||
|
||||
Assert.assertEquals(0b1, testDecoder.getMessageVersion());
|
||||
Assert.assertEquals(time, testDecoder.getTime());
|
||||
Assert.assertEquals(1, testDecoder.getAck());
|
||||
Assert.assertEquals(time2, testDecoder.getStartTime());
|
||||
Assert.assertEquals(2, testDecoder.getRaceID());
|
||||
Assert.assertEquals((char)3, testDecoder.getNotification());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import javafx.scene.paint.Color;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Networking.MessageDecoders.RaceStatusDecoder;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class RaceStatusDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
BoatInRace boat1 = new BoatInRace("Test", 1, Color.ALICEBLUE, "tt");
|
||||
Leg testLeg = new Leg("test leg", 1);
|
||||
boat1.setCurrentLeg(testLeg);
|
||||
BoatInRace boat2 = new BoatInRace("Test2", 1, Color.ALICEBLUE, "tt");
|
||||
boat2.setCurrentLeg(testLeg);
|
||||
ArrayList boats = new ArrayList();
|
||||
boats.add(boat1);
|
||||
boats.add(boat2);
|
||||
long time2 = System.currentTimeMillis();
|
||||
|
||||
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
|
||||
|
||||
byte[] encodedRaceStatus = raceVisionByteEncoder.raceStatus(time, 1, 2, time2,
|
||||
(short)2, (short)3,4, boats);
|
||||
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);
|
||||
|
||||
Assert.assertEquals(0b10, decoderTest.getVersionNum());
|
||||
Assert.assertEquals(time, decoderTest.getTime());
|
||||
Assert.assertEquals(1, decoderTest.getRace());
|
||||
Assert.assertEquals(2, decoderTest.getRaceState());
|
||||
Assert.assertEquals(time2, decoderTest.getStartTime());
|
||||
Assert.assertEquals((short)2, decoderTest.getRaceWindDir());
|
||||
Assert.assertEquals((short)3, decoderTest.getRaceWindSpeed());
|
||||
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getBoatStatus());
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getLegNumber());
|
||||
Assert.assertEquals(0, decoderTest.getBoats().get(0).getNumPenaltiesAwarded());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
*/
|
||||
public class XMLMessageDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
try{
|
||||
StringBuilder xmlString;
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(encodedXML);
|
||||
decoderXML.decode();
|
||||
|
||||
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
|
||||
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 19/04/17.
|
||||
*/
|
||||
public class XMLMessageEncoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
StringBuilder xmlString;
|
||||
try{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
//1 + 2 + 6 + 1 + 2 + 2 + 374
|
||||
Assert.assertEquals(388, encodedXML.length);
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByteArrayNullTest(){
|
||||
StringBuilder xmlString;
|
||||
try{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
|
||||
String line;
|
||||
xmlString = new StringBuilder();
|
||||
while((line=br.readLine())!= null){
|
||||
xmlString.append(line.trim());
|
||||
}
|
||||
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, System.currentTimeMillis(), (byte)7, (short)1, (short)1, xmlString.toString());
|
||||
|
||||
byte[] encodedXML = testEncoder.encode();
|
||||
|
||||
Assert.assertEquals(null, encodedXML);
|
||||
}catch (IOException e){
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue