diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 33da488f..d33d5607 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -27,7 +27,6 @@ public class App extends Application { primaryStage.setOnCloseRequest(e -> { StreamParser.appClose(); StreamReceiver.noMoreBytes(); - System.out.println("[CLIENT] Exiting program"); System.exit(0); }); diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index e39c0ebe..b9ffec5e 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -45,17 +45,12 @@ public class CanvasController { private Group group; private GraphicsContext gc; private ImageView mapImage; - - private final int MARK_SIZE = 10; + private final int BUFFER_SIZE = 50; - private final int PANEL_WIDTH = 1260; // it should be 1280 but, minors 40 to cancel the bias. - private final int PANEL_HEIGHT = 960; + private final int PANEL_WIDTH = 1260; // it should be 1280 but, minors 40 to cancel the bias. + private final int PANEL_HEIGHT = 960; private final int CANVAS_WIDTH = 720; private final int CANVAS_HEIGHT = 720; - private final int LHS_BUFFER = BUFFER_SIZE; - private final int RHS_BUFFER = BUFFER_SIZE; - private final int TOP_BUFFER = BUFFER_SIZE; - private final int BOT_BUFFER = TOP_BUFFER; private boolean horizontalInversion = false; private double distanceScaleFactor; @@ -113,8 +108,6 @@ public class CanvasController { gc.setGlobalAlpha(0.5); fitMarksToCanvas(); drawGoogleMap(); - - // TODO: 1/05/17 wmu16 - Change this call to now draw the marks as from the xml initializeBoats(); initializeMarks(); @@ -144,9 +137,6 @@ public class CanvasController { } raceViewController.updateSparkLine(); } - - // TODO: 1/05/17 cir27 - Make the RaceObjects update on the actual delay. - elapsedNanos = 1000 / 60; updateGroups(); if (StreamParser.isRaceFinished()) { this.stop(); @@ -418,24 +408,24 @@ public class CanvasController { if (scaleDirection == ScaleDirection.HORIZONTAL) { referenceAngle = Math.abs(Mark.calculateHeadingRad(referencePoint, minLonPoint)); - referencePointX = LHS_BUFFER + distanceScaleFactor * Math.sin(referenceAngle) * Mark.calculateDistance(referencePoint, minLonPoint); + referencePointX = BUFFER_SIZE + distanceScaleFactor * Math.sin(referenceAngle) * Mark.calculateDistance(referencePoint, minLonPoint); referenceAngle = Math.abs(Mark.calculateHeadingRad(referencePoint, maxLatPoint)); - referencePointY = CANVAS_HEIGHT - (TOP_BUFFER + BOT_BUFFER); + referencePointY = CANVAS_HEIGHT - (BUFFER_SIZE + BUFFER_SIZE); referencePointY -= distanceScaleFactor * Math.cos(referenceAngle) * Mark.calculateDistance(referencePoint, maxLatPoint); referencePointY = referencePointY / 2; - referencePointY += TOP_BUFFER; + referencePointY += BUFFER_SIZE; referencePointY += distanceScaleFactor * Math.cos(referenceAngle) * Mark.calculateDistance(referencePoint, maxLatPoint); } else { - referencePointY = CANVAS_HEIGHT - BOT_BUFFER; + referencePointY = CANVAS_HEIGHT - BUFFER_SIZE; referenceAngle = Math.abs(Mark.calculateHeadingRad(referencePoint, minLonPoint)); - referencePointX = LHS_BUFFER; + referencePointX = BUFFER_SIZE; referencePointX += distanceScaleFactor * Math.sin(referenceAngle) * Mark.calculateDistance(referencePoint, minLonPoint); - referencePointX += ((CANVAS_WIDTH - (LHS_BUFFER + RHS_BUFFER)) - (minLonToMaxLon * distanceScaleFactor)) / 2; + referencePointX += ((CANVAS_WIDTH - (BUFFER_SIZE + BUFFER_SIZE)) - (minLonToMaxLon * distanceScaleFactor)) / 2; } if(horizontalInversion) { - referencePointX = CANVAS_WIDTH - RHS_BUFFER - (referencePointX - LHS_BUFFER); + referencePointX = CANVAS_WIDTH - BUFFER_SIZE - (referencePointX - BUFFER_SIZE); } } @@ -456,10 +446,10 @@ public class CanvasController { horiAngle = horiAngle - (Math.PI / 2); double horiDistance = Math.cos(horiAngle) * Mark.calculateDistance(minLonPoint, maxLonPoint); - double vertScale = (CANVAS_HEIGHT - (TOP_BUFFER + BOT_BUFFER)) / vertDistance; + double vertScale = (CANVAS_HEIGHT - (BUFFER_SIZE + BUFFER_SIZE)) / vertDistance; - if ((horiDistance * vertScale) > (CANVAS_WIDTH - (RHS_BUFFER + LHS_BUFFER))) { - distanceScaleFactor = (CANVAS_WIDTH - (RHS_BUFFER + LHS_BUFFER)) / horiDistance; + if ((horiDistance * vertScale) > (CANVAS_WIDTH - (BUFFER_SIZE + BUFFER_SIZE))) { + distanceScaleFactor = (CANVAS_WIDTH - (BUFFER_SIZE + BUFFER_SIZE)) / horiDistance; scaleDirection = ScaleDirection.HORIZONTAL; } else { distanceScaleFactor = vertScale; @@ -497,7 +487,7 @@ public class CanvasController { yAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference); } if(horizontalInversion) { - xAxisLocation = CANVAS_WIDTH - RHS_BUFFER - (xAxisLocation - LHS_BUFFER); + xAxisLocation = CANVAS_WIDTH - BUFFER_SIZE - (xAxisLocation - BUFFER_SIZE); } return new Point2D(xAxisLocation, yAxisLocation); } diff --git a/src/main/java/seng302/controllers/StartScreenController.java b/src/main/java/seng302/controllers/StartScreenController.java index 22e5dd1b..5fe0036c 100644 --- a/src/main/java/seng302/controllers/StartScreenController.java +++ b/src/main/java/seng302/controllers/StartScreenController.java @@ -12,7 +12,6 @@ import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; -import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 6784550b..0bb8a81b 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -5,10 +5,7 @@ import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.CacheHint; import javafx.scene.Group; -import javafx.scene.input.MouseDragEvent; -import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; -import javafx.scene.paint.Paint; import javafx.scene.shape.Line; import javafx.scene.shape.Polygon; import javafx.scene.text.Text; diff --git a/src/main/java/seng302/models/Colors.java b/src/main/java/seng302/models/Colors.java index 8d1b9c25..0078e505 100644 --- a/src/main/java/seng302/models/Colors.java +++ b/src/main/java/seng302/models/Colors.java @@ -3,7 +3,7 @@ package seng302.models; import javafx.scene.paint.Color; /** - * Created by ryan_ on 16/03/2017. + * Enum for randomly generating colours. */ public enum Colors { RED, PERU, SEAGREEN, GREEN, BLUE, PURPLE; diff --git a/src/main/java/seng302/models/TimelineInfo.java b/src/main/java/seng302/models/TimelineInfo.java deleted file mode 100644 index 867ce67b..00000000 --- a/src/main/java/seng302/models/TimelineInfo.java +++ /dev/null @@ -1,31 +0,0 @@ -package seng302.models; - -import javafx.animation.Timeline; -import javafx.beans.property.DoubleProperty; - - -/** - * Created by zyt10 on 17/03/17. - * this class is literally just to associate a timeline with a DoubleProperty x and y - */ -public class TimelineInfo { - private Timeline timeline; - private DoubleProperty x; - private DoubleProperty y; - - public TimelineInfo(Timeline timeline, DoubleProperty x, DoubleProperty y) { - this.timeline = timeline; - this.x = x; - this.y = y; - } - - public Timeline getTimeline() { - return timeline; - } - public DoubleProperty getX() { - return x; - } - public DoubleProperty getY() { - return y; - } -} diff --git a/src/main/java/seng302/models/mark/Mark.java b/src/main/java/seng302/models/mark/Mark.java index 31791d99..027bf6d3 100644 --- a/src/main/java/seng302/models/mark/Mark.java +++ b/src/main/java/seng302/models/mark/Mark.java @@ -50,20 +50,24 @@ public abstract class Mark { } /** - * Calculate the heading in radians from geographical location with latitude1, longitude 1 to geographical - * latitude2, longitude 2 + * Calculate the heading in radians from geographical location with latitude1, longitude 1 to + * geographical latitude2, longitude 2 + * * @param longitude1 Longitude of first point in degrees * @param longitude2 Longitude of second point in degrees - * @param latitude1 Latitude of first point in degrees + * @param latitude1 Latitude of first point in degrees * @param latitude2 Latitude of first point in degrees * @return Heading in radians */ - public static double calculateHeadingRad (Double latitude1, Double longitude1, Double latitude2, Double longitude2) { + public static double calculateHeadingRad(Double latitude1, Double longitude1, Double latitude2, + Double longitude2) { latitude1 = Math.toRadians(latitude1); latitude2 = Math.toRadians(latitude2); - Double longDiff= Math.toRadians(longitude2-longitude1); - Double y = Math.sin(longDiff)*Math.cos(latitude2); - Double x = Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff); + Double longDiff = Math.toRadians(longitude2 - longitude1); + Double y = Math.sin(longDiff) * Math.cos(latitude2); + Double x = + Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) + * Math.cos(longDiff); return Math.atan2(y, x); } @@ -83,33 +87,35 @@ public abstract class Mark { } /** - * Calculate the distance in meters from geographical location with latitude1, longitude 1 to geographical - * latitude2, longitude 2 + * Calculate the distance in meters from geographical location with latitude1, longitude 1 to + * geographical latitude2, longitude 2 * * @param longitude1 Longitude of first point in degrees * @param longitude2 Longitude of second point in degrees - * @param latitude1 Latitude of first point in degrees + * @param latitude1 Latitude of first point in degrees * @param latitude2 Latitude of first point in degrees * @return Distance in meters */ - public static Double calculateDistance (Double latitude1, Double longitude1, Double latitude2, Double longitude2) { + public static Double calculateDistance(Double latitude1, Double longitude1, Double latitude2, + Double longitude2) { Double theta = longitude1 - longitude2; Double dist = Math.sin(Math.toRadians(latitude1)) * Math.sin(Math.toRadians(latitude2)) + - Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2)) * - Math.cos(Math.toRadians(theta)); + Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2)) * + Math.cos(Math.toRadians(theta)); dist = Math.acos(dist); dist = Math.toDegrees(dist); - dist = dist * 60 * 1.1508; //nautical mile (distance between two degrees) * (degrees in a minute) + dist = dist * 60 + * 1.1508; //nautical mile (distance between two degrees) * (degrees in a minute) dist = dist * 1609.344; //ratio of miles to metres return dist; } - + public String getName() { return name; } public void setName(String name) { - this.name = name; + this.name = name; } public MarkType getMarkType() { diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/models/mark/MarkGroup.java index 011154a6..db55656d 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/models/mark/MarkGroup.java @@ -11,7 +11,7 @@ import javafx.scene.shape.Line; import seng302.GeometryUtils; /** - * Created by CJIRWIN on 26/04/2017. + * Grouping of javaFX objects needed to represent a Mark on screen. */ public class MarkGroup extends Group { diff --git a/src/main/java/seng302/models/mark/MarkType.java b/src/main/java/seng302/models/mark/MarkType.java index 4ac6a9e3..92b3c93b 100644 --- a/src/main/java/seng302/models/mark/MarkType.java +++ b/src/main/java/seng302/models/mark/MarkType.java @@ -5,5 +5,5 @@ package seng302.models.mark; * Created by Haoming Yin (hyi25) on 17/3/17. */ public enum MarkType { - SINGLE_MARK, OPEN_GATE, CLOSED_GATE + SINGLE_MARK, OPEN_GATE } diff --git a/src/main/java/seng302/models/stream/StreamParser.java b/src/main/java/seng302/models/stream/StreamParser.java index 88e99b8f..36961e78 100644 --- a/src/main/java/seng302/models/stream/StreamParser.java +++ b/src/main/java/seng302/models/stream/StreamParser.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.TimeZone; import java.util.TreeMap; @@ -32,25 +33,25 @@ import seng302.models.stream.packets.StreamPacket; * that are threadsafe so the visualiser can always access the latest speed and position available * Created by kre39 on 23/04/17. */ -public class StreamParser extends Thread{ +public class StreamParser extends Thread { - public static ConcurrentHashMap> markPositions = new ConcurrentHashMap<>(); - public static ConcurrentHashMap> boatPositions = new ConcurrentHashMap<>(); - private String threadName; - private Thread t; - private static boolean newRaceXmlReceived = false; - private static boolean raceStarted = false; - private static XMLParser xmlObject; - private static boolean raceFinished = false; - private static boolean streamStatus = false; - private static long timeSinceStart = -1; - private static Map boats = new ConcurrentHashMap<>(); - private static Map boatsPos = new ConcurrentSkipListMap<>(); - private static double windDirection = 0; - private static Double windSpeed = 0d; + public static ConcurrentHashMap> markPositions = new ConcurrentHashMap<>(); + public static ConcurrentHashMap> boatPositions = new ConcurrentHashMap<>(); + private String threadName; + private Thread t; + private static boolean newRaceXmlReceived = false; + private static boolean raceStarted = false; + private static XMLParser xmlObject; + private static boolean raceFinished = false; + private static boolean streamStatus = false; + private static long timeSinceStart = -1; + private static Map boats = new ConcurrentHashMap<>(); + private static Map boatsPos = new ConcurrentSkipListMap<>(); + private static double windDirection = 0; + private static Double windSpeed = 0d; private static Long currentTimeLong; - private static String currentTimeString; - private static boolean appRunning; + private static String currentTimeString; + private static boolean appRunning; //CONVERSION CONSTANTS @@ -61,45 +62,41 @@ public class StreamParser extends Thread{ * * @param threadName name of the thread */ - public StreamParser(String threadName){ - this.threadName = threadName; - } + public StreamParser(String threadName) { + this.threadName = threadName; + } /** - * Used to within threading so when the stream parser thread runs, it will keep looking for a packet to - * process until it is unable to find anymore packets - * + * Used to within threading so when the stream parser thread runs, it will keep looking for a + * packet to process until it is unable to find anymore packets */ - public void run(){ + public void run() { appRunning = true; - try { - System.out.println("[CLIENT] Start of stream"); - streamStatus = true; - xmlObject = new XMLParser(); - while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) { - Thread.sleep(1); - } - while (appRunning){ - StreamPacket packet = StreamReceiver.packetBuffer.take(); - parsePacket(packet); - Thread.sleep(1); - while (StreamReceiver.packetBuffer.peek() == null) { - } - } - } catch (Exception e){ - e.printStackTrace(); - } - } + try { + streamStatus = true; + xmlObject = new XMLParser(); + while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) { + Thread.sleep(1); + } + while (appRunning) { + StreamPacket packet = StreamReceiver.packetBuffer.take(); + parsePacket(packet); + Thread.sleep(1); + while (StreamReceiver.packetBuffer.peek() == null) { + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } /** * Used to start the stream parser thread when multithreading - * */ - public void start () { - System.out.println("[CLIENT] Starting " + threadName ); + public void start() { if (t == null) { - t = new Thread (this, threadName); - t.start (); + t = new Thread(this, threadName); + t.start(); } } @@ -109,55 +106,53 @@ public class StreamParser extends Thread{ * * @param packet the packet to be looked at and processed */ - private static void parsePacket(StreamPacket packet) { - try{ - switch (packet.getType()){ - case HEARTBEAT: - extractHeartBeat(packet); - break; - case RACE_STATUS: - extractRaceStatus(packet); - break; - case DISPLAY_TEXT_MESSAGE: - extractDisplayMessage(packet); - break; - case XML_MESSAGE: - newRaceXmlReceived = true; - extractXmlMessage(packet); - break; - case RACE_START_STATUS: - extractRaceStartStatus(packet); - break; - case YACHT_EVENT_CODE: - extractYachtEventCode(packet); - break; - case YACHT_ACTION_CODE: - extractYachtActionCode(packet); - break; - case CHATTER_TEXT: - extractChatterText(packet); - break; - case BOAT_LOCATION: - extractBoatLocation(packet); - break; - case MARK_ROUNDING: - extractMarkRounding(packet); - break; - case COURSE_WIND: - extractCourseWind(packet); - break; - case AVG_WIND: - extractAvgWind(packet); - break; - default: - break; - //System.out.println(packet.getType().toString()); - } - } - catch (NullPointerException e){ - System.out.println("Error parsing packet"); - } - } + private static void parsePacket(StreamPacket packet) { + try { + switch (packet.getType()) { + case HEARTBEAT: + extractHeartBeat(packet); + break; + case RACE_STATUS: + extractRaceStatus(packet); + break; + case DISPLAY_TEXT_MESSAGE: + extractDisplayMessage(packet); + break; + case XML_MESSAGE: + newRaceXmlReceived = true; + extractXmlMessage(packet); + break; + case RACE_START_STATUS: + extractRaceStartStatus(packet); + break; + case YACHT_EVENT_CODE: + extractYachtEventCode(packet); + break; + case YACHT_ACTION_CODE: + extractYachtActionCode(packet); + break; + case CHATTER_TEXT: + extractChatterText(packet); + break; + case BOAT_LOCATION: + extractBoatLocation(packet); + break; + case MARK_ROUNDING: + extractMarkRounding(packet); + break; + case COURSE_WIND: + extractCourseWind(packet); + break; + case AVG_WIND: + extractAvgWind(packet); + break; + default: + break; + } + } catch (NullPointerException e) { + System.out.println("Error parsing packet"); + } + } /** * Extracts the seq num used in the heartbeat packet @@ -185,17 +180,18 @@ public class StreamParser extends Thread{ } /** - * Extracts the useful race status data from race status type packets. This method will also print to the - * console the current state of the race (if it has started/finished or is about to start), along side - * this it'll also display the amount of time since the race has started or time till it starts + * Extracts the useful race status data from race status type packets. This method will also + * print to the console the current state of the race (if it has started/finished or is about to + * start), along side this it'll also display the amount of time since the race has started or + * time till it starts * * @param packet Packet parsed in to use the payload */ - private static void extractRaceStatus(StreamPacket packet){ + private static void extractRaceStatus(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long currentTime = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long raceId = bytesToLong(Arrays.copyOfRange(payload,7,11)); + long currentTime = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long raceId = bytesToLong(Arrays.copyOfRange(payload, 7, 11)); int raceStatus = payload[11]; long expectedStartTime = bytesToLong(Arrays.copyOfRange(payload,12,18)); long windDir = bytesToLong(Arrays.copyOfRange(payload,18,20)); @@ -205,22 +201,20 @@ public class StreamParser extends Thread{ DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); if (xmlObject.getRegattaXML() != null) { format.setTimeZone(TimeZone.getTimeZone(getTimeZoneString())); - currentTimeString = format.format((new Date (currentTime)).getTime()); + currentTimeString = format.format((new Date(currentTime)).getTime()); } - long timeTillStart = ((new Date (expectedStartTime)).getTime() - (new Date (currentTime)).getTime())/1000; + long timeTillStart = + ((new Date(expectedStartTime)).getTime() - (new Date(currentTime)).getTime()) / 1000; if (timeTillStart > 0) { timeSinceStart = timeTillStart; - //System.out.println("Time till start: " + timeTillStart + " Seconds"); } else { - if (raceStatus == 4 || raceStatus == 8){ + if (raceStatus == 4 || raceStatus == 8) { raceFinished = true; raceStarted = false; - System.out.println("[CLIENT] Race has finished"); - } else if (!raceStarted){ + } else if (!raceStarted) { raceStarted = true; raceFinished = false; - System.out.println("[CLIENT] Race has started"); } timeSinceStart = timeTillStart; } @@ -232,18 +226,20 @@ public class StreamParser extends Thread{ int noBoats = payload[22]; int raceType = payload[23]; boatsPos = new TreeMap<>(); - for (int i = 0; i < noBoats; i++){ - long boatStatusSourceID = bytesToLong(Arrays.copyOfRange(payload,24 + (i * 20),28+ (i * 20))); + for (int i = 0; i < noBoats; i++) { + long boatStatusSourceID = bytesToLong( + Arrays.copyOfRange(payload, 24 + (i * 20), 28 + (i * 20))); Yacht boat = boats.get((int) boatStatusSourceID); - boat.setBoatStatus((int)payload[28 + (i * 20)]); - boat.setLegNumber((int)payload[29 + (i * 20)]); - boat.setPenaltiesAwarded((int)payload[30 + (i * 20)]); - boat.setPenaltiesServed((int)payload[31 + (i * 20)]); - Long estTimeAtNextMark = bytesToLong(Arrays.copyOfRange(payload,32 + (i * 20),38+ (i * 20))); + boat.setBoatStatus((int) payload[28 + (i * 20)]); + boat.setLegNumber((int) payload[29 + (i * 20)]); + boat.setPenaltiesAwarded((int) payload[30 + (i * 20)]); + boat.setPenaltiesServed((int) payload[31 + (i * 20)]); + Long estTimeAtNextMark = bytesToLong( + Arrays.copyOfRange(payload, 32 + (i * 20), 38 + (i * 20))); boat.setEstimateTimeAtNextMark(estTimeAtNextMark); - Long estTimeAtFinish = bytesToLong(Arrays.copyOfRange(payload,38 + (i * 20),44+ (i * 20))); + Long estTimeAtFinish = bytesToLong( + Arrays.copyOfRange(payload, 38 + (i * 20), 44 + (i * 20))); boat.setEstimateTimeAtFinish(estTimeAtFinish); - boatsPos.put(estTimeAtFinish, boat); // String boatStatus = "SourceID: " + boatStatusSourceID; // boatStatus += "\nBoat Status: " + (int)payload[28 + (i * 20)]; // boatStatus += "\nLegNumber: " + (int)payload[29 + (i * 20)]; @@ -271,15 +267,16 @@ public class StreamParser extends Thread{ * * @param packet Packet parsed in to use the payload */ - private static void extractDisplayMessage(StreamPacket packet){ + private static void extractDisplayMessage(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; int numOfLines = payload[3]; int totalLen = 0; - for (int i = 0; i < numOfLines; i++){ + for (int i = 0; i < numOfLines; i++) { int lineNum = payload[4 + totalLen]; int textLength = payload[5 + totalLen]; - byte[] messageTextBytes = Arrays.copyOfRange(payload,6 + totalLen,6 + textLength + totalLen); + byte[] messageTextBytes = Arrays + .copyOfRange(payload, 6 + totalLen, 6 + textLength + totalLen); String messageText = new String(messageTextBytes); totalLen += 2 + textLength; } @@ -290,13 +287,14 @@ public class StreamParser extends Thread{ * * @param packet Packet parsed in to use the payload */ - private static void extractXmlMessage(StreamPacket packet){ + private static void extractXmlMessage(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageType = payload[9]; - long messageLength = bytesToLong(Arrays.copyOfRange(payload,12,14)); - String xmlMessage = new String((Arrays.copyOfRange(payload,14,(int) (14 + messageLength)))).trim(); + long messageLength = bytesToLong(Arrays.copyOfRange(payload, 12, 14)); + String xmlMessage = new String( + (Arrays.copyOfRange(payload, 14, (int) (14 + messageLength)))).trim(); //Create XML document Object DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); @@ -325,12 +323,12 @@ public class StreamParser extends Thread{ * * @param packet Packet parsed in to use the payload */ - private static void extractRaceStartStatus(StreamPacket packet){ + private static void extractRaceStartStatus(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long raceStartTime = bytesToLong(Arrays.copyOfRange(payload,9,15)); - long raceId = bytesToLong(Arrays.copyOfRange(payload,15,19)); + long timeStamp = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long raceStartTime = bytesToLong(Arrays.copyOfRange(payload, 9, 15)); + long raceId = bytesToLong(Arrays.copyOfRange(payload, 15, 19)); int notificationType = payload[19]; } @@ -340,28 +338,28 @@ public class StreamParser extends Thread{ * * @param packet Packet parsed in to use the payload */ - private static void extractYachtEventCode(StreamPacket packet){ + private static void extractYachtEventCode(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long raceId = bytesToLong(Arrays.copyOfRange(payload,9,13)); - long subjectId = bytesToLong(Arrays.copyOfRange(payload,13,17)); - long incidentId = bytesToLong(Arrays.copyOfRange(payload,17,21)); + long timeStamp = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long raceId = bytesToLong(Arrays.copyOfRange(payload, 9, 13)); + long subjectId = bytesToLong(Arrays.copyOfRange(payload, 13, 17)); + long incidentId = bytesToLong(Arrays.copyOfRange(payload, 17, 21)); int eventId = payload[21]; } /** - * When a yacht action occurs this will parse the parse the byte array to retrieve the necessary info, - * currently unused + * When a yacht action occurs this will parse the parse the byte array to retrieve the necessary + * info, currently unused * * @param packet Packet parsed in to use the payload */ - private static void extractYachtActionCode(StreamPacket packet){ + private static void extractYachtActionCode(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long subjectId = bytesToLong(Arrays.copyOfRange(payload,9,13)); - long incidentId = bytesToLong(Arrays.copyOfRange(payload,13,17)); + long timeStamp = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long subjectId = bytesToLong(Arrays.copyOfRange(payload, 9, 13)); + long incidentId = bytesToLong(Arrays.copyOfRange(payload, 13, 17)); int eventId = payload[17]; } @@ -370,51 +368,54 @@ public class StreamParser extends Thread{ * * @param packet Packet parsed in to use the payload */ - private static void extractChatterText(StreamPacket packet){ + private static void extractChatterText(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; int messageType = payload[1]; int length = payload[2]; - String message = new String(Arrays.copyOfRange(payload,3,3 + length)); + String message = new String(Arrays.copyOfRange(payload, 3, 3 + length)); } /** - * Used to breakdown the boatlocation packets so the boat coordinates, id and groundspeed are all used - * All the other extra data is still being read and translated however is unused. + * Used to breakdown the boatlocation packets so the boat coordinates, id and groundspeed are + * all used All the other extra data is still being read and translated however is unused. * * @param packet Packet parsed in to use the payload */ - private static void extractBoatLocation(StreamPacket packet){ + private static void extractBoatLocation(StreamPacket packet) { byte[] payload = packet.getPayload(); - int deviceType = (int)payload[15]; - long timeValid = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long seq = bytesToLong(Arrays.copyOfRange(payload,11,15)); - long boatId = bytesToLong(Arrays.copyOfRange(payload,7,11)); - long rawLat = bytesToLong(Arrays.copyOfRange(payload,16,20)); - long rawLon = bytesToLong(Arrays.copyOfRange(payload,20,24)); + int deviceType = (int) payload[15]; + long timeValid = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long seq = bytesToLong(Arrays.copyOfRange(payload, 11, 15)); + long boatId = bytesToLong(Arrays.copyOfRange(payload, 7, 11)); + long rawLat = bytesToLong(Arrays.copyOfRange(payload, 16, 20)); + long rawLon = bytesToLong(Arrays.copyOfRange(payload, 20, 24)); //Converts the double to a usable lat/lon - double lat = ((180d * (double)rawLat)/Math.pow(2,31)); - double lon = ((180d *(double)rawLon)/Math.pow(2,31)); - long heading = bytesToLong(Arrays.copyOfRange(payload,28,30)); - double groundSpeed = bytesToLong(Arrays.copyOfRange(payload,38,40))/1000.0; + double lat = ((180d * (double) rawLat) / Math.pow(2, 31)); + double lon = ((180d * (double) rawLon) / Math.pow(2, 31)); + long heading = bytesToLong(Arrays.copyOfRange(payload, 28, 30)); + double groundSpeed = bytesToLong(Arrays.copyOfRange(payload, 38, 40)) / 1000.0; //type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat - if (deviceType == 1){ - BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); + if (deviceType == 1) { + BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, + heading, groundSpeed); //add a new priority que to the boatPositions HashMap - if (!boatPositions.containsKey(boatId)){ - boatPositions.put(boatId, new PriorityBlockingQueue<>(256, new Comparator() { - @Override - public int compare(BoatPositionPacket p1, BoatPositionPacket p2) { - return (int) (p1.getTimeValid() - p2.getTimeValid()); - } - })); + if (!boatPositions.containsKey(boatId)) { + boatPositions.put(boatId, + new PriorityBlockingQueue<>(256, new Comparator() { + @Override + public int compare(BoatPositionPacket p1, BoatPositionPacket p2) { + return (int) (p1.getTimeValid() - p2.getTimeValid()); + } + })); } boatPositions.get(boatId).put(boatPacket); - } else if (deviceType == 3){ - BoatPositionPacket markPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); + } else if (deviceType == 3) { + BoatPositionPacket markPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, + heading, groundSpeed); //add a new priority que to the boatPositions HashMap if (!markPositions.containsKey(boatId)) { @@ -427,8 +428,6 @@ public class StreamParser extends Thread{ })); } markPositions.get(boatId).put(markPacket); - - } } @@ -437,12 +436,12 @@ public class StreamParser extends Thread{ * * @param packet The packet containing the payload */ - private static void extractMarkRounding(StreamPacket packet){ + private static void extractMarkRounding(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long raceId = bytesToLong(Arrays.copyOfRange(payload,9,13)); - long subjectId = bytesToLong(Arrays.copyOfRange(payload,13,17)); + long timeStamp = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long raceId = bytesToLong(Arrays.copyOfRange(payload, 9, 13)); + long subjectId = bytesToLong(Arrays.copyOfRange(payload, 13, 17)); int boatStatus = payload[17]; int roundingSide = payload[18]; int markType = payload[19]; @@ -463,21 +462,29 @@ public class StreamParser extends Thread{ * * @param packet The packet containing the payload */ - private static void extractCourseWind(StreamPacket packet){ + private static void extractCourseWind(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; int selectedWindId = payload[1]; int loopCount = payload[2]; ArrayList windInfo = new ArrayList<>(); - for (int i = 0; i < loopCount; i++){ + for (int i = 0; i < loopCount; i++) { String wind = "WindId: " + payload[3 + (20 * i)]; - wind += "\nTime: " + bytesToLong(Arrays.copyOfRange(payload,4 + (20 * i),10 + (20 * i))); - wind += "\nRaceId: " + bytesToLong(Arrays.copyOfRange(payload,10 + (20 * i),14 + (20 * i))); - wind += "\nWindDirection: " + bytesToLong(Arrays.copyOfRange(payload,14 + (20 * i),16 + (20 * i))); - wind += "\nWindSpeed: " + bytesToLong(Arrays.copyOfRange(payload,16 + (20 * i),18 + (20 * i))); - wind += "\nBestUpWindAngle: " + bytesToLong(Arrays.copyOfRange(payload,18 + (20 * i),20 + (20 * i))); - wind += "\nBestDownWindAngle: " + bytesToLong(Arrays.copyOfRange(payload,20 + (20 * i),22 + (20 * i))); - wind += "\nFlags: " + String.format("%8s", Integer.toBinaryString(payload[22 + (20 * i)] & 0xFF)).replace(' ', '0'); + wind += + "\nTime: " + bytesToLong(Arrays.copyOfRange(payload, 4 + (20 * i), 10 + (20 * i))); + wind += "\nRaceId: " + bytesToLong( + Arrays.copyOfRange(payload, 10 + (20 * i), 14 + (20 * i))); + wind += "\nWindDirection: " + bytesToLong( + Arrays.copyOfRange(payload, 14 + (20 * i), 16 + (20 * i))); + wind += "\nWindSpeed: " + bytesToLong( + Arrays.copyOfRange(payload, 16 + (20 * i), 18 + (20 * i))); + wind += "\nBestUpWindAngle: " + bytesToLong( + Arrays.copyOfRange(payload, 18 + (20 * i), 20 + (20 * i))); + wind += "\nBestDownWindAngle: " + bytesToLong( + Arrays.copyOfRange(payload, 20 + (20 * i), 22 + (20 * i))); + wind += "\nFlags: " + String + .format("%8s", Integer.toBinaryString(payload[22 + (20 * i)] & 0xFF)) + .replace(' ', '0'); windInfo.add(wind); } } @@ -487,18 +494,18 @@ public class StreamParser extends Thread{ * * @param packet The packet containing the paylaod */ - private static void extractAvgWind(StreamPacket packet){ + private static void extractAvgWind(StreamPacket packet) { byte[] payload = packet.getPayload(); int messageVersionNo = payload[0]; - long timeStamp = bytesToLong(Arrays.copyOfRange(payload,1,7)); - long rawPeriod = bytesToLong(Arrays.copyOfRange(payload,7,9)); - long rawSamplePeriod = bytesToLong(Arrays.copyOfRange(payload,9,11)); - long period2 = bytesToLong(Arrays.copyOfRange(payload,11,13)); - long speed2 = bytesToLong(Arrays.copyOfRange(payload,13,15)); - long period3 = bytesToLong(Arrays.copyOfRange(payload,15,17)); - long speed3 = bytesToLong(Arrays.copyOfRange(payload,17,19)); - long period4 = bytesToLong(Arrays.copyOfRange(payload,19,21)); - long speed4 = bytesToLong(Arrays.copyOfRange(payload,21,23)); + long timeStamp = bytesToLong(Arrays.copyOfRange(payload, 1, 7)); + long rawPeriod = bytesToLong(Arrays.copyOfRange(payload, 7, 9)); + long rawSamplePeriod = bytesToLong(Arrays.copyOfRange(payload, 9, 11)); + long period2 = bytesToLong(Arrays.copyOfRange(payload, 11, 13)); + long speed2 = bytesToLong(Arrays.copyOfRange(payload, 13, 15)); + long period3 = bytesToLong(Arrays.copyOfRange(payload, 15, 17)); + long speed3 = bytesToLong(Arrays.copyOfRange(payload, 17, 19)); + long period4 = bytesToLong(Arrays.copyOfRange(payload, 19, 21)); + long speed4 = bytesToLong(Arrays.copyOfRange(payload, 21, 23)); } /** @@ -507,11 +514,11 @@ public class StreamParser extends Thread{ * * @return a positive long if there is less than 7 bytes -1 otherwise */ - private static long bytesToLong(byte[] bytes){ + private static long bytesToLong(byte[] bytes) { long partialLong = 0; int index = 0; - for (byte b: bytes){ - if (index > 6){ + for (byte b : bytes) { + if (index > 6) { return -1; } partialLong = partialLong | (b & 0xFFL) << (index * 8); @@ -620,9 +627,8 @@ public class StreamParser extends Thread{ return currentTimeLong; } - public static void appClose(){ + public static void appClose() { appRunning = false; - System.out.println("[CLIENT] Shutting down stream parser"); } /** @@ -631,8 +637,8 @@ public class StreamParser extends Thread{ * * @return the status of if new xml has been received */ - public static boolean isNewRaceXmlReceived(){ - if (newRaceXmlReceived){ + public static boolean isNewRaceXmlReceived() { + if (newRaceXmlReceived) { newRaceXmlReceived = false; return true; } else { diff --git a/src/main/java/seng302/models/stream/StreamReceiver.java b/src/main/java/seng302/models/stream/StreamReceiver.java index b3303806..b1ddb996 100644 --- a/src/main/java/seng302/models/stream/StreamReceiver.java +++ b/src/main/java/seng302/models/stream/StreamReceiver.java @@ -44,7 +44,6 @@ public class StreamReceiver extends Thread { } public void start () { - System.out.println("[CLIENT] Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); @@ -155,6 +154,5 @@ public class StreamReceiver extends Thread { public static void noMoreBytes(){ moreBytes = false; - System.out.println("[CLIENT] Shutting down stream receiver"); } } diff --git a/src/main/java/seng302/server/ServerThread.java b/src/main/java/seng302/server/ServerThread.java index 094845ab..98038ea2 100644 --- a/src/main/java/seng302/server/ServerThread.java +++ b/src/main/java/seng302/server/ServerThread.java @@ -29,8 +29,6 @@ public class ServerThread implements Runnable, Observer { Thread runner = new Thread(this, threadName); runner.setDaemon(true); - serverLog("Spawning Server", 0); - raceSimulator = new Simulator(BOAT_LOCATION_PERIOD); raceSimulator.addObserver(this); // run race simulator, so it can send boats' static location. @@ -134,7 +132,6 @@ public class ServerThread implements Runnable, Observer { * Starts an instance of the race simulator */ private void startRaceSim(){ - serverLog("Starting Running Race Simulator", 0); // set race started to true, so the simulator will start moving boats raceSimulator.setRaceStarted(true); } @@ -142,8 +139,7 @@ public class ServerThread implements Runnable, Observer { /** * Starts sending heartbeat messages to the client */ - private void startSendingHeartbeats(){ - serverLog("Sending Heartbeats", 0); + private void startSendingHeartbeats() { Timer t = new Timer(); t.schedule(new TimerTask() { @@ -154,7 +150,7 @@ public class ServerThread implements Runnable, Observer { try { server.send(heartbeat); } catch (IOException e) { - System.out.print(""); + e.printStackTrace(); } } }, 0, HEARTBEAT_PERIOD); @@ -174,14 +170,13 @@ public class ServerThread implements Runnable, Observer { if (startTime < System.currentTimeMillis() && !raceStarted){ startRaceSim(); raceStarted = true; - serverLog("Race Started", 0); } else{ server.send(raceStartStatusMessage); } } catch (IOException e) { - System.out.print(""); + e.printStackTrace(); } } }, 0, RACE_START_STATUS_PERIOD); @@ -191,7 +186,6 @@ public class ServerThread implements Runnable, Observer { * Start sending race start status messages until race starts */ private void startSendingRaceStatusMessages(){ - serverLog("Sending race status messages", 0); Timer t = new Timer(); t.schedule(new TimerTask() { @Override @@ -199,9 +193,8 @@ public class ServerThread implements Runnable, Observer { Message raceStatusMessage = getRaceStatusMessage(); try { server.send(raceStatusMessage); - } catch (IOException e) { - System.out.print(""); + e.printStackTrace(); } } }, 0, RACE_STATUS_PERIOD); @@ -218,17 +211,12 @@ public class ServerThread implements Runnable, Observer { if (raceData != null){ server.send(raceData); - serverLog("Sending race data", 0); } - if (boatData != null){ server.send(boatData); - serverLog("Sending boat data", 0); } - if (regatta != null){ server.send(regatta); - serverLog("Sending regatta data", 0); } } catch (IOException e) { serverLog("Couldn't send an XML Message: " + e.getMessage(), 0); @@ -247,7 +235,6 @@ public class ServerThread implements Runnable, Observer { Message raceData = getXmlMessage("/server_config/courseLimits.xml", XMLMessageSubType.RACE); if (raceData != null) { server.send(raceData); - serverLog("Sending race data", 0); } }catch (IOException e) { serverLog("Couldn't send an XML Message: " + e.getMessage(), 0); @@ -295,7 +282,7 @@ public class ServerThread implements Runnable, Observer { } } catch (IOException e) { - System.out.print(""); + e.printStackTrace(); } } }, 0, BOAT_LOCATION_PERIOD); @@ -322,7 +309,6 @@ public class ServerThread implements Runnable, Observer { numOfBoatsFinished ++; if (!boatsFinished.get(boat.getSourceID())) { boatsFinished.put(boat.getSourceID(), true); - serverLog("Boat " + boat.getSourceID() + " finished the race", 1); } } Message m = new BoatLocationMessage(boat.getSourceID(), 1, boat.getLat(), diff --git a/src/main/java/seng302/server/StreamingServerSocket.java b/src/main/java/seng302/server/StreamingServerSocket.java index dc249ea4..35297f9f 100644 --- a/src/main/java/seng302/server/StreamingServerSocket.java +++ b/src/main/java/seng302/server/StreamingServerSocket.java @@ -28,7 +28,6 @@ class StreamingServerSocket { } void start(){ - ServerThread.serverLog("Listening For Connections",0); try { client = socket.accept(); } catch (IOException e) { @@ -39,7 +38,6 @@ class StreamingServerSocket { } else{ isServerStarted = true; - ServerThread.serverLog("client connected from " + client.socket().getInetAddress(),0); } } diff --git a/src/main/java/seng302/server/simulator/Simulator.java b/src/main/java/seng302/server/simulator/Simulator.java index 72d2717a..514ecc25 100644 --- a/src/main/java/seng302/server/simulator/Simulator.java +++ b/src/main/java/seng302/server/simulator/Simulator.java @@ -54,7 +54,6 @@ public class Simulator extends Observable implements Runnable { numOfFinishedBoats += moveBoat(boat, lapse); } } - //System.out.println(boats.get(0)); setChanged(); notifyObservers(boats); @@ -65,8 +64,6 @@ public class Simulator extends Observable implements Runnable { e.printStackTrace(); } } - - System.out.println("[SERVER] Race simulator has been terminated"); } /** diff --git a/src/test/java/seng302/models/stream/StreamReceiverTest.java b/src/test/java/seng302/models/stream/StreamReceiverTest.java index 4d0bea44..96f89b93 100644 --- a/src/test/java/seng302/models/stream/StreamReceiverTest.java +++ b/src/test/java/seng302/models/stream/StreamReceiverTest.java @@ -99,7 +99,7 @@ public class StreamReceiverTest { byte[] emptyArray = {}; assert bytesToLong.invoke(streamReceiver, emptyArray).equals(0L); } catch (Exception e){ - System.out.println(""); + e.printStackTrace(); } }