refactoring Methods message() in ChatWindowController and ClientConnectionHandler

This commit is contained in:
schrom01 2022-04-12 09:43:24 +02:00
parent b246c3c7e3
commit 424a174847
2 changed files with 25 additions and 19 deletions

View File

@ -19,7 +19,6 @@ import java.util.regex.Pattern;
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
public class ChatWindowController { public class ChatWindowController {
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
private ClientConnectionHandler connectionHandler; private ClientConnectionHandler connectionHandler;
private ClientMessageList messages; private ClientMessageList messages;
@ -84,24 +83,17 @@ public class ChatWindowController {
@FXML @FXML
private void message() { private void message() {
if (connectionHandler == null) {
addError("No connection handler");
return;
}
String messageString = messageField.getText().strip(); String messageString = messageField.getText().strip();
messageField.clear(); try {
Matcher matcher = messagePattern.matcher(messageString); if (connectionHandler == null) {
if (matcher.find()) { addError("No connection handler");
String receiver = matcher.group(1); } else if (!connectionHandler.message(messageString)) {
String message = matcher.group(2); addError("Not a valid message format.");
if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; } else {
try { messageField.clear();
connectionHandler.message(receiver, message);
} catch (ChatProtocolException e) {
addError(e.getMessage());
} }
} else { } catch (ChatProtocolException e) {
addError("Not a valid message format."); addError(e.getMessage());
} }
} }

View File

@ -7,6 +7,8 @@ import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.net.SocketException; import java.net.SocketException;
import java.util.Scanner; import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; 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_NONE = "";
public static final String USER_ALL = "*"; public static final String USER_ALL = "*";
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
private String userName = USER_NONE; private String userName = USER_NONE;
private State state = NEW; private State state = NEW;
@ -194,9 +198,19 @@ public class ClientConnectionHandler implements Runnable {
this.setState(CONFIRM_DISCONNECT); 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); 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;
}
} }
} }