finished the raceStatusDecoder
-added a new boat status class #story[782]
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package seng302.Networking;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,38 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Networking.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 {
|
||||
byte versionNum;
|
||||
byte[] timeBytes;
|
||||
byte[] raceID;
|
||||
byte raceStatus;
|
||||
byte[] expectedStart;
|
||||
byte[] raceWind;
|
||||
byte[] windSpeed;
|
||||
byte numBoats;
|
||||
byte[] bytesRaceType;
|
||||
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];
|
||||
@@ -25,7 +43,83 @@ public class RaceStatusDecoder {
|
||||
raceWind = Arrays.copyOfRange(encodedRaceStatus, 19, 21);
|
||||
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 21, 23);
|
||||
numBoats = encodedRaceStatus[23];
|
||||
bytesRaceType = Arrays.copyOfRange(encodedRaceStatus, 24, 24+20*this.numBoats);
|
||||
bytesRaceType = encodedRaceStatus[24];
|
||||
boatsBytes = Arrays.copyOfRange(encodedRaceStatus, 25, 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;
|
||||
}
|
||||
|
||||
//boats info
|
||||
int sourceID;
|
||||
byte[] legNum;
|
||||
byte[] numPenalties;
|
||||
byte[] numPenaltiesServed;
|
||||
byte[] estNextMarkTime;
|
||||
byte[] estFinishTime;
|
||||
}
|
||||
|
||||
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[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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user