WIP adding race status messages
#story[778]
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package seng302.Networking.MessageDecoders;
|
||||
|
||||
import seng302.Networking.Utils.BoatStatus;
|
||||
import seng302.Networking.Utils.BoatStatusMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -32,7 +30,7 @@ public class RaceStatusDecoder {
|
||||
private short raceWindSpeed;
|
||||
private int numberOfBoats;
|
||||
private int raceType;
|
||||
private ArrayList<BoatStatus> boats = new ArrayList<>();
|
||||
private ArrayList<BoatStatusMessage> boats = new ArrayList<>();
|
||||
|
||||
|
||||
public RaceStatusDecoder(byte[] encodedRaceStatus){
|
||||
@@ -68,7 +66,7 @@ public class RaceStatusDecoder {
|
||||
byte[] estTimeAtNextMark = Arrays.copyOfRange(boatBytes, 8, 14);
|
||||
byte[] estTimeAtFinish = Arrays.copyOfRange(boatBytes, 14, 20);
|
||||
|
||||
BoatStatus boat = new BoatStatus(bytesToInt(sourceID),boatStatus,
|
||||
BoatStatusMessage boat = new BoatStatusMessage(bytesToInt(sourceID),boatStatus,
|
||||
legNumber, numPenaltiesAwarded, numPenaltiesServed,
|
||||
bytesToLong(estTimeAtNextMark), bytesToLong(estTimeAtFinish));
|
||||
|
||||
@@ -113,7 +111,7 @@ public class RaceStatusDecoder {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public ArrayList<BoatStatus> getBoats() {
|
||||
public ArrayList<BoatStatusMessage> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-13
@@ -4,9 +4,7 @@ package seng302.Networking.MessageEncoders;
|
||||
import seng302.Networking.Utils.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.*;
|
||||
|
||||
@@ -34,9 +32,9 @@ public class RaceVisionByteEncoder {
|
||||
*/
|
||||
public static byte[] raceStatus(RaceStatus raceStatus){
|
||||
|
||||
ArrayList<BoatStatus> boatStatuses = raceStatus.getBoatStatuses();
|
||||
ArrayList<BoatStatusMessage> boatStatusMessages = raceStatus.getBoatStatusMessages();
|
||||
|
||||
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20*boatStatuses.size());
|
||||
ByteBuffer raceStatusMessage = ByteBuffer.allocate(24 + 20* boatStatusMessages.size());
|
||||
//Version Number 1 bytes
|
||||
byte versionNum = 0b10; //this changes with the pdf. (2)
|
||||
byte[] timeBytes = longToBytes(raceStatus.getCurrentTime(), 6);//time (6 bytes)
|
||||
@@ -45,7 +43,7 @@ public class RaceVisionByteEncoder {
|
||||
byte[] expectedStart = longToBytes(raceStatus.getExpectedStartTime(), 6);//number of milliseconds from Jan 1, 1970 for when the data is valid
|
||||
byte[] raceWind = ByteBuffer.allocate(2).put(intToBytes(raceStatus.getWindDirection(), 2)).array();//North = 0x0000 East = 0x4000 South = 0x8000.
|
||||
byte[] windSpeed = ByteBuffer.allocate(2).put(intToBytes(raceStatus.getWindSpeed(), 2)).array();//mm/sec
|
||||
byte[] numBoats = intToBytes(boatStatuses.size(), 1);
|
||||
byte[] numBoats = intToBytes(boatStatusMessages.size(), 1);
|
||||
byte[] bytesRaceType = intToBytes(raceStatus.getRaceType(), 1);//1 match race, 2 fleet race
|
||||
|
||||
raceStatusMessage.put(versionNum);
|
||||
@@ -58,14 +56,14 @@ public class RaceVisionByteEncoder {
|
||||
raceStatusMessage.put(numBoats);
|
||||
raceStatusMessage.put(bytesRaceType);
|
||||
|
||||
for (int i = 0; i < boatStatuses.size(); i++){
|
||||
byte[] sourceID = intToBytes(boatStatuses.get(i).getSourceID());
|
||||
byte[] boatStatus = intToBytes(boatStatuses.get(i).getBoatStatus(), 1);
|
||||
byte[] legNum = intToBytes(boatStatuses.get(i).getLegNumber(), 1);
|
||||
byte[] numPenalties = intToBytes(boatStatuses.get(i).getNumPenaltiesAwarded(), 1);
|
||||
byte[] numPenaltiesServed = intToBytes(boatStatuses.get(i).getNumPenaltiesServed(), 1);
|
||||
byte[] estNextMarkTime = longToBytes(boatStatuses.get(i).getEstTimeAtNextMark(), 6);
|
||||
byte[] estFinishTime = longToBytes( boatStatuses.get(i).getEstTimeAtFinish(), 6);
|
||||
for (int i = 0; i < boatStatusMessages.size(); i++){
|
||||
byte[] sourceID = intToBytes(boatStatusMessages.get(i).getSourceID());
|
||||
byte[] boatStatus = intToBytes(boatStatusMessages.get(i).getBoatStatus(), 1);
|
||||
byte[] legNum = intToBytes(boatStatusMessages.get(i).getLegNumber(), 1);
|
||||
byte[] numPenalties = intToBytes(boatStatusMessages.get(i).getNumPenaltiesAwarded(), 1);
|
||||
byte[] numPenaltiesServed = intToBytes(boatStatusMessages.get(i).getNumPenaltiesServed(), 1);
|
||||
byte[] estNextMarkTime = longToBytes(boatStatusMessages.get(i).getEstTimeAtNextMark(), 6);
|
||||
byte[] estFinishTime = longToBytes( boatStatusMessages.get(i).getEstTimeAtFinish(), 6);
|
||||
|
||||
raceStatusMessage.put(sourceID);
|
||||
raceStatusMessage.put(boatStatus);
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import SharedModel.BoatInRace;
|
||||
import com.sun.jmx.remote.internal.ArrayQueue;
|
||||
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
|
||||
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
|
||||
import seng302.Networking.Utils.BoatLocationMessage;
|
||||
import seng302.Networking.Utils.MessageType;
|
||||
import seng302.Networking.Utils.RaceStatus;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
|
||||
/**
|
||||
* TCP client to recive information from AC35 data source
|
||||
@@ -18,23 +21,26 @@ public class MockOutput implements Runnable
|
||||
private long lastHeartbeatTime;
|
||||
private RaceVisionByteEncoder messageEncoder = new RaceVisionByteEncoder();
|
||||
|
||||
//socket port 4942 as 4940 is ac35 live port and 4941 is ac35 test port
|
||||
//socket port 5003 as 4940 is ac35 live port and 4941 is ac35 test port
|
||||
private ServerSocket serverSocket;
|
||||
private Socket mockSocket;
|
||||
private DataOutputStream outToVisualiser;
|
||||
|
||||
//a buffer that contains items that are waiting to be sent
|
||||
private ArrayList<byte[]> messagesToSendBuffer = new ArrayList<>();
|
||||
private ArrayBlockingQueue<byte[]> messagesToSendBuffer = new ArrayBlockingQueue<>(99999999);
|
||||
|
||||
private short messageNumber = 1;
|
||||
private short xmlSequenceNumber = 1;
|
||||
private int heartbeatSequenceNum = 1;
|
||||
private int boatLocationSequenceNumber = 1;
|
||||
private int raceStatusSequenceNumber = 1;
|
||||
|
||||
public MockOutput() throws IOException {
|
||||
//start Time
|
||||
|
||||
|
||||
lastHeartbeatTime = System.currentTimeMillis();
|
||||
serverSocket = new ServerSocket(4942);
|
||||
serverSocket = new ServerSocket(5003);
|
||||
|
||||
|
||||
}
|
||||
@@ -61,7 +67,7 @@ public class MockOutput implements Runnable
|
||||
* @param xmlString the xml string to send
|
||||
* @param messageType the kind of xml string, values given in AC35 spec (5 regatta, 6 race, 7 boat)
|
||||
*/
|
||||
public void parseXMLString(String xmlString, int messageType){
|
||||
public synchronized void parseXMLString(String xmlString, int messageType){
|
||||
XMLMessageEncoder encoder = new XMLMessageEncoder(messageNumber, System.currentTimeMillis(), messageType, xmlSequenceNumber,(short) xmlString.length(), xmlString);
|
||||
//iterates the sequence numbers
|
||||
xmlSequenceNumber++;
|
||||
@@ -77,7 +83,7 @@ public class MockOutput implements Runnable
|
||||
/**
|
||||
* Used to give the mocOutput information about boat location to be made into a message and sent
|
||||
*/
|
||||
public void parseBoatLocation(BoatInRace boat){
|
||||
public synchronized void parseBoatLocation(BoatInRace boat){
|
||||
BoatLocationMessage boatLocationMessage = new BoatLocationMessage(boat, boatLocationSequenceNumber);
|
||||
//iterates the sequence number
|
||||
boatLocationSequenceNumber++;
|
||||
@@ -95,7 +101,15 @@ public class MockOutput implements Runnable
|
||||
addMessageToBufferToSend(binaryMessageEncoder.getFullMessage());
|
||||
}
|
||||
|
||||
private void addMessageToBufferToSend(byte[] messagesToSendBuffer) {
|
||||
public synchronized void parseRaceStatus(RaceStatus raceStatus) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private synchronized void addMessageToBufferToSend(byte[] messagesToSendBuffer) {
|
||||
this.messagesToSendBuffer.add(messagesToSendBuffer);
|
||||
}
|
||||
|
||||
|
||||
+16
-2
@@ -1,9 +1,11 @@
|
||||
package seng302.Networking.Utils;
|
||||
|
||||
import seng302.Networking.Utils.Enums.BoatStatus;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
public class BoatStatus{
|
||||
public class BoatStatusMessage {
|
||||
private int sourceID;
|
||||
private byte boatStatus;
|
||||
private byte legNumber;
|
||||
@@ -12,7 +14,7 @@ public class BoatStatus{
|
||||
private long estTimeAtNextMark;
|
||||
private long estTimeAtFinish;
|
||||
|
||||
public BoatStatus(int sourceID, byte boatStatus, byte legNumber, byte numPenaltiesAwarded, byte numPenaltiesServed, long estTimeAtNextMark, long estTimeAtFinish) {
|
||||
public BoatStatusMessage(int sourceID, byte boatStatus, byte legNumber, byte numPenaltiesAwarded, byte numPenaltiesServed, long estTimeAtNextMark, long estTimeAtFinish) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatStatus = boatStatus;
|
||||
this.legNumber = legNumber;
|
||||
@@ -22,6 +24,18 @@ public class BoatStatus{
|
||||
this.estTimeAtFinish = estTimeAtFinish;
|
||||
}
|
||||
|
||||
|
||||
public BoatStatusMessage(int sourceID, BoatStatus boatStatus, int legNum) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatStatus = boatStatus.getValue();
|
||||
this.legNumber = ByteConverter.intToBytes(legNum)[0];
|
||||
numPenaltiesAwarded = 0;
|
||||
numPenaltiesServed = 0;
|
||||
estTimeAtFinish = 0;
|
||||
estTimeAtNextMark = 0;
|
||||
}
|
||||
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package seng302.Networking.Utils.Enums;
|
||||
|
||||
/**
|
||||
* Created by esa46 on 28/04/17.
|
||||
*/
|
||||
public enum BoatStatus {
|
||||
|
||||
UNDEFINED(0), PRESTART(1), RACING(2), FINISHED(3), DNS(4), DNF(5), DSQ(6), OCS(7), NOT_A_STATUS(-1);
|
||||
|
||||
private byte value;
|
||||
|
||||
private BoatStatus(int value) { this.value = (byte)value; }
|
||||
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static BoatStatus valueOf(byte bite){
|
||||
switch(bite){
|
||||
case 0:
|
||||
return UNDEFINED;
|
||||
case 1:
|
||||
return PRESTART;
|
||||
case 2:
|
||||
return RACING;
|
||||
case 3:
|
||||
return FINISHED;
|
||||
case 4:
|
||||
return DNS;
|
||||
case 5:
|
||||
return DNF;
|
||||
case 6:
|
||||
return DSQ;
|
||||
case 7:
|
||||
return OCS;
|
||||
default:
|
||||
return NOT_A_STATUS;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,9 +13,9 @@ public class RaceStatus extends AC35Data{
|
||||
private int windDirection;
|
||||
private int windSpeed;
|
||||
private int raceType;
|
||||
private ArrayList<BoatStatus> boatStatuses;
|
||||
private ArrayList<BoatStatusMessage> boatStatusMessages;
|
||||
|
||||
public RaceStatus(long currentTime, int raceID, int raceStatus, long expectedStartTime, int windDirection, int windSpeed, int raceType, ArrayList<BoatStatus> boatStatuses){
|
||||
public RaceStatus(long currentTime, int raceID, int raceStatus, long expectedStartTime, int windDirection, int windSpeed, int raceType, ArrayList<BoatStatusMessage> boatStatusMessages){
|
||||
super(MessageType.RACESTATUS);
|
||||
this.currentTime = currentTime;
|
||||
this.raceID = raceID;
|
||||
@@ -24,10 +24,17 @@ public class RaceStatus extends AC35Data{
|
||||
this.windDirection = windDirection;
|
||||
this.windSpeed = windSpeed;
|
||||
this.raceType = raceType;
|
||||
this.boatStatuses = boatStatuses;//note this is a copy so any alterations to the parent will affect this.
|
||||
this.boatStatusMessages = boatStatusMessages;//note this is a copy so any alterations to the parent will affect this.
|
||||
}
|
||||
|
||||
|
||||
public RaceStatus( long currentTime, int raceID, int raceStatus, int raceType, ArrayList<BoatStatusMessage> boatStatusMessages) {
|
||||
super(MessageType.RACESTATUS);
|
||||
this.currentTime = currentTime;
|
||||
this.raceID = raceID;
|
||||
this.raceStatus = raceStatus;
|
||||
this.raceType = raceType;
|
||||
this.boatStatusMessages = boatStatusMessages;
|
||||
}
|
||||
///Getters.
|
||||
|
||||
public long getCurrentTime()
|
||||
@@ -65,8 +72,8 @@ public class RaceStatus extends AC35Data{
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public ArrayList<BoatStatus> getBoatStatuses()
|
||||
public ArrayList<BoatStatusMessage> getBoatStatusMessages()
|
||||
{
|
||||
return boatStatuses;
|
||||
return boatStatusMessages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,10 @@ public class VisualiserInput implements Runnable
|
||||
|
||||
VisualiserInput() throws IOException{
|
||||
|
||||
// connectionSocket = new Socket(InetAddress.getLocalHost(), 4942);
|
||||
connectionSocket = new Socket(InetAddress.getLocalHost(), 5003);
|
||||
|
||||
//this is the test data that streams form the AC35 website
|
||||
connectionSocket = new Socket("livedata.americascup.com",4941);
|
||||
// connectionSocket = new Socket("livedata.americascup.com",4941);
|
||||
|
||||
|
||||
//start Time
|
||||
@@ -98,18 +98,17 @@ public class VisualiserInput implements Runnable
|
||||
System.out.println("HeartBeat Message! " + heartbeatSeqNum);
|
||||
break;
|
||||
case RACESTATUS:
|
||||
// System.out.println("Race Status Message");
|
||||
System.out.println("Race Status Message");
|
||||
break;
|
||||
case DISPLAYTEXTMESSAGE:
|
||||
// System.out.println("Display Text Message");
|
||||
//no decoder for this.
|
||||
break;
|
||||
case XMLMESSAGE:
|
||||
// System.out.println("XML Message!");
|
||||
System.out.println(((XMLMessage)data).getXmlMessage());
|
||||
System.out.println("XML Message!");
|
||||
break;
|
||||
case RACESTARTSTATUS:
|
||||
// System.out.println("Race Start Status Message");
|
||||
System.out.println("Race Start Status Message");
|
||||
break;
|
||||
case YACHTEVENTCODE:
|
||||
// System.out.println("Yacht Action Code!");
|
||||
@@ -124,7 +123,7 @@ public class VisualiserInput implements Runnable
|
||||
//no decoder
|
||||
break;
|
||||
case BOATLOCATION:
|
||||
// System.out.println("Boat Location Message!");
|
||||
System.out.println("Boat Location Message!");
|
||||
break;
|
||||
case MARKROUNDING:
|
||||
// System.out.println("Mark Rounding Message!");
|
||||
@@ -136,7 +135,7 @@ public class VisualiserInput implements Runnable
|
||||
// System.out.println("Average Wind Message!");
|
||||
break;
|
||||
default:
|
||||
// System.out.println("Broken Message!");
|
||||
System.out.println("Broken Message!");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user