Fix bugs that server doesn't send boat location before and after the race.

- server will sends boat location not only during the race, but also before the race and after all boats have finished the race.
- refactored simulator so that it runs at the begining to send boat location, and if its "isStarted" set to true, then it starts moving the boats.

#story[715]
This commit is contained in:
Haoming Yin
2017-05-02 22:51:35 +12:00
parent 772ece25a0
commit f0d6312fa5
2 changed files with 26 additions and 10 deletions
+14 -5
View File
@@ -32,7 +32,12 @@ public class ServerThread implements Runnable, Observer {
public ServerThread(String threadName){
runner = new Thread(this, threadName);
serverLog("Spawning Server", 0);
raceSimulator = new Simulator(BOAT_LOCATION_PERIOD);
raceSimulator.addObserver(this);
// run race simulator, so it can send boats' static location.
Thread raceSimulatorThread = new Thread(raceSimulator, "Race Simulator");
boats = raceSimulator.getBoats();
for (Boat b : boats){
@@ -40,6 +45,7 @@ public class ServerThread implements Runnable, Observer {
}
runner.start();
raceSimulatorThread.start();
}
public static void serverLog(String message, int logLevel){
@@ -130,9 +136,9 @@ public class ServerThread implements Runnable, Observer {
* Starts an instance of the race simulator
*/
private void startRaceSim(){
serverLog("Starting Race Simulator", 0);
raceSimulator.addObserver(this);
new Thread(raceSimulator).start();
serverLog("Starting Running Race Simulator", 0);
// set race started to true, so the simulator will start moving boats
raceSimulator.setRaceStarted(true);
}
/**
@@ -258,11 +264,12 @@ public class ServerThread implements Runnable, Observer {
@Override
public void update(Observable o, Object arg) {
// Only send if server started
if(!server.isStarted()){
// TODO: I don't understand why i need to check server is null or not ... confused - haoming 2/5/17
if(server == null || !server.isStarted()){
return;
}
for (Boat b : ((Simulator) o).getBoats()){
for (Boat b : (List<Boat>) arg){
try {
Message m = new BoatLocationMessage(b.getSourceID(), 1, b.getLat(),
b.getLng(), b.getLastPassedCorner().getBearingToNextCorner(),
@@ -273,6 +280,8 @@ public class ServerThread implements Runnable, Observer {
return;
}
catch (NullPointerException e){
//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);
}
@@ -14,6 +14,7 @@ public class Simulator extends Observable implements Runnable {
private List<Corner> course;
private List<Boat> boats;
private long lapse;
private boolean isRaceStarted;
/**
* Creates a simulator instance with given time lapse.
@@ -24,6 +25,7 @@ public class Simulator extends Observable implements Runnable {
course = rp.getCourse();
boats = rp.getBoats();
this.lapse = lapse;
isRaceStarted = false;
setLegs();
@@ -35,7 +37,7 @@ public class Simulator extends Observable implements Runnable {
boat.setLng(startLng);
boat.setLastPassedCorner(course.get(0));
boat.setHeadingCorner(course.get(1));
boat.setSpeed(ThreadLocalRandom.current().nextInt(40000, 60000 + 1));
boat.setSpeed(ThreadLocalRandom.current().nextInt(400000, 600000 + 1));
}
}
@@ -45,15 +47,18 @@ public class Simulator extends Observable implements Runnable {
int numOfFinishedBoats = 0;
while (numOfFinishedBoats < boats.size()) {
// if race has started, then boat should start to move.
if (isRaceStarted) {
for (Boat boat : boats) {
numOfFinishedBoats += moveBoat(boat, lapse);
}
}
//System.out.println(boats.get(0));
setChanged();
notifyObservers(boats);
// sleep for 1 second.
try {
Thread.sleep(lapse);
} catch (InterruptedException e) {
@@ -64,7 +69,6 @@ public class Simulator extends Observable implements Runnable {
/**
* Moves a boat with given time duration.
*
* @param boat the boat to be moved
* @param duration the moving duration in milliseconds
* @return 1 if the boat has reached the final line, otherwise return 0
@@ -126,4 +130,7 @@ public class Simulator extends Observable implements Runnable {
return boats;
}
public void setRaceStarted(boolean raceStarted) {
isRaceStarted = raceStarted;
}
}