Merge branch 'develop' into story1273_minimap

This commit is contained in:
Calum
2017-09-28 10:21:23 +13:00
7 changed files with 301 additions and 213 deletions
@@ -1,5 +1,14 @@
package seng302.visualiser;
import javafx.application.Platform;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import seng302.gameServer.messages.*;
import seng302.model.stream.packets.PacketType;
import seng302.model.stream.packets.StreamPacket;
import seng302.utilities.XMLParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -42,6 +51,8 @@ import seng302.visualiser.controllers.ViewManager;
*/
public class ClientToServerThread implements Runnable {
private boolean isStarted = false;
/**
* Functional interface for receiving packets from client socket.
*/
@@ -117,6 +128,8 @@ public class ClientToServerThread implements Runnable {
* variable is false.
*/
public void run() {
isStarted = true;
int sync1;
int sync2;
// TODO: 14/07/17 wmu16 - Work out how to fix this while loop
@@ -167,8 +180,10 @@ public class ClientToServerThread implements Runnable {
notifyDisconnectListeners("Connection to server was terminated");
closeSocket();
ViewManager.getInstance().goToStartView();
ViewManager.getInstance().showErrorSnackBar("Server rejected connection.");
Platform.runLater(() -> {
ViewManager.getInstance().showErrorSnackBar("Server rejected connection.");
ViewManager.getInstance().goToStartView();
});
}
public void sendCustomizationRequest(CustomizeRequestType reqType, byte[] payload) {
@@ -194,12 +209,6 @@ public class ClientToServerThread implements Runnable {
if (connectionErrorListener != null){
connectionErrorListener.notifyConnectionError(message);
}
try {
this.socket.close();
} catch (IOException e) {
logger.error("Couldn't close socket");
}
}
/**
@@ -390,9 +399,9 @@ public class ClientToServerThread implements Runnable {
}
if (currentByte == -1) {
notifyDisconnectListeners("Cannot read from server.");
closeSocket();
logger.warn("InputStream reach end of stream", 1);
handleConnectionError("Could not connect to server. Server is no longer available.");
closeSocket();
}
return currentByte;
}
@@ -435,4 +444,8 @@ public class ClientToServerThread implements Runnable {
).getBuffer()
);
}
public boolean hasStarted() {
return isStarted;
}
}
@@ -113,7 +113,7 @@ public class GameClient {
getServerThread().setConnectionErrorListener((eMessage) -> {
ViewManager.getInstance().showErrorSnackBar(eMessage);
destroyClientToServerThread();
//destroyClientToServerThread();
});
this.lobbyController = ViewManager.getInstance().goToLobby(true);
@@ -141,18 +141,45 @@ public class GameClient {
server = new MainServerThread();
while (!server.hasStarted()){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
startClientToServerThread(ipAddress, 4942);
} catch (IOException e) {
showConnectionError("Cannot connect to server as host");
}
// Wait for C2S thread
while (!socketThread.hasStarted()){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
socketThread.sendXML(race, serverName, numLegs, maxPlayers, tokensEnabled);
while (regattaData == null){
int triesLeft = 15;
while (regattaData == null && triesLeft > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
triesLeft--;
}
if (triesLeft <= 0){
showConnectionError("Could not launch server");
return null;
}
this.lobbyController = ViewManager.getInstance().goToLobby(false);
@@ -119,6 +119,12 @@ public class ServerCreationController implements Initializable {
.runAsHost("localhost", 4941, serverName.getText(), (int) maxPlayersSlider
.getValue(), mapMaker.getCurrentRacePath(), (int) legsSlider.getValue(), pickupsCheckBox.isSelected());
if (serverDescription == null){
ViewManager.getInstance().getGameClient().getServerThread().closeSocket();
ViewManager.getInstance().getGameClient().stopGame();
return;
}
ViewManager.getInstance().setProperty("serverName", serverDescription.getName());
ViewManager.getInstance().setProperty("mapName", serverDescription.getMapName());
}