refactoring Methods message() in ChatWindowController and ClientConnectionHandler
This commit is contained in:
parent
b246c3c7e3
commit
424a174847
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue