# Conflicts: # racevisionGame/src/main/java/mock/app/App.java # racevisionGame/src/main/java/visualiser/Controllers/ConnectionController.java # racevisionGame/src/main/java/visualiser/app/App.java # racevisionGame/src/main/java/visualiser/model/RaceConnection.java # racevisionGame/src/main/resources/visualiser/scenes/connect.fxmlmain
commit
88604df66d
@ -0,0 +1,3 @@
|
||||
<component name="CopyrightManager">
|
||||
<settings default="" />
|
||||
</component>
|
||||
@ -1,58 +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.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class App extends Application implements Runnable {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
public void run(){
|
||||
launch();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,211 @@
|
||||
package mock.app;
|
||||
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
import network.Messages.LatestMessages;
|
||||
import network.Messages.XMLMessage;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Connection acceptor for multiple clients
|
||||
*/
|
||||
public class ConnectionAcceptor implements Runnable {
|
||||
|
||||
|
||||
/**
|
||||
* Port to expose server on.
|
||||
*/
|
||||
private int serverPort = 4942;
|
||||
/**
|
||||
* Socket used to listen for clients on.
|
||||
*/
|
||||
private ServerSocket serverSocket;
|
||||
//mock outputs
|
||||
private ArrayBlockingQueue<MockOutput> mockOutputList = new ArrayBlockingQueue<>(16, true);
|
||||
//latest messages
|
||||
private LatestMessages latestMessages;
|
||||
//Acknowledgement number for packets
|
||||
private int ackNumber = 0;
|
||||
//race xml sequence number
|
||||
private short raceXMLSequenceNumber;
|
||||
//boat xml sequence number
|
||||
private short boatXMLSequenceNumber;
|
||||
//regatta xml sequence number
|
||||
private short regattaXMLSequenceNumber;
|
||||
|
||||
/**
|
||||
* Connection Acceptor Constructor
|
||||
* @param latestMessages Latest messages to be sent
|
||||
* @throws IOException if a server socket cannot be instantiated.
|
||||
*/
|
||||
public ConnectionAcceptor(LatestMessages latestMessages) throws IOException {
|
||||
|
||||
this.latestMessages =latestMessages;
|
||||
this.serverSocket = new ServerSocket(serverPort);
|
||||
CheckClientConnection checkClientConnection = new CheckClientConnection(mockOutputList);
|
||||
new Thread(checkClientConnection).start();
|
||||
}
|
||||
|
||||
public String getAddress() throws UnknownHostException {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
}
|
||||
|
||||
public int getServerPort() {
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the Acceptor
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){//should be connections not filled up
|
||||
try {
|
||||
System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE
|
||||
Socket mockSocket = serverSocket.accept();
|
||||
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
|
||||
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
|
||||
new Thread(mockOutput).start();
|
||||
mockOutputList.add(mockOutput);
|
||||
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Nested class to remove disconnected clients
|
||||
*/
|
||||
class CheckClientConnection implements Runnable{
|
||||
|
||||
private ArrayBlockingQueue<MockOutput> mocks;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param mocks Mocks "connected"
|
||||
*/
|
||||
public CheckClientConnection(ArrayBlockingQueue<MockOutput> mocks){
|
||||
this.mocks = mocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the remover.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
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);
|
||||
for (MockOutput mo : m) {
|
||||
try {
|
||||
mo.sendHeartBeat();
|
||||
} catch (IOException e) {
|
||||
mocks.remove(mo);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Race XML to send.
|
||||
* @param raceXml XML to send to the Client.
|
||||
*/
|
||||
public void setRaceXml(String raceXml) {
|
||||
//Create the message.
|
||||
XMLMessage message = this.createXMLMessage(raceXml, XMLMessageType.RACE);
|
||||
|
||||
//Place it in LatestMessages.
|
||||
this.latestMessages.setRaceXMLMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Regatta XMl to send.
|
||||
* @param regattaXml XML to send to Client.
|
||||
*/
|
||||
public void setRegattaXml(String regattaXml) {
|
||||
//Create the message.
|
||||
XMLMessage message = this.createXMLMessage(regattaXml, XMLMessageType.REGATTA);
|
||||
|
||||
//Place it in LatestMessages.
|
||||
this.latestMessages.setRegattaXMLMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Boats XML to send.
|
||||
* @param boatsXml XMl to send to the Client.
|
||||
*/
|
||||
public void setBoatsXml(String boatsXml) {
|
||||
//Create the message.
|
||||
XMLMessage message = this.createXMLMessage(boatsXml, XMLMessageType.BOAT);
|
||||
|
||||
//Place it in LatestMessages.
|
||||
this.latestMessages.setBoatXMLMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an XMLMessage of a specified subtype using the xml contents string.
|
||||
* @param xmlString The contents of the xml file.
|
||||
* @param messageType The subtype of xml message (race, regatta, boat).
|
||||
* @return The created XMLMessage object.
|
||||
*/
|
||||
private XMLMessage createXMLMessage(String xmlString, XMLMessageType messageType) {
|
||||
|
||||
//Get the correct sequence number to use, and increment it.
|
||||
short sequenceNumber = 0;
|
||||
if (messageType == XMLMessageType.RACE) {
|
||||
sequenceNumber = this.raceXMLSequenceNumber;
|
||||
this.raceXMLSequenceNumber++;
|
||||
|
||||
} else if (messageType == XMLMessageType.BOAT) {
|
||||
sequenceNumber = this.boatXMLSequenceNumber;
|
||||
this.boatXMLSequenceNumber++;
|
||||
|
||||
} else if (messageType == XMLMessageType.REGATTA) {
|
||||
sequenceNumber = this.regattaXMLSequenceNumber;
|
||||
this.regattaXMLSequenceNumber++;
|
||||
|
||||
}
|
||||
|
||||
//Create the message.
|
||||
XMLMessage message = new XMLMessage(
|
||||
XMLMessage.currentVersionNumber,
|
||||
getNextAckNumber(),
|
||||
System.currentTimeMillis(),
|
||||
messageType,
|
||||
sequenceNumber,
|
||||
xmlString);
|
||||
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the ackNumber value, and returns it.
|
||||
* @return Incremented ackNumber.
|
||||
*/
|
||||
private int getNextAckNumber(){
|
||||
this.ackNumber++;
|
||||
|
||||
return this.ackNumber;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
package visualiser.gameController;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.scene.Scene;
|
||||
import visualiser.gameController.Keys.ControlKey;
|
||||
import visualiser.gameController.Keys.KeyFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static javafx.application.Application.launch;
|
||||
|
||||
/**
|
||||
* Class for checking what keys are currently being used
|
||||
*/
|
||||
public class InputChecker {
|
||||
private HashMap<String, Boolean> currentlyActiveKeys = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Controller loop that detects key presses that runs parallel to the main scene.
|
||||
* @param scene Scene the controller is to run in parallel with.
|
||||
*/
|
||||
public void runWithScene(Scene scene){
|
||||
scene.setOnKeyPressed(event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
if (!currentlyActiveKeys.containsKey(codeString)) {
|
||||
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||
if (controlKey != null) {
|
||||
controlKey.onAction();
|
||||
System.out.println(controlKey.toString() + " is Pressed.");
|
||||
}
|
||||
currentlyActiveKeys.put(codeString, true);
|
||||
}
|
||||
});
|
||||
|
||||
scene.setOnKeyReleased(event -> {
|
||||
String codeString = event.getCode().toString();
|
||||
ControlKey controlKey = KeyFactory.getKey(codeString);
|
||||
if (controlKey != null) {
|
||||
controlKey.onRelease();
|
||||
System.out.println(controlKey.toString() + " is Released.");
|
||||
}
|
||||
currentlyActiveKeys.remove(event.getCode().toString());
|
||||
});
|
||||
|
||||
new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
for (String key: currentlyActiveKeys.keySet()){
|
||||
ControlKey controlKey = KeyFactory.getKey(key);
|
||||
if (controlKey != null){
|
||||
controlKey.onHold();
|
||||
System.out.println(controlKey.toString() + " is Held.");
|
||||
}
|
||||
}
|
||||
// for (String key : InputKeys.stringKeysMap.keySet()){
|
||||
// if (removeActiveKey(key)) {
|
||||
// System.out.println(key);
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a key from the active dictionary
|
||||
* @param codeString string of the key press to remove
|
||||
* @return whether or not the key has been removed or not.
|
||||
*/
|
||||
private boolean removeActiveKey(String codeString) {
|
||||
Boolean isActive = currentlyActiveKeys.get(codeString);
|
||||
|
||||
if (isActive != null && isActive) {
|
||||
currentlyActiveKeys.put(codeString, false);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key for the controller, part of the abstract factory KeyFactory
|
||||
*/
|
||||
public abstract class ControlKey {
|
||||
|
||||
private String name;
|
||||
private KeyCode keyCode;
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public ControlKey(String name, KeyCode keyCode){
|
||||
this.name = name;
|
||||
this.keyCode = keyCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* To String method
|
||||
* @return returns the name of the key
|
||||
*/
|
||||
public String toString(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* What this key should do when the command is issued for it to do its job.
|
||||
*/
|
||||
public abstract void onAction();//may want to make it take in a visualiser and stuff in the future.
|
||||
|
||||
/**
|
||||
* What to do when the key is held
|
||||
*/
|
||||
public abstract void onHold();
|
||||
|
||||
/**
|
||||
* What to do when the key is released.
|
||||
*/
|
||||
public abstract void onRelease();
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to send downwind packet to server
|
||||
*/
|
||||
public class DownWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public DownWindKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Factory for creating Keys, these could be predefined in the future.
|
||||
*/
|
||||
public class KeyFactory {
|
||||
|
||||
/**
|
||||
* Get the Control Key incharge of a key press
|
||||
* @param key key pressed (String value of KeyCode)
|
||||
* @return the Control Key behaviour of the key pressed.
|
||||
*/
|
||||
public static ControlKey getKey(String key){
|
||||
switch(key){
|
||||
case "Z":
|
||||
return new ZoomInKey("Z", KeyCode.Z);
|
||||
case "X":
|
||||
return new ZoomOutKey("X", KeyCode.X);
|
||||
case "SPACE":
|
||||
return new VMGKey("SPACE", KeyCode.SPACE);
|
||||
case "SHIFT":
|
||||
return new SailsToggleKey("SHIFT", KeyCode.SHIFT);
|
||||
case "ENTER":
|
||||
return new TackGybeKey("ENTER", KeyCode.ENTER);
|
||||
case "PAGE_UP":
|
||||
return new UpWindKey("PAGE_UP", KeyCode.PAGE_UP);
|
||||
case "PAGE_DOWN":
|
||||
return new DownWindKey("PAGE_DOWN", KeyCode.PAGE_DOWN);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to toggle the sails
|
||||
*/
|
||||
public class SailsToggleKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public SailsToggleKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* key to toggle between tacking and gybing
|
||||
*/
|
||||
public class TackGybeKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public TackGybeKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to go upwind
|
||||
*/
|
||||
public class UpWindKey extends ControlKey {
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public UpWindKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to trigger auto VMG
|
||||
*/
|
||||
public class VMGKey extends ControlKey{
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public VMGKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* key to zoom into the game
|
||||
*/
|
||||
public class ZoomInKey extends ControlKey {
|
||||
|
||||
public ZoomInKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package visualiser.gameController.Keys;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
/**
|
||||
* Key to zoom out of the game.
|
||||
*/
|
||||
public class ZoomOutKey extends ControlKey{
|
||||
|
||||
/**
|
||||
* Constructor for Control
|
||||
*
|
||||
* @param name name of the key
|
||||
* @param keyCode key code for the key
|
||||
*/
|
||||
public ZoomOutKey(String name, KeyCode keyCode) {
|
||||
super(name, keyCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHold() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRelease() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package visualiser.gameController;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import visualiser.gameController.InputChecker;
|
||||
|
||||
/**
|
||||
* Start to manually test the game controller
|
||||
*/
|
||||
public class GameControllerManualTest extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
||||
@Override
|
||||
public void handle(WindowEvent event) {
|
||||
Platform.exit();
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
GridPane root = new GridPane();
|
||||
Scene scene = new Scene(root, 1200, 800);
|
||||
|
||||
InputChecker inputChecker = new InputChecker();
|
||||
inputChecker.runWithScene(scene);
|
||||
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("RaceVision - Team 7 - Input Tester Manual Test");
|
||||
stage.show();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue