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,8 +13,14 @@ 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*(.*)$");
|
||||||
|
@ -42,24 +48,62 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method which sets a new State.
|
||||||
|
*
|
||||||
|
* @param newState the state that will be set
|
||||||
|
*/
|
||||||
public void setState(State newState) {
|
public void setState(State newState) {
|
||||||
state.set(newState);
|
state.set(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Standard run method which will directly start the startReceiving method.
|
||||||
|
*/
|
||||||
public void run() {
|
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 {
|
||||||
|
@ -85,6 +129,9 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
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);
|
||||||
|
@ -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)
|
||||||
|
throw new ChatProtocolException("Illegal state for disconnect: " + state);
|
||||||
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(), null);
|
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);
|
||||||
|
|
||||||
|
|
|
@ -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