Skip to content

Commit

Permalink
Merge pull request #75 from Spindler-ICS4UC-1-1819/host-button-discon…
Browse files Browse the repository at this point in the history
…nect-disable

Fixed issue with host-disconnect-host
  • Loading branch information
NicolasHawrysh authored Jan 20, 2019
2 parents 3ad5acc + f1d0f9a commit 9573c48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
23 changes: 16 additions & 7 deletions ICS4UC_RST/src/menu/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class MainMenu extends Application {
public MainMenu() {
currentInstance = this;
PartyHandler.setIncomingMessageListener(this::messageReceived);
PartyHandler.setOnReceiverClosed(this::receiverClosed);

StringBuilder builder = new StringBuilder(MENU_HELP_TEXT);
for (Game game : games) {
Expand All @@ -147,6 +148,18 @@ public MainMenu() {
helpText = builder.toString();
}

/**
* Called when the party's receiving thread is closed.
*/
private void receiverClosed() {
hostMenuItem.setDisable(false);
hostMenuItem.setConnected(false);
connectMenuItem.setConnected(false);
connectMenuItem.setDisable(false);
hostMenuItem.setActive(false);
connectMenuItem.setActive(false);
}

/**
* Gets the current instance of the main menu.
*
Expand Down Expand Up @@ -406,18 +419,14 @@ private void disconnect(boolean notifyOtherClient) {
connectTask.cancel(true);
}

if (notifyOtherClient) {
if (notifyOtherClient && PartyHandler.isConnected()) {
NetworkMessage message = new NetworkMessage(HostStatus.DISCONNECTING);
sendNetworkMessage(message);
hostMenuItem.setDisable(true);
} else {
PartyHandler.disconnect();
receiverClosed();
}
hostMenuItem.setConnected(false);
connectMenuItem.setConnected(false);
hostMenuItem.setDisable(false);
connectMenuItem.setDisable(false);
hostMenuItem.setActive(false);
connectMenuItem.setActive(false);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions ICS4UC_RST/src/network/party/PartyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PartyHandler {
private static ReceiverTask incomingTask;
private static SenderTask outgoingTask;
private static Consumer<ReceivedDataEvent> incomingListener;
private static Runnable receiverClosedListener;

/**
* Begins a party session with the user at the given IP address.
Expand Down Expand Up @@ -230,6 +231,18 @@ public static ExecutorService createFixedTimeoutExecutorService(
*/
private static void receiverClosed() {
disconnect();
if (receiverClosedListener != null) {
receiverClosedListener.run();
}
}

/**
* Sets an action to be performed when the receiving thread is closed.
*
* @param action The action to run.
*/
public static void setOnReceiverClosed(Runnable action) {
receiverClosedListener = action;
}

/**
Expand Down

0 comments on commit 9573c48

Please sign in to comment.