Merge branch 'sprint4_master' into story36_boundary_fix_attempt_2
This commit is contained in:
@@ -49,6 +49,16 @@ public class App extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the Initial Race XML files that are necessary to run the mock.
|
||||
* @param path path of the XML
|
||||
* @param encoding encoding of the xml
|
||||
* @return
|
||||
* @throws IOException No file etc
|
||||
* @throws ParserConfigurationException Issue with the XML formatting
|
||||
* @throws SAXException Issue with XML formatting
|
||||
* @throws TransformerException Issue with the XML format
|
||||
*/
|
||||
private String readFile(String path, Charset encoding) throws IOException, ParserConfigurationException, SAXException, TransformerException {
|
||||
|
||||
InputSource fXmlFile = new InputSource(getClass().getClassLoader().getResourceAsStream(path));
|
||||
|
||||
@@ -6,7 +6,7 @@ import seng302.Model.Mark;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by cbt24 on 10/05/17.
|
||||
* Boats Data
|
||||
*/
|
||||
public interface BoatDataSource {
|
||||
Map<Integer, Boat> getBoats();
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by cbt24 on 10/05/17.
|
||||
* Xml Reader class for Boat XML used for the race
|
||||
*/
|
||||
public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
|
||||
@@ -42,12 +42,18 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the XML
|
||||
*/
|
||||
public void read() {
|
||||
readSettings();
|
||||
readShapes();
|
||||
readBoats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the Boats
|
||||
*/
|
||||
private void readBoats() {
|
||||
Element nBoats = (Element) doc.getElementsByTagName("Boats").item(0);
|
||||
for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) {
|
||||
@@ -72,6 +78,11 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the node (XMl data node) is a Yacht or not
|
||||
* @param boatNode Node from the XML
|
||||
* @return Whether the node is a yacht node or not
|
||||
*/
|
||||
private boolean isYachtNode(Node boatNode) {
|
||||
return boatNode.getAttributes().getNamedItem("Type").getTextContent().toLowerCase().equals("yacht");
|
||||
}
|
||||
@@ -88,6 +99,12 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
else readMark(boatNode, sourceID, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a Yacht Node
|
||||
* @param boatNode Node to be read
|
||||
* @param sourceID Source ID of the Yacht
|
||||
* @param name Name of the Boat
|
||||
*/
|
||||
private void readYacht(Node boatNode, int sourceID, String name) {
|
||||
String shortName = boatNode.getAttributes().getNamedItem("ShortName").getTextContent();
|
||||
if (exists(boatNode, "Country")) {
|
||||
@@ -98,6 +115,12 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Marker Boats
|
||||
* @param boatNode Node to be read
|
||||
* @param sourceID Source ID of the boat
|
||||
* @param name Name of the Marker Boat
|
||||
*/
|
||||
private void readMark(Node boatNode, int sourceID, String name) {
|
||||
Node nCoord = ((Element)boatNode).getElementsByTagName("GPSposition").item(0);
|
||||
double x = Double.parseDouble(nCoord.getAttributes().getNamedItem("X").getTextContent());
|
||||
@@ -106,11 +129,19 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
markerMap.put(sourceID, mark);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the boats that are going to participate in this race
|
||||
* @return Dictionary of boats that are to participate in this race indexed by SourceID
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Boat> getBoats() {
|
||||
return boatMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the marker BOats that are participating in this race
|
||||
* @return Dictionary of the Markers BOats that are in this race indexed by their Source ID.
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Mark> getMarkerBoats() {
|
||||
return markerMap;
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by connortaylorbrown on 19/04/17.
|
||||
* Data Class for a Race
|
||||
*/
|
||||
public interface RaceDataSource {
|
||||
List<Boat> getBoats();
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by jjg64 on 21/04/17.
|
||||
* XML Reader that reads in the race data required for this race
|
||||
*/
|
||||
public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
private static final double COORDINATEPADDING = 0.000;
|
||||
@@ -103,6 +103,9 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
postpone = Boolean.parseBoolean(raceTimeTag.getNamedItem("Postpone").getTextContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in the participants for htis race
|
||||
*/
|
||||
private void readParticipants() {
|
||||
Element nParticipants = (Element) doc.getElementsByTagName("Participants").item(0);
|
||||
nParticipants.getChildNodes().getLength();
|
||||
@@ -166,6 +169,11 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
return marker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a mark from an Element
|
||||
* @param mark Element the mark is suppose to be part of
|
||||
* @return a Mark that existed in the element
|
||||
*/
|
||||
private Mark getMark(Element mark) {
|
||||
int sourceID = Integer.parseInt(mark.getAttribute("SourceID"));
|
||||
return marks.get(sourceID);
|
||||
@@ -209,6 +217,9 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the boundary limits of the course
|
||||
*/
|
||||
private void readCourseLimit() {
|
||||
Element nCourseLimit = (Element) doc.getElementsByTagName("CourseLimit").item(0);
|
||||
for(int i = 0; i < nCourseLimit.getChildNodes().getLength(); i++) {
|
||||
|
||||
@@ -18,7 +18,7 @@ import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 26/03/2017.
|
||||
* Base Reader for XML Files
|
||||
*/
|
||||
public abstract class XMLReader {
|
||||
|
||||
@@ -48,6 +48,11 @@ public abstract class XMLReader {
|
||||
doc.getDocumentElement().normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternate constructor
|
||||
* @param xmlFile
|
||||
* @param isWholeFile
|
||||
*/
|
||||
public XMLReader(String xmlFile, Boolean isWholeFile) {
|
||||
|
||||
}
|
||||
@@ -84,6 +89,12 @@ public abstract class XMLReader {
|
||||
return node.getAttributes().getNamedItem(attribute) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of the XML FILe.
|
||||
* @param document
|
||||
* @return
|
||||
* @throws TransformerException
|
||||
*/
|
||||
public static String getContents(Document document) throws TransformerException {
|
||||
DOMSource source = new DOMSource(document);
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ public class MockOutput implements Runnable
|
||||
private String regattaXml;
|
||||
private String boatsXml;
|
||||
|
||||
private boolean stop = false; //whether or not hte thread keeps running
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
@@ -158,18 +159,25 @@ public class MockOutput implements Runnable
|
||||
this.messagesToSendQueue.add(messagesToSendBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sending loop of the Server
|
||||
*/
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
while (true){
|
||||
while (!stop){
|
||||
System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE
|
||||
mockSocket = serverSocket.accept();
|
||||
|
||||
outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
|
||||
|
||||
|
||||
while(boatsXml == null || regattaXml == null || raceXml == null) {
|
||||
|
||||
if (boatsXml == null || regattaXml == null || raceXml == null){
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
parseXMLString(raceXml, XMLMessage.XMLTypeRace);
|
||||
@@ -205,18 +213,30 @@ public class MockOutput implements Runnable
|
||||
}
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
stop = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Race XML to send
|
||||
* @param raceXml XML to send to the CLient
|
||||
*/
|
||||
public void setRaceXml(String raceXml) {
|
||||
this.raceXml = raceXml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Regatta XMl to send
|
||||
* @param regattaXml XML to send to CLient
|
||||
*/
|
||||
public void setRegattaXml(String regattaXml) {
|
||||
this.regattaXml = regattaXml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Boats XML to send
|
||||
* @param boatsXml XMl to send to the CLient
|
||||
*/
|
||||
public void setBoatsXml(String boatsXml) {
|
||||
this.boatsXml = boatsXml;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import seng302.Networking.Messages.Enums.BoatStatusEnum;
|
||||
|
||||
|
||||
/**
|
||||
* This class represents a boat during a race.
|
||||
* Boat Model that is used to store information on the boats that are running in the race.
|
||||
*/
|
||||
public class Boat {
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.time.format.DateTimeFormatter;
|
||||
|
||||
|
||||
/**
|
||||
* Created by esa46 on 21/04/17.
|
||||
* A Race Event, this holds all of the race's information as well as handling the connection to its clients.
|
||||
*/
|
||||
public class Event {
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package seng302.Model;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a leg of a race.
|
||||
* Leg of the race, this is what each part of the race is divided into, from mark to mark.
|
||||
*/
|
||||
public class Leg {
|
||||
|
||||
|
||||
@@ -379,7 +379,7 @@ public class Race implements Runnable {
|
||||
|
||||
} else {
|
||||
//Otherwise, the race is over!
|
||||
|
||||
raceFinished.start();
|
||||
setRaceStatusEnum(RaceStatusEnum.FINISHED);
|
||||
this.stop();
|
||||
}
|
||||
@@ -402,6 +402,22 @@ public class Race implements Runnable {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Broadcast that the race has finished.
|
||||
*/
|
||||
protected AnimationTimer raceFinished = new AnimationTimer(){
|
||||
int iters = 0;
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, 0, 2300, 2, null);
|
||||
mockOutput.parseRaceStatus(raceStatus);
|
||||
if (iters > 500){
|
||||
mockOutput.stop();
|
||||
stop();
|
||||
}
|
||||
iters++;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialise the boats in the race.
|
||||
@@ -621,7 +637,6 @@ public class Race implements Runnable {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the upper and lower bounds that the boat may have in order to not go outside of the course.
|
||||
* @param boat The boat to check.
|
||||
|
||||
@@ -14,7 +14,7 @@ import static junit.framework.TestCase.assertEquals;
|
||||
*/
|
||||
public class LegTest {
|
||||
|
||||
private CompoundMark ORIGIN_Compound_MARKER = new CompoundMark(new Mark(1, "test mark1", new GPSCoordinate(0, 0)));
|
||||
private CompoundMark ORIGIN_COMPOUND_MARKER = new CompoundMark(new Mark(1, "test mark1", new GPSCoordinate(0, 0)));
|
||||
|
||||
@Test
|
||||
public void calculateDistanceHandles5nmNorth() {
|
||||
@@ -23,7 +23,7 @@ public class LegTest {
|
||||
calc.setDirection(0, 5 * Constants.NMToMetersConversion);
|
||||
|
||||
CompoundMark endMarker = getEndMarker(calc.getDestinationGeographicPoint());
|
||||
Leg test = new Leg("Test", ORIGIN_Compound_MARKER, endMarker, 0);
|
||||
Leg test = new Leg("Test", ORIGIN_COMPOUND_MARKER, endMarker, 0);
|
||||
assertEquals(test.getDistanceNauticalMiles(), 5, 1e-8);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class LegTest {
|
||||
calc.setDirection(90, 12 * Constants.NMToMetersConversion);
|
||||
|
||||
CompoundMark endMarker = getEndMarker(calc.getDestinationGeographicPoint());
|
||||
Leg test = new Leg("Test", ORIGIN_Compound_MARKER, endMarker, 0);
|
||||
Leg test = new Leg("Test", ORIGIN_COMPOUND_MARKER, endMarker, 0);
|
||||
assertEquals(test.getDistanceNauticalMiles(), 12, 1e-8);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class LegTest {
|
||||
calc.setDirection(180, 0.5 * Constants.NMToMetersConversion);
|
||||
|
||||
CompoundMark endMarker = getEndMarker(calc.getDestinationGeographicPoint());
|
||||
Leg test = new Leg("Test", ORIGIN_Compound_MARKER, endMarker, 0);
|
||||
Leg test = new Leg("Test", ORIGIN_COMPOUND_MARKER, endMarker, 0);
|
||||
assertEquals(test.getDistanceNauticalMiles(), 0.5, 1e-8);
|
||||
}
|
||||
|
||||
@@ -56,14 +56,14 @@ public class LegTest {
|
||||
calc.setDirection(-90, 0.1 * Constants.NMToMetersConversion);
|
||||
|
||||
CompoundMark endMarker = getEndMarker(calc.getDestinationGeographicPoint());
|
||||
Leg test = new Leg("Test", ORIGIN_Compound_MARKER, endMarker, 0);
|
||||
Leg test = new Leg("Test", ORIGIN_COMPOUND_MARKER, endMarker, 0);
|
||||
assertEquals(test.getDistanceNauticalMiles(), 0.1, 1e-8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calculateDistanceHandlesZeroDifference() {
|
||||
|
||||
Leg test = new Leg("Test", ORIGIN_Compound_MARKER, ORIGIN_Compound_MARKER, 0);
|
||||
Leg test = new Leg("Test", ORIGIN_COMPOUND_MARKER, ORIGIN_COMPOUND_MARKER, 0);
|
||||
assertEquals(test.getDistanceNauticalMiles(), 0, 1e-8);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user