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;
|
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;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by hba56 on 21/04/17.
|
* Created by hba56 on 21/04/17.
|
||||||
*/
|
*/
|
||||||
public class RaceStatusDecoder {
|
public class RaceStatusDecoder {
|
||||||
byte versionNum;
|
private byte versionNum;
|
||||||
byte[] timeBytes;
|
private byte[] timeBytes;
|
||||||
byte[] raceID;
|
private byte[] raceID;
|
||||||
byte raceStatus;
|
private byte raceStatus;
|
||||||
byte[] expectedStart;
|
private byte[] expectedStart;
|
||||||
byte[] raceWind;
|
private byte[] raceWind;
|
||||||
byte[] windSpeed;
|
private byte[] windSpeed;
|
||||||
byte numBoats;
|
private byte numBoats;
|
||||||
byte[] bytesRaceType;
|
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){
|
public RaceStatusDecoder(byte[] encodedRaceStatus){
|
||||||
versionNum = encodedRaceStatus[0];
|
versionNum = encodedRaceStatus[0];
|
||||||
@@ -25,7 +43,83 @@ public class RaceStatusDecoder {
|
|||||||
raceWind = Arrays.copyOfRange(encodedRaceStatus, 19, 21);
|
raceWind = Arrays.copyOfRange(encodedRaceStatus, 19, 21);
|
||||||
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 21, 23);
|
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 21, 23);
|
||||||
numBoats = encodedRaceStatus[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