Merge branch 'master' into MapWindRotation
# Conflicts: # racevisionGame/src/main/java/mock/app/Event.java # racevisionGame/src/main/java/mock/model/MockRace.java # racevisionGame/src/main/java/visualiser/app/VisualiserInput.java # racevisionGame/src/main/java/visualiser/model/VisualiserRace.java
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
package mock.app;
|
||||
|
||||
import mock.model.RaceLogic;
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
import network.Messages.LatestMessages;
|
||||
import network.Messages.XMLMessage;
|
||||
import org.mockito.Mock;
|
||||
import visualiser.gameController.ControllerServer;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
|
||||
/**
|
||||
@@ -43,6 +40,10 @@ public class ConnectionAcceptor implements Runnable {
|
||||
private short boatXMLSequenceNumber;
|
||||
//regatta xml sequence number
|
||||
private short regattaXMLSequenceNumber;
|
||||
//controller server
|
||||
private ControllerServer controllerServer;
|
||||
//
|
||||
private RaceLogic rl = null;
|
||||
|
||||
/**
|
||||
* Connection Acceptor Constructor
|
||||
@@ -51,10 +52,10 @@ public class ConnectionAcceptor implements Runnable {
|
||||
*/
|
||||
public ConnectionAcceptor(LatestMessages latestMessages) throws IOException {
|
||||
|
||||
this.latestMessages =latestMessages;
|
||||
this.latestMessages = latestMessages;
|
||||
this.serverSocket = new ServerSocket(serverPort);
|
||||
CheckClientConnection checkClientConnection = new CheckClientConnection(mockOutputList);
|
||||
new Thread(checkClientConnection).start();
|
||||
new Thread(checkClientConnection, "ConnectionAcceptor()->CheckClientConnection thread").start();
|
||||
}
|
||||
|
||||
public String getAddress() throws UnknownHostException {
|
||||
@@ -65,24 +66,40 @@ public class ConnectionAcceptor implements Runnable {
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
|
||||
public void setRace(RaceLogic rl){
|
||||
this.rl = rl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the Acceptor
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){//should be connections not filled up
|
||||
|
||||
while(mockOutputList.remainingCapacity() > 0) {
|
||||
|
||||
try {
|
||||
|
||||
System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE
|
||||
|
||||
Socket mockSocket = serverSocket.accept();
|
||||
//TODO at this point we need to assign the connection a boat source ID, if they requested to participate.
|
||||
|
||||
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
|
||||
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
|
||||
ControllerServer controllerServer = new ControllerServer(mockSocket);
|
||||
new Thread(mockOutput).start();
|
||||
new Thread(controllerServer).start();
|
||||
ControllerServer controllerServer = new ControllerServer(mockSocket, this.rl); //TODO probably pass assigned boat source ID into ControllerServer.
|
||||
|
||||
new Thread(mockOutput, "ConnectionAcceptor.run()->MockOutput thread" + mockOutput).start();
|
||||
new Thread(controllerServer, "ConnectionAcceptor.run()->ControllerServer thread" + controllerServer).start();
|
||||
|
||||
mockOutputList.add(mockOutput);
|
||||
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));
|
||||
|
||||
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));//TEMP
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace();//TODO handle this properly
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -111,7 +128,7 @@ public class ConnectionAcceptor implements Runnable {
|
||||
double timeSinceLastHeartBeat = System.currentTimeMillis();
|
||||
while(true) {
|
||||
//System.out.println(mocks.size());//used to see current amount of visualisers connected.
|
||||
ArrayBlockingQueue<MockOutput> m = new ArrayBlockingQueue(16, true, mocks);
|
||||
ArrayBlockingQueue<MockOutput> m = new ArrayBlockingQueue<>(16, true, mocks);
|
||||
for (MockOutput mo : m) {
|
||||
try {
|
||||
mo.sendHeartBeat();
|
||||
|
||||
@@ -4,6 +4,7 @@ import mock.dataInput.PolarParser;
|
||||
import mock.model.MockRace;
|
||||
import mock.model.Polars;
|
||||
import mock.xml.RaceXMLCreator;
|
||||
import mock.model.RaceLogic;
|
||||
import network.Messages.LatestMessages;
|
||||
import org.xml.sax.SAXException;
|
||||
import shared.dataInput.*;
|
||||
@@ -38,7 +39,7 @@ public class Event {
|
||||
|
||||
private Polars boatPolars;
|
||||
|
||||
private ConnectionAcceptor mockOutput;
|
||||
private ConnectionAcceptor connectionAcceptor;
|
||||
private LatestMessages latestMessages;
|
||||
|
||||
/**
|
||||
@@ -60,7 +61,7 @@ public class Event {
|
||||
this.boatPolars = PolarParser.parse("mock/polars/acc_polars.csv");
|
||||
|
||||
this.latestMessages = new LatestMessages();
|
||||
this.mockOutput = new ConnectionAcceptor(latestMessages);
|
||||
this.connectionAcceptor = new ConnectionAcceptor(latestMessages);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -80,11 +81,11 @@ public class Event {
|
||||
}
|
||||
|
||||
public String getAddress() throws UnknownHostException {
|
||||
return mockOutput.getAddress();
|
||||
return connectionAcceptor.getAddress();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return mockOutput.getServerPort();
|
||||
return connectionAcceptor.getServerPort();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,8 +95,8 @@ public class Event {
|
||||
* @throws InvalidBoatDataException Thrown if the boat xml file cannot be parsed.
|
||||
* @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed.
|
||||
*/
|
||||
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException, ParserConfigurationException, JAXBException, SAXException, IOException {
|
||||
new Thread(mockOutput).start();
|
||||
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException, ParserConfigurationException, JAXBException, SAXException, IOException {
|
||||
new Thread(connectionAcceptor, "Event.Start()->ConnectionAcceptor thread").start();
|
||||
|
||||
sendXMLs();
|
||||
|
||||
@@ -109,9 +110,11 @@ public class Event {
|
||||
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, this.xmlFileType);
|
||||
|
||||
//Create and start race.
|
||||
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
|
||||
RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages);
|
||||
|
||||
connectionAcceptor.setRace(newRace);
|
||||
new Thread(newRace, "Event.Start()->RaceLogic thread").start();
|
||||
|
||||
new Thread(newRace).start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,11 +122,11 @@ public class Event {
|
||||
*/
|
||||
private void sendXMLs() {
|
||||
|
||||
mockOutput.setRegattaXml(regattaXML);
|
||||
connectionAcceptor.setRegattaXml(regattaXML);
|
||||
|
||||
mockOutput.setRaceXml(raceXML);
|
||||
connectionAcceptor.setRaceXml(raceXML);
|
||||
|
||||
mockOutput.setBoatsXml(boatXML);
|
||||
connectionAcceptor.setBoatsXml(boatXML);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,16 +3,16 @@ package mock.app;
|
||||
|
||||
|
||||
import network.BinaryMessageEncoder;
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.*;
|
||||
import network.Messages.Enums.MessageType;
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* TCP server to send race information to connected clients.
|
||||
@@ -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
|
||||
@@ -98,24 +96,25 @@ public class MockOutput implements Runnable
|
||||
* Generates the next heartbeat message and returns it. Increments the heartbeat sequence number.
|
||||
* @return The next heartbeat message.
|
||||
*/
|
||||
private Heartbeat createHeartbeatMessage() {
|
||||
private HeartBeat createHeartbeatMessage() {
|
||||
|
||||
//Create the heartbeat message.
|
||||
Heartbeat heartbeat = new Heartbeat(this.heartbeatSequenceNum);
|
||||
HeartBeat heartBeat = new HeartBeat(this.heartbeatSequenceNum);
|
||||
heartbeatSequenceNum++;
|
||||
|
||||
return heartbeat;
|
||||
return heartBeat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a heartbeat message into a packet to be sent, and returns the byte array.
|
||||
* @param heartbeat The heartbeat message to serialize.
|
||||
* @param heartBeat The heartbeat message to serialize.
|
||||
* @return Byte array containing the next heartbeat message.
|
||||
* @throws InvalidMessageException Thrown if the message cannot be encoded.
|
||||
*/
|
||||
private byte[] parseHeartbeat(Heartbeat heartbeat) {
|
||||
private byte[] parseHeartbeat(HeartBeat heartBeat) throws InvalidMessageException {
|
||||
|
||||
//Serializes the heartbeat message.
|
||||
byte[] heartbeatMessage = RaceVisionByteEncoder.heartBeat(heartbeat);
|
||||
byte[] heartbeatMessage = RaceVisionByteEncoder.encode(heartBeat);
|
||||
|
||||
//Places the serialized message in a packet.
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(
|
||||
@@ -133,11 +132,12 @@ public class MockOutput implements Runnable
|
||||
* Encodes/serialises a XMLMessage message, and returns it.
|
||||
* @param xmlMessage The XMLMessage message to serialise.
|
||||
* @return The XMLMessage message in a serialised form.
|
||||
* @throws InvalidMessageException Thrown if the message cannot be encoded.
|
||||
*/
|
||||
private synchronized byte[] parseXMLMessage(XMLMessage xmlMessage) {
|
||||
private synchronized byte[] parseXMLMessage(XMLMessage xmlMessage) throws InvalidMessageException {
|
||||
|
||||
//Serialize the xml message.
|
||||
byte[] encodedXML = RaceVisionByteEncoder.xmlMessage(xmlMessage);
|
||||
byte[] encodedXML = RaceVisionByteEncoder.encode(xmlMessage);
|
||||
|
||||
//Place the message in a packet.
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(
|
||||
@@ -158,12 +158,13 @@ public class MockOutput implements Runnable
|
||||
* Encodes/serialises a BoatLocation message, and returns it.
|
||||
* @param boatLocation The BoatLocation message to serialise.
|
||||
* @return The BoatLocation message in a serialised form.
|
||||
* @throws InvalidMessageException If the message cannot be encoded.
|
||||
*/
|
||||
private synchronized byte[] parseBoatLocation(BoatLocation boatLocation){
|
||||
private synchronized byte[] parseBoatLocation(BoatLocation boatLocation) throws InvalidMessageException {
|
||||
|
||||
|
||||
//Encodes the message.
|
||||
byte[] encodedBoatLoc = RaceVisionByteEncoder.boatLocation(boatLocation);
|
||||
byte[] encodedBoatLoc = RaceVisionByteEncoder.encode(boatLocation);
|
||||
|
||||
//Encodes the full message with header.
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(
|
||||
@@ -182,11 +183,12 @@ public class MockOutput implements Runnable
|
||||
* Encodes/serialises a RaceStatus message, and returns it.
|
||||
* @param raceStatus The RaceStatus message to serialise.
|
||||
* @return The RaceStatus message in a serialised form.
|
||||
* @throws InvalidMessageException Thrown if the message cannot be encoded.
|
||||
*/
|
||||
private synchronized byte[] parseRaceStatus(RaceStatus raceStatus){
|
||||
private synchronized byte[] parseRaceStatus(RaceStatus raceStatus) throws InvalidMessageException {
|
||||
|
||||
//Encodes the messages.
|
||||
byte[] encodedRaceStatus = RaceVisionByteEncoder.raceStatus(raceStatus);
|
||||
byte[] encodedRaceStatus = RaceVisionByteEncoder.encode(raceStatus);
|
||||
|
||||
//Encodes the full message with header.
|
||||
BinaryMessageEncoder binaryMessageEncoder = new BinaryMessageEncoder(
|
||||
@@ -209,7 +211,15 @@ public class MockOutput implements Runnable
|
||||
public void sendHeartBeat() throws IOException {
|
||||
//Sends a heartbeat every so often.
|
||||
if (timeSinceHeartbeat() >= heartbeatPeriod) {
|
||||
outToVisualiser.write(parseHeartbeat(createHeartbeatMessage()));
|
||||
|
||||
HeartBeat heartBeat = createHeartbeatMessage();
|
||||
|
||||
try {
|
||||
outToVisualiser.write(parseHeartbeat(heartBeat));
|
||||
} catch (InvalidMessageException e) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Could not encode HeartBeat: " + heartBeat, e);
|
||||
}
|
||||
|
||||
lastHeartbeatTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -219,39 +229,47 @@ public class MockOutput implements Runnable
|
||||
*/
|
||||
public void run() {
|
||||
|
||||
|
||||
//Wait until all of the xml files have been set.
|
||||
while (!this.latestMessages.hasAllXMLMessages()) {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
boolean sentXMLs = false;
|
||||
|
||||
try {
|
||||
while (!stop){
|
||||
while (!Thread.interrupted()) {
|
||||
|
||||
//Wait until all of the xml files have been set.
|
||||
if (!this.latestMessages.hasAllXMLMessages()) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
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;
|
||||
|
||||
//We only attempt to send packets every X milliseconds.
|
||||
long minimumFramePeriod = 16;
|
||||
if (framePeriod >= minimumFramePeriod) {
|
||||
|
||||
while(true) {
|
||||
try {
|
||||
//Send XML messages.
|
||||
if (!sentXMLs) {
|
||||
//Serialise them.
|
||||
|
||||
long currentFrameTime = System.currentTimeMillis();
|
||||
|
||||
//This is the time elapsed, in milliseconds, since the last server "frame".
|
||||
long framePeriod = currentFrameTime - previousFrameTime;
|
||||
|
||||
//We only attempt to send packets every X milliseconds.
|
||||
long minimumFramePeriod = 16;
|
||||
if (framePeriod >= minimumFramePeriod) {
|
||||
|
||||
//Send XML messages.
|
||||
if (!sentXMLs) {
|
||||
//Serialise them.
|
||||
try {
|
||||
byte[] raceXMLBlob = parseXMLMessage(latestMessages.getRaceXMLMessage());
|
||||
byte[] regattaXMLBlob = parseXMLMessage(latestMessages.getRegattaXMLMessage());
|
||||
byte[] boatsXMLBlob = parseXMLMessage(latestMessages.getBoatXMLMessage());
|
||||
@@ -261,55 +279,74 @@ public class MockOutput implements Runnable
|
||||
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) {
|
||||
}
|
||||
|
||||
//Sends the RaceStatus message.
|
||||
if (this.latestMessages.getRaceStatus() != null) {
|
||||
|
||||
try {
|
||||
byte[] raceStatusBlob = this.parseRaceStatus(this.latestMessages.getRaceStatus());
|
||||
|
||||
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);
|
||||
|
||||
//Write it.
|
||||
this.outToVisualiser.write(boatLocationBlob);
|
||||
|
||||
} catch (InvalidMessageException e) {
|
||||
Logger.getGlobal().log(Level.WARNING, "Could not encode BoatLocation: " + boatLocation, e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (SocketException e) {
|
||||
break;
|
||||
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.
|
||||
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 (IOException e) {
|
||||
@@ -317,8 +354,5 @@ public class MockOutput implements Runnable
|
||||
}
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
stop = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user