Development #34
@@ -291,5 +291,12 @@ public static void restartApp() {
|
|||||||
throw new RuntimeException("restart failed");
|
throw new RuntimeException("restart failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void afterGameCleanup() {
|
||||||
|
MainView main = (MainView) mainView;
|
||||||
|
|
||||||
|
main.getJoinDialog().disconnect();
|
||||||
|
main.getHostDialog().shutdownServer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ private void handleLobby(Notification notification) {
|
|||||||
if (notification instanceof TskSelectNotification n) {
|
if (notification instanceof TskSelectNotification n) {
|
||||||
lobbyView.setTaken(n.getColor(), true, n.isSelf(), n.getName());
|
lobbyView.setTaken(n.getColor(), true, n.isSelf(), n.getName());
|
||||||
} else if (notification instanceof StartDialogNotification) {
|
} else if (notification instanceof StartDialogNotification) {
|
||||||
|
app.afterGameCleanup();
|
||||||
app.enter(MdgaState.MAIN);
|
app.enter(MdgaState.MAIN);
|
||||||
} else if (notification instanceof TskUnselectNotification n) {
|
} else if (notification instanceof TskUnselectNotification n) {
|
||||||
lobbyView.setTaken(n.getColor(), false, false, null);
|
lobbyView.setTaken(n.getColor(), false, false, null);
|
||||||
@@ -170,6 +171,7 @@ private void handleGame(Notification notification) {
|
|||||||
} else if (notification instanceof ShieldSuppressedNotification n) {
|
} else if (notification instanceof ShieldSuppressedNotification n) {
|
||||||
boardHandler.suppressShield(n.getPieceId());
|
boardHandler.suppressShield(n.getPieceId());
|
||||||
} else if (notification instanceof StartDialogNotification) {
|
} else if (notification instanceof StartDialogNotification) {
|
||||||
|
app.afterGameCleanup();
|
||||||
app.enter(MdgaState.MAIN);
|
app.enter(MdgaState.MAIN);
|
||||||
} else if (notification instanceof SwapPieceNotification n) {
|
} else if (notification instanceof SwapPieceNotification n) {
|
||||||
// boardHandler.swapPieces(n.getFirstPiece(), n.getSecondPiece());
|
// boardHandler.swapPieces(n.getFirstPiece(), n.getSecondPiece());
|
||||||
@@ -196,6 +198,7 @@ private void handleGame(Notification notification) {
|
|||||||
|
|
||||||
private void handleCeremony(Notification notification) {
|
private void handleCeremony(Notification notification) {
|
||||||
if (notification instanceof StartDialogNotification) {
|
if (notification instanceof StartDialogNotification) {
|
||||||
|
app.afterGameCleanup();
|
||||||
app.enter(MdgaState.MAIN);
|
app.enter(MdgaState.MAIN);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("notification not expected in ceremony: " + notification.getClass().getName());
|
throw new RuntimeException("notification not expected in ceremony: " + notification.getClass().getName());
|
||||||
|
|||||||
@@ -92,4 +92,16 @@ public void resetPort() {
|
|||||||
public void connectToServer() {
|
public void connectToServer() {
|
||||||
connectServer();
|
connectServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void disconnect() {
|
||||||
|
NetworkSupport network = getNetwork();
|
||||||
|
if (network != null) {
|
||||||
|
try {
|
||||||
|
network.disconnect();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error while disconnecting: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ public abstract class NetworkDialog extends Dialog {
|
|||||||
private String hostname;
|
private String hostname;
|
||||||
private int portNumber;
|
private int portNumber;
|
||||||
private Future<Object> connectionFuture;
|
private Future<Object> connectionFuture;
|
||||||
|
private MdgaServer serverInstance;
|
||||||
|
private Thread serverThread;
|
||||||
|
|
||||||
public NetworkDialog(MdgaApp app, Node node, NetworkSupport network) {
|
public NetworkDialog(MdgaApp app, Node node, NetworkSupport network) {
|
||||||
super(app, node);
|
super(app, node);
|
||||||
@@ -28,7 +30,6 @@ public void setPortNumber(int portNumber) {
|
|||||||
this.portNumber = portNumber;
|
this.portNumber = portNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected Object initNetwork() {
|
protected Object initNetwork() {
|
||||||
try {
|
try {
|
||||||
this.network.initNetwork(this.hostname, this.portNumber);
|
this.network.initNetwork(this.hostname, this.portNumber);
|
||||||
@@ -39,25 +40,49 @@ protected Object initNetwork() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void connectServer() {
|
protected void connectServer() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connectionFuture = this.network.getApp().getExecutor().submit(this::initNetwork);
|
connectionFuture = this.network.getApp().getExecutor().submit(this::initNetwork);
|
||||||
} catch (NumberFormatException var2) {
|
} catch (NumberFormatException var2) {
|
||||||
throw new NumberFormatException("Port must be a number");
|
throw new NumberFormatException("Port must be a number");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void startServer() {
|
protected void startServer() {
|
||||||
(new Thread(() -> {
|
serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
MdgaServer mdgaServer = new MdgaServer(portNumber);
|
serverInstance = new MdgaServer(portNumber);
|
||||||
mdgaServer.run();
|
serverInstance.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
})).start();
|
serverThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdownServer() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
System.err.println("Thread was interrupted: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverInstance != null) {
|
||||||
|
serverInstance.shutdown();
|
||||||
|
serverInstance = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverThread != null && serverThread.isAlive()) {
|
||||||
|
serverThread.interrupt();
|
||||||
|
try {
|
||||||
|
serverThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
serverThread = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(float delta) {
|
public void update(float delta) {
|
||||||
@@ -65,14 +90,14 @@ public void update(float delta) {
|
|||||||
try {
|
try {
|
||||||
this.connectionFuture.get();
|
this.connectionFuture.get();
|
||||||
} catch (ExecutionException ignored) {
|
} catch (ExecutionException ignored) {
|
||||||
// todo: implement
|
// TODO: implement
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public NetworkSupport getNetwork(){
|
public NetworkSupport getNetwork() {
|
||||||
return network;
|
return network;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -231,5 +231,13 @@ public void back() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JoinDialog getJoinDialog() {
|
||||||
|
return joinDialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HostDialog getHostDialog() {
|
||||||
|
return hostDialog;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user