Remove ChatWindowController from ClientConnectionHandler

Make ChatWindowController listen for message changes

fixed Issue #23
This commit is contained in:
Andrin Fassbind 2022-04-13 15:55:08 +02:00
parent 1777b62582
commit 3eedf58685
3 changed files with 68 additions and 33 deletions

View File

@ -41,11 +41,11 @@ public class ChatWindowController {
public void initialize() { public void initialize() {
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
stateChanged(NEW);
} }
public void setMessages(ClientMessageList messages) { public void setMessages(ClientMessageList messages) {
this.messages = messages; this.messages = messages;
messageListener();
} }
private void applicationClose() { private void applicationClose() {
@ -64,7 +64,6 @@ public class ChatWindowController {
private void connect() { private void connect() {
try { try {
messages.clear(); // clear message list messages.clear(); // clear message list
redrawMessageList();
startConnectionHandler(); startConnectionHandler();
connectionHandler.connect(); connectionHandler.connect();
} catch(ChatProtocolException | IOException e) { } catch(ChatProtocolException | IOException e) {
@ -111,7 +110,7 @@ public class ChatWindowController {
int serverPort = Integer.parseInt(serverPortField.getText()); int serverPort = Integer.parseInt(serverPortField.getText());
connectionHandler = new ClientConnectionHandler( connectionHandler = new ClientConnectionHandler(
NetworkHandler.openConnection(serverAddress, serverPort), userName, NetworkHandler.openConnection(serverAddress, serverPort), userName,
this); messages);
new Thread(connectionHandler).start(); new Thread(connectionHandler).start();
//register Listener //register Listener
@ -170,19 +169,8 @@ public class ChatWindowController {
}); });
} }
public void addMessage(String sender, String receiver, String message) {
messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, message));
this.redrawMessageList();
}
public void addInfo(String message) {
messages.addMessage(new Message(Message.MessageType.INFO, null, null, message));
this.redrawMessageList();
}
public void addError(String message) { public void addError(String message) {
messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message));
this.redrawMessageList();
} }
private void redrawMessageList() { private void redrawMessageList() {
@ -204,6 +192,36 @@ public class ChatWindowController {
stateChanged(newValue); stateChanged(newValue);
} }
}); });
connectionHandler.getUserNameProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
setUserName(newValue);
}
});
connectionHandler.getServerAddressProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
setServerAddress(newValue);
}
});
connectionHandler.getServerPortProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
setServerPort(newValue.intValue());
}
});
}
private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
redrawMessageList();
}
});
} }
} }

View File

@ -2,7 +2,9 @@ package ch.zhaw.pm2.multichat.client;
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@ -15,7 +17,6 @@ import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
public class ClientConnectionHandler implements Runnable { public class ClientConnectionHandler implements Runnable {
private final NetworkHandler.NetworkConnection<String> connection; private final NetworkHandler.NetworkConnection<String> connection;
private final ChatWindowController controller;
// Data types used for the Chat Protocol // Data types used for the Chat Protocol
private static final String DATA_TYPE_CONNECT = "CONNECT"; private static final String DATA_TYPE_CONNECT = "CONNECT";
@ -29,8 +30,11 @@ public class ClientConnectionHandler implements Runnable {
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
private String userName = USER_NONE; private SimpleStringProperty userName;
private SimpleObjectProperty<State> state; private SimpleObjectProperty<State> state;
private ClientMessageList messages;
private SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort;
enum State { enum State {
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
@ -38,17 +42,25 @@ public class ClientConnectionHandler implements Runnable {
public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection, public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
String userName, String userName,
ChatWindowController controller) { ClientMessageList messages) {
this.connection = connection; this.connection = connection;
this.userName = (userName == null || userName.isBlank())? USER_NONE : userName; this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
this.controller = controller; this.messages = messages;
state = new SimpleObjectProperty<>(NEW); state = new SimpleObjectProperty<>(NEW);
serverAddress = new SimpleStringProperty();
serverPort = new SimpleIntegerProperty();
} }
public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
public SimpleObjectProperty<State> getStateProperty() { public SimpleObjectProperty<State> getStateProperty() {
return this.state; return this.state;
} }
public SimpleStringProperty getUserNameProperty() { return userName; }
public void setState (State newState) { public void setState (State newState) {
state.set(newState); state.set(newState);
@ -126,15 +138,14 @@ public class ClientConnectionHandler implements Runnable {
System.err.println("Illegal connect request from server"); System.err.println("Illegal connect request from server");
} else if (type.equals(DATA_TYPE_CONFIRM)) { } else if (type.equals(DATA_TYPE_CONFIRM)) {
if (state.get() == CONFIRM_CONNECT) { if (state.get() == CONFIRM_CONNECT) {
this.userName = reciever; this.userName.set(reciever);
controller.setUserName(userName); this.serverPort.set(connection.getRemotePort());
controller.setServerPort(connection.getRemotePort()); this.serverAddress.set(connection.getRemoteHost());
controller.setServerAddress(connection.getRemoteHost()); messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
controller.addInfo(payload);
System.out.println("CONFIRM: " + payload); System.out.println("CONFIRM: " + payload);
this.setState(CONNECTED); this.setState(CONNECTED);
} else if (state.get() == CONFIRM_DISCONNECT) { } else if (state.get() == CONFIRM_DISCONNECT) {
controller.addInfo(payload); messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
System.out.println("CONFIRM: " + payload); System.out.println("CONFIRM: " + payload);
this.setState(DISCONNECTED); this.setState(DISCONNECTED);
} else { } else {
@ -145,7 +156,7 @@ public class ClientConnectionHandler implements Runnable {
System.out.println("DISCONNECT: Already in disconnected: " + payload); System.out.println("DISCONNECT: Already in disconnected: " + payload);
return; return;
} }
controller.addInfo(payload); messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
System.out.println("DISCONNECT: " + payload); System.out.println("DISCONNECT: " + payload);
this.setState(DISCONNECTED); this.setState(DISCONNECTED);
} else if (type.equals(DATA_TYPE_MESSAGE)) { } else if (type.equals(DATA_TYPE_MESSAGE)) {
@ -153,17 +164,17 @@ public class ClientConnectionHandler implements Runnable {
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload); System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
return; return;
} }
controller.addMessage(sender, reciever, payload); messages.addMessage(new Message(Message.MessageType.MESSAGE,sender,reciever,payload));
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload); System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
} else if (type.equals(DATA_TYPE_ERROR)) { } else if (type.equals(DATA_TYPE_ERROR)) {
controller.addError(payload); messages.addMessage(new Message(Message.MessageType.ERROR,sender,reciever,payload));
System.out.println("ERROR: " + payload); System.out.println("ERROR: " + payload);
} else { } else {
System.out.println("Unknown data type received: " + type); System.out.println("Unknown data type received: " + type);
} }
} catch (ChatProtocolException e) { } catch (ChatProtocolException e) {
System.err.println("Error while processing data: " + e.getMessage()); System.err.println("Error while processing data: " + e.getMessage());
sendData(USER_NONE, userName, DATA_TYPE_ERROR, e.getMessage()); sendData(USER_NONE, userName.get(), DATA_TYPE_ERROR, e.getMessage());
} }
} }
@ -190,13 +201,13 @@ public class ClientConnectionHandler implements Runnable {
public void connect() throws ChatProtocolException { public void connect() throws ChatProtocolException {
if (state.get() != 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.sendData(userName.get(), USER_NONE, DATA_TYPE_CONNECT,null);
this.setState(CONFIRM_CONNECT); this.setState(CONFIRM_CONNECT);
} }
public void disconnect() throws ChatProtocolException { public void disconnect() throws ChatProtocolException {
if (state.get() != NEW && state.get() != 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.sendData(userName.get(), USER_NONE, DATA_TYPE_DISCONNECT,null);
this.setState(CONFIRM_DISCONNECT); this.setState(CONFIRM_DISCONNECT);
} }
@ -211,7 +222,7 @@ public class ClientConnectionHandler implements Runnable {
return false; return false;
} }
if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL;
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message);
return true; return true;
} else { } else {
return false; return false;

View File

@ -1,14 +1,17 @@
package ch.zhaw.pm2.multichat.client; package ch.zhaw.pm2.multichat.client;
import javafx.beans.property.SimpleBooleanProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ClientMessageList { public class ClientMessageList {
private List<Message> messages = new ArrayList<>(); private List<Message> messages = new ArrayList<>();
private SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
public void addMessage(Message message) { public void addMessage(Message message) {
messages.add(message); messages.add(message);
changed.set(!changed.get());
} }
public String getFilteredMessages(String filter) { public String getFilteredMessages(String filter) {
@ -30,6 +33,9 @@ public class ClientMessageList {
public void clear() { public void clear() {
messages = new ArrayList<>(); messages = new ArrayList<>();
changed.set(!changed.get());
} }
public SimpleBooleanProperty getChangedProperty() { return changed; }
} }