Refactored mock.Event to work with MockRace.

Fixed the networking imports.
Race now keeps a reference to its Race, Boat, and Regatta data sources.
VisualiserRace can be provided with new Race/Boat/Regatta data sources during runtime. Currently it updates boundary and legs with a new RaceDataSource, and marker boats with a new BoatDataSource.
This commit is contained in:
fjc40
2017-07-06 15:25:00 +12:00
parent 8d36d89570
commit 3a0b81834f
30 changed files with 179 additions and 83 deletions
@@ -1,7 +1,11 @@
package network;
import seng302.Networking.Exceptions.InvalidMessageException;
import seng302.Networking.Messages.Enums.MessageType;
import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.*;
import network.Messages.*;
import network.Messages.Enums.MessageType;
import static network.Utils.ByteConverter.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
@@ -69,20 +73,20 @@ public class BinaryMessageDecoder {
this.headerMessageType = this.messageHeader[2];
//Get the header timestamp.
this.headerTimeStamp = ByteConverter.bytesToLong(Arrays.copyOfRange(this.messageHeader, 3, 9));
this.headerTimeStamp = bytesToLong(Arrays.copyOfRange(this.messageHeader, 3, 9));
//Get the source ID for the message.
this.headerSourceID = ByteConverter.bytesToInt(Arrays.copyOfRange(this.messageHeader, 9, 13));
this.headerSourceID = bytesToInt(Arrays.copyOfRange(this.messageHeader, 9, 13));
//Get the length of the message body.
this.messageBodyLength = ByteConverter.bytesToInt(Arrays.copyOfRange(this.messageHeader, 13, 15));
this.messageBodyLength = bytesToInt(Arrays.copyOfRange(this.messageHeader, 13, 15));
//Get the messageBody.
this.messageBody = Arrays.copyOfRange(this.fullMessage, this.headerLength, this.headerLength + this.messageBodyLength);
//Get the CRC value.
this.messageCRCValue = ByteConverter.bytesToLong(Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - CRCLength, this.fullMessage.length));
this.messageCRCValue = bytesToLong(Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - CRCLength, this.fullMessage.length));
//Combine the header and body into a single array.
ByteBuffer headerBodyByteBuffer = ByteBuffer.allocate(messageHeader.length + messageBody.length);
@@ -131,7 +135,7 @@ public class BinaryMessageDecoder {
//System.out.println("Decoding HeartBeat Message!");
//TODO maybe use HeartbeatDecoder.decode(message).
//TODO also, decoders for each message type should encapsulate the constructing of the object. E.g., return HeartbeatDecoder.decode(message);.
return new Heartbeat(ByteConverter.bytesToLong(messageBody));
return new Heartbeat(bytesToLong(messageBody));
case RACESTATUS:
//System.out.println("Race Status Message");
@@ -1,12 +1,13 @@
package network;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
import static network.Utils.ByteConverter.*;
import java.nio.ByteBuffer;
import java.util.zip.CRC32;
import static seng302.Networking.Utils.ByteConverter.*;
/**
* This class can be used to encode/convert a byte array message body, plus header data into a byte array containing the entire message, ready to send.
@@ -1,7 +1,8 @@
package network.MessageDecoders;
import seng302.Networking.Messages.AverageWind;
import seng302.Networking.Utils.ByteConverter;
import network.Messages.AverageWind;
import network.Utils.ByteConverter;
import java.util.Arrays;
@@ -1,10 +1,14 @@
package network.MessageDecoders;
import seng302.Networking.Messages.BoatLocation;
import network.Messages.BoatLocation;
import java.util.Arrays;
import static seng302.Networking.Utils.ByteConverter.*;
import static network.Utils.ByteConverter.bytesToInt;
import static network.Utils.ByteConverter.bytesToLong;
import static network.Utils.ByteConverter.bytesToShort;
/**
* Created by hba56 on 21/04/17.
@@ -1,11 +1,14 @@
package network.MessageDecoders;
import seng302.Networking.Messages.CourseWind;
import network.Messages.CourseWind;
import java.util.ArrayList;
import java.util.Arrays;
import static seng302.Networking.Utils.ByteConverter.*;
import static network.Utils.ByteConverter.bytesToInt;
import static network.Utils.ByteConverter.bytesToLong;
/**
* Created by hba56 on 23/04/17.
@@ -1,7 +1,8 @@
package network.MessageDecoders;
import seng302.Networking.Messages.MarkRounding;
import seng302.Networking.Utils.ByteConverter;
import network.Messages.MarkRounding;
import network.Utils.ByteConverter;
import java.util.Arrays;
@@ -3,7 +3,8 @@ package network.MessageDecoders;
import java.util.Arrays;
import static seng302.Networking.Utils.ByteConverter.*;
import static network.Utils.ByteConverter.*;
/**
* Created by hba56 on 21/04/17.
@@ -1,11 +1,15 @@
package network.MessageDecoders;
import seng302.Networking.Messages.BoatStatus;
import network.Messages.BoatStatus;
import java.util.ArrayList;
import java.util.Arrays;
import static seng302.Networking.Utils.ByteConverter.*;
import static network.Utils.ByteConverter.bytesToInt;
import static network.Utils.ByteConverter.bytesToLong;
import static network.Utils.ByteConverter.bytesToShort;
/**
* Created by hba56 on 21/04/17.
@@ -24,7 +28,7 @@ public class RaceStatusDecoder {
private long time;
private int race;
private int raceState;
private byte raceState;
private long startTime;
private int raceWindDir;
private short raceWindSpeed;
@@ -47,7 +51,7 @@ public class RaceStatusDecoder {
time = bytesToLong(timeBytes);
race = bytesToInt(raceID);
raceState = bytesToInt(raceStatus);
raceState = raceStatus;
startTime = bytesToLong(expectedStart);
raceWindDir = bytesToInt(raceWind);
raceWindSpeed = bytesToShort(windSpeed);
@@ -87,7 +91,7 @@ public class RaceStatusDecoder {
return race;
}
public int getRaceState() {
public byte getRaceState() {
return raceState;
}
@@ -5,8 +5,9 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static seng302.Networking.Utils.ByteConverter.bytesToLong;
import static seng302.Networking.Utils.ByteConverter.bytesToShort;
import static network.Utils.ByteConverter.bytesToLong;
import static network.Utils.ByteConverter.bytesToShort;
/**
* Created by hba56 on 20/04/17.
@@ -1,11 +1,15 @@
package network.MessageEncoders;
import network.Messages.*;
import static network.Utils.ByteConverter.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import static seng302.Networking.Utils.ByteConverter.*;
/**
* Created by fwy13 on 19/04/17.
@@ -2,7 +2,8 @@ package network.MessageEncoders;
import java.nio.ByteBuffer;
import static seng302.Networking.Utils.ByteConverter.*;
import static network.Utils.ByteConverter.*;
/**
* Encodes a XML file into a message of AC35 format
@@ -1,7 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* The base class for all message types.
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Created by fwy13 on 25/04/17.
@@ -1,19 +1,18 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import seng302.Networking.Utils.AC35UnitConverter;
import static seng302.Networking.Utils.AC35UnitConverter.convertGPS;
import static seng302.Networking.Utils.AC35UnitConverter.convertGPSToInt;
import network.Messages.Enums.MessageType;
import network.Utils.AC35UnitConverter;
import shared.model.Constants;
import static network.Utils.AC35UnitConverter.convertGPS;
import static network.Utils.AC35UnitConverter.convertGPSToInt;
/**
* Represents the information in a boat location message (AC streaming spec: 4.9).
*/
public class BoatLocation extends AC35Data {
//Knots x this = meters per second.
public static final double KnotsToMetersPerSecondConversionFactor =
0.514444;
public static final byte Unknown = 0;
public static final byte RacingYacht = 1;
public static final byte CommitteeBoat = 2;
@@ -263,11 +262,8 @@ public class BoatLocation extends AC35Data {
* @return Speed in millimeters per second, stored as an int (using only the two least significant bytes).
*/
public static int convertBoatSpeedDoubleToInt(double speed) {
//Calculate meters per second.
double metersPerSecond = speed * KnotsToMetersPerSecondConversionFactor;
//Calculate millimeters per second.
double millimetersPerSecond = metersPerSecond * 1000.0;
double millimetersPerSecond = speed * Constants.KnotsToMMPerSecond;
//Convert to an int.
int millimetersPerSecondInt = (int) Math.round(millimetersPerSecond);
@@ -282,11 +278,9 @@ public class BoatLocation extends AC35Data {
* @return Speed in knots, stored as a double.
*/
public static double convertBoatSpeedIntToDouble(int speed) {
//Calculate meters per second.
double metersPerSecond = speed / 1000.0;
//Calculate knots.
double knots = metersPerSecond / KnotsToMetersPerSecondConversionFactor;
double knots = speed / Constants.KnotsToMMPerSecond;
return knots;
}
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Created by fwy13 on 21/04/17.
@@ -1,8 +1,9 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import java.util.ArrayList;
import network.Messages.Enums.MessageType;
import java.util.List;
/**
* Created by fwy13 on 25/04/17.
@@ -11,9 +12,9 @@ public class CourseWinds extends AC35Data {
private int msgVerNum;
private int selectedWindID;
private ArrayList<CourseWind> courseWinds;
private List<CourseWind> courseWinds;
public CourseWinds(int msgVerNum, int selectedWindID, ArrayList<CourseWind> courseWinds){
public CourseWinds(int msgVerNum, int selectedWindID, List<CourseWind> courseWinds){
super(MessageType.COURSEWIND);
this.msgVerNum = msgVerNum;
this.selectedWindID = selectedWindID;
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Represents a Heartbeat message.
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Created by fwy13 on 25/04/17.
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Created by fwy13 on 19/04/17.
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
/**
* Created by fwy13 on 25/04/17.
@@ -21,7 +21,7 @@ public class RaceStatus extends AC35Data {
private int raceType;
private List<BoatStatus> boatStatuses;
public RaceStatus(long currentTime, int raceID, int raceStatus, long expectedStartTime, int windDirection, int windSpeed, int raceType, List<BoatStatus> boatStatuses){
public RaceStatus(long currentTime, int raceID, byte raceStatus, long expectedStartTime, int windDirection, int windSpeed, int raceType, List<BoatStatus> boatStatuses){
super(MessageType.RACESTATUS);
this.currentTime = currentTime;
this.raceID = raceID;
@@ -1,6 +1,7 @@
package network.Messages;
import seng302.Networking.Messages.Enums.MessageType;
import network.Messages.Enums.MessageType;
import java.io.InputStream;
@@ -1,8 +1,9 @@
package network.PacketDump;
import seng302.Networking.BinaryMessageDecoder;
import seng302.Networking.Exceptions.InvalidMessageException;
import seng302.Networking.Messages.AC35Data;
import network.BinaryMessageDecoder;
import network.Exceptions.InvalidMessageException;
import network.Messages.AC35Data;
import java.io.IOException;
import java.net.URISyntaxException;