From 1777b6258204ec82fe977bef559e7d70440f6074 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 13 Apr 2022 15:03:07 +0200 Subject: [PATCH] Make State in ClientConnectionHandler property Add change listener for stateproperty in ChatWindowController fixed Issue #21 --- .../client/ChatWindowController.java | 16 ++++++++++++- .../client/ClientConnectionHandler.java | 24 ++++++++++--------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java index 9eac20a..9b9606e 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java @@ -4,6 +4,8 @@ import ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import javafx.application.Platform; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -52,7 +54,7 @@ public class ChatWindowController { @FXML private void toggleConnection () { - if (connectionHandler == null || connectionHandler.getState() != CONNECTED) { + if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) { connect(); } else { disconnect(); @@ -112,6 +114,9 @@ public class ChatWindowController { this); new Thread(connectionHandler).start(); + //register Listener + startListener(); + // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); } @@ -192,4 +197,13 @@ public class ChatWindowController { } + public void startListener() { + connectionHandler.getStateProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, State oldValue, State newValue) { + stateChanged(newValue); + } + }); + } + } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java index 22e4ce2..e8a0881 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java @@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.client; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.NetworkHandler; +import javafx.beans.property.SimpleObjectProperty; import java.io.EOFException; import java.io.IOException; @@ -29,7 +30,7 @@ public class ClientConnectionHandler implements Runnable { private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); private String userName = USER_NONE; - private State state = NEW; + private SimpleObjectProperty state; enum State { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; @@ -41,15 +42,16 @@ public class ClientConnectionHandler implements Runnable { this.connection = connection; this.userName = (userName == null || userName.isBlank())? USER_NONE : userName; this.controller = controller; + state = new SimpleObjectProperty<>(NEW); } - public State getState() { + public SimpleObjectProperty getStateProperty() { return this.state; } public void setState (State newState) { - this.state = newState; - controller.stateChanged(newState); + state.set(newState); + } public void run () { @@ -123,7 +125,7 @@ public class ClientConnectionHandler implements Runnable { if (type.equals(DATA_TYPE_CONNECT)) { System.err.println("Illegal connect request from server"); } else if (type.equals(DATA_TYPE_CONFIRM)) { - if (state == CONFIRM_CONNECT) { + if (state.get() == CONFIRM_CONNECT) { this.userName = reciever; controller.setUserName(userName); controller.setServerPort(connection.getRemotePort()); @@ -131,7 +133,7 @@ public class ClientConnectionHandler implements Runnable { controller.addInfo(payload); System.out.println("CONFIRM: " + payload); this.setState(CONNECTED); - } else if (state == CONFIRM_DISCONNECT) { + } else if (state.get() == CONFIRM_DISCONNECT) { controller.addInfo(payload); System.out.println("CONFIRM: " + payload); this.setState(DISCONNECTED); @@ -139,7 +141,7 @@ public class ClientConnectionHandler implements Runnable { System.err.println("Got unexpected confirm message: " + payload); } } else if (type.equals(DATA_TYPE_DISCONNECT)) { - if (state == DISCONNECTED) { + if (state.get() == DISCONNECTED) { System.out.println("DISCONNECT: Already in disconnected: " + payload); return; } @@ -147,7 +149,7 @@ public class ClientConnectionHandler implements Runnable { System.out.println("DISCONNECT: " + payload); this.setState(DISCONNECTED); } else if (type.equals(DATA_TYPE_MESSAGE)) { - if (state != CONNECTED) { + if (state.get() != CONNECTED) { System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload); return; } @@ -187,19 +189,19 @@ public class ClientConnectionHandler implements Runnable { } public void connect() throws ChatProtocolException { - if (state != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); + if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null); this.setState(CONFIRM_CONNECT); } public void disconnect() throws ChatProtocolException { - if (state != NEW && state != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state); + if (state.get() != NEW && state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state); this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null); this.setState(CONFIRM_DISCONNECT); } public boolean message(String messageString) throws ChatProtocolException { - if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); + if (state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); Matcher matcher = messagePattern.matcher(messageString); if (matcher.find()) {