Fixed most of issue #19 with new master?
This commit is contained in:
@@ -37,6 +37,8 @@ public class ConnectionAcceptor implements Runnable {
|
|||||||
*/
|
*/
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
|
private Socket mockSocket = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of client connections.
|
* List of client connections.
|
||||||
@@ -124,7 +126,9 @@ public class ConnectionAcceptor implements Runnable {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket mockSocket = serverSocket.accept();
|
try {
|
||||||
|
this.mockSocket = serverSocket.accept();
|
||||||
|
} catch (Exception e){}
|
||||||
|
|
||||||
Logger.getGlobal().log(Level.INFO, String.format("Client connected. client ip/port = %s. Local ip/port = %s.", mockSocket.getRemoteSocketAddress(), mockSocket.getLocalSocketAddress()));
|
Logger.getGlobal().log(Level.INFO, String.format("Client connected. client ip/port = %s. Local ip/port = %s.", mockSocket.getRemoteSocketAddress(), mockSocket.getLocalSocketAddress()));
|
||||||
|
|
||||||
|
|||||||
@@ -103,6 +103,8 @@ public class MockOutput implements RunnableWithFramePeriod {
|
|||||||
Logger.getGlobal().log(Level.WARNING, "MockOutput.run() interrupted while putting message in queue.", e);
|
Logger.getGlobal().log(Level.WARNING, "MockOutput.run() interrupted while putting message in queue.", e);
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
return;
|
return;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ public class HeartBeatService implements RunnableWithFramePeriod {
|
|||||||
* Puts a HeartBeat message on the message queue.
|
* Puts a HeartBeat message on the message queue.
|
||||||
* @throws InterruptedException Thrown if the thread is interrupted.
|
* @throws InterruptedException Thrown if the thread is interrupted.
|
||||||
*/
|
*/
|
||||||
private void sendHeartBeat() throws InterruptedException {
|
private void sendHeartBeat() throws Exception {
|
||||||
|
|
||||||
HeartBeat heartBeat = createHeartbeatMessage();
|
HeartBeat heartBeat = createHeartbeatMessage();
|
||||||
|
|
||||||
@@ -92,13 +92,17 @@ public class HeartBeatService implements RunnableWithFramePeriod {
|
|||||||
|
|
||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
long currentFrameTime = System.currentTimeMillis();
|
long currentFrameTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
waitForFramePeriod(lastHeartbeatTime, currentFrameTime, heartbeatPeriod);
|
waitForFramePeriod(lastHeartbeatTime, currentFrameTime, heartbeatPeriod);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
lastHeartbeatTime = currentFrameTime;
|
lastHeartbeatTime = currentFrameTime;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sendHeartBeat();
|
sendHeartBeat();
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (Exception e) {
|
||||||
Logger.getGlobal().log(Level.WARNING, "HeartBeatService: " + this + " sendHeartBeat() was interrupted on thread: " + Thread.currentThread(), e);
|
Logger.getGlobal().log(Level.WARNING, "HeartBeatService: " + this + " sendHeartBeat() was interrupted on thread: " + Thread.currentThread(), e);
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -43,9 +43,17 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
|||||||
public void run() {
|
public void run() {
|
||||||
race.initialiseBoats();
|
race.initialiseBoats();
|
||||||
|
|
||||||
|
try {
|
||||||
countdown();
|
countdown();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
raceLoop();
|
raceLoop();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void boolFalse(){
|
public void boolFalse(){
|
||||||
@@ -56,7 +64,7 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
|||||||
/**
|
/**
|
||||||
* Countdown timer until race starts.
|
* Countdown timer until race starts.
|
||||||
*/
|
*/
|
||||||
private void countdown() {
|
private void countdown() throws Exception {
|
||||||
|
|
||||||
long previousFrameTime = System.currentTimeMillis();
|
long previousFrameTime = System.currentTimeMillis();
|
||||||
|
|
||||||
@@ -95,7 +103,7 @@ public class RaceLogic implements RunnableWithFramePeriod {
|
|||||||
/**
|
/**
|
||||||
* Timer that runs for the duration of the race, until all boats finish.
|
* Timer that runs for the duration of the race, until all boats finish.
|
||||||
*/
|
*/
|
||||||
private void raceLoop() {
|
private void raceLoop() throws Exception {
|
||||||
|
|
||||||
long previousFrameTime = System.currentTimeMillis();
|
long previousFrameTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,11 @@ public class MessageSerialiser implements RunnableWithFramePeriod {
|
|||||||
|
|
||||||
|
|
||||||
long currentFrameTime = System.currentTimeMillis();
|
long currentFrameTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
previousFrameTime = currentFrameTime;
|
previousFrameTime = currentFrameTime;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public interface RunnableWithFramePeriod extends Runnable {
|
|||||||
* @param currentFrameTime The timestamp of the current frame.
|
* @param currentFrameTime The timestamp of the current frame.
|
||||||
* @param minimumFramePeriod The minimum period the frame must be.
|
* @param minimumFramePeriod The minimum period the frame must be.
|
||||||
*/
|
*/
|
||||||
default void waitForFramePeriod(long previousFrameTime, long currentFrameTime, long minimumFramePeriod) {
|
default void waitForFramePeriod(long previousFrameTime, long currentFrameTime, long minimumFramePeriod) throws Exception {
|
||||||
|
|
||||||
|
|
||||||
//This is the time elapsed, in milliseconds, since the last server "frame".
|
//This is the time elapsed, in milliseconds, since the last server "frame".
|
||||||
@@ -39,10 +39,10 @@ public interface RunnableWithFramePeriod extends Runnable {
|
|||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
//If we get interrupted, exit the function.
|
//If we get interrupted, exit the function.
|
||||||
Logger.getGlobal().log(Level.SEVERE, "RunnableWithFramePeriod.waitForFramePeriod().sleep(framePeriod) was interrupted on thread: " + Thread.currentThread(), e);
|
/*Logger.getGlobal().log(Level.SEVERE, "RunnableWithFramePeriod.waitForFramePeriod().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;*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public class ConnectionController extends Controller {
|
|||||||
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
||||||
socket.setKeepAlive(true);
|
socket.setKeepAlive(true);
|
||||||
connectionWrapper.setVisible(false);
|
connectionWrapper.setVisible(false);
|
||||||
parent.enterLobby(socket);
|
//parent.enterLobby(socket);
|
||||||
} catch (IOException e) { /* Never reached */ }
|
} catch (IOException e) { /* Never reached */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class HostController extends Controller {
|
|||||||
try{
|
try{
|
||||||
Socket socket = new Socket(address, port);
|
Socket socket = new Socket(address, port);
|
||||||
hostWrapper.setVisible(false);
|
hostWrapper.setVisible(false);
|
||||||
parent.enterLobby(socket);
|
parent.enterLobby(socket, true);
|
||||||
} catch (IOException e) { /* Never reached */ }
|
} catch (IOException e) { /* Never reached */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ public class LobbyController extends Controller {
|
|||||||
RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem();
|
RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem();
|
||||||
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
||||||
lobbyWrapper.setVisible(false);
|
lobbyWrapper.setVisible(false);
|
||||||
parent.enterLobby(socket);
|
parent.enterLobby(socket, false);
|
||||||
} catch (IOException e) { /* Never reached */
|
} catch (IOException e) { /* Never reached */
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ public class MainController extends Controller {
|
|||||||
* @param visualiserRace The object modelling the race.
|
* @param visualiserRace The object modelling the race.
|
||||||
* @param controllerClient Socket Client that manipulates the controller.
|
* @param controllerClient Socket Client that manipulates the controller.
|
||||||
*/
|
*/
|
||||||
public void beginRace(VisualiserRaceEvent visualiserRace, ControllerClient controllerClient) {
|
public void beginRace(VisualiserRaceEvent visualiserRace, ControllerClient controllerClient, Boolean isHost) {
|
||||||
raceController.startRace(visualiserRace, controllerClient);
|
raceController.startRace(visualiserRace, controllerClient, isHost);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endEvent() throws IOException { hostController.endEvent(); }
|
public void endEvent() throws IOException { hostController.endEvent(); }
|
||||||
@@ -51,8 +51,8 @@ public class MainController extends Controller {
|
|||||||
* Transitions from the server selection screen to the pre-race lobby for a given server.
|
* Transitions from the server selection screen to the pre-race lobby for a given server.
|
||||||
* @param socket The server to read data from.
|
* @param socket The server to read data from.
|
||||||
*/
|
*/
|
||||||
public void enterLobby(Socket socket) {
|
public void enterLobby(Socket socket, Boolean isHost) {
|
||||||
startController.enterLobby(socket);
|
startController.enterLobby(socket, isHost);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,11 +67,7 @@ public class MainController extends Controller {
|
|||||||
* Transitions into the title screen
|
* Transitions into the title screen
|
||||||
*/
|
*/
|
||||||
public void enterTitle() {
|
public void enterTitle() {
|
||||||
try {
|
titleController.enterTitle();
|
||||||
App.loadStart(App.getStage());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import javafx.scene.layout.StackPane;
|
|||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
import network.Messages.Enums.RaceStatusEnum;
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
import shared.model.Leg;
|
import shared.model.Leg;
|
||||||
|
import visualiser.app.App;
|
||||||
import visualiser.gameController.ControllerClient;
|
import visualiser.gameController.ControllerClient;
|
||||||
import visualiser.gameController.Keys.ControlKey;
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
import visualiser.gameController.Keys.KeyFactory;
|
import visualiser.gameController.Keys.KeyFactory;
|
||||||
@@ -28,6 +29,7 @@ import visualiser.network.ServerConnection;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -52,7 +54,7 @@ public class RaceController extends Controller {
|
|||||||
private ControllerClient controllerClient;
|
private ControllerClient controllerClient;
|
||||||
|
|
||||||
|
|
||||||
|
private boolean isHost;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The canvas that draws the race.
|
* The canvas that draws the race.
|
||||||
@@ -124,13 +126,31 @@ public class RaceController extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(event.getCode() == KeyCode.ESCAPE) {
|
if(event.getCode() == KeyCode.ESCAPE) {
|
||||||
race.setVisible(false);
|
|
||||||
try {
|
try {
|
||||||
|
if (isHost) {
|
||||||
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
|
alert.setTitle("Exit Race");
|
||||||
|
alert.setContentText("Do you wish to quit the race? You are the host");
|
||||||
|
Optional<ButtonType> result = alert.showAndWait();
|
||||||
|
if (result.get() == ButtonType.OK) {
|
||||||
parent.endEvent();
|
parent.endEvent();
|
||||||
|
race.setVisible(false);
|
||||||
|
App.app.loadStart(App.getStage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
|
alert.setTitle("Exit Race");
|
||||||
|
alert.setContentText("Do you wish to quit the race?");
|
||||||
|
Optional<ButtonType> result = alert.showAndWait();
|
||||||
|
if (result.get() == ButtonType.OK) {
|
||||||
|
race.setVisible(false);
|
||||||
|
App.app.loadStart(App.getStage());
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
parent.enterTitle();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -371,10 +391,11 @@ public class RaceController extends Controller {
|
|||||||
* @param visualiserRace Object modelling the race.
|
* @param visualiserRace Object modelling the race.
|
||||||
* @param controllerClient Socket Client that manipulates the controller.
|
* @param controllerClient Socket Client that manipulates the controller.
|
||||||
*/
|
*/
|
||||||
public void startRace(VisualiserRaceEvent visualiserRace, ControllerClient controllerClient) {
|
public void startRace(VisualiserRaceEvent visualiserRace, ControllerClient controllerClient, Boolean isHost) {
|
||||||
|
|
||||||
this.visualiserRace = visualiserRace;
|
this.visualiserRace = visualiserRace;
|
||||||
this.controllerClient = controllerClient;
|
this.controllerClient = controllerClient;
|
||||||
|
this.isHost = isHost;
|
||||||
|
|
||||||
initialiseRace();
|
initialiseRace();
|
||||||
|
|
||||||
@@ -438,7 +459,12 @@ public class RaceController extends Controller {
|
|||||||
//Return to main screen if we lose connection.
|
//Return to main screen if we lose connection.
|
||||||
if (!visualiserRace.getServerConnection().isAlive()) {
|
if (!visualiserRace.getServerConnection().isAlive()) {
|
||||||
race.setVisible(false);
|
race.setVisible(false);
|
||||||
parent.enterTitle();
|
//parent.enterTitle();
|
||||||
|
try {
|
||||||
|
App.app.loadStart(App.getStage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
//TODO currently this doesn't work correctly - the title screen remains visible after clicking join game
|
//TODO currently this doesn't work correctly - the title screen remains visible after clicking join game
|
||||||
//TODO we should display an error to the user
|
//TODO we should display an error to the user
|
||||||
//TODO also need to "reset" any state (race, connections, etc...).
|
//TODO also need to "reset" any state (race, connections, etc...).
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class StartController extends Controller {
|
|||||||
*/
|
*/
|
||||||
private ControllerClient controllerClient;
|
private ControllerClient controllerClient;
|
||||||
|
|
||||||
|
private boolean isHost;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ public class StartController extends Controller {
|
|||||||
startWrapper.setVisible(false);
|
startWrapper.setVisible(false);
|
||||||
//start.setVisible(false);//TODO is this needed?
|
//start.setVisible(false);//TODO is this needed?
|
||||||
|
|
||||||
parent.beginRace(visualiserRaceEvent, controllerClient);
|
parent.beginRace(visualiserRaceEvent, controllerClient, isHost);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,10 +250,13 @@ public class StartController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Show starting information for a race given a socket.
|
* Show starting information for a race given a socket.
|
||||||
* @param socket network source of information
|
* @param socket network source of information
|
||||||
|
* @param isHost
|
||||||
*/
|
*/
|
||||||
public void enterLobby(Socket socket) {
|
public void enterLobby(Socket socket, Boolean isHost) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
this.isHost = isHost;
|
||||||
|
|
||||||
this.visualiserRaceEvent = new VisualiserRaceEvent(socket, RequestToJoinEnum.PARTICIPANT);
|
this.visualiserRaceEvent = new VisualiserRaceEvent(socket, RequestToJoinEnum.PARTICIPANT);
|
||||||
|
|
||||||
this.controllerClient = visualiserRaceEvent.getControllerClient();
|
this.controllerClient = visualiserRaceEvent.getControllerClient();
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class App extends Application {
|
public class App extends Application {
|
||||||
private static Stage stage;
|
private static Stage stage;
|
||||||
private static App app;
|
public static App app;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entry point for running the programme
|
* Entry point for running the programme
|
||||||
@@ -45,7 +45,7 @@ public class App extends Application {
|
|||||||
loadStart(App.stage);
|
loadStart(App.stage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadStart(Stage stage) throws IOException {
|
public void loadStart(Stage stage) throws IOException {
|
||||||
FXMLLoader loader = new FXMLLoader(App.class.getResource("/visualiser/scenes/main.fxml"));
|
FXMLLoader loader = new FXMLLoader(App.class.getResource("/visualiser/scenes/main.fxml"));
|
||||||
Parent root = loader.load();
|
Parent root = loader.load();
|
||||||
stage.setResizable(false);
|
stage.setResizable(false);
|
||||||
|
|||||||
@@ -69,7 +69,11 @@ public class VisualiserRaceService implements RunnableWithFramePeriod {
|
|||||||
|
|
||||||
long currentFrameTime = System.currentTimeMillis();
|
long currentFrameTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
try {
|
||||||
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
previousFrameTime = currentFrameTime;
|
previousFrameTime = currentFrameTime;
|
||||||
|
|
||||||
|
|||||||
@@ -330,7 +330,11 @@ public class ServerConnection implements RunnableWithFramePeriod {
|
|||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
|
|
||||||
long currentFrameTime = System.currentTimeMillis();
|
long currentFrameTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
waitForFramePeriod(previousFrameTime, currentFrameTime, 100);
|
waitForFramePeriod(previousFrameTime, currentFrameTime, 100);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
previousFrameTime = currentFrameTime;
|
previousFrameTime = currentFrameTime;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user