mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Added method to check if a boat is finished the race or not.
- updated server if all boats finish the race, then start sending race finished message. - race simulator will terminate if all boats finish the race, and prints log. #story[715]
This commit is contained in:
@@ -3,21 +3,16 @@ package seng302.server;
|
|||||||
import seng302.server.messages.*;
|
import seng302.server.messages.*;
|
||||||
import seng302.server.simulator.Boat;
|
import seng302.server.simulator.Boat;
|
||||||
import seng302.server.simulator.Simulator;
|
import seng302.server.simulator.Simulator;
|
||||||
import sun.misc.IOUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class ServerThread implements Runnable, Observer {
|
public class ServerThread implements Runnable, Observer {
|
||||||
private Thread runner;
|
|
||||||
private StreamingServerSocket server;
|
private StreamingServerSocket server;
|
||||||
private long startTime;
|
private long startTime;
|
||||||
boolean raceStarted = false;
|
private boolean raceStarted = false;
|
||||||
Map<Integer,Boolean> boatsFinished = new HashMap<>();
|
private Map<Integer,Boolean> boatsFinished = new HashMap<>();
|
||||||
private List<Boat> boats;
|
private List<Boat> boats;
|
||||||
private Simulator raceSimulator;
|
private Simulator raceSimulator;
|
||||||
private boolean sendingRaceFinishedLocationMessages = true;
|
private boolean sendingRaceFinishedLocationMessages = true;
|
||||||
@@ -31,7 +26,7 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
private static final int LOG_LEVEL = 1;
|
private static final int LOG_LEVEL = 1;
|
||||||
|
|
||||||
public ServerThread(String threadName){
|
public ServerThread(String threadName){
|
||||||
runner = new Thread(this, threadName);
|
Thread runner = new Thread(this, threadName);
|
||||||
runner.setDaemon(true);
|
runner.setDaemon(true);
|
||||||
|
|
||||||
serverLog("Spawning Server", 0);
|
serverLog("Spawning Server", 0);
|
||||||
@@ -51,7 +46,7 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
raceSimulatorThread.start();
|
raceSimulatorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void serverLog(String message, int logLevel){
|
static void serverLog(String message, int logLevel){
|
||||||
if(logLevel <= LOG_LEVEL){
|
if(logLevel <= LOG_LEVEL){
|
||||||
System.out.println("[SERVER] " + message);
|
System.out.println("[SERVER] " + message);
|
||||||
}
|
}
|
||||||
@@ -63,7 +58,7 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
* @param type The XML Message type
|
* @param type The XML Message type
|
||||||
* @return The XML Message
|
* @return The XML Message
|
||||||
*/
|
*/
|
||||||
public Message getXmlMessage(String fileName, XMLMessageSubType type){
|
private Message getXmlMessage(String fileName, XMLMessageSubType type){
|
||||||
String fileContents = null;
|
String fileContents = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -85,8 +80,8 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
/**
|
/**
|
||||||
* @return Get a race status message for the current race
|
* @return Get a race status message for the current race
|
||||||
*/
|
*/
|
||||||
public Message getRaceStatusMessage(){
|
private Message getRaceStatusMessage(){
|
||||||
List<BoatSubMessage> boatSubMessages = new ArrayList<BoatSubMessage>();
|
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||||
BoatStatus boatStatus;
|
BoatStatus boatStatus;
|
||||||
RaceStatus raceStatus;
|
RaceStatus raceStatus;
|
||||||
boolean thereAreBoatsNotFinished = false;
|
boolean thereAreBoatsNotFinished = false;
|
||||||
@@ -289,6 +284,7 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
* @param arg .
|
* @param arg .
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void update(Observable o, Object arg) {
|
public void update(Observable o, Object arg) {
|
||||||
// Only send if server started
|
// Only send if server started
|
||||||
// TODO: I don't understand why i need to check server is null or not ... confused - haoming 2/5/17
|
// TODO: I don't understand why i need to check server is null or not ... confused - haoming 2/5/17
|
||||||
@@ -296,22 +292,32 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Boat b : (List<Boat>) arg){
|
int numOfBoatsFinished = 0;
|
||||||
|
for (Boat boat : (List<Boat>) arg){
|
||||||
try {
|
try {
|
||||||
Message m = new BoatLocationMessage(b.getSourceID(), server.getSequenceNumber(), b.getLat(),
|
if (boat.isFinished()) {
|
||||||
b.getLng(), b.getLastPassedCorner().getBearingToNextCorner(),
|
numOfBoatsFinished ++;
|
||||||
((long) b.getSpeed()));
|
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(),
|
||||||
|
boat.getLng(), boat.getLastPassedCorner().getBearingToNextCorner(),
|
||||||
|
((long) boat.getSpeed()));
|
||||||
server.send(m);
|
server.send(m);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
serverLog("Couldn't send a boat status message", 3);
|
serverLog("Couldn't send a boat status message", 3);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch (NullPointerException e){
|
catch (NullPointerException e){
|
||||||
//e.printStackTrace();
|
e.printStackTrace();
|
||||||
//TODO: add a method in boat to check if a boat has finished the race. - haoming 2/5/17
|
|
||||||
serverLog("Boat " + b.getSourceID() + " finished the race", 1);
|
|
||||||
boatsFinished.put(b.getSourceID(), true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (numOfBoatsFinished == ((List<Boat>) arg).size()) {
|
||||||
|
startSendingRaceFinishedBoatPostions();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,14 @@ public class Boat {
|
|||||||
private double lng;
|
private double lng;
|
||||||
private double speed; // in mm/sec
|
private double speed; // in mm/sec
|
||||||
private String boatName, shortName, shorterName;
|
private String boatName, shortName, shorterName;
|
||||||
|
private boolean isFinished;
|
||||||
|
|
||||||
private Corner lastPassedCorner, headingCorner;
|
private Corner lastPassedCorner, headingCorner;
|
||||||
|
|
||||||
public Boat(int sourceID, String boatName) {
|
public Boat(int sourceID, String boatName) {
|
||||||
this.sourceID = sourceID;
|
this.sourceID = sourceID;
|
||||||
this.boatName = boatName;
|
this.boatName = boatName;
|
||||||
|
this.isFinished = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,4 +108,12 @@ public class Boat {
|
|||||||
public void setHeadingCorner(Corner headingCorner) {
|
public void setHeadingCorner(Corner headingCorner) {
|
||||||
this.headingCorner = headingCorner;
|
this.headingCorner = headingCorner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isFinished() {
|
||||||
|
return isFinished;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFinished(boolean finished) {
|
||||||
|
isFinished = finished;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("[SERVER] Race simulator has been terminated");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,7 +91,10 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
boat.setHeadingCorner(boat.getLastPassedCorner().getNextCorner());
|
boat.setHeadingCorner(boat.getLastPassedCorner().getNextCorner());
|
||||||
|
|
||||||
// heading corner == null means boat has reached the final mark
|
// heading corner == null means boat has reached the final mark
|
||||||
if (boat.getHeadingCorner() == null) return 1;
|
if (boat.getHeadingCorner() == null) {
|
||||||
|
boat.setFinished(true);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// move compensate distance for the mark just passed
|
// move compensate distance for the mark just passed
|
||||||
Position pos = GeoUtility.getGeoCoordinate(
|
Position pos = GeoUtility.getGeoCoordinate(
|
||||||
|
|||||||
Reference in New Issue
Block a user