improved java doc in ChatWindowController and Client class finished Client Connection Handler in Progress
This commit is contained in:
parent
4edcc92649
commit
7c8bdb3b33
|
@ -25,7 +25,6 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
public class ChatWindowController {
|
public class ChatWindowController {
|
||||||
private ClientConnectionHandler connectionHandler;
|
private ClientConnectionHandler connectionHandler;
|
||||||
private ClientMessageList messages;
|
private ClientMessageList messages;
|
||||||
|
|
||||||
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
|
|
@ -13,11 +13,17 @@ import java.net.SocketException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client Connection Handler Class is used for the connection of the Client to a server
|
||||||
|
* it is used part as a Model storing properties like userName, state, serverAddress and Port.
|
||||||
|
* Also holds methods to
|
||||||
|
*/
|
||||||
public class ClientConnectionHandler extends ConnectionHandler implements Runnable {
|
public class ClientConnectionHandler extends ConnectionHandler implements Runnable {
|
||||||
|
|
||||||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\S*))?\\s*(.*)$" );
|
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
|
||||||
|
|
||||||
private SimpleStringProperty userName;
|
private SimpleStringProperty userName;
|
||||||
private SimpleObjectProperty<State> state;
|
private SimpleObjectProperty<State> state;
|
||||||
|
@ -39,27 +45,65 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
this.serverAddress.set(serverAddress);
|
this.serverAddress.set(serverAddress);
|
||||||
this.serverPort.set(serverPort);
|
this.serverPort.set(serverPort);
|
||||||
setConnection(NetworkHandler.openConnection(serverAddress, serverPort));
|
setConnection(NetworkHandler.openConnection(serverAddress, serverPort));
|
||||||
this.userName.set((userName == null || userName.isBlank())? USER_NONE : userName);
|
this.userName.set((userName == null || userName.isBlank()) ? USER_NONE : userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
|
/**
|
||||||
|
* Observable getter Method for the stored serverAddress
|
||||||
|
*
|
||||||
|
* @return the stored serverAddress
|
||||||
|
*/
|
||||||
|
public SimpleStringProperty getServerAddressProperty() {
|
||||||
|
return serverAddress;
|
||||||
|
}
|
||||||
|
|
||||||
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
|
/**
|
||||||
|
* Observable getter Method for the stored serverPort
|
||||||
|
*
|
||||||
|
* @return the stored serverPort
|
||||||
|
*/
|
||||||
|
public SimpleIntegerProperty getServerPortProperty() {
|
||||||
|
return serverPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observable getter Method for the stored state
|
||||||
|
*
|
||||||
|
* @return the stored state
|
||||||
|
*/
|
||||||
public SimpleObjectProperty<State> getStateProperty() {
|
public SimpleObjectProperty<State> getStateProperty() {
|
||||||
return this.state;
|
return this.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleStringProperty getUserNameProperty() { return userName; }
|
/**
|
||||||
|
* Observable getter Method for the stored userName
|
||||||
|
*
|
||||||
|
* @return the stored userName
|
||||||
|
*/
|
||||||
|
public SimpleStringProperty getUserNameProperty() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
public void setState (State newState) {
|
/**
|
||||||
|
* Method which sets a new State.
|
||||||
|
*
|
||||||
|
* @param newState the state that will be set
|
||||||
|
*/
|
||||||
|
public void setState(State newState) {
|
||||||
state.set(newState);
|
state.set(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run () {
|
/**
|
||||||
|
* Standard run method which will directly start the startReceiving method.
|
||||||
|
*/
|
||||||
|
public void run() {
|
||||||
startReceiving();
|
startReceiving();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that is started by the run method, starts a connection handler.
|
||||||
|
* Figures out if connection is avaible if not determines the error cause and gives an error acordingly.
|
||||||
|
*/
|
||||||
private void startReceiving() {
|
private void startReceiving() {
|
||||||
System.out.println("Starting Connection Handler");
|
System.out.println("Starting Connection Handler");
|
||||||
try {
|
try {
|
||||||
|
@ -77,14 +121,17 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
System.out.println("Connection terminated by remote");
|
System.out.println("Connection terminated by remote");
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
System.err.println("Unregistered because connection terminated" + e.getMessage());
|
System.err.println("Unregistered because connection terminated" + e.getMessage());
|
||||||
} catch(IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Communication error" + e);
|
System.err.println("Communication error" + e);
|
||||||
} catch(ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
System.err.println("Received object of unknown type" + e.getMessage());
|
System.err.println("Received object of unknown type" + e.getMessage());
|
||||||
}
|
}
|
||||||
System.out.println("Stopped Connection Handler");
|
System.out.println("Stopped Connection Handler");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method which is used to stop receiving data, gets the current connection and closes it.
|
||||||
|
*/
|
||||||
public void stopReceiving() {
|
public void stopReceiving() {
|
||||||
System.out.println("Closing Connection Handler to Server");
|
System.out.println("Closing Connection Handler to Server");
|
||||||
try {
|
try {
|
||||||
|
@ -94,9 +141,14 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed to close connection." + e.getMessage());
|
System.err.println("Failed to close connection." + e.getMessage());
|
||||||
}
|
}
|
||||||
System.out.println("Closed Connection Handler to Server");
|
System.out.println("Closed Connection Handler to Server"); //TODO should be shown also when failed to close ?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method which processes data and determines its type then uses the corresponding method to proccess it.
|
||||||
|
*
|
||||||
|
* @param data that is received in a form of a String and then used depending on its determined cause.
|
||||||
|
*/
|
||||||
private void processData(String data) {
|
private void processData(String data) {
|
||||||
try {
|
try {
|
||||||
Scanner scanner = new Scanner(data);
|
Scanner scanner = new Scanner(data);
|
||||||
|
@ -104,7 +156,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
StringBuilder reciever = new StringBuilder();
|
StringBuilder reciever = new StringBuilder();
|
||||||
StringBuilder type = new StringBuilder();
|
StringBuilder type = new StringBuilder();
|
||||||
StringBuilder payload = new StringBuilder();
|
StringBuilder payload = new StringBuilder();
|
||||||
super.processData(scanner,sender,reciever,type,payload);
|
super.processData(scanner, sender, reciever, type, payload);
|
||||||
|
|
||||||
// dispatch operation based on type parameter
|
// dispatch operation based on type parameter
|
||||||
if (type.toString().equals(getDataTypeConnect())) {
|
if (type.toString().equals(getDataTypeConnect())) {
|
||||||
|
@ -112,9 +164,9 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||||
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||||
caseDisconnect(sender.toString(),reciever.toString(),payload.toString());
|
caseDisconnect(sender.toString(), reciever.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||||
caseMessage(sender.toString(),reciever.toString(),payload.toString());
|
caseMessage(sender.toString(), reciever.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeError())) {
|
} else if (type.toString().equals(getDataTypeError())) {
|
||||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
caseError(sender.toString(), reciever.toString(), payload.toString());
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,11 +183,11 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
this.userName.set(reciever);
|
this.userName.set(reciever);
|
||||||
this.serverPort.set(getConnection().getRemotePort());
|
this.serverPort.set(getConnection().getRemotePort());
|
||||||
this.serverAddress.set(getConnection().getRemoteHost());
|
this.serverAddress.set(getConnection().getRemoteHost());
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
|
messages.addMessage(new Message(Message.MessageType.INFO, sender, reciever, payload));
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(CONNECTED);
|
this.setState(CONNECTED);
|
||||||
} else if (state.get() == CONFIRM_DISCONNECT) {
|
} else if (state.get() == CONFIRM_DISCONNECT) {
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,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 {
|
||||||
|
@ -143,42 +195,79 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void caseDisconnect(String sender, String reciever, String payload) {
|
/**
|
||||||
|
* Initiates the disconnect sequence and sends the message with all its info.
|
||||||
|
*
|
||||||
|
* @param sender the sender name of the message
|
||||||
|
* @param receiver the receiver name of the message
|
||||||
|
* @param payload the added payload that corresponds to the message
|
||||||
|
*/
|
||||||
|
private void caseDisconnect(String sender, String receiver, String payload) {
|
||||||
if (state.get() == DISCONNECTED) {
|
if (state.get() == DISCONNECTED) {
|
||||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
|
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
||||||
System.out.println("DISCONNECT: " + payload);
|
System.out.println("DISCONNECT: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void caseMessage(String sender, String reciever, String payload) {
|
/**
|
||||||
|
* Initiates the procedure to send a new message and sends one as such.
|
||||||
|
*
|
||||||
|
* @param sender the sender name of the message
|
||||||
|
* @param receiver the receiver name of the message
|
||||||
|
* @param payload the added payload that corresponds to the message
|
||||||
|
*/
|
||||||
|
private void caseMessage(String sender, String receiver, String payload) {
|
||||||
if (state.get() != 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;
|
||||||
}
|
}
|
||||||
messages.addMessage(new Message(Message.MessageType.MESSAGE,sender,reciever,payload));
|
messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, payload));
|
||||||
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
|
System.out.println("MESSAGE: From " + sender + " to " + receiver + ": " + payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void caseError(String sender, String reciever, String payload) {
|
/**
|
||||||
messages.addMessage(new Message(Message.MessageType.ERROR,sender,reciever,payload));
|
* Stores the message as an error message and displays it as such aswell.
|
||||||
|
*
|
||||||
|
* @param sender the sender name of the message
|
||||||
|
* @param receiver the receiver name of the message
|
||||||
|
* @param payload the added payload that corresponds to the message
|
||||||
|
*/
|
||||||
|
private void caseError(String sender, String receiver, String payload) {
|
||||||
|
messages.addMessage(new Message(Message.MessageType.ERROR, sender, receiver, payload));
|
||||||
System.out.println("ERROR: " + payload);
|
System.out.println("ERROR: " + payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects TODO
|
||||||
|
*
|
||||||
|
* @throws ChatProtocolException Error that is thrown if the state is not set to NEW
|
||||||
|
*/
|
||||||
public void connect() throws ChatProtocolException {
|
public void connect() throws ChatProtocolException {
|
||||||
if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
||||||
this.sendData(userName.get(), USER_NONE, getDataTypeConnect(),null);
|
this.sendData(userName.get(), USER_NONE, getDataTypeConnect(), null);
|
||||||
this.setState(CONFIRM_CONNECT);
|
this.setState(CONFIRM_CONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO
|
||||||
|
* @throws ChatProtocolException
|
||||||
|
*/
|
||||||
public void disconnect() throws ChatProtocolException {
|
public void disconnect() throws ChatProtocolException {
|
||||||
if (state.get() != NEW && state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
if (state.get() != NEW && state.get() != CONNECTED)
|
||||||
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(),null);
|
throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
||||||
|
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(), null);
|
||||||
this.setState(CONFIRM_DISCONNECT);
|
this.setState(CONFIRM_DISCONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO
|
||||||
|
* @param messageString
|
||||||
|
* @return
|
||||||
|
* @throws ChatProtocolException
|
||||||
|
*/
|
||||||
public boolean message(String messageString) throws ChatProtocolException {
|
public boolean message(String messageString) throws ChatProtocolException {
|
||||||
if (state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
if (state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
||||||
|
|
||||||
|
@ -186,11 +275,11 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
String receiver = matcher.group(1);
|
String receiver = matcher.group(1);
|
||||||
String message = matcher.group(2);
|
String message = matcher.group(2);
|
||||||
if(message.length() < 1){
|
if (message.length() < 1) {
|
||||||
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.get(), receiver, getDataTypeMessage(),message);
|
this.sendData(userName.get(), receiver, getDataTypeMessage(), message);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -5,6 +5,9 @@ import javafx.beans.property.SimpleBooleanProperty;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that is use
|
||||||
|
*/
|
||||||
public class ClientMessageList {
|
public class ClientMessageList {
|
||||||
private List<Message> messages = new ArrayList<>();
|
private List<Message> messages = new ArrayList<>();
|
||||||
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
|
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
|
||||||
|
|
Loading…
Reference in New Issue