Server sends mark locations to test

- Added a timer to send boat location messages containing the mark locations to test the receiver

#story[891]
This commit is contained in:
Michael Rausch
2017-05-18 13:32:24 +12:00
parent cf4d7e03f5
commit 6a6ed3ed44
2 changed files with 67 additions and 0 deletions
@@ -1,5 +1,7 @@
package seng302.server; package seng302.server;
import seng302.server.simulator.mark.CompoundMark;
import seng302.server.simulator.mark.Mark;
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;
@@ -257,6 +259,53 @@ public class ServerThread implements Runnable, Observer {
//Delays the new course xml data for 25 seconds so the boats are able to pass the starting line //Delays the new course xml data for 25 seconds so the boats are able to pass the starting line
} }
/**
* Starts sending boat location messages containing the mark positions
* Marks are flipped by 90 degrees from their original position
*/
private void startUpdatingMarkPositions(){
Timer t = new Timer();
t.schedule(new TimerTask() {
/**
* Send the mark location message
* @param m The mark to send
* @param offset How far to move the marks from their original position
*/
private void sendMark(Mark m, Double offset){
Message markLocation = new BoatLocationMessage(m.getSourceID(), server.getSequenceNumber(),
m.getLat()-offset, m.getLng()+offset*2, 0, 0);
try {
server.send(markLocation);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
for (CompoundMark m : raceSimulator.getMarks()){
if (m == null){
continue;
}
Mark mark1 = m.getMark1();
Mark mark2 = m.getMark2();
if (mark1 != null){
sendMark(mark1, 0.0002);
}
if (mark2 != null){
sendMark(mark2, 0.0005);
}
}
}
}, 21000, 1000);
}
public void run() { public void run() {
try{ try{
server = new StreamingServerSocket(PORT_NUMBER); server = new StreamingServerSocket(PORT_NUMBER);
@@ -275,6 +324,7 @@ public class ServerThread implements Runnable, Observer {
startSendingRaceStartStatusMessages(); startSendingRaceStartStatusMessages();
startSendingRaceStatusMessages(); startSendingRaceStatusMessages();
sendPostStartCourseXml(); sendPostStartCourseXml();
startUpdatingMarkPositions();
} }
/** /**
@@ -1,12 +1,15 @@
package seng302.server.simulator; package seng302.server.simulator;
import seng302.server.simulator.mark.CompoundMark;
import seng302.server.simulator.mark.Corner; import seng302.server.simulator.mark.Corner;
import seng302.server.simulator.mark.Mark; import seng302.server.simulator.mark.Mark;
import seng302.server.simulator.mark.Position; import seng302.server.simulator.mark.Position;
import seng302.server.simulator.parsers.RaceParser; import seng302.server.simulator.parsers.RaceParser;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Observable; import java.util.Observable;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class Simulator extends Observable implements Runnable { public class Simulator extends Observable implements Runnable {
@@ -138,4 +141,18 @@ public class Simulator extends Observable implements Runnable {
public void setRaceStarted(boolean raceStarted) { public void setRaceStarted(boolean raceStarted) {
isRaceStarted = raceStarted; isRaceStarted = raceStarted;
} }
/**
* @return A list of marks in the race
*/
public Set<CompoundMark> getMarks(){
Set<CompoundMark> marks = new HashSet<>();
for (Corner c : course){
marks.add(c.getCompoundMark());
marks.add(c.getCompoundMark());
}
return marks;
}
} }