added raceStatusDecpderTest and some debugging
-added raceStatusDecpderTest -fix a problem where the encoder method for race status added 4 bytes where it should have put 1 -added getters to the info in raceStatusDecoder -fixed a problem with the values looked for in the decoder #story[782]
This commit is contained in:
@@ -38,13 +38,13 @@ public class RaceStatusDecoder {
|
|||||||
versionNum = encodedRaceStatus[0];
|
versionNum = encodedRaceStatus[0];
|
||||||
timeBytes = Arrays.copyOfRange(encodedRaceStatus, 1, 7);
|
timeBytes = Arrays.copyOfRange(encodedRaceStatus, 1, 7);
|
||||||
raceID = Arrays.copyOfRange(encodedRaceStatus, 7, 11);
|
raceID = Arrays.copyOfRange(encodedRaceStatus, 7, 11);
|
||||||
raceStatus = encodedRaceStatus[12];
|
raceStatus = encodedRaceStatus[11];
|
||||||
expectedStart = Arrays.copyOfRange(encodedRaceStatus, 13, 19);
|
expectedStart = Arrays.copyOfRange(encodedRaceStatus, 12, 18);
|
||||||
raceWind = Arrays.copyOfRange(encodedRaceStatus, 19, 21);
|
raceWind = Arrays.copyOfRange(encodedRaceStatus, 18, 20);
|
||||||
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 21, 23);
|
windSpeed = Arrays.copyOfRange(encodedRaceStatus, 20, 22);
|
||||||
numBoats = encodedRaceStatus[23];
|
numBoats = encodedRaceStatus[22];
|
||||||
bytesRaceType = encodedRaceStatus[24];
|
bytesRaceType = encodedRaceStatus[23];
|
||||||
boatsBytes = Arrays.copyOfRange(encodedRaceStatus, 25, 25+20*this.numBoats);
|
boatsBytes = Arrays.copyOfRange(encodedRaceStatus, 24, 25+20*this.numBoats);
|
||||||
|
|
||||||
time = bytesToLong(timeBytes);
|
time = bytesToLong(timeBytes);
|
||||||
race = bytesToInt(raceID);
|
race = bytesToInt(raceID);
|
||||||
@@ -74,14 +74,6 @@ public class RaceStatusDecoder {
|
|||||||
boats.add(boat);
|
boats.add(boat);
|
||||||
boatLoopIndex += 20;
|
boatLoopIndex += 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
//boats info
|
|
||||||
int sourceID;
|
|
||||||
byte[] legNum;
|
|
||||||
byte[] numPenalties;
|
|
||||||
byte[] numPenaltiesServed;
|
|
||||||
byte[] estNextMarkTime;
|
|
||||||
byte[] estFinishTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int byteToInt(byte bytesInt){
|
private int byteToInt(byte bytesInt){
|
||||||
@@ -112,14 +104,48 @@ public class RaceStatusDecoder {
|
|||||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||||
byteBuffer.put((byte) 0);
|
byteBuffer.put((byte) 0);
|
||||||
byteBuffer.put((byte) 0);
|
byteBuffer.put((byte) 0);
|
||||||
byteBuffer.put(bytesLong[0]);
|
byteBuffer.put(bytesLong);
|
||||||
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);
|
long longVal = byteBuffer.getLong(0);
|
||||||
return longVal;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,20 +19,20 @@ public class RaceVisionByteEncoder {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] raceStatus(long time, int race, int raceState, long startTime, short raceWindDir, short raceWindSpeed, int raceType, BoatInRace[] boats){
|
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.length);
|
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20*boats.size());
|
||||||
//Version Number 1 bytes
|
//Version Number 1 bytes
|
||||||
int versionNum = 0b10; //this changes with the pdf. (2)
|
byte versionNum = 0b10; //this changes with the pdf. (2)
|
||||||
byte[] timeBytes = convert(time, 6);//time (6 bytes)
|
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[] 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[] 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[] 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[] raceWind = ByteBuffer.allocate(2).putShort(raceWindDir).array();//North = 0x0000 East = 0x4000 South = 0x8000
|
||||||
byte[] windSpeed = ByteBuffer.allocate(2).putShort(raceWindSpeed).array();//mm/sec
|
byte[] windSpeed = ByteBuffer.allocate(2).putShort(raceWindSpeed).array();//mm/sec
|
||||||
byte[] numBoats = convert(boats.length, 1);
|
byte[] numBoats = convert(boats.size(), 1);
|
||||||
byte[] bytesRaceType = convert(raceType, 1);//1 match race, 2 fleet race
|
byte[] bytesRaceType = convert(raceType, 1);//1 match race, 2 fleet race
|
||||||
|
|
||||||
raceStatusMessage.putInt(versionNum);
|
raceStatusMessage.put(versionNum);
|
||||||
raceStatusMessage.put(timeBytes);
|
raceStatusMessage.put(timeBytes);
|
||||||
raceStatusMessage.put(raceID);
|
raceStatusMessage.put(raceID);
|
||||||
raceStatusMessage.put(raceStatus);
|
raceStatusMessage.put(raceStatus);
|
||||||
@@ -42,9 +42,9 @@ public class RaceVisionByteEncoder {
|
|||||||
raceStatusMessage.put(numBoats);
|
raceStatusMessage.put(numBoats);
|
||||||
raceStatusMessage.put(bytesRaceType);
|
raceStatusMessage.put(bytesRaceType);
|
||||||
|
|
||||||
for (int i = 0; i < boats.length; i++){
|
for (int i = 0; i < boats.size(); i++){
|
||||||
int sourceID = 0; //TODO use boats source id.
|
int sourceID = 0; //TODO use boats source id.
|
||||||
byte[] legNum = convert(boats[i].getCurrentLeg().getLegNumber(), 1);
|
byte[] legNum = convert(boats.get(i).getCurrentLeg().getLegNumber(), 1);
|
||||||
byte[] numPenalties = convert(0, 1); //TODO use boats in race penalties class
|
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[] 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[] estNextMarkTime = convert((long)0, 6);//TODO use boats estimated time to next mark.
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
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.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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user