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:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user