Host starts with button in ConnectionController

- Converted Event to Singleton for configuring and hosting no more than one game instance.
- Retrieve address and port from ConnectionAcceptor
- Automatically add local host to host list

#story[1010]
This commit is contained in:
Connor Taylor-Brown
2017-07-25 15:52:53 +12:00
parent a27c16d413
commit 7e3e865563
7 changed files with 108 additions and 98 deletions
@@ -1,56 +0,0 @@
package mock.app;
import javafx.application.Application;
import javafx.stage.Stage;
import mock.dataInput.PolarParser;
import mock.model.Polars;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import shared.dataInput.XMLReader;
import shared.enums.XMLFileType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class App extends Application {
/**
* Entry point for running the programme
*
* @param args for starting the programme
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
Polars boatPolars = PolarParser.parse("mock/polars/acc_polars.csv");
String regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
String raceXML = XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8);
String boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
Event raceEvent = new Event(raceXML, regattaXML, boatXML, XMLFileType.Contents, boatPolars);
raceEvent.start();
} catch (Exception e) {
//Catch all exceptions, print, and exit.
e.printStackTrace();
System.exit(1);
}
}
}
@@ -8,8 +8,10 @@ import org.mockito.Mock;
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;
@@ -54,6 +56,14 @@ public class ConnectionAcceptor implements Runnable {
new Thread(checkClientConnection).start();
}
public String getAddress() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
public int getServerPort() {
return serverPort;
}
/**
* Run the Acceptor
*/
@@ -1,5 +1,6 @@
package mock.app;
import mock.dataInput.PolarParser;
import mock.model.MockRace;
import mock.model.Polars;
import network.Messages.LatestMessages;
@@ -11,7 +12,10 @@ import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import shared.model.Constants;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -20,6 +24,7 @@ import java.time.format.DateTimeFormatter;
* A Race Event, this holds all of the race's information as well as handling the connection to its clients.
*/
public class Event {
private static Event theEvent = new Event();
private String raceXML;
private String regattaXML;
@@ -32,36 +37,42 @@ public class Event {
private ConnectionAcceptor mockOutput;
private LatestMessages latestMessages;
/**
* Constructs an event, using various XML files.
* @param raceXML The race.xml file.
* @param regattaXML The regatta.xml file.
* @param boatXML The boat.xml file.
* @param type How to read the file - e.g., load as resource.
* @param boatPolars polars that the boat uses
*/
public Event(String raceXML, String regattaXML, String boatXML, XMLFileType type, Polars boatPolars) {
this.raceXML = getRaceXMLAtCurrentTime(raceXML);
this.boatXML = boatXML;
this.regattaXML = regattaXML;
this.xmlFileType = type;
this.boatPolars = boatPolars;
this.latestMessages = new LatestMessages();
private Event() {
try {
this.mockOutput = new ConnectionAcceptor(latestMessages);
new Thread(mockOutput).start();
this.raceXML = getRaceXMLAtCurrentTime(XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8));
this.boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
this.regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
this.xmlFileType = XMLFileType.Contents;
} catch (IOException e) {
this.boatPolars = PolarParser.parse("mock/polars/acc_polars.csv");
this.latestMessages = new LatestMessages();
this.mockOutput = new ConnectionAcceptor(latestMessages);
}
catch (IOException e) {
e.printStackTrace();
} catch (XMLReaderException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public static Event getEvent() {
return theEvent;
}
public String getAddress() throws UnknownHostException {
return mockOutput.getAddress();
}
public int getPort() {
return mockOutput.getServerPort();
}
/**
* Sends the initial race data and then begins race simulation.
* @throws InvalidRaceDataException Thrown if the race xml file cannot be parsed.
@@ -70,6 +81,7 @@ public class Event {
* @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed.
*/
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException {
new Thread(mockOutput).start();
sendXMLs();
@@ -119,5 +131,4 @@ public class Event {
return raceXML;
}
}
@@ -8,11 +8,17 @@ import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import mock.app.Event;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import visualiser.model.RaceConnection;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;
/**
@@ -41,8 +47,6 @@ public class ConnectionController extends Controller {
public void initialize(URL location, ResourceBundle resources) {
// TODO - replace with config file
connections = FXCollections.observableArrayList();
connections.add(new RaceConnection("livedata.americascup.com", 4941));
connections.add(new RaceConnection("localhost", 4942));
connectionTable.setItems(connections);
hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
@@ -95,4 +99,28 @@ public class ConnectionController extends Controller {
}
}
/**
* Sets up a new host
*/
public void addLocal() {
try {
Event game = Event.getEvent();
urlField.textProperty().set(game.getAddress());
portField.textProperty().set(Integer.toString(game.getPort()));
game.start();
addConnection();
} catch (InvalidRaceDataException e) {
e.printStackTrace();
} catch (XMLReaderException e) {
e.printStackTrace();
} catch (InvalidBoatDataException e) {
e.printStackTrace();
} catch (InvalidRegattaDataException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
@@ -15,6 +15,11 @@ public class RaceConnection {
private final int port;
private final StringProperty status;
/**
* Constructor for remote host connections.
* @param hostname URL for remote host
* @param port port for game feed
*/
public RaceConnection(String hostname, int port) {
this.hostname = new SimpleStringProperty(hostname);
this.port = port;