|
|
|
|
@ -54,8 +54,6 @@ public class MockOutput implements Runnable
|
|
|
|
|
private int heartbeatSequenceNum = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean stop = false; //whether or not hte thread keeps running
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ctor.
|
|
|
|
|
* @param latestMessages Latests Messages that the Mock is to send out
|
|
|
|
|
@ -231,119 +229,124 @@ public class MockOutput implements Runnable
|
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
while (!stop){
|
|
|
|
|
|
|
|
|
|
//Wait until all of the xml files have been set.
|
|
|
|
|
if (!this.latestMessages.hasAllXMLMessages()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Wait until all of the xml files have been set.
|
|
|
|
|
while (!this.latestMessages.hasAllXMLMessages()) {
|
|
|
|
|
|
|
|
|
|
long previousFrameTime = System.currentTimeMillis();
|
|
|
|
|
boolean sentXMLs = false;
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
//If we get interrupted, exit the function.
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "MockOutput.run().sleep(waitForXMLs) was interrupted on thread: " + Thread.currentThread(), e);
|
|
|
|
|
|
|
|
|
|
//Re-set the interrupt flag.
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
long currentFrameTime = System.currentTimeMillis();
|
|
|
|
|
long previousFrameTime = System.currentTimeMillis();
|
|
|
|
|
boolean sentXMLs = false;
|
|
|
|
|
|
|
|
|
|
//This is the time elapsed, in milliseconds, since the last server "frame".
|
|
|
|
|
long framePeriod = currentFrameTime - previousFrameTime;
|
|
|
|
|
try {
|
|
|
|
|
while (!Thread.interrupted()) {
|
|
|
|
|
|
|
|
|
|
//We only attempt to send packets every X milliseconds.
|
|
|
|
|
long minimumFramePeriod = 16;
|
|
|
|
|
if (framePeriod >= minimumFramePeriod) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
//Send XML messages.
|
|
|
|
|
if (!sentXMLs) {
|
|
|
|
|
//Serialise them.
|
|
|
|
|
long currentFrameTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
byte[] raceXMLBlob = parseXMLMessage(latestMessages.getRaceXMLMessage());
|
|
|
|
|
byte[] regattaXMLBlob = parseXMLMessage(latestMessages.getRegattaXMLMessage());
|
|
|
|
|
byte[] boatsXMLBlob = parseXMLMessage(latestMessages.getBoatXMLMessage());
|
|
|
|
|
//This is the time elapsed, in milliseconds, since the last server "frame".
|
|
|
|
|
long framePeriod = currentFrameTime - previousFrameTime;
|
|
|
|
|
|
|
|
|
|
//Send them.
|
|
|
|
|
outToVisualiser.write(raceXMLBlob);
|
|
|
|
|
outToVisualiser.write(regattaXMLBlob);
|
|
|
|
|
outToVisualiser.write(boatsXMLBlob);
|
|
|
|
|
sentXMLs = true;
|
|
|
|
|
//We only attempt to send packets every X milliseconds.
|
|
|
|
|
long minimumFramePeriod = 16;
|
|
|
|
|
if (framePeriod >= minimumFramePeriod) {
|
|
|
|
|
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode XMLMessage: " + latestMessages.getRaceXMLMessage(), e);
|
|
|
|
|
continue; //Go to next iteration.
|
|
|
|
|
}
|
|
|
|
|
//Send XML messages.
|
|
|
|
|
if (!sentXMLs) {
|
|
|
|
|
//Serialise them.
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
byte[] raceXMLBlob = parseXMLMessage(latestMessages.getRaceXMLMessage());
|
|
|
|
|
byte[] regattaXMLBlob = parseXMLMessage(latestMessages.getRegattaXMLMessage());
|
|
|
|
|
byte[] boatsXMLBlob = parseXMLMessage(latestMessages.getBoatXMLMessage());
|
|
|
|
|
|
|
|
|
|
//Send them.
|
|
|
|
|
outToVisualiser.write(raceXMLBlob);
|
|
|
|
|
outToVisualiser.write(regattaXMLBlob);
|
|
|
|
|
outToVisualiser.write(boatsXMLBlob);
|
|
|
|
|
sentXMLs = true;
|
|
|
|
|
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode XMLMessage: " + latestMessages.getRaceXMLMessage(), e);
|
|
|
|
|
continue; //Go to next iteration.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Sends the RaceStatus message.
|
|
|
|
|
if (this.latestMessages.getRaceStatus() != null) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
byte[] raceStatusBlob = this.parseRaceStatus(this.latestMessages.getRaceStatus());
|
|
|
|
|
//Sends the RaceStatus message.
|
|
|
|
|
if (this.latestMessages.getRaceStatus() != null) {
|
|
|
|
|
|
|
|
|
|
this.outToVisualiser.write(raceStatusBlob);
|
|
|
|
|
try {
|
|
|
|
|
byte[] raceStatusBlob = this.parseRaceStatus(this.latestMessages.getRaceStatus());
|
|
|
|
|
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode RaceStatus: " + latestMessages.getRaceStatus(), e);
|
|
|
|
|
}
|
|
|
|
|
this.outToVisualiser.write(raceStatusBlob);
|
|
|
|
|
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode RaceStatus: " + latestMessages.getRaceStatus(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Send all of the BoatLocation messages.
|
|
|
|
|
for (int sourceID : this.latestMessages.getBoatLocationMap().keySet()) {
|
|
|
|
|
//Send all of the BoatLocation messages.
|
|
|
|
|
for (int sourceID : this.latestMessages.getBoatLocationMap().keySet()) {
|
|
|
|
|
|
|
|
|
|
//Get the message.
|
|
|
|
|
BoatLocation boatLocation = this.latestMessages.getBoatLocation(sourceID);
|
|
|
|
|
if (boatLocation != null) {
|
|
|
|
|
//Get the message.
|
|
|
|
|
BoatLocation boatLocation = this.latestMessages.getBoatLocation(sourceID);
|
|
|
|
|
if (boatLocation != null) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//Encode.
|
|
|
|
|
byte[] boatLocationBlob = this.parseBoatLocation(boatLocation);
|
|
|
|
|
try {
|
|
|
|
|
//Encode.
|
|
|
|
|
byte[] boatLocationBlob = this.parseBoatLocation(boatLocation);
|
|
|
|
|
|
|
|
|
|
//Write it.
|
|
|
|
|
this.outToVisualiser.write(boatLocationBlob);
|
|
|
|
|
//Write it.
|
|
|
|
|
this.outToVisualiser.write(boatLocationBlob);
|
|
|
|
|
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode BoatLocation: " + boatLocation, e);
|
|
|
|
|
}
|
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "Could not encode BoatLocation: " + boatLocation, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previousFrameTime = currentFrameTime;
|
|
|
|
|
previousFrameTime = currentFrameTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
//Wait until the frame period will be large enough.
|
|
|
|
|
long timeToWait = minimumFramePeriod - framePeriod;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(timeToWait);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
//If we get interrupted, exit the function.
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
//Re-set the interrupt flag.
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//Wait until the frame period will be large enough.
|
|
|
|
|
long timeToWait = minimumFramePeriod - framePeriod;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(timeToWait);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
//If we get interrupted, exit the function.
|
|
|
|
|
Logger.getGlobal().log(Level.WARNING, "MockOutput.run().sleep(framePeriod) was interrupted on thread: " + Thread.currentThread(), e);
|
|
|
|
|
//Re-set the interrupt flag.
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
@ -351,8 +354,5 @@ public class MockOutput implements Runnable
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stop(){
|
|
|
|
|
stop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|