Added names to threads created.

MockOutput had a nested while loop, but it wasn't needed. Also tidied some of the error handling in MockOutput.

#story[1095]
main
fjc40 9 years ago
parent 134586f407
commit 634d78ab70

@ -54,7 +54,7 @@ public class ConnectionAcceptor implements Runnable {
this.latestMessages = latestMessages; this.latestMessages = latestMessages;
this.serverSocket = new ServerSocket(serverPort); this.serverSocket = new ServerSocket(serverPort);
CheckClientConnection checkClientConnection = new CheckClientConnection(mockOutputList); CheckClientConnection checkClientConnection = new CheckClientConnection(mockOutputList);
new Thread(checkClientConnection).start(); new Thread(checkClientConnection, "ConnectionAcceptor()->CheckClientConnection thread").start();
} }
public String getAddress() throws UnknownHostException { public String getAddress() throws UnknownHostException {
@ -70,19 +70,30 @@ public class ConnectionAcceptor implements Runnable {
*/ */
@Override @Override
public void run() { public void run() {
while(true){//should be connections not filled up
while(mockOutputList.remainingCapacity() > 0) {
try { try {
System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE
Socket mockSocket = serverSocket.accept(); 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()); DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser); MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
ControllerServer controllerServer = new ControllerServer(mockSocket); ControllerServer controllerServer = new ControllerServer(mockSocket); //TODO probably pass assigned boat source ID into ControllerServer.
new Thread(mockOutput).start();
new Thread(controllerServer).start(); new Thread(mockOutput, "ConnectionAcceptor.run()->MockOutput thread" + mockOutput).start();
new Thread(controllerServer, "ConnectionAcceptor.run()->ControllerServer thread" + controllerServer).start();
mockOutputList.add(mockOutput); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();//TODO handle this properly
} }
} }
@ -111,7 +122,7 @@ public class ConnectionAcceptor implements Runnable {
double timeSinceLastHeartBeat = System.currentTimeMillis(); double timeSinceLastHeartBeat = System.currentTimeMillis();
while(true) { while(true) {
//System.out.println(mocks.size());//used to see current amount of visualisers connected. //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) { for (MockOutput mo : m) {
try { try {
mo.sendHeartBeat(); mo.sendHeartBeat();

@ -35,7 +35,7 @@ public class Event {
private Polars boatPolars; private Polars boatPolars;
private ConnectionAcceptor mockOutput; private ConnectionAcceptor connectionAcceptor;
private LatestMessages latestMessages; private LatestMessages latestMessages;
/** /**
@ -51,7 +51,7 @@ public class Event {
this.boatPolars = PolarParser.parse("mock/polars/acc_polars.csv"); this.boatPolars = PolarParser.parse("mock/polars/acc_polars.csv");
this.latestMessages = new LatestMessages(); this.latestMessages = new LatestMessages();
this.mockOutput = new ConnectionAcceptor(latestMessages); this.connectionAcceptor = new ConnectionAcceptor(latestMessages);
} }
catch (IOException e) { catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -67,11 +67,11 @@ public class Event {
} }
public String getAddress() throws UnknownHostException { public String getAddress() throws UnknownHostException {
return mockOutput.getAddress(); return connectionAcceptor.getAddress();
} }
public int getPort() { public int getPort() {
return mockOutput.getServerPort(); return connectionAcceptor.getServerPort();
} }
/** /**
@ -82,7 +82,7 @@ public class Event {
* @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed. * @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed.
*/ */
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException { public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException {
new Thread(mockOutput).start(); new Thread(connectionAcceptor, "Event.Start()->ConnectionAcceptor thread").start();
sendXMLs(); sendXMLs();
@ -94,7 +94,7 @@ public class Event {
//Create and start race. //Create and start race.
RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages); RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages);
new Thread(newRace).start(); new Thread(newRace, "Event.Start()->RaceLogic thread").start();
} }
/** /**
@ -102,11 +102,11 @@ public class Event {
*/ */
private void sendXMLs() { private void sendXMLs() {
mockOutput.setRegattaXml(regattaXML); connectionAcceptor.setRegattaXml(regattaXML);
mockOutput.setRaceXml(raceXML); connectionAcceptor.setRaceXml(raceXML);
mockOutput.setBoatsXml(boatXML); connectionAcceptor.setBoatsXml(boatXML);
} }
/** /**

@ -54,8 +54,6 @@ public class MockOutput implements Runnable
private int heartbeatSequenceNum = 1; private int heartbeatSequenceNum = 1;
private boolean stop = false; //whether or not hte thread keeps running
/** /**
* Ctor. * Ctor.
* @param latestMessages Latests Messages that the Mock is to send out * @param latestMessages Latests Messages that the Mock is to send out
@ -231,25 +229,31 @@ public class MockOutput implements Runnable
*/ */
public void run() { public void run() {
try {
while (!stop){
//Wait until all of the xml files have been set. //Wait until all of the xml files have been set.
if (!this.latestMessages.hasAllXMLMessages()) { while (!this.latestMessages.hasAllXMLMessages()) {
try { try {
Thread.sleep(500); Thread.sleep(500);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); //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;
} }
continue;
} }
long previousFrameTime = System.currentTimeMillis(); long previousFrameTime = System.currentTimeMillis();
boolean sentXMLs = false; boolean sentXMLs = false;
try {
while (!Thread.interrupted()) {
while(true) {
try { try {
long currentFrameTime = System.currentTimeMillis(); long currentFrameTime = System.currentTimeMillis();
@ -331,7 +335,7 @@ public class MockOutput implements Runnable
Thread.sleep(timeToWait); Thread.sleep(timeToWait);
} catch (InterruptedException e) { } catch (InterruptedException e) {
//If we get interrupted, exit the function. //If we get interrupted, exit the function.
e.printStackTrace(); Logger.getGlobal().log(Level.WARNING, "MockOutput.run().sleep(framePeriod) was interrupted on thread: " + Thread.currentThread(), e);
//Re-set the interrupt flag. //Re-set the interrupt flag.
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
return; return;
@ -344,15 +348,11 @@ public class MockOutput implements Runnable
} }
} }
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void stop(){
stop = true;
}
} }

@ -4,6 +4,7 @@ package network.MessageEncoders;
import network.Exceptions.InvalidMessageException; import network.Exceptions.InvalidMessageException;
import network.Exceptions.InvalidMessageTypeException; import network.Exceptions.InvalidMessageTypeException;
import network.Messages.*; import network.Messages.*;
import network.Messages.Enums.MessageType;
import static network.Utils.ByteConverter.*; import static network.Utils.ByteConverter.*;

Loading…
Cancel
Save