Merge pull request #40 from PM2-IT21bWIN-ruiz-mach-krea/Refactoring_ClientConnectionHandler

Refactoring client connection handler
This commit is contained in:
fassband 2022-04-14 22:24:13 +02:00 committed by GitHub Enterprise
commit 2a8b701f48
5 changed files with 23 additions and 31 deletions

View File

@ -47,6 +47,10 @@ public class ChatWindowController {
messageListener(); messageListener();
} }
public void setConnectionHandler(ClientConnectionHandler connectionHandler){
this.connectionHandler = connectionHandler;
}
private void applicationClose() { private void applicationClose() {
disconnect(); disconnect();
} }
@ -108,9 +112,7 @@ public class ChatWindowController {
if(!userName.contains(" ")) { if(!userName.contains(" ")) {
String serverAddress = serverAddressField.getText(); String serverAddress = serverAddressField.getText();
int serverPort = Integer.parseInt(serverPortField.getText()); int serverPort = Integer.parseInt(serverPortField.getText());
connectionHandler = new ClientConnectionHandler( connectionHandler.initialize(serverAddress, serverPort, userName);
NetworkHandler.openConnection(serverAddress, serverPort), userName,
messages);
new Thread(connectionHandler).start(); new Thread(connectionHandler).start();
//register Listener //register Listener
@ -123,16 +125,7 @@ public class ChatWindowController {
} }
} }
private void terminateConnectionHandler() { public void stateChanged(State newState) {
// unregister window close handler
rootPane.getScene().getWindow().removeEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
if (connectionHandler != null) {
connectionHandler.stopReceiving();
connectionHandler = null;
}
}
public void stateChanged(ConnectionHandler.State newState) {
// update UI (need to be run in UI thread: see Platform.runLater()) // update UI (need to be run in UI thread: see Platform.runLater())
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
@ -140,8 +133,8 @@ public class ChatWindowController {
connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect"); connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect");
} }
}); });
if (newState == DISCONNECTED) { if(newState == DISCONNECTED){
terminateConnectionHandler(); connectionHandler.stopReceiving();
} }
} }

View File

@ -25,17 +25,20 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
private SimpleStringProperty serverAddress; private SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort; private SimpleIntegerProperty serverPort;
public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection, public ClientConnectionHandler(ClientMessageList messages) {
String userName, super();
ClientMessageList messages) {
super(connection);
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
this.messages = messages; this.messages = messages;
state = new SimpleObjectProperty<>(State.NEW); state = new SimpleObjectProperty<>(State.NEW);
serverAddress = new SimpleStringProperty(); serverAddress = new SimpleStringProperty();
serverPort = new SimpleIntegerProperty(); serverPort = new SimpleIntegerProperty();
} }
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
state = new SimpleObjectProperty<>(NEW);
setConnection(NetworkHandler.openConnection(serverAddress, serverPort));
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
}
public SimpleStringProperty getServerAddressProperty() { return serverAddress; } public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
public SimpleIntegerProperty getServerPortProperty() { return serverPort; } public SimpleIntegerProperty getServerPortProperty() { return serverPort; }

View File

@ -8,6 +8,7 @@ import javafx.stage.Stage;
public class ClientUI extends Application { public class ClientUI extends Application {
private ClientMessageList clientMessageList = new ClientMessageList(); private ClientMessageList clientMessageList = new ClientMessageList();
private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
@ -21,6 +22,7 @@ public class ClientUI extends Application {
ChatWindowController chatWindowController = loader.getController(); ChatWindowController chatWindowController = loader.getController();
chatWindowController.setMessages(clientMessageList); chatWindowController.setMessages(clientMessageList);
chatWindowController.setConnectionHandler(connectionHandler);
// fill in scene and stage setup // fill in scene and stage setup
Scene scene = new Scene(rootPane); Scene scene = new Scene(rootPane);

View File

@ -17,10 +17,6 @@ public abstract class ConnectionHandler {
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
} }
public ConnectionHandler(NetworkHandler.NetworkConnection<String> connection) {
this.connection = connection;
}
public static String getDataTypeConnect() { public static String getDataTypeConnect() {
return DATA_TYPE_CONNECT; return DATA_TYPE_CONNECT;
} }
@ -45,7 +41,7 @@ public abstract class ConnectionHandler {
return connection; return connection;
} }
protected void setConnection() { protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
this.connection = connection; this.connection = connection;
} }
} }

View File

@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.server;
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler; import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import java.io.EOFException; 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.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
public class ServerConnectionHandler extends ConnectionHandler implements Runnable{ public class ServerConnectionHandler extends ConnectionHandler implements Runnable{
private static final AtomicInteger connectionCounter = new AtomicInteger(0); private static final AtomicInteger connectionCounter = new AtomicInteger(0);
@ -34,13 +35,10 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
startReceiving(); startReceiving();
} }
enum State {
NEW, CONNECTED, DISCONNECTED;
}
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection, public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) { Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
super(connection); super();
setConnection(connection);
Objects.requireNonNull(connection, "Connection must not be null"); Objects.requireNonNull(connection, "Connection must not be null");
Objects.requireNonNull(registry, "Registry must not be null"); Objects.requireNonNull(registry, "Registry must not be null");
this.connectionRegistry = registry; this.connectionRegistry = registry;