Compare commits

..
Author SHA1 Message Date
Andrin Fassbind b6fd5b569d fixed Issue #28 by checking if username contains space before starting connection 2022-04-14 19:33:18 +02:00
Andrin Fassbind b47d98b960 fixed Issue #19 by changing messagePattern 2022-04-14 12:11:03 +02:00
fassbandandGitHub Enterprise 7c68472859 Merge pull request #32 from PM2-IT21bWIN-ruiz-mach-krea/Fixing_private_Messages
fixed Problem "private Messages are visible for sender"
2022-04-13 17:35:48 +02:00
Roman SchenkandGitHub Enterprise b9ffb2b133 Update ServerConnectionHandler.java
added Commment for documentation.
2022-04-13 17:35:07 +02:00
schrom01 e69ced4081 fixed Problem "private Messages are visible for sender"
#28
2022-04-13 17:24:22 +02:00
Roman SchenkandGitHub Enterprise d1dfe6c1ab Merge pull request #31 from PM2-IT21bWIN-ruiz-mach-krea/Remove_Controller_From_Handler
Remove ChatWindowController from ClientConnectionHandler
2022-04-13 16:13:33 +02:00
Andrin Fassbind 3eedf58685 Remove ChatWindowController from ClientConnectionHandler
Make ChatWindowController listen for message changes

fixed Issue #23
2022-04-13 15:55:08 +02:00
Andrin Fassbind 1777b62582 Make State in ClientConnectionHandler property
Add change listener for stateproperty in ChatWindowController

fixed Issue #21
2022-04-13 15:03:07 +02:00
Roman SchenkandGitHub Enterprise a0b7b363c3 Merge pull request #30 from PM2-IT21bWIN-ruiz-mach-krea/Server_Console_Display
fixed issue #5
2022-04-12 21:35:26 +02:00
Leonardo Brandenberger 914fa8f1e2 fixed #5 2022-04-12 16:42:19 +02:00
fassbandandGitHub Enterprise 9eb352c0fe Merge pull request #25 from PM2-IT21bWIN-ruiz-mach-krea/Class_Message
Class Message
2022-04-12 15:23:35 +02:00
fassbandandGitHub Enterprise a27b7f4446 Merge pull request #27 from PM2-IT21bWIN-ruiz-mach-krea/ChatWindow_Layout
Chat window layout
2022-04-12 15:23:22 +02:00
schrom01 8661b44fe2 activated Textwrapping for Textarea in ChatWindow.fxml 2022-04-12 13:20:25 +02:00
schrom01 84c852c9b5 fixed Minumum Size of ChatWindow. 2022-04-12 13:16:58 +02:00
6 changed files with 125 additions and 60 deletions
@@ -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.ChatProtocolException;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
@@ -39,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() {
@@ -52,7 +54,7 @@ public class ChatWindowController {
@FXML @FXML
private void toggleConnection () { private void toggleConnection () {
if (connectionHandler == null || connectionHandler.getState() != CONNECTED) { if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) {
connect(); connect();
} else { } else {
disconnect(); disconnect();
@@ -62,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) {
@@ -105,15 +106,22 @@ public class ChatWindowController {
private void startConnectionHandler() throws IOException { private void startConnectionHandler() throws IOException {
String userName = userNameField.getText(); String userName = userNameField.getText();
String serverAddress = serverAddressField.getText(); if(!userName.contains(" ")) {
int serverPort = Integer.parseInt(serverPortField.getText()); String serverAddress = serverAddressField.getText();
connectionHandler = new ClientConnectionHandler( int serverPort = Integer.parseInt(serverPortField.getText());
NetworkHandler.openConnection(serverAddress, serverPort), userName, connectionHandler = new ClientConnectionHandler(
this); NetworkHandler.openConnection(serverAddress, serverPort), userName,
new Thread(connectionHandler).start(); messages);
new Thread(connectionHandler).start();
// register window close handler //register Listener
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); startListener();
// register window close handler
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
} else {
addError("It is not allowed to have spaces in username!");
}
} }
private void terminateConnectionHandler() { private void terminateConnectionHandler() {
@@ -165,19 +173,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() {
@@ -192,4 +189,43 @@ public class ChatWindowController {
} }
public void startListener() {
connectionHandler.getStateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> observable, State oldValue, State 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();
}
});
}
} }
@@ -2,6 +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.SimpleStringProperty;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@@ -14,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";
@@ -26,10 +28,13 @@ public class ClientConnectionHandler implements Runnable {
public static final String USER_NONE = ""; public static final String USER_NONE = "";
public static final String USER_ALL = "*"; public static final String USER_ALL = "*";
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); private final Pattern messagePattern = Pattern.compile( "^(?:@(\\S*))?\\s*(.*)$" );
private String userName = USER_NONE; private SimpleStringProperty userName;
private State state = NEW; 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;
@@ -37,19 +42,27 @@ 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);
serverAddress = new SimpleStringProperty();
serverPort = new SimpleIntegerProperty();
} }
public State getState() { public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
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) {
this.state = newState; state.set(newState);
controller.stateChanged(newState);
} }
public void run () { public void run () {
@@ -123,45 +136,44 @@ public class ClientConnectionHandler implements Runnable {
if (type.equals(DATA_TYPE_CONNECT)) { if (type.equals(DATA_TYPE_CONNECT)) {
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 == 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 == 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 {
System.err.println("Got unexpected confirm message: " + payload); System.err.println("Got unexpected confirm message: " + payload);
} }
} else if (type.equals(DATA_TYPE_DISCONNECT)) { } else if (type.equals(DATA_TYPE_DISCONNECT)) {
if (state == DISCONNECTED) { if (state.get() == DISCONNECTED) {
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)) {
if (state != CONNECTED) { if (state.get() != CONNECTED) {
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());
} }
} }
@@ -187,19 +199,19 @@ public class ClientConnectionHandler implements Runnable {
} }
public void connect() throws ChatProtocolException { 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.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 != 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.sendData(userName.get(), USER_NONE, DATA_TYPE_DISCONNECT,null);
this.setState(CONFIRM_DISCONNECT); this.setState(CONFIRM_DISCONNECT);
} }
public boolean message(String messageString) throws ChatProtocolException { 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); Matcher matcher = messagePattern.matcher(messageString);
if (matcher.find()) { if (matcher.find()) {
@@ -209,7 +221,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;
@@ -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; }
} }
@@ -28,10 +28,10 @@ public class ClientUI extends Application {
// configure and show stage // configure and show stage
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.setMinWidth(420);
primaryStage.setMinHeight(250); primaryStage.setMinHeight(250);
primaryStage.setTitle("Multichat Client"); primaryStage.setTitle("Multichat Client");
primaryStage.show(); primaryStage.show();
primaryStage.setMinWidth(primaryStage.getWidth()); //use automatically computed size as Minimum Size.
} catch(Exception e) { } catch(Exception e) {
System.err.println("Error starting up UI" + e.getMessage()); System.err.println("Error starting up UI" + e.getMessage());
} }
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.Button?>
<?import javafx.scene.layout.*?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController"> <BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
<top> <top>
<VBox BorderPane.alignment="CENTER"> <VBox BorderPane.alignment="CENTER">
<children> <children>
@@ -68,7 +76,7 @@
</HBox> </HBox>
</bottom> </bottom>
<center> <center>
<TextArea fx:id="messageArea" editable="false"> <TextArea fx:id="messageArea" editable="false" wrapText="true">
<BorderPane.margin> <BorderPane.margin>
<Insets left="5.0" right="5.0" /> <Insets left="5.0" right="5.0" />
</BorderPane.margin> </BorderPane.margin>
@@ -55,7 +55,7 @@ public class ServerConnectionHandler implements Runnable{
} }
public void startReceiving() { public void startReceiving() {
System.out.println("Starting Connection Handler for " + userName); System.out.println("Starting Connection Handler for new User");
try { try {
System.out.println("Start receiving data..."); System.out.println("Start receiving data...");
while (connection.isAvailable()) { while (connection.isAvailable()) {
@@ -149,6 +149,9 @@ public class ServerConnectionHandler implements Runnable{
ServerConnectionHandler handler = connectionRegistry.get(reciever); ServerConnectionHandler handler = connectionRegistry.get(reciever);
if (handler != null) { if (handler != null) {
handler.sendData(sender, reciever, type, payload); handler.sendData(sender, reciever, type, payload);
if(!reciever.equals(sender)){
sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
}
} else { } else {
this.sendData(USER_NONE, userName, DATA_TYPE_ERROR, "Unknown User: " + reciever); this.sendData(USER_NONE, userName, DATA_TYPE_ERROR, "Unknown User: " + reciever);
} }