From 615b3844e3c2208a578e875515dfc072c50ecc54 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 14 Apr 2022 21:13:36 +0200 Subject: [PATCH 1/3] refactoring of ClientConnectionHandler Instance is now created in ClientUI.java solved #24 and #15 --- .../client/ChatWindowController.java | 21 +++++++------------ .../client/ClientConnectionHandler.java | 14 +++++++------ .../zhaw/pm2/multichat/client/ClientUI.java | 2 ++ 3 files changed, 17 insertions(+), 20 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 6a2f224..f2e9563 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 @@ -48,6 +48,10 @@ public class ChatWindowController { messageListener(); } + public void setConnectionHandler(ClientConnectionHandler connectionHandler){ + this.connectionHandler = connectionHandler; + } + private void applicationClose() { disconnect(); } @@ -108,9 +112,7 @@ public class ChatWindowController { String userName = userNameField.getText(); String serverAddress = serverAddressField.getText(); int serverPort = Integer.parseInt(serverPortField.getText()); - connectionHandler = new ClientConnectionHandler( - NetworkHandler.openConnection(serverAddress, serverPort), userName, - messages); + connectionHandler.initialize(serverAddress, serverPort, userName); new Thread(connectionHandler).start(); //register Listener @@ -120,15 +122,6 @@ public class ChatWindowController { rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); } - private void terminateConnectionHandler() { - // unregister window close handler - rootPane.getScene().getWindow().removeEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); - if (connectionHandler != null) { - connectionHandler.stopReceiving(); - connectionHandler = null; - } - } - public void stateChanged(State newState) { // update UI (need to be run in UI thread: see Platform.runLater()) Platform.runLater(new Runnable() { @@ -137,8 +130,8 @@ public class ChatWindowController { connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect"); } }); - if (newState == DISCONNECTED) { - terminateConnectionHandler(); + if(newState == DISCONNECTED){ + connectionHandler.stopReceiving(); } } 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 9da83da..b79195f 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 @@ -16,7 +16,7 @@ import java.util.regex.Pattern; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; public class ClientConnectionHandler implements Runnable { - private final NetworkHandler.NetworkConnection connection; + private NetworkHandler.NetworkConnection connection; // Data types used for the Chat Protocol private static final String DATA_TYPE_CONNECT = "CONNECT"; @@ -40,17 +40,19 @@ public class ClientConnectionHandler implements Runnable { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; } - public ClientConnectionHandler(NetworkHandler.NetworkConnection connection, - String userName, - ClientMessageList messages) { - this.connection = connection; - this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName); + public ClientConnectionHandler(ClientMessageList messages) { this.messages = messages; state = new SimpleObjectProperty<>(NEW); serverAddress = new SimpleStringProperty(); serverPort = new SimpleIntegerProperty(); } + public void initialize(String serverAddress, int serverPort, String userName) throws IOException { + state = new SimpleObjectProperty<>(NEW); + this.connection = NetworkHandler.openConnection(serverAddress, serverPort); + this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName); + } + public SimpleStringProperty getServerAddressProperty() { return serverAddress; } public SimpleIntegerProperty getServerPortProperty() { return serverPort; } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java index bde3d1d..67a1d96 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java @@ -8,6 +8,7 @@ import javafx.stage.Stage; public class ClientUI extends Application { private ClientMessageList clientMessageList = new ClientMessageList(); + private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList); @Override public void start(Stage primaryStage) { @@ -21,6 +22,7 @@ public class ClientUI extends Application { ChatWindowController chatWindowController = loader.getController(); chatWindowController.setMessages(clientMessageList); + chatWindowController.setConnectionHandler(connectionHandler); // fill in scene and stage setup Scene scene = new Scene(rootPane); From 2277fee73acb77c98a638c4aadff71df8eb973c4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 14 Apr 2022 22:12:35 +0200 Subject: [PATCH 2/3] merging --- .../ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java | 6 +----- .../zhaw/pm2/multichat/server/ServerConnectionHandler.java | 3 ++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java index a0ddd57..f7acd4a 100644 --- a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java +++ b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java @@ -17,10 +17,6 @@ public abstract class ConnectionHandler { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; } - public ConnectionHandler(NetworkHandler.NetworkConnection connection) { - this.connection = connection; - } - public static String getDataTypeConnect() { return DATA_TYPE_CONNECT; } @@ -45,7 +41,7 @@ public abstract class ConnectionHandler { return connection; } - protected void setConnection() { + protected void setConnection(NetworkHandler.NetworkConnection connection) { this.connection = connection; } } diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java index 4e3db14..d3bd33f 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java @@ -40,7 +40,8 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab public ServerConnectionHandler(NetworkHandler.NetworkConnection connection, Map registry, ReentrantLock mutex, Condition nameComplete) { - super(connection); + super(); + setConnection(connection); Objects.requireNonNull(connection, "Connection must not be null"); Objects.requireNonNull(registry, "Registry must not be null"); this.connectionRegistry = registry; From 86aa801b34055d1c1e3c76777c6e4e4ac07037a3 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 14 Apr 2022 22:23:49 +0200 Subject: [PATCH 3/3] removed Enum State from ServerConnectionHandler.java because it's in ConnectionHandler.java now. --- .../zhaw/pm2/multichat/server/ServerConnectionHandler.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java index d3bd33f..c911b7b 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java @@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.server; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.ConnectionHandler; +import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*; import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import java.io.EOFException; @@ -15,7 +16,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; -import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*; + public class ServerConnectionHandler extends ConnectionHandler implements Runnable{ private static final AtomicInteger connectionCounter = new AtomicInteger(0); @@ -34,10 +35,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab startReceiving(); } - enum State { - NEW, CONNECTED, DISCONNECTED; - } - public ServerConnectionHandler(NetworkHandler.NetworkConnection connection, Map registry, ReentrantLock mutex, Condition nameComplete) { super();