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 {
|
||||
private ClientConnectionHandler connectionHandler;
|
||||
private ClientMessageList messages;
|
||||
|
||||
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
||||
|
||||
@FXML
|
||||
|
|
|
@ -13,11 +13,17 @@ import java.net.SocketException;
|
|||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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 {
|
||||
|
||||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\S*))?\\s*(.*)$" );
|
||||
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
|
||||
|
||||
private SimpleStringProperty userName;
|
||||
private SimpleObjectProperty<State> state;
|
||||
|
@ -25,7 +31,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
private SimpleStringProperty serverAddress;
|
||||
private SimpleIntegerProperty serverPort;
|
||||
|
||||
public ClientConnectionHandler(ClientMessageList messages) {
|
||||
public ClientConnectionHandler(ClientMessageList messages) {
|
||||
super();
|
||||
this.messages = messages;
|
||||
state = new SimpleObjectProperty<>(State.NEW);
|
||||
|
@ -39,27 +45,65 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
this.serverAddress.set(serverAddress);
|
||||
this.serverPort.set(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() {
|
||||
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);
|
||||
}
|
||||
|
||||
public void run () {
|
||||
/**
|
||||
* Standard run method which will directly start the startReceiving method.
|
||||
*/
|
||||
public void run() {
|
||||
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() {
|
||||
System.out.println("Starting Connection Handler");
|
||||
try {
|
||||
|
@ -77,14 +121,17 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
System.out.println("Connection terminated by remote");
|
||||
this.setState(DISCONNECTED);
|
||||
System.err.println("Unregistered because connection terminated" + e.getMessage());
|
||||
} catch(IOException e) {
|
||||
} catch (IOException e) {
|
||||
System.err.println("Communication error" + e);
|
||||
} catch(ClassNotFoundException e) {
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("Received object of unknown type" + e.getMessage());
|
||||
}
|
||||
System.out.println("Stopped Connection Handler");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method which is used to stop receiving data, gets the current connection and closes it.
|
||||
*/
|
||||
public void stopReceiving() {
|
||||
System.out.println("Closing Connection Handler to Server");
|
||||
try {
|
||||
|
@ -94,9 +141,14 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
} catch (IOException e) {
|
||||
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) {
|
||||
try {
|
||||
Scanner scanner = new Scanner(data);
|
||||
|
@ -104,7 +156,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
StringBuilder reciever = new StringBuilder();
|
||||
StringBuilder type = 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
|
||||
if (type.toString().equals(getDataTypeConnect())) {
|
||||
|
@ -112,9 +164,9 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
||||
} 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())) {
|
||||
caseMessage(sender.toString(),reciever.toString(),payload.toString());
|
||||
caseMessage(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeError())) {
|
||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else {
|
||||
|
@ -131,11 +183,11 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
this.userName.set(reciever);
|
||||
this.serverPort.set(getConnection().getRemotePort());
|
||||
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);
|
||||
this.setState(CONNECTED);
|
||||
} 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);
|
||||
this.setState(DISCONNECTED);
|
||||
} 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) {
|
||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
||||
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);
|
||||
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) {
|
||||
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
|
||||
return;
|
||||
}
|
||||
messages.addMessage(new Message(Message.MessageType.MESSAGE,sender,reciever,payload));
|
||||
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
|
||||
messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects TODO
|
||||
*
|
||||
* @throws ChatProtocolException Error that is thrown if the state is not set to NEW
|
||||
*/
|
||||
public void connect() throws ChatProtocolException {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* @throws ChatProtocolException
|
||||
*/
|
||||
public void disconnect() throws ChatProtocolException {
|
||||
if (state.get() != NEW && state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
||||
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(),null);
|
||||
if (state.get() != NEW && state.get() != CONNECTED)
|
||||
throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
||||
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(), null);
|
||||
this.setState(CONFIRM_DISCONNECT);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* @param messageString
|
||||
* @return
|
||||
* @throws ChatProtocolException
|
||||
*/
|
||||
public boolean message(String messageString) throws ChatProtocolException {
|
||||
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()) {
|
||||
String receiver = matcher.group(1);
|
||||
String message = matcher.group(2);
|
||||
if(message.length() < 1){
|
||||
if (message.length() < 1) {
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -5,6 +5,9 @@ import javafx.beans.property.SimpleBooleanProperty;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class that is use
|
||||
*/
|
||||
public class ClientMessageList {
|
||||
private List<Message> messages = new ArrayList<>();
|
||||
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
|
||||
|
|
Loading…
Reference in New Issue