From e11fb136cfa0682068c06d3582efa07eaa3a88e1 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 07:36:14 +0200 Subject: [PATCH 1/9] created Class Message --- .../client/ChatWindowController.java | 6 +-- .../multichat/client/ClientMessageList.java | 37 ++++++------------- .../ch/zhaw/pm2/multichat/client/Message.java | 35 ++++++++++++++++++ 3 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java 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 e9a704e..bc7e57c 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 @@ -170,17 +170,17 @@ public class ChatWindowController { } public void addMessage(String sender, String receiver, String message) { - messages.addMessage(ClientMessageList.MessageType.MESSAGE, sender, receiver, message); + messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, message)); this.redrawMessageList(); } public void addInfo(String message) { - messages.addMessage(ClientMessageList.MessageType.INFO, null, null, message); + messages.addMessage(new Message(Message.MessageType.INFO, null, null, message)); this.redrawMessageList(); } public void addError(String message) { - messages.addMessage(ClientMessageList.MessageType.ERROR, null, null, message); + messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); this.redrawMessageList(); } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 100cb67..55dec4d 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -5,47 +5,34 @@ import java.util.List; import java.util.Objects; public class ClientMessageList { - private final List typeList = new ArrayList<>(); - private final List senderList = new ArrayList<>(); - private final List receiverList = new ArrayList<>(); - private final List messageList = new ArrayList<>(); + private final List messages = new ArrayList<>(); private final ChatWindowController gui; public ClientMessageList(ChatWindowController gui) { this.gui = gui; } - public void addMessage(MessageType type, String sender, String receiver, String message) { - typeList.add(type); - senderList.add(sender); - receiverList.add(receiver); - messageList.add(message); + public void addMessage(Message message) { + messages.add(message); } public void writeFilteredMessages(String filter) { boolean showAll = filter == null || filter.isBlank(); gui.clearMessageArea(); - for(int i=0; i Date: Tue, 12 Apr 2022 08:25:21 +0200 Subject: [PATCH 2/9] Refactoring ClientMessageList --- .../client/ChatWindowController.java | 37 +++++-------------- .../multichat/client/ClientMessageList.java | 22 ++++------- .../ch/zhaw/pm2/multichat/client/Message.java | 6 +++ 3 files changed, 24 insertions(+), 41 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 bc7e57c..93b36de 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 @@ -41,7 +41,7 @@ public class ChatWindowController { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); stateChanged(NEW); - messages = new ClientMessageList(this); + messages = new ClientMessageList(); } private void applicationClose() { @@ -59,30 +59,30 @@ public class ChatWindowController { private void connect() { try { - messages = new ClientMessageList(this); // clear message list + messages = new ClientMessageList(); // clear message list startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } private void disconnect() { if (connectionHandler == null) { - writeError("No connection handler"); + addError("No connection handler"); return; } try { connectionHandler.disconnect(); } catch (ChatProtocolException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } @FXML private void message() { if (connectionHandler == null) { - writeError("No connection handler"); + addError("No connection handler"); return; } String messageString = messageField.getText().strip(); @@ -95,10 +95,10 @@ public class ChatWindowController { try { connectionHandler.message(receiver, message); } catch (ChatProtocolException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } else { - writeError("Not a valid message format."); + addError("Not a valid message format."); } } @@ -184,28 +184,11 @@ public class ChatWindowController { this.redrawMessageList(); } - public void clearMessageArea() { - this.messageArea.clear(); - } - - private void redrawMessageList() { - Platform.runLater(() -> messages.writeFilteredMessages(filterValue.getText().strip())); + this.messageArea.clear(); + Platform.runLater(() -> this.messageArea.appendText(messages.getFilteredMessages(filterValue.getText().strip()))); } - public void writeError(String message) { - this.messageArea.appendText(String.format("[ERROR] %s\n", message)); - } - - public void writeInfo(String message) { - this.messageArea.appendText(String.format("[INFO] %s\n", message)); - } - - public void writeMessage(String sender, String reciever, String message) { - this.messageArea.appendText(String.format("[%s -> %s] %s\n", sender, reciever, message)); - } - - class WindowCloseHandler implements EventHandler { public void handle(WindowEvent event) { applicationClose(); diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 55dec4d..62d8ca0 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -6,33 +6,27 @@ import java.util.Objects; public class ClientMessageList { private final List messages = new ArrayList<>(); - private final ChatWindowController gui; - public ClientMessageList(ChatWindowController gui) { - this.gui = gui; - } public void addMessage(Message message) { messages.add(message); } - public void writeFilteredMessages(String filter) { + public String getFilteredMessages(String filter) { + String result = null; boolean showAll = filter == null || filter.isBlank(); - gui.clearMessageArea(); for(Message message : messages) { - if(showAll || - message.getSender().contains(filter) || - message.getReceiver().contains(filter) || - message.getText().contains(filter)) + if(showAll || message.matchesFilter(filter)) { switch (message.getType()) { - case MESSAGE: gui.writeMessage(message.getSender(), message.getReceiver(), message.getText()); break; - case ERROR: gui.writeError(message.getText()); break; - case INFO: gui.writeInfo(message.getText()); break; - default: gui.writeError("Unexpected message type: " + message.getType()); + case MESSAGE: result = result + String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()); break; + case ERROR: result = result + String.format("[ERROR] %s\n", message.getText()); break; + case INFO: result = result + String.format("[INFO] %s\n", message.getText()); break; + default: result = result + String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()); } } } + return result; } } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java index ea41cd7..c1ed9c6 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java @@ -13,6 +13,12 @@ public class Message { this.text = text; } + public boolean matchesFilter(String filter){ + return sender.contains(filter) || + receiver.contains(filter) || + text.contains(filter); + } + public MessageType getType() { return type; } From eb0e6d0dbc71110ee1ffb3a5b1e749226e99ea75 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 08:43:19 +0200 Subject: [PATCH 3/9] Refactoring creating ClientMessageList Object in ClientUI --- .../client/ChatWindowController.java | 10 ++++----- .../multichat/client/ClientMessageList.java | 21 +++++++++++-------- .../zhaw/pm2/multichat/client/ClientUI.java | 5 +++++ 3 files changed, 22 insertions(+), 14 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 93b36de..51bc48f 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 @@ -41,7 +41,10 @@ public class ChatWindowController { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); stateChanged(NEW); - messages = new ClientMessageList(); + } + + public void setMessages(ClientMessageList messages) { + this.messages = messages; } private void applicationClose() { @@ -59,7 +62,7 @@ public class ChatWindowController { private void connect() { try { - messages = new ClientMessageList(); // clear message list + messages.clear(); // clear message list startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { @@ -196,7 +199,4 @@ public class ChatWindowController { } - - - } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 62d8ca0..402d19d 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -2,10 +2,9 @@ package ch.zhaw.pm2.multichat.client; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class ClientMessageList { - private final List messages = new ArrayList<>(); + private List messages = new ArrayList<>(); public void addMessage(Message message) { @@ -13,20 +12,24 @@ public class ClientMessageList { } public String getFilteredMessages(String filter) { - String result = null; + StringBuilder result = null; boolean showAll = filter == null || filter.isBlank(); for(Message message : messages) { if(showAll || message.matchesFilter(filter)) { - switch (message.getType()) { - case MESSAGE: result = result + String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()); break; - case ERROR: result = result + String.format("[ERROR] %s\n", message.getText()); break; - case INFO: result = result + String.format("[INFO] %s\n", message.getText()); break; - default: result = result + String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()); + switch (message.getType()) { + case MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText())); + case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText())); + case INFO -> result.append(String.format("[INFO] %s\n", message.getText())); + default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType())); } } } - return result; + return result.toString(); } + public void clear() { + messages = new ArrayList<>(); + } + } 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 5f27276..2141237 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 @@ -7,6 +7,7 @@ import javafx.scene.layout.Pane; import javafx.stage.Stage; public class ClientUI extends Application { + private ClientMessageList clientMessageList = new ClientMessageList(); @Override public void start(Stage primaryStage) { @@ -17,6 +18,10 @@ public class ClientUI extends Application { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml")); Pane rootPane = loader.load(); + + ChatWindowController chatWindowController = loader.getController(); + chatWindowController.setMessages(clientMessageList); + // fill in scene and stage setup Scene scene = new Scene(rootPane); //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); From b246c3c7e33e7934c14a3720dbfb6bce69eefa7a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 08:59:53 +0200 Subject: [PATCH 4/9] fix Problem with StringBuilder in ClientMessageList --- .../java/ch/zhaw/pm2/multichat/client/ChatWindowController.java | 2 +- .../java/ch/zhaw/pm2/multichat/client/ClientMessageList.java | 2 +- 2 files changed, 2 insertions(+), 2 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 51bc48f..f73e498 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 @@ -189,7 +189,7 @@ public class ChatWindowController { private void redrawMessageList() { this.messageArea.clear(); - Platform.runLater(() -> this.messageArea.appendText(messages.getFilteredMessages(filterValue.getText().strip()))); + Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); } class WindowCloseHandler implements EventHandler { diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 402d19d..a811406 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -12,7 +12,7 @@ public class ClientMessageList { } public String getFilteredMessages(String filter) { - StringBuilder result = null; + StringBuilder result = new StringBuilder(); boolean showAll = filter == null || filter.isBlank(); for(Message message : messages) { if(showAll || message.matchesFilter(filter)) From 424a174847ddbde272bfe110f9879bc04a71cf41 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 09:43:24 +0200 Subject: [PATCH 5/9] refactoring Methods message() in ChatWindowController and ClientConnectionHandler --- .../client/ChatWindowController.java | 26 +++++++------------ .../client/ClientConnectionHandler.java | 18 +++++++++++-- 2 files changed, 25 insertions(+), 19 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 f73e498..9761287 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 @@ -19,7 +19,6 @@ import java.util.regex.Pattern; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; public class ChatWindowController { - private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); private ClientConnectionHandler connectionHandler; private ClientMessageList messages; @@ -84,24 +83,17 @@ public class ChatWindowController { @FXML private void message() { - if (connectionHandler == null) { - addError("No connection handler"); - return; - } String messageString = messageField.getText().strip(); - messageField.clear(); - Matcher matcher = messagePattern.matcher(messageString); - if (matcher.find()) { - String receiver = matcher.group(1); - String message = matcher.group(2); - if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; - try { - connectionHandler.message(receiver, message); - } catch (ChatProtocolException e) { - addError(e.getMessage()); + try { + if (connectionHandler == null) { + addError("No connection handler"); + } else if (!connectionHandler.message(messageString)) { + addError("Not a valid message format."); + } else { + messageField.clear(); } - } else { - addError("Not a valid message format."); + } catch (ChatProtocolException e) { + addError(e.getMessage()); } } 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 156010d..7fa5a6f 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 @@ -7,6 +7,8 @@ import java.io.EOFException; import java.io.IOException; import java.net.SocketException; import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; @@ -24,6 +26,8 @@ public class ClientConnectionHandler implements Runnable { public static final String USER_NONE = ""; public static final String USER_ALL = "*"; + private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); + private String userName = USER_NONE; private State state = NEW; @@ -194,9 +198,19 @@ public class ClientConnectionHandler implements Runnable { this.setState(CONFIRM_DISCONNECT); } - public void message(String receiver, String message) throws ChatProtocolException { + public boolean message(String messageString) throws ChatProtocolException { if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); - this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + + Matcher matcher = messagePattern.matcher(messageString); + if (matcher.find()) { + String receiver = matcher.group(1); + String message = matcher.group(2); + if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; + this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + return true; + } else { + return false; + } } } From 3082da8a91cc1b29c2c8a07a5eb95a783dceb56b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 10:00:31 +0200 Subject: [PATCH 6/9] Solved Problem "Empty Messages can be sent" --- .../ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7fa5a6f..fb57727 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 @@ -202,7 +202,7 @@ public class ClientConnectionHandler implements Runnable { if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); Matcher matcher = messagePattern.matcher(messageString); - if (matcher.find()) { + if (messageString != "" && matcher.find()) { String receiver = matcher.group(1); String message = matcher.group(2); if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; From 7e7333dfafb664c1634f0ad3988f730a6acea75a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 11:01:28 +0200 Subject: [PATCH 7/9] fixed matchesFilter in Message.java Problem solved if any String is null. --- .../ch/zhaw/pm2/multichat/client/ChatWindowController.java | 1 + .../src/main/java/ch/zhaw/pm2/multichat/client/Message.java | 6 +++--- 2 files changed, 4 insertions(+), 3 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 9761287..9eac20a 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 @@ -62,6 +62,7 @@ public class ChatWindowController { private void connect() { try { messages.clear(); // clear message list + redrawMessageList(); startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java index c1ed9c6..3506943 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java @@ -14,9 +14,9 @@ public class Message { } public boolean matchesFilter(String filter){ - return sender.contains(filter) || - receiver.contains(filter) || - text.contains(filter); + return (sender != null && sender.contains(filter)) || + (receiver != null && receiver.contains(filter)) || + (text != null && text.contains(filter)); } public MessageType getType() { From 56200b749dd06028425eb402750ce42a1e1c7a5d Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 12:36:43 +0200 Subject: [PATCH 8/9] added Javadoc in Class Message --- .../ch/zhaw/pm2/multichat/client/Message.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java index 3506943..d805f68 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java @@ -1,11 +1,21 @@ package ch.zhaw.pm2.multichat.client; +/** +A Message object represents one Message of a client. Can be stored in ClientMessageList. + */ public class Message { private MessageType type; private String sender; private String receiver; private String text; + /** + * Constructor of Message. Needs all Information about a Message to save them. + * @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and reciever will be null) + * @param sender The User who has sent the message. + * @param receiver The User who should recieve the message. + * @param text The Text of the message. + */ public Message(MessageType type, String sender, String receiver, String text) { this.type = type; this.sender = sender; @@ -13,28 +23,48 @@ public class Message { this.text = text; } + /** + * Checks if the Filter String is contained in one of the datafields sender, receiver or text + * @param filter The Filter String + * @return true if it the Filter String is contained in a datafield. + */ public boolean matchesFilter(String filter){ return (sender != null && sender.contains(filter)) || (receiver != null && receiver.contains(filter)) || (text != null && text.contains(filter)); } + /** + * @return The type of the message. + */ public MessageType getType() { return type; } + /** + * @return The User who has sent the Message. + */ public String getSender() { return sender; } + /** + * @return The Reciever who recieves the Message. + */ public String getReceiver() { return receiver; } + /** + * @return The Text of the Message. + */ public String getText() { return text; } + /** + * Enummeration of Message Types. + */ public enum MessageType { INFO, MESSAGE, ERROR; } From 1a188f6bbd4a0bb8f96ce94ad87e666a31332a62 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 14:26:26 +0200 Subject: [PATCH 9/9] Fixed Problem "Empty Messages can be sent" if receiver != * --- .../zhaw/pm2/multichat/client/ClientConnectionHandler.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 fb57727..22e4ce2 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 @@ -202,9 +202,12 @@ public class ClientConnectionHandler implements Runnable { if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); Matcher matcher = messagePattern.matcher(messageString); - if (messageString != "" && matcher.find()) { + if (matcher.find()) { String receiver = matcher.group(1); String message = matcher.group(2); + if(message.length() < 1){ + return false; + } if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); return true;