From 424a174847ddbde272bfe110f9879bc04a71cf41 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 09:43:24 +0200 Subject: [PATCH] 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; + } } }