From fdd994b7c0bc0edf4e0727d680eccebf21b2bdea Mon Sep 17 00:00:00 2001 From: hba56 Date: Sun, 23 Apr 2017 20:32:58 +1200 Subject: [PATCH] finished the raceStatusDecoder -added a new boat status class #story[782] --- .../java/seng302/Networking/BoatStatus.java | 52 ++++++++ .../MessageDecoders/RaceStatusDecoder.java | 114 ++++++++++++++++-- 2 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 src/main/java/seng302/Networking/BoatStatus.java diff --git a/src/main/java/seng302/Networking/BoatStatus.java b/src/main/java/seng302/Networking/BoatStatus.java new file mode 100644 index 00000000..ba3f9b5d --- /dev/null +++ b/src/main/java/seng302/Networking/BoatStatus.java @@ -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; + } +} diff --git a/src/main/java/seng302/Networking/MessageDecoders/RaceStatusDecoder.java b/src/main/java/seng302/Networking/MessageDecoders/RaceStatusDecoder.java index 9bce1218..69ea5114 100644 --- a/src/main/java/seng302/Networking/MessageDecoders/RaceStatusDecoder.java +++ b/src/main/java/seng302/Networking/MessageDecoders/RaceStatusDecoder.java @@ -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 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; } }