Development #34
@@ -291,5 +291,12 @@ public static void restartApp() {
|
||||
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) {
|
||||
lobbyView.setTaken(n.getColor(), true, n.isSelf(), n.getName());
|
||||
} else if (notification instanceof StartDialogNotification) {
|
||||
app.afterGameCleanup();
|
||||
app.enter(MdgaState.MAIN);
|
||||
} else if (notification instanceof TskUnselectNotification n) {
|
||||
lobbyView.setTaken(n.getColor(), false, false, null);
|
||||
@@ -174,6 +175,7 @@ private void handleGame(Notification notification) {
|
||||
} else if (notification instanceof ShieldSuppressedNotification n) {
|
||||
boardHandler.suppressShield(n.getPieceId());
|
||||
} else if (notification instanceof StartDialogNotification) {
|
||||
app.afterGameCleanup();
|
||||
app.enter(MdgaState.MAIN);
|
||||
} else if (notification instanceof SwapPieceNotification n) {
|
||||
// boardHandler.swapPieces(n.getFirstPiece(), n.getSecondPiece());
|
||||
@@ -200,6 +202,7 @@ private void handleGame(Notification notification) {
|
||||
|
||||
private void handleCeremony(Notification notification) {
|
||||
if (notification instanceof StartDialogNotification) {
|
||||
app.afterGameCleanup();
|
||||
app.enter(MdgaState.MAIN);
|
||||
} else {
|
||||
throw new RuntimeException("notification not expected in ceremony: " + notification.getClass().getName());
|
||||
|
||||
@@ -92,4 +92,16 @@ public void resetPort() {
|
||||
public void connectToServer() {
|
||||
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 int portNumber;
|
||||
private Future<Object> connectionFuture;
|
||||
private MdgaServer serverInstance;
|
||||
private Thread serverThread;
|
||||
|
||||
public NetworkDialog(MdgaApp app, Node node, NetworkSupport network) {
|
||||
super(app, node);
|
||||
@@ -28,7 +30,6 @@ public void setPortNumber(int portNumber) {
|
||||
this.portNumber = portNumber;
|
||||
}
|
||||
|
||||
|
||||
protected Object initNetwork() {
|
||||
try {
|
||||
this.network.initNetwork(this.hostname, this.portNumber);
|
||||
@@ -39,25 +40,49 @@ protected Object initNetwork() {
|
||||
}
|
||||
|
||||
protected void connectServer() {
|
||||
|
||||
try {
|
||||
connectionFuture = this.network.getApp().getExecutor().submit(this::initNetwork);
|
||||
} catch (NumberFormatException var2) {
|
||||
throw new NumberFormatException("Port must be a number");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void startServer() {
|
||||
(new Thread(() -> {
|
||||
serverThread = new Thread(() -> {
|
||||
try {
|
||||
MdgaServer mdgaServer = new MdgaServer(portNumber);
|
||||
mdgaServer.run();
|
||||
serverInstance = new MdgaServer(portNumber);
|
||||
serverInstance.run();
|
||||
} catch (Exception 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) {
|
||||
@@ -65,7 +90,7 @@ public void update(float delta) {
|
||||
try {
|
||||
this.connectionFuture.get();
|
||||
} catch (ExecutionException ignored) {
|
||||
// todo: implement
|
||||
// TODO: implement
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
@@ -231,5 +231,13 @@ public void back() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public JoinDialog getJoinDialog() {
|
||||
return joinDialog;
|
||||
}
|
||||
|
||||
public HostDialog getHostDialog() {
|
||||
return hostDialog;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user