Start refactoring ClientConnectionHandler.java
This commit is contained in:
parent
f2945b3075
commit
f2fb32bbd4
|
@ -42,7 +42,14 @@ public class ChatWindowController {
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
|
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
|
||||||
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
|
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
|
||||||
messages = new ClientMessageList(this);
|
}
|
||||||
|
|
||||||
|
public void chatWindowController(ClientMessageList messages,ClientConnectionHandler connectionHandler) {
|
||||||
|
this.connectionHandler = connectionHandler;
|
||||||
|
this.messages = messages;
|
||||||
|
|
||||||
|
//register changelistener
|
||||||
|
startChangeListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applicationClose() {
|
private void applicationClose() {
|
||||||
|
@ -111,16 +118,12 @@ public class ChatWindowController {
|
||||||
String userName = userNameField.getText();
|
String userName = userNameField.getText();
|
||||||
String serverAddress = serverAddressField.getText();
|
String serverAddress = serverAddressField.getText();
|
||||||
int serverPort = Integer.parseInt(serverPortField.getText());
|
int serverPort = Integer.parseInt(serverPortField.getText());
|
||||||
connectionHandler = new ClientConnectionHandler(
|
connectionHandler.openConnection(serverAddress,serverPort,userName);
|
||||||
NetworkHandler.openConnection(serverAddress, serverPort), userName,
|
|
||||||
this);
|
|
||||||
new Thread(connectionHandler).start();
|
new Thread(connectionHandler).start();
|
||||||
|
|
||||||
// register window close handler
|
// register window close handler
|
||||||
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
||||||
|
|
||||||
//register changelistener
|
|
||||||
startChangeListener();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void terminateConnectionHandler() {
|
private void terminateConnectionHandler() {
|
||||||
|
@ -146,6 +149,7 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startChangeListener() {
|
private void startChangeListener() {
|
||||||
|
//Listener for State
|
||||||
connectionHandler.getStateObjectProperty().addListener(new ChangeListener<State>() {
|
connectionHandler.getStateObjectProperty().addListener(new ChangeListener<State>() {
|
||||||
@Override
|
@Override
|
||||||
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
|
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
|
||||||
|
@ -153,6 +157,29 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Listener for Username
|
||||||
|
connectionHandler.getUserNameProperty().addListener(new ChangeListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
||||||
|
setUserName(newValue);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Listener for Address
|
||||||
|
connectionHandler.getServerAddressProperty().addListener(new ChangeListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
||||||
|
setServerAddress(newValue);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Listener for Port
|
||||||
|
connectionHandler.getServerPortProperty().addListener(new ChangeListener<Number>() {
|
||||||
|
@Override
|
||||||
|
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
||||||
|
setServerPort(newValue.intValue());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
|
|
|
@ -3,7 +3,9 @@ package ch.zhaw.pm2.multichat.client;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
import javafx.beans.property.ObjectProperty;
|
import javafx.beans.property.ObjectProperty;
|
||||||
|
import javafx.beans.property.SimpleIntegerProperty;
|
||||||
import javafx.beans.property.SimpleObjectProperty;
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -13,8 +15,7 @@ import java.util.Scanner;
|
||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
||||||
|
|
||||||
public class ClientConnectionHandler implements Runnable {
|
public class ClientConnectionHandler implements Runnable {
|
||||||
private final NetworkHandler.NetworkConnection<String> connection;
|
private NetworkHandler.NetworkConnection<String> connection;
|
||||||
private final ChatWindowController controller;
|
|
||||||
|
|
||||||
// Data types used for the Chat Protocol
|
// Data types used for the Chat Protocol
|
||||||
private static final String DATA_TYPE_CONNECT = "CONNECT";
|
private static final String DATA_TYPE_CONNECT = "CONNECT";
|
||||||
|
@ -26,27 +27,46 @@ 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 String userName = USER_NONE;
|
private SimpleStringProperty userName;
|
||||||
|
private SimpleIntegerProperty serverPort;
|
||||||
|
private SimpleStringProperty serverAddress;
|
||||||
private ObjectProperty<State> stateObjectProperty;
|
private ObjectProperty<State> stateObjectProperty;
|
||||||
|
private ClientMessageList messageList;
|
||||||
|
|
||||||
enum State {
|
enum State {
|
||||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ClientConnectionHandler(ClientMessageList messageList) {
|
||||||
String userName,
|
|
||||||
ChatWindowController controller) {
|
|
||||||
this.connection = connection;
|
|
||||||
this.userName = (userName == null || userName.isBlank())? USER_NONE : userName;
|
|
||||||
this.controller = controller;
|
|
||||||
this.stateObjectProperty = new SimpleObjectProperty<>(NEW);
|
this.stateObjectProperty = new SimpleObjectProperty<>(NEW);
|
||||||
|
connection = null;
|
||||||
|
userName = new SimpleStringProperty(USER_NONE);
|
||||||
|
serverPort = new SimpleIntegerProperty();
|
||||||
|
serverAddress = new SimpleStringProperty();
|
||||||
|
this.messageList = messageList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void openConnection(String serverAddress, int serverPort, String userName) throws IOException{
|
||||||
|
connection = NetworkHandler.openConnection(serverAddress,serverPort);
|
||||||
|
this.userName.set((userName == null || userName.isBlank())? USER_NONE : userName);
|
||||||
|
}
|
||||||
|
|
||||||
public ObjectProperty<State> getStateObjectProperty() {
|
public ObjectProperty<State> getStateObjectProperty() {
|
||||||
return stateObjectProperty;
|
return stateObjectProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SimpleStringProperty getUserNameProperty() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleStringProperty getServerAddressProperty() {
|
||||||
|
return serverAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleIntegerProperty getServerPortProperty() {
|
||||||
|
return serverPort;
|
||||||
|
}
|
||||||
|
|
||||||
public void setState (State newState) {
|
public void setState (State newState) {
|
||||||
this.stateObjectProperty.set(newState);
|
this.stateObjectProperty.set(newState);
|
||||||
}
|
}
|
||||||
|
@ -123,15 +143,16 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
System.err.println("Illegal connect request from server");
|
System.err.println("Illegal connect request from server");
|
||||||
} else if (type.equals(DATA_TYPE_CONFIRM)) {
|
} else if (type.equals(DATA_TYPE_CONFIRM)) {
|
||||||
if (stateObjectProperty.get() == CONFIRM_CONNECT) {
|
if (stateObjectProperty.get() == CONFIRM_CONNECT) {
|
||||||
this.userName = reciever;
|
this.userName.set(reciever);
|
||||||
controller.setUserName(userName);
|
controller.addInfo(payload);//TODO: Make Changelistener
|
||||||
controller.setServerPort(connection.getRemotePort());
|
messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload);
|
||||||
controller.setServerAddress(connection.getRemoteHost());
|
|
||||||
controller.addInfo(payload);
|
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(CONNECTED);
|
this.setState(CONNECTED);
|
||||||
} else if (stateObjectProperty.get() == CONFIRM_DISCONNECT) {
|
} else if (stateObjectProperty.get() == CONFIRM_DISCONNECT) {
|
||||||
controller.addInfo(payload);
|
controller.addInfo(payload);//TODO: Make Changelistener
|
||||||
|
messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload);
|
||||||
|
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
} else {
|
} else {
|
||||||
|
@ -143,6 +164,8 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
controller.addInfo(payload);
|
controller.addInfo(payload);
|
||||||
|
messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload);
|
||||||
|
|
||||||
System.out.println("DISCONNECT: " + payload);
|
System.out.println("DISCONNECT: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
} else if (type.equals(DATA_TYPE_MESSAGE)) {
|
} else if (type.equals(DATA_TYPE_MESSAGE)) {
|
||||||
|
@ -151,16 +174,20 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
controller.addMessage(sender, reciever, payload);
|
controller.addMessage(sender, reciever, payload);
|
||||||
|
messageList.addMessage(ClientMessageList.MessageType.MESSAGE,sender,reciever,payload);
|
||||||
|
|
||||||
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
|
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
|
||||||
} else if (type.equals(DATA_TYPE_ERROR)) {
|
} else if (type.equals(DATA_TYPE_ERROR)) {
|
||||||
controller.addError(payload);
|
controller.addError(payload);
|
||||||
|
messageList.addMessage(ClientMessageList.MessageType.ERROR,sender,reciever,payload);
|
||||||
|
|
||||||
System.out.println("ERROR: " + payload);
|
System.out.println("ERROR: " + payload);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Unknown data type received: " + type);
|
System.out.println("Unknown data type received: " + type);
|
||||||
}
|
}
|
||||||
} catch (ChatProtocolException e) {
|
} catch (ChatProtocolException e) {
|
||||||
System.err.println("Error while processing data: " + e.getMessage());
|
System.err.println("Error while processing data: " + e.getMessage());
|
||||||
sendData(USER_NONE, userName, DATA_TYPE_ERROR, e.getMessage());
|
sendData(USER_NONE, userName.get(), DATA_TYPE_ERROR, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,19 +214,19 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
|
|
||||||
public void connect() throws ChatProtocolException {
|
public void connect() throws ChatProtocolException {
|
||||||
if (stateObjectProperty.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + stateObjectProperty.get());
|
if (stateObjectProperty.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null);
|
this.sendData(userName.get(), USER_NONE, DATA_TYPE_CONNECT,null);
|
||||||
this.setState(CONFIRM_CONNECT);
|
this.setState(CONFIRM_CONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws ChatProtocolException {
|
public void disconnect() throws ChatProtocolException {
|
||||||
if (stateObjectProperty.get() != NEW && stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + stateObjectProperty.get());
|
if (stateObjectProperty.get() != NEW && stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null);
|
this.sendData(userName.get(), USER_NONE, DATA_TYPE_DISCONNECT,null);
|
||||||
this.setState(CONFIRM_DISCONNECT);
|
this.setState(CONFIRM_DISCONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void message(String receiver, String message) throws ChatProtocolException {
|
public void message(String receiver, String message) throws ChatProtocolException {
|
||||||
if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get());
|
if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get());
|
||||||
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
|
this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package ch.zhaw.pm2.multichat.client;
|
package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleListProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -8,9 +10,10 @@ public class ClientMessageList {
|
||||||
private final List<MessageType> typeList = new ArrayList<>();
|
private final List<MessageType> typeList = new ArrayList<>();
|
||||||
private final List<String> senderList = new ArrayList<>();
|
private final List<String> senderList = new ArrayList<>();
|
||||||
private final List<String> receiverList = new ArrayList<>();
|
private final List<String> receiverList = new ArrayList<>();
|
||||||
private final List<String> messageList = new ArrayList<>();
|
private final SimpleListProperty<String> messageList = new SimpleListProperty<>();
|
||||||
private final ChatWindowController gui;
|
private final ChatWindowController gui;
|
||||||
|
|
||||||
|
|
||||||
public ClientMessageList(ChatWindowController gui) {
|
public ClientMessageList(ChatWindowController gui) {
|
||||||
this.gui = gui;
|
this.gui = gui;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +47,10 @@ public class ClientMessageList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SimpleListProperty<String> getMessageListProperty() {
|
||||||
|
return messageList;
|
||||||
|
}
|
||||||
|
|
||||||
public enum MessageType {
|
public enum MessageType {
|
||||||
INFO, MESSAGE, ERROR;
|
INFO, MESSAGE, ERROR;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,14 @@ public class ClientUI extends Application {
|
||||||
private void chatWindow(Stage primaryStage) {
|
private void chatWindow(Stage primaryStage) {
|
||||||
try {
|
try {
|
||||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml"));
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml"));
|
||||||
|
|
||||||
|
// set up Controller
|
||||||
|
|
||||||
|
ChatWindowController controller = loader.getController();
|
||||||
|
ClientMessageList messageList = new ClientMessageList(controller);
|
||||||
|
ClientConnectionHandler connectionHandler = new ClientConnectionHandler(messageList);
|
||||||
|
controller.chatWindowController(messageList,connectionHandler);
|
||||||
|
|
||||||
Pane rootPane = loader.load();
|
Pane rootPane = loader.load();
|
||||||
// fill in scene and stage setup
|
// fill in scene and stage setup
|
||||||
Scene scene = new Scene(rootPane);
|
Scene scene = new Scene(rootPane);
|
||||||
|
|
Loading…
Reference in New Issue