refactored networking code from networkingTest
#story[778, 782]
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Networking.UtilFiles.*;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.Utils.CourseWind;
|
||||
import seng302.Networking.Utils.RaceMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package seng302.Networking.MessageEncoders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.Networking.UtilFiles;
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by f123 on 21-Apr-17.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.Networking.UtilFiles;
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.Networking.UtilFiles;
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 21/04/17.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.Networking.UtilFiles;
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.Networking.UtilFiles;
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 19/04/17.
|
||||
Reference in New Issue
Block a user