mock.app.App now tells mock.app.Event that the xml file strings are XMLFileType.Contents.

Made network.MessageEncoders.RaceVisionByteEncoder methods static (they were effectively static already).

shared.dataInput.XMLReader#readXMLFileToString(..) now throws an XMLReaderException if the file cannot be found.

visualiser.Controllers.StartController assumed XML messages were resource paths instead of file contents - fixed.

Testing:
networking tests now work again.
This commit is contained in:
fjc40
2017-07-20 01:01:29 +12:00
parent 4bcfc01ceb
commit 5cddaaed8e
24 changed files with 1031 additions and 142 deletions
@@ -9,6 +9,7 @@ import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import shared.dataInput.XMLReader;
import shared.enums.XMLFileType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -38,7 +39,7 @@ public class App extends Application {
String raceXML = XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8);
String boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
Event raceEvent = new Event(raceXML, regattaXML, boatXML, boatPolars);
Event raceEvent = new Event(raceXML, regattaXML, boatXML, XMLFileType.Contents, boatPolars);
raceEvent.start();
} catch (Exception e) {
@@ -2,11 +2,7 @@ package mock.app;
import mock.model.MockRace;
import mock.model.Polars;
import network.Messages.Enums.MessageType;
import network.Messages.Enums.XMLMessageType;
import network.Messages.LatestMessages;
import network.Messages.XMLMessage;
import org.xml.sax.SAXException;
import shared.dataInput.*;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidBoatDataException;
@@ -15,9 +11,7 @@ import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import shared.model.Constants;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.text.ParseException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -30,16 +24,31 @@ public class Event {
private String raceXML;
private String regattaXML;
private String boatXML;
private XMLFileType xmlFileType;
private Polars boatPolars;
private MockOutput mockOutput;
private LatestMessages latestMessages;
public Event(String raceXML, String regattaXML, String boatXML, Polars boatPolars) {
/**
* Constructs an event, using various XML files.
* @param raceXML The race.xml file.
* @param regattaXML The regatta.xml file.
* @param boatXML The boat.xml file.
* @param type How to read the file - e.g., load as resource.
* @param boatPolars
*/
public Event(String raceXML, String regattaXML, String boatXML, XMLFileType type, Polars boatPolars) {
this.raceXML = getRaceXMLAtCurrentTime(raceXML);
this.boatXML = boatXML;
this.regattaXML = regattaXML;
this.xmlFileType = type;
this.boatPolars = boatPolars;
this.latestMessages = new LatestMessages();
@@ -55,26 +64,26 @@ public class Event {
}
/**
* Sends the initial race data and then begins race simulation
* Sends the initial race data and then begins race simulation.
* @throws InvalidRaceDataException Thrown if the race xml file cannot be parsed.
* @throws XMLReaderException Thrown if any of the xml files cannot be parsed.
* @throws InvalidBoatDataException Thrown if the boat xml file cannot be parsed.
* @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed.
*/
public void start() {
try {
sendXMLs();
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException {
//Parse the XML files into data sources.
RaceDataSource raceDataSource = new RaceXMLReader(this.raceXML, XMLFileType.ResourcePath);
BoatDataSource boatDataSource = new BoatXMLReader(this.boatXML, XMLFileType.ResourcePath);
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, XMLFileType.ResourcePath);
sendXMLs();
//Create and start race.
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
//Parse the XML files into data sources.
RaceDataSource raceDataSource = new RaceXMLReader(this.raceXML, this.xmlFileType);
BoatDataSource boatDataSource = new BoatXMLReader(this.boatXML, this.xmlFileType);
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, this.xmlFileType);
new Thread(newRace).start();
//Create and start race.
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
new Thread(newRace).start();
} catch (XMLReaderException | InvalidBoatDataException | InvalidRaceDataException | InvalidRegattaDataException e) {
e.printStackTrace();
//TODO if this occurs, we should print and error and abort.
}
}
/**
@@ -84,7 +84,7 @@ public class RaceVisionByteEncoder {
return raceStatusMessage.array();
}
public byte[] displayTextMessage(RaceMessage[] message){
public static byte[] displayTextMessage(RaceMessage[] message){
//ByteBuffer result = ByteBuffer.allocate(4 + numLines * 32);
int messageVersionNumber = 0b1;//version number
short ackNum = 0;//no clue what this does just a placeholder for 2 bytes.
@@ -125,7 +125,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] raceStartStatus(long time, short ack, long startTime, int raceID, char notification){
public static byte[] raceStartStatus(long time, short ack, long startTime, int raceID, char notification){
int messageVersion = 0b1;
byte[] timestamp = longToBytes(time, 6);
byte[] ackNumber = intToBytes(ack, 2);
@@ -144,7 +144,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] yachtEventCode(long time, short acknowledgeNumber, int raceID, int destSourceID, int incidentID,
public static byte[] yachtEventCode(long time, short acknowledgeNumber, int raceID, int destSourceID, int incidentID,
int eventID){
int messageVersion = 0b10;
byte[] encodeTime = longToBytes(time, 6);
@@ -165,7 +165,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] chatterText(int messageType, String message){
public static byte[] chatterText(int messageType, String message){
int messageVersion = 0b1;
byte[] type = intToBytes(messageType, 1);
byte[] text = message.getBytes();
@@ -268,7 +268,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] markRounding(int time, int ackNumber, int raceID, int sourceID, int boatStatus, int roundingSide, int markType, int markID){
public static byte[] markRounding(int time, int ackNumber, int raceID, int sourceID, int boatStatus, int roundingSide, int markType, int markID){
int messageVersionNumber = 0b1;
byte[] byteTime = longToBytes(time, 6);
byte[] byteAck = intToBytes(ackNumber, 2);
@@ -292,7 +292,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] courseWind(byte windID, ArrayList<CourseWind> courseWinds){
public static byte[] courseWind(byte windID, ArrayList<CourseWind> courseWinds){
int messageVersionNumber = 0b1;
byte byteWindID = windID;
byte[] loopcount = intToBytes(courseWinds.size(), 1);
@@ -313,7 +313,7 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] averageWind(int time, int rawPeriod, int rawSampleSpeed, int period2, int speed2, int period3, int speed3, int period4, int speed4){
public static byte[] averageWind(int time, int rawPeriod, int rawSampleSpeed, int period2, int speed2, int period3, int speed3, int period4, int speed4){
int messageVersionNumber = 0b1;
byte[] byteTime = longToBytes(time,6);
byte[] byteRawPeriod = intToBytes(rawPeriod, 2);
@@ -30,15 +30,15 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
/**
* Constructor for Boat XML using a file read as a resource.
* Constructor for Boat XML using a file.
*
* @param fileContents Contents of xml file.
* @param file The file to read.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if the file cannot be parsed.
* @throws InvalidBoatDataException Thrown if the file cannot be parsed correctly.
*/
public BoatXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidBoatDataException {
super(fileContents, type);
public BoatXMLReader(String file, XMLFileType type) throws XMLReaderException, InvalidBoatDataException {
super(file, type);
//Attempt to read boat xml file.
try {
@@ -83,14 +83,14 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
/**
* Constructor for Streamed Race XML
* @param fileContents Contents of xml file.
* @param file The file to read.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if an XML reader cannot be constructed for the given file.
* @throws InvalidRaceDataException Thrown if the XML file is invalid in some way.
*/
public RaceXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidRaceDataException {
public RaceXMLReader(String file, XMLFileType type) throws XMLReaderException, InvalidRaceDataException {
super(fileContents, type);
super(file, type);
//Attempt to read race xml file.
@@ -65,15 +65,15 @@ public class RegattaXMLReader extends XMLReader implements RegattaDataSource {
/**
* Constructor for Regatta XML using a file read as a resource.
* Constructor for Regatta XML using a file.
*
* @param fileContents Contents of xml file.
* @param file The file.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if the file cannot be parsed.
* @throws InvalidRegattaDataException Thrown if the file cannot be parsed correctly.
*/
public RegattaXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidRegattaDataException {
super(fileContents, type);
public RegattaXMLReader(String file, XMLFileType type) throws XMLReaderException, InvalidRegattaDataException {
super(file, type);
//Attempt to read boat xml file.
try {
@@ -173,6 +173,11 @@ public abstract class XMLReader {
InputStream fileStream = XMLReader.class.getClassLoader().getResourceAsStream(path);
//Resource can't be found.
if (fileStream == null) {
throw new XMLReaderException("Could not open resource: " + path, new IOException());
}
Document doc = XMLReader.parseInputStream(fileStream);
doc.getDocumentElement().normalize();
@@ -116,9 +116,9 @@ public class StartController extends Controller implements Observer {
private void startRace(LatestMessages latestMessages) throws XMLReaderException, InvalidRaceDataException, InvalidBoatDataException, InvalidRegattaDataException {
//Create data sources from latest messages for the race.
RaceDataSource raceDataSource = new RaceXMLReader(latestMessages.getRaceXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
BoatDataSource boatDataSource = new BoatXMLReader(latestMessages.getBoatXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
RegattaDataSource regattaDataSource = new RegattaXMLReader(latestMessages.getRegattaXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
RaceDataSource raceDataSource = new RaceXMLReader(latestMessages.getRaceXMLMessage().getXmlMessage(), XMLFileType.Contents);
BoatDataSource boatDataSource = new BoatXMLReader(latestMessages.getBoatXMLMessage().getXmlMessage(), XMLFileType.Contents);
RegattaDataSource regattaDataSource = new RegattaXMLReader(latestMessages.getRegattaXMLMessage().getXmlMessage(), XMLFileType.Contents);
//Create race.
this.visualiserRace = new VisualiserRace(boatDataSource, raceDataSource, regattaDataSource, latestMessages, this.colors);