Merge remote-tracking branch 'origin/sprint4_master' into 33CherryPick
This commit is contained in:
@@ -0,0 +1,170 @@
|
|||||||
|
package seng302;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import seng302.Mock.BoatXMLReader;
|
||||||
|
import seng302.Mock.RegattaXMLReader;
|
||||||
|
import seng302.Mock.StreamedCourseXMLException;
|
||||||
|
import seng302.Mock.StreamedCourseXMLReader;
|
||||||
|
import seng302.Networking.Messages.*;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Gondr on 18/05/2017.
|
||||||
|
*/
|
||||||
|
public class AC35RaceStatus {
|
||||||
|
private final VisualiserInput visualiserInput;
|
||||||
|
protected AverageWind averageWind;
|
||||||
|
protected final Map<Integer, BoatLocation> boatLocation = new HashMap<>();
|
||||||
|
protected final Map<Integer, BoatStatus> boatStatusMap = new HashMap<>();
|
||||||
|
protected final Map<Integer, MarkRounding> markRoundingMap = new HashMap<>();
|
||||||
|
protected CourseWinds courseWinds;
|
||||||
|
protected RaceMessage raceMessage;
|
||||||
|
protected RaceStartStatus raceStartStatus;
|
||||||
|
protected RaceStatus raceStatus;
|
||||||
|
|
||||||
|
public AC35RaceStatus(VisualiserInput visualiserInput){
|
||||||
|
this.visualiserInput = visualiserInput;
|
||||||
|
averageWind = null;
|
||||||
|
courseWinds = null;
|
||||||
|
raceMessage = null;
|
||||||
|
raceStartStatus = null;
|
||||||
|
raceStatus = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(AC35Data message){
|
||||||
|
//Checks which message is being received and does what is needed for that message.
|
||||||
|
//Heartbeat.
|
||||||
|
switch(message.getType()) {
|
||||||
|
case HEARTBEAT:
|
||||||
|
Heartbeat heartbeat = (Heartbeat) message;
|
||||||
|
|
||||||
|
//Check that the heartbeat number is greater than the previous value, and then set the last heartbeat time.
|
||||||
|
if (heartbeat.getSequenceNumber() > visualiserInput.getLastHeartbeatSequenceNum()) {
|
||||||
|
visualiserInput.setLastHeartbeatTime(System.currentTimeMillis());
|
||||||
|
visualiserInput.setLastHeartbeatSequenceNum(heartbeat.getSequenceNumber());
|
||||||
|
//System.out.println("HeartBeat Message! " + lastHeartbeatSequenceNum);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RACESTATUS:
|
||||||
|
RaceStatus raceStatus = (RaceStatus) message;
|
||||||
|
|
||||||
|
//System.out.println("Race Status Message");
|
||||||
|
this.raceStatus = raceStatus;
|
||||||
|
for (BoatStatus boatStatus : this.raceStatus.getBoatStatuses()) {
|
||||||
|
this.boatStatusMap.put(boatStatus.getSourceID(), boatStatus);
|
||||||
|
}
|
||||||
|
visualiserInput.setCourseWindDirection(raceStatus.getScaledWindDirection());
|
||||||
|
break;
|
||||||
|
case DISPLAYTEXTMESSAGE:
|
||||||
|
//System.out.println("Display Text Message");
|
||||||
|
//No decoder for this.
|
||||||
|
break;
|
||||||
|
case XMLMESSAGE:
|
||||||
|
XMLMessage xmlMessage = (XMLMessage) message;
|
||||||
|
|
||||||
|
//System.out.println("XML Message!");
|
||||||
|
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeRegatta) {
|
||||||
|
//System.out.println("Setting Regatta");
|
||||||
|
try {
|
||||||
|
visualiserInput.getCourse().setRegattaXMLReader(new RegattaXMLReader(xmlMessage.getXmlMessage()));
|
||||||
|
|
||||||
|
}
|
||||||
|
//TODO REFACTOR should put all of these exceptions behind a RegattaXMLReaderException.
|
||||||
|
catch (IOException | SAXException | ParserConfigurationException e) {
|
||||||
|
System.err.println("Error creating RegattaXMLReader: " + e.getMessage());
|
||||||
|
//Continue to the next loop iteration/message.
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeRace) {
|
||||||
|
//System.out.println("Setting Course");
|
||||||
|
try {
|
||||||
|
visualiserInput.getCourse().setStreamedCourseXMLReader(new StreamedCourseXMLReader(xmlMessage.getXmlMessage()));
|
||||||
|
}
|
||||||
|
//TODO REFACTOR should put all of these exceptions behind a StreamedCourseXMLReaderException.
|
||||||
|
catch (IOException | SAXException | ParserConfigurationException | StreamedCourseXMLException e) {
|
||||||
|
System.err.println("Error creating StreamedCourseXMLReader: " + e.getMessage());
|
||||||
|
//Continue to the next loop iteration/message.
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeBoat) {
|
||||||
|
//System.out.println("Setting Boats");
|
||||||
|
try {
|
||||||
|
visualiserInput.getCourse().setBoatXMLReader(new BoatXMLReader(xmlMessage.getXmlMessage()));
|
||||||
|
}
|
||||||
|
//TODO REFACTOR should put all of these exceptions behind a BoatXMLReaderException.
|
||||||
|
catch (IOException | SAXException | ParserConfigurationException e) {
|
||||||
|
System.err.println("Error creating BoatXMLReader: " + e.getMessage());
|
||||||
|
//Continue to the next loop iteration/message.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case RACESTARTSTATUS:
|
||||||
|
//System.out.println("Race Start Status Message");
|
||||||
|
break;
|
||||||
|
case YACHTEVENTCODE:
|
||||||
|
//System.out.println("Yacht Event Code!");
|
||||||
|
//No decoder for this.
|
||||||
|
break;
|
||||||
|
case YACHTACTIONCODE:
|
||||||
|
//System.out.println("Yacht Action Code!");
|
||||||
|
//No decoder for this.
|
||||||
|
break;
|
||||||
|
case CHATTERTEXT:
|
||||||
|
//System.out.println("Chatter Text Message!");
|
||||||
|
//No decoder for this.
|
||||||
|
break;
|
||||||
|
case BOATLOCATION:
|
||||||
|
BoatLocation location = (BoatLocation) message;
|
||||||
|
|
||||||
|
//System.out.println("Boat Location!");
|
||||||
|
if (this.boatLocation.containsKey(location.getSourceID())) {
|
||||||
|
//If our boatlocation map already contains a boat location message for this boat, check that the new message is actually for a later timestamp (i.e., newer).
|
||||||
|
if (location.getTime() > this.boatLocation.get(location.getSourceID()).getTime()) {
|
||||||
|
//If it is, replace the old message.
|
||||||
|
this.boatLocation.put(location.getSourceID(), location);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//If the map _doesn't_ already contain a message for this boat, insert the message.
|
||||||
|
this.boatLocation.put(location.getSourceID(), location);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MARKROUNDING:
|
||||||
|
MarkRounding markRounding = (MarkRounding) message;
|
||||||
|
|
||||||
|
//System.out.println("Mark Rounding Message!");
|
||||||
|
|
||||||
|
if (this.markRoundingMap.containsKey(markRounding.getSourceID())) {
|
||||||
|
//If our markRoundingMap already contains a mark rounding message for this boat, check that the new message is actually for a later timestamp (i.e., newer).
|
||||||
|
if (markRounding.getTime() > this.markRoundingMap.get(markRounding.getSourceID()).getTime()) {
|
||||||
|
//If it is, replace the old message.
|
||||||
|
this.markRoundingMap.put(markRounding.getSourceID(), markRounding);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//If the map _doesn't_ already contain a message for this boat, insert the message.
|
||||||
|
this.markRoundingMap.put(markRounding.getSourceID(), markRounding);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case COURSEWIND:
|
||||||
|
//System.out.println("Course Wind Message!");
|
||||||
|
this.courseWinds = (CourseWinds) message;
|
||||||
|
break;
|
||||||
|
case AVGWIND:
|
||||||
|
//System.out.println("Average Wind Message!");
|
||||||
|
this.averageWind = (AverageWind) message;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Broken Message!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,208 +0,0 @@
|
|||||||
//package seng302.Model;
|
|
||||||
//
|
|
||||||
//import org.geotools.referencing.GeodeticCalculator;
|
|
||||||
//import seng302.Constants;
|
|
||||||
//import seng302.Controllers.RaceController;
|
|
||||||
//import seng302.GPSCoordinate;
|
|
||||||
//import seng302.RaceDataSource;
|
|
||||||
//
|
|
||||||
//import java.awt.geom.Point2D;
|
|
||||||
//import java.util.ArrayList;
|
|
||||||
//import java.util.Arrays;
|
|
||||||
//import java.util.List;
|
|
||||||
//import java.util.Random;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * Created by cbt24 on 6/03/17.
|
|
||||||
// *
|
|
||||||
// * @deprecated
|
|
||||||
// */
|
|
||||||
//public class ConstantVelocityRace extends Race {
|
|
||||||
//
|
|
||||||
// private int dnfChance = 0; //%percentage chance a boat fails at each checkpoint
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Initializer for a constant velocity race without standard data source
|
|
||||||
// *
|
|
||||||
// * @param startingBoats in race
|
|
||||||
// * @param legs in race
|
|
||||||
// * @param controller for graphics
|
|
||||||
// * @param scaleFactor of timer
|
|
||||||
// */
|
|
||||||
// public ConstantVelocityRace(List<BoatInRace> startingBoats, List<Leg> legs, RaceController controller, int scaleFactor) {
|
|
||||||
// super(startingBoats, legs, controller, scaleFactor);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Initializer for legacy tests
|
|
||||||
// *
|
|
||||||
// * @param startingBoats in race
|
|
||||||
// * @param legs in race
|
|
||||||
// * @param controller for graphics
|
|
||||||
// * @param scaleFactor of timer
|
|
||||||
// *
|
|
||||||
// * @deprecated Please use {@link #ConstantVelocityRace(List, List, RaceController, int) } for future tests.
|
|
||||||
// */
|
|
||||||
// public ConstantVelocityRace(BoatInRace[] startingBoats, List<Leg> legs, RaceController controller, int scaleFactor) {
|
|
||||||
// super(Arrays.asList(startingBoats), legs, controller, scaleFactor);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Initializer for constant velocity race with standard data source
|
|
||||||
// * @param raceData for race
|
|
||||||
// * @param controller for graphics
|
|
||||||
// * @param scaleFactor of timer
|
|
||||||
// */
|
|
||||||
// public ConstantVelocityRace(RaceDataSource raceData, RaceController controller, int scaleFactor) {
|
|
||||||
// super(raceData, controller, scaleFactor);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public void initialiseBoats() {
|
|
||||||
// Leg officialStart = legs.get(0);
|
|
||||||
// String name = officialStart.getName();
|
|
||||||
// CompoundMark endMarker = officialStart.getEndCompoundMark();
|
|
||||||
//
|
|
||||||
// BoatInRace.setTrackPointTimeInterval(BoatInRace.getBaseTrackPointTimeInterval() / scaleFactor);
|
|
||||||
//
|
|
||||||
// ArrayList<CompoundMark> startMarkers = getSpreadStartingPositions();
|
|
||||||
// for (int i = 0; i < startingBoats.size(); i++) {
|
|
||||||
// BoatInRace boat = startingBoats.get(i);
|
|
||||||
// if (boat != null) {
|
|
||||||
// boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
||||||
// Leg startLeg = new Leg(name, 0);
|
|
||||||
// boat.setCurrentPosition(startMarkers.get(i).getAverageGPSCoordinate());
|
|
||||||
// startLeg.setStartCompoundMark(startMarkers.get(i));
|
|
||||||
// startLeg.setEndCompoundMark(endMarker);
|
|
||||||
// startLeg.calculateDistance();
|
|
||||||
// boat.setCurrentLeg(startLeg);
|
|
||||||
// boat.setHeading(boat.calculateHeading());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Creates a list of starting positions for the different boats, so they do not appear cramped at the start line
|
|
||||||
// *
|
|
||||||
// * @return list of starting positions
|
|
||||||
// */
|
|
||||||
// public ArrayList<CompoundMark> getSpreadStartingPositions() {
|
|
||||||
//
|
|
||||||
// int nBoats = startingBoats.size();
|
|
||||||
// CompoundMark marker = legs.get(0).getStartCompoundMark();
|
|
||||||
//
|
|
||||||
// GeodeticCalculator initialCalc = new GeodeticCalculator();
|
|
||||||
// initialCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
||||||
// initialCalc.setDestinationGeographicPoint(marker.getMark2().getLongitude(), marker.getMark2().getLatitude());
|
|
||||||
//
|
|
||||||
// double azimuth = initialCalc.getAzimuth();
|
|
||||||
// double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
|
|
||||||
// double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
|
|
||||||
//
|
|
||||||
// GeodeticCalculator positionCalc = new GeodeticCalculator();
|
|
||||||
// positionCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
||||||
// ArrayList<CompoundMark> positions = new ArrayList<>();
|
|
||||||
//
|
|
||||||
// for (int i = 0; i < nBoats; i++) {
|
|
||||||
// positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
|
||||||
// Point2D position = positionCalc.getDestinationGeographicPoint();
|
|
||||||
// positions.add(new CompoundMark(new GPSCoordinate(position.getY(), position.getX())));
|
|
||||||
//
|
|
||||||
// positionCalc = new GeodeticCalculator();
|
|
||||||
// positionCalc.setStartingGeographicPoint(position);
|
|
||||||
// }
|
|
||||||
// return positions;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Sets the chance each boat has of failing at a gate or marker
|
|
||||||
// * @param chance percentage chance a boat has of failing per checkpoint.
|
|
||||||
// */
|
|
||||||
// protected void setDnfChance(int chance) {
|
|
||||||
// if (chance >= 0 && chance <= 100) {
|
|
||||||
// dnfChance = chance;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// protected boolean doNotFinish() {
|
|
||||||
// Random rand = new Random();
|
|
||||||
// return rand.nextInt(100) < dnfChance;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Calculates the distance a boat has travelled and updates its current position according to this value.
|
|
||||||
// *
|
|
||||||
// * @param boat to be updated
|
|
||||||
// * @param millisecondsElapsed since last update
|
|
||||||
// */
|
|
||||||
// protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
|
|
||||||
//
|
|
||||||
// //distanceTravelled = velocity (nm p hr) * time taken to update loop
|
|
||||||
// double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000;
|
|
||||||
//
|
|
||||||
// double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
|
|
||||||
//
|
|
||||||
// boolean finish = boat.getCurrentLeg().getName().equals("Finish");
|
|
||||||
// if (!finish) {
|
|
||||||
// boat.setHeading(boat.calculateHeading());
|
|
||||||
// //update boat's distance travelled
|
|
||||||
// boat.setDistanceTravelledInLeg(totalDistanceTravelled);
|
|
||||||
// //Calculate boat's new position by adding the distance travelled onto the start point of the leg
|
|
||||||
// boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartCompoundMark().getAverageGPSCoordinate(),
|
|
||||||
// totalDistanceTravelled, boat.calculateAzimuth()));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
||||||
// if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
||||||
// //boat has passed onto new leg
|
|
||||||
// if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
||||||
// //boat has finished
|
|
||||||
// boatsFinished++;
|
|
||||||
// boat.setFinished(true);
|
|
||||||
// boat.setTimeFinished(timeElapsed);
|
|
||||||
// } else if (doNotFinish()) {
|
|
||||||
// boatsFinished++;
|
|
||||||
// boat.setFinished(true);
|
|
||||||
// boat.setCurrentLeg(new Leg("DNF", -1));
|
|
||||||
// boat.setVelocity(0);
|
|
||||||
// boat.setScaledVelocity(0);
|
|
||||||
// } else {
|
|
||||||
// //Calculate how much the boat overshot the marker by
|
|
||||||
// boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
||||||
// //Move boat on to next leg
|
|
||||||
// Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
||||||
//
|
|
||||||
// boat.setCurrentLeg(nextLeg);
|
|
||||||
// //Add overshoot distance into the distance travelled for the next leg
|
|
||||||
// boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
||||||
// }
|
|
||||||
// //Update the boat display table in the GUI to reflect the leg change
|
|
||||||
// updatePositions();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Calculates the boats next GPS position based on its distance travelled and heading
|
|
||||||
// *
|
|
||||||
// * @param oldCoordinates GPS coordinates of the boat's starting position
|
|
||||||
// * @param distanceTravelled distance in nautical miles
|
|
||||||
// * @param azimuth boat's current direction. Value between -180 and 180
|
|
||||||
// * @return The boat's new coordinate
|
|
||||||
// */
|
|
||||||
// public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) {
|
|
||||||
//
|
|
||||||
// //Find new coordinate using current heading and distance
|
|
||||||
//
|
|
||||||
// GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
|
|
||||||
// //Load start point into calculator
|
|
||||||
// Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude());
|
|
||||||
// geodeticCalculator.setStartingGeographicPoint(startPoint);
|
|
||||||
// //load direction and distance tranvelled into calculator
|
|
||||||
// geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion);
|
|
||||||
// //get new point
|
|
||||||
// Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
|
|
||||||
//
|
|
||||||
// return new GPSCoordinate(endPoint.getY(), endPoint.getX());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
@@ -217,7 +217,6 @@ public class ResizableRaceCanvas extends ResizableCanvas {
|
|||||||
*/
|
*/
|
||||||
public void update() {
|
public void update() {
|
||||||
this.draw();
|
this.draw();
|
||||||
this.updateBoats();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,20 +1,15 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
import javafx.application.Platform;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import seng302.Mock.*;
|
import seng302.Mock.*;
|
||||||
import seng302.Networking.BinaryMessageDecoder;
|
import seng302.Networking.BinaryMessageDecoder;
|
||||||
import seng302.Networking.Exceptions.InvalidMessageException;
|
import seng302.Networking.Exceptions.InvalidMessageException;
|
||||||
import seng302.Networking.Messages.*;
|
import seng302.Networking.Messages.*;
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ArrayBlockingQueue;
|
|
||||||
|
|
||||||
import static seng302.Networking.Utils.ByteConverter.bytesToShort;
|
import static seng302.Networking.Utils.ByteConverter.bytesToShort;
|
||||||
|
|
||||||
@@ -34,23 +29,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
///Object to store parsed course data. //TODO comment?
|
///Object to store parsed course data. //TODO comment?
|
||||||
private StreamedCourse course;
|
private StreamedCourse course;
|
||||||
|
|
||||||
///The last RaceStatus message received.
|
private final AC35RaceStatus ac35RaceStatus;
|
||||||
private RaceStatus raceStatus;
|
|
||||||
|
|
||||||
///A map of the last BoatStatus message received, for each boat.
|
|
||||||
private final Map<Integer, BoatStatus> boatStatusMap = new HashMap<>();
|
|
||||||
|
|
||||||
///A map of the last BoatLocation message received, for each boat.
|
|
||||||
private final Map<Integer, BoatLocation> boatLocationMap = new HashMap<>();
|
|
||||||
|
|
||||||
///The last AverageWind message received.
|
|
||||||
private AverageWind averageWind;
|
|
||||||
|
|
||||||
///The last CourseWinds message received.
|
|
||||||
private CourseWinds courseWinds;
|
|
||||||
|
|
||||||
///A map of the last MarkRounding message received, for each boat.
|
|
||||||
private final Map<Integer, MarkRounding> markRoundingMap = new HashMap<>();
|
|
||||||
|
|
||||||
///InputStream (from the socket).
|
///InputStream (from the socket).
|
||||||
private DataInputStream inStream;
|
private DataInputStream inStream;
|
||||||
@@ -66,7 +45,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
//We wrap a DataInputStream around the socket's InputStream because it has the stream.readFully(buffer) function, which is a blocking read until the buffer has been filled.
|
//We wrap a DataInputStream around the socket's InputStream because it has the stream.readFully(buffer) function, which is a blocking read until the buffer has been filled.
|
||||||
this.inStream = new DataInputStream(connectionSocket.getInputStream());
|
this.inStream = new DataInputStream(connectionSocket.getInputStream());
|
||||||
this.course = course;
|
this.course = course;
|
||||||
|
this.ac35RaceStatus = new AC35RaceStatus(this);
|
||||||
this.lastHeartbeatTime = System.currentTimeMillis();
|
this.lastHeartbeatTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +64,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return The most recent location message.
|
* @return The most recent location message.
|
||||||
*/
|
*/
|
||||||
public BoatLocation getBoatLocationMessage(int sourceID) {
|
public BoatLocation getBoatLocationMessage(int sourceID) {
|
||||||
return boatLocationMap.get(sourceID);
|
return ac35RaceStatus.boatLocation.get(sourceID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BoatStatus getBoatStatusMessage(int sourceID) {
|
public BoatStatus getBoatStatusMessage(int sourceID) {
|
||||||
@@ -106,7 +85,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return Map of boat locations.
|
* @return Map of boat locations.
|
||||||
*/
|
*/
|
||||||
public Map<Integer, BoatLocation> getBoatLocationMap() {
|
public Map<Integer, BoatLocation> getBoatLocationMap() {
|
||||||
return boatLocationMap;
|
return ac35RaceStatus.boatLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,7 +93,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return The status of the race.
|
* @return The status of the race.
|
||||||
*/
|
*/
|
||||||
public RaceStatus getRaceStatus() {
|
public RaceStatus getRaceStatus() {
|
||||||
return raceStatus;
|
return ac35RaceStatus.raceStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -122,7 +101,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return Map of boat statuses.
|
* @return Map of boat statuses.
|
||||||
*/
|
*/
|
||||||
public Map<Integer, BoatStatus> getBoatStatusMap() {
|
public Map<Integer, BoatStatus> getBoatStatusMap() {
|
||||||
return boatStatusMap;
|
return ac35RaceStatus.boatStatusMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -130,7 +109,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return Average wind in the race.
|
* @return Average wind in the race.
|
||||||
*/
|
*/
|
||||||
public AverageWind getAverageWind() {
|
public AverageWind getAverageWind() {
|
||||||
return averageWind;
|
return ac35RaceStatus.averageWind;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,7 +117,7 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return Winds that are in the course.
|
* @return Winds that are in the course.
|
||||||
*/
|
*/
|
||||||
public CourseWinds getCourseWinds() {
|
public CourseWinds getCourseWinds() {
|
||||||
return courseWinds;
|
return ac35RaceStatus.courseWinds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -147,17 +126,41 @@ public class VisualiserInput implements Runnable {
|
|||||||
* @return Map of mark roundings.
|
* @return Map of mark roundings.
|
||||||
*/
|
*/
|
||||||
public Map<Integer, MarkRounding> getMarkRoundingMap() {
|
public Map<Integer, MarkRounding> getMarkRoundingMap() {
|
||||||
return markRoundingMap;
|
return ac35RaceStatus.markRoundingMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the wind direction for the current course.
|
* Sets the wind direction for the current course.
|
||||||
* @param direction The new wind direction for the course.
|
* @param direction The new wind direction for the course.
|
||||||
*/
|
*/
|
||||||
private void setCourseWindDirection(double direction) {
|
protected void setCourseWindDirection(double direction) {
|
||||||
this.course.setWindDirection(direction);
|
this.course.setWindDirection(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets last time the heartbeat was read
|
||||||
|
* @param lastHeartbeatTime time that the heart beat was read (in ms)
|
||||||
|
*/
|
||||||
|
public void setLastHeartbeatTime(long lastHeartbeatTime) {
|
||||||
|
this.lastHeartbeatTime = lastHeartbeatTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the last sequencenumber of the heartbeat
|
||||||
|
* @return the last heartbeat sequence number
|
||||||
|
*/
|
||||||
|
public long getLastHeartbeatSequenceNum() {
|
||||||
|
return lastHeartbeatSequenceNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the last heartbeat squence number
|
||||||
|
* @param lastHeartbeatSequenceNum sequence number of heartbeat
|
||||||
|
*/
|
||||||
|
public void setLastHeartbeatSequenceNum(long lastHeartbeatSequenceNum) {
|
||||||
|
this.lastHeartbeatSequenceNum = lastHeartbeatSequenceNum;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads and returns the next message as an array of bytes from the socket. Use getNextMessage() to get the actual message object instead.
|
* Reads and returns the next message as an array of bytes from the socket. Use getNextMessage() to get the actual message object instead.
|
||||||
* @return Encoded binary message bytes.
|
* @return Encoded binary message bytes.
|
||||||
@@ -235,13 +238,10 @@ public class VisualiserInput implements Runnable {
|
|||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
System.err.println("Unable to reconnect.");
|
System.err.println("Unable to reconnect.");
|
||||||
|
try {
|
||||||
//Wait 500ms. Ugly hack, should refactor.
|
Thread.sleep(500);
|
||||||
long waitPeriod = 500;
|
} catch (InterruptedException e1) {
|
||||||
long waitTimeStart = System.currentTimeMillis() + waitPeriod;
|
e1.printStackTrace();
|
||||||
|
|
||||||
while (System.currentTimeMillis() < waitTimeStart){
|
|
||||||
//Nothing. Busyloop.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Swallow the exception.
|
//Swallow the exception.
|
||||||
@@ -261,169 +261,14 @@ public class VisualiserInput implements Runnable {
|
|||||||
try {
|
try {
|
||||||
inStream.reset();
|
inStream.reset();
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
e1.printStackTrace();
|
//swallow the exception
|
||||||
}
|
}
|
||||||
//Continue to the next loop iteration/message.
|
//Continue to the next loop iteration/message.
|
||||||
continue;
|
continue;
|
||||||
}/*
|
|
||||||
|
|
||||||
//Add it to message queue.
|
|
||||||
this.messagesReceivedQueue.add(message);*/
|
|
||||||
|
|
||||||
|
|
||||||
//Checks which message is being received and does what is needed for that message.
|
|
||||||
//Heartbeat.
|
|
||||||
if (message instanceof Heartbeat) {
|
|
||||||
Heartbeat heartbeat = (Heartbeat) message;
|
|
||||||
|
|
||||||
//Check that the heartbeat number is greater than the previous value, and then set the last heartbeat time.
|
|
||||||
if (heartbeat.getSequenceNumber() > this.lastHeartbeatSequenceNum) {
|
|
||||||
lastHeartbeatTime = System.currentTimeMillis();
|
|
||||||
lastHeartbeatSequenceNum = heartbeat.getSequenceNumber();
|
|
||||||
//System.out.println("HeartBeat Message! " + lastHeartbeatSequenceNum);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//RaceStatus.
|
|
||||||
else if (message instanceof RaceStatus) {
|
|
||||||
RaceStatus raceStatus = (RaceStatus) message;
|
|
||||||
|
|
||||||
//System.out.println("Race Status Message");
|
ac35RaceStatus.update(message);
|
||||||
this.raceStatus = raceStatus;
|
|
||||||
for (BoatStatus boatStatus: this.raceStatus.getBoatStatuses()) {
|
|
||||||
this.boatStatusMap.put(boatStatus.getSourceID(), boatStatus);
|
|
||||||
}
|
|
||||||
setCourseWindDirection(raceStatus.getScaledWindDirection());
|
|
||||||
}
|
|
||||||
//DisplayTextMessage.
|
|
||||||
/*else if (message instanceof DisplayTextMessage) {
|
|
||||||
//System.out.println("Display Text Message");
|
|
||||||
//No decoder for this.
|
|
||||||
}*/
|
|
||||||
//XMLMessage.
|
|
||||||
else if (message instanceof XMLMessage) {
|
|
||||||
XMLMessage xmlMessage = (XMLMessage) message;
|
|
||||||
|
|
||||||
//System.out.println("XML Message!");
|
|
||||||
|
|
||||||
Platform.runLater(()-> {
|
|
||||||
if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeRegatta) {
|
|
||||||
//System.out.println("Setting Regatta");
|
|
||||||
try {
|
|
||||||
course.setRegattaXMLReader(new RegattaXMLReader(xmlMessage.getXmlMessage()));
|
|
||||||
|
|
||||||
}
|
|
||||||
//TODO REFACTOR should put all of these exceptions behind a RegattaXMLReaderException.
|
|
||||||
catch (IOException | SAXException | ParserConfigurationException e) {
|
|
||||||
System.err.println("Error creating RegattaXMLReader: " + e.getMessage());
|
|
||||||
//Continue to the next loop iteration/message.
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeRace) {
|
|
||||||
//System.out.println("Setting Course");
|
|
||||||
try {
|
|
||||||
course.setStreamedCourseXMLReader(new StreamedCourseXMLReader(xmlMessage.getXmlMessage()));
|
|
||||||
}
|
|
||||||
//TODO REFACTOR should put all of these exceptions behind a StreamedCourseXMLReaderException.
|
|
||||||
catch (IOException | SAXException | ParserConfigurationException | StreamedCourseXMLException e) {
|
|
||||||
System.err.println("Error creating StreamedCourseXMLReader: " + e.getMessage());
|
|
||||||
//Continue to the next loop iteration/message.
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (xmlMessage.getXmlMsgSubType() == XMLMessage.XMLTypeBoat) {
|
|
||||||
//System.out.println("Setting Boats");
|
|
||||||
try {
|
|
||||||
course.setBoatXMLReader(new BoatXMLReader(xmlMessage.getXmlMessage()));
|
|
||||||
}
|
|
||||||
//TODO REFACTOR should put all of these exceptions behind a BoatXMLReaderException.
|
|
||||||
catch (IOException | SAXException | ParserConfigurationException e) {
|
|
||||||
System.err.println("Error creating BoatXMLReader: " + e.getMessage());
|
|
||||||
//Continue to the next loop iteration/message.
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//RaceStartStatus.
|
|
||||||
else if (message instanceof RaceStartStatus) {
|
|
||||||
|
|
||||||
//System.out.println("Race Start Status Message");
|
|
||||||
}
|
|
||||||
//YachtEventCode.
|
|
||||||
/*else if (message instanceof YachtEventCode) {
|
|
||||||
YachtEventCode yachtEventCode = (YachtEventCode) message;
|
|
||||||
|
|
||||||
//System.out.println("Yacht Event Code!");
|
|
||||||
//No decoder for this.
|
|
||||||
|
|
||||||
}*/
|
|
||||||
//YachtActionCode.
|
|
||||||
/*else if (message instanceof YachtActionCode) {
|
|
||||||
YachtActionCode yachtActionCode = (YachtActionCode) message;
|
|
||||||
|
|
||||||
//System.out.println("Yacht Action Code!");
|
|
||||||
//No decoder for this.
|
|
||||||
|
|
||||||
}*/
|
|
||||||
//ChatterText.
|
|
||||||
/*else if (message instanceof ChatterText) {
|
|
||||||
ChatterText chatterText = (ChatterText) message;
|
|
||||||
|
|
||||||
//System.out.println("Chatter Text Message!");
|
|
||||||
//No decoder for this.
|
|
||||||
|
|
||||||
}*/
|
|
||||||
//BoatLocation.
|
|
||||||
else if (message instanceof BoatLocation) {
|
|
||||||
BoatLocation boatLocation = (BoatLocation) message;
|
|
||||||
|
|
||||||
//System.out.println("Boat Location!");
|
|
||||||
if (this.boatLocationMap.containsKey(boatLocation.getSourceID())) {
|
|
||||||
//If our boatlocation map already contains a boat location message for this boat, check that the new message is actually for a later timestamp (i.e., newer).
|
|
||||||
if (boatLocation.getTime() > this.boatLocationMap.get(boatLocation.getSourceID()).getTime()){
|
|
||||||
//If it is, replace the old message.
|
|
||||||
this.boatLocationMap.put(boatLocation.getSourceID(), boatLocation);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
//If the map _doesn't_ already contain a message for this boat, insert the message.
|
|
||||||
this.boatLocationMap.put(boatLocation.getSourceID(), boatLocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//MarkRounding.
|
|
||||||
else if (message instanceof MarkRounding) {
|
|
||||||
MarkRounding markRounding = (MarkRounding) message;
|
|
||||||
|
|
||||||
//System.out.println("Mark Rounding Message!");
|
|
||||||
|
|
||||||
if (this.markRoundingMap.containsKey(markRounding.getSourceID())) {
|
|
||||||
//If our markRoundingMap already contains a mark rounding message for this boat, check that the new message is actually for a later timestamp (i.e., newer).
|
|
||||||
if (markRounding.getTime() > this.markRoundingMap.get(markRounding.getSourceID()).getTime()){
|
|
||||||
//If it is, replace the old message.
|
|
||||||
this.markRoundingMap.put(markRounding.getSourceID(), markRounding);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
//If the map _doesn't_ already contain a message for this boat, insert the message.
|
|
||||||
this.markRoundingMap.put(markRounding.getSourceID(), markRounding);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//CourseWinds.
|
|
||||||
else if (message instanceof CourseWinds) {
|
|
||||||
|
|
||||||
//System.out.println("Course Wind Message!");
|
|
||||||
this.courseWinds = (CourseWinds) message;
|
|
||||||
|
|
||||||
}
|
|
||||||
//AverageWind.
|
|
||||||
else if (message instanceof AverageWind) {
|
|
||||||
|
|
||||||
//System.out.println("Average Wind Message!");
|
|
||||||
this.averageWind = (AverageWind) message;
|
|
||||||
|
|
||||||
}
|
|
||||||
//Unrecognised message.
|
|
||||||
else {
|
|
||||||
System.out.println("Broken Message!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user