From a4cc5f222c2883944b5f53dad07f37547d3916fb Mon Sep 17 00:00:00 2001 From: Kusal Ekanayake Date: Thu, 4 May 2017 13:29:53 +1200 Subject: [PATCH] Refactored course boundary to be a shade rather than a line and made the stream parser and stream receiver exit gracefully before the app closes. #story[820] --- src/main/java/seng302/App.java | 7 ++++--- .../seng302/controllers/CanvasController.java | 15 ++++++++++----- .../java/seng302/models/parsers/StreamParser.java | 15 ++++++++++++--- .../seng302/models/parsers/StreamReceiver.java | 10 +++++++--- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index dd58de7c..c8f86815 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -19,8 +19,10 @@ public class App extends Application primaryStage.setMaximized(true); primaryStage.show(); - primaryStage.setOnCloseRequest(e -> { + StreamParser.appClose(); + StreamReceiver.noMoreBytes(); + System.out.println("[CLIENT] Exiting program"); System.exit(0); }); @@ -39,8 +41,7 @@ public class App extends Application if (args.length == 3 && args[0].equals("-server")){ sr = new StreamReceiver(args[1], Integer.valueOf(args[2]), "RaceStream"); - } - else if(args.length == 2 && args[0].equals("-server")){ + } else if(args.length == 2 && args[0].equals("-server")){ switch (args[1]) { case "internal": sr = new StreamReceiver("localhost", 8085, "RaceStream"); diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index f7071edb..60981d24 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -11,6 +11,7 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; +import javafx.scene.shape.Polygon; import javafx.scene.text.Font; import javafx.stage.Stage; import seng302.models.Boat; @@ -151,9 +152,10 @@ public class CanvasController { private void addRaceBorder() { XMLParser.RaceXMLObject raceXMLObject = StreamParser.getXmlObject().getRaceXML(); ArrayList courseLimits = raceXMLObject.getCourseLimit(); - gc.setStroke(Color.DARKRED); + gc.setStroke(Color.DARKBLUE); gc.setLineWidth(3); - + double[] xBoundaryPoints = new double[courseLimits.size()]; + double[] yBoundaryPoints = new double[courseLimits.size()]; for (int i = 0; i < courseLimits.size() - 1; i++) { Limit thisPoint1 = courseLimits.get(i); SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); @@ -163,18 +165,21 @@ public class CanvasController { Point2D borderPoint2 = findScaledXY(thisMark2); gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), borderPoint2.getX(), borderPoint2.getY()); - + xBoundaryPoints[i] = borderPoint1.getX(); + yBoundaryPoints[i] = borderPoint1.getY(); } - Limit thisPoint1 = courseLimits.get(courseLimits.size()-1); SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); Limit thisPoint2 = courseLimits.get(0); SingleMark thisMark2 = new SingleMark("", thisPoint2.getLat(), thisPoint2.getLng(), thisPoint2.getSeqID()); Point2D borderPoint1 = findScaledXY(thisMark1); Point2D borderPoint2 = findScaledXY(thisMark2); - gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), borderPoint2.getX(), borderPoint2.getY()); + xBoundaryPoints[courseLimits.size()-1] = borderPoint1.getX(); + yBoundaryPoints[courseLimits.size()-1] = borderPoint1.getY(); + gc.setFill(Color.LIGHTBLUE); + gc.fillPolygon(xBoundaryPoints,yBoundaryPoints,yBoundaryPoints.length); } diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index 5613485a..6e7b36a8 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -32,7 +32,7 @@ public class StreamParser extends Thread{ private String threadName; private Thread t; private static boolean raceStarted = false; - public static XMLParser xmlObject; + private static XMLParser xmlObject; private static boolean raceFinished = false; private static boolean streamStatus = false; private static long timeSinceStart = -1; @@ -40,6 +40,7 @@ public class StreamParser extends Thread{ private static Map boatsPos = new TreeMap<>(); private static double windDirection = 0; private static String currentTimeString; + private static boolean appRunning; /** * Used to initialise the thread name and stream parser object so a thread can be executed @@ -56,6 +57,7 @@ public class StreamParser extends Thread{ * */ public void run(){ + appRunning = true; try { System.out.println("[CLIENT] Start of stream"); streamStatus = true; @@ -63,7 +65,7 @@ public class StreamParser extends Thread{ while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) { Thread.sleep(1); } - while (true){ + while (appRunning){ StreamPacket packet = StreamReceiver.packetBuffer.peek(); //this code adds a delay to reading from the packetBuffer so //out of order packets have time to order themselves in the queue @@ -105,7 +107,7 @@ public class StreamParser extends Thread{ * Looks at the type of the packet then sends it to the appropriate parser to extract the * specific data associated with that packet type * - * @param packet the packet to be looked at + * @param packet the packet to be looked at and processed */ private static void parsePacket(StreamPacket packet) { try{ @@ -370,6 +372,7 @@ public class StreamParser extends Thread{ //type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat if (deviceType == 1 || deviceType == 3){ 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() { @@ -379,6 +382,7 @@ public class StreamParser extends Thread{ } })); } + //Adding the boatPacket to the priority que boatPositions.get(boatId).put(boatPacket); } } @@ -543,5 +547,10 @@ public class StreamParser extends Thread{ public static Map getBoatsPos() { return boatsPos; } + + public static void appClose(){ + appRunning = false; + System.out.println("[CLIENT] Shutting down stream parser"); + } } diff --git a/src/main/java/seng302/models/parsers/StreamReceiver.java b/src/main/java/seng302/models/parsers/StreamReceiver.java index 5eb66d78..65d7c525 100644 --- a/src/main/java/seng302/models/parsers/StreamReceiver.java +++ b/src/main/java/seng302/models/parsers/StreamReceiver.java @@ -21,6 +21,7 @@ public class StreamReceiver extends Thread { private Thread t; private String threadName; public static PriorityBlockingQueue packetBuffer; + private static boolean moreBytes; public StreamReceiver(String hostAddress, int hostPort, String threadName) { this.threadName = threadName; @@ -69,7 +70,7 @@ public class StreamReceiver extends Thread { int sync1; int sync2; - boolean moreBytes = true; + moreBytes = true; while(moreBytes) { try { crcBuffer = new ByteArrayOutputStream(); @@ -121,7 +122,6 @@ public class StreamReceiver extends Thread { return bytes; } - private void skipBytes(long n) throws Exception{ for (int i=0; i < n; i++){ readByte(); @@ -147,7 +147,6 @@ public class StreamReceiver extends Thread { return partialLong; } - public static void main(String[] args) { StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941,"TestThread1"); @@ -155,4 +154,9 @@ public class StreamReceiver extends Thread { sr.start(); } + + public static void noMoreBytes(){ + moreBytes = false; + System.out.println("[CLIENT] Shutting down stream receiver"); + } }