Make State in ClientConnectionHandler property
Add change listener for stateproperty in ChatWindowController fixed Issue #21
This commit is contained in:
parent
a0b7b363c3
commit
1777b62582
|
@ -4,6 +4,8 @@ import ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State;
|
|||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
|
@ -52,7 +54,7 @@ public class ChatWindowController {
|
|||
|
||||
@FXML
|
||||
private void toggleConnection () {
|
||||
if (connectionHandler == null || connectionHandler.getState() != CONNECTED) {
|
||||
if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) {
|
||||
connect();
|
||||
} else {
|
||||
disconnect();
|
||||
|
@ -112,6 +114,9 @@ public class ChatWindowController {
|
|||
this);
|
||||
new Thread(connectionHandler).start();
|
||||
|
||||
//register Listener
|
||||
startListener();
|
||||
|
||||
// register window close handler
|
||||
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
||||
}
|
||||
|
@ -192,4 +197,13 @@ public class ChatWindowController {
|
|||
|
||||
}
|
||||
|
||||
public void startListener() {
|
||||
connectionHandler.getStateProperty().addListener(new ChangeListener<State>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
|
||||
stateChanged(newValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.client;
|
|||
|
||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
|
@ -29,7 +30,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
|
||||
|
||||
private String userName = USER_NONE;
|
||||
private State state = NEW;
|
||||
private SimpleObjectProperty<State> state;
|
||||
|
||||
enum State {
|
||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
||||
|
@ -41,15 +42,16 @@ public class ClientConnectionHandler implements Runnable {
|
|||
this.connection = connection;
|
||||
this.userName = (userName == null || userName.isBlank())? USER_NONE : userName;
|
||||
this.controller = controller;
|
||||
state = new SimpleObjectProperty<>(NEW);
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
public SimpleObjectProperty<State> getStateProperty() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public void setState (State newState) {
|
||||
this.state = newState;
|
||||
controller.stateChanged(newState);
|
||||
state.set(newState);
|
||||
|
||||
}
|
||||
|
||||
public void run () {
|
||||
|
@ -123,7 +125,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||
if (type.equals(DATA_TYPE_CONNECT)) {
|
||||
System.err.println("Illegal connect request from server");
|
||||
} else if (type.equals(DATA_TYPE_CONFIRM)) {
|
||||
if (state == CONFIRM_CONNECT) {
|
||||
if (state.get() == CONFIRM_CONNECT) {
|
||||
this.userName = reciever;
|
||||
controller.setUserName(userName);
|
||||
controller.setServerPort(connection.getRemotePort());
|
||||
|
@ -131,7 +133,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||
controller.addInfo(payload);
|
||||
System.out.println("CONFIRM: " + payload);
|
||||
this.setState(CONNECTED);
|
||||
} else if (state == CONFIRM_DISCONNECT) {
|
||||
} else if (state.get() == CONFIRM_DISCONNECT) {
|
||||
controller.addInfo(payload);
|
||||
System.out.println("CONFIRM: " + payload);
|
||||
this.setState(DISCONNECTED);
|
||||
|
@ -139,7 +141,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||
System.err.println("Got unexpected confirm message: " + payload);
|
||||
}
|
||||
} else if (type.equals(DATA_TYPE_DISCONNECT)) {
|
||||
if (state == DISCONNECTED) {
|
||||
if (state.get() == DISCONNECTED) {
|
||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
||||
return;
|
||||
}
|
||||
|
@ -147,7 +149,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||
System.out.println("DISCONNECT: " + payload);
|
||||
this.setState(DISCONNECTED);
|
||||
} else if (type.equals(DATA_TYPE_MESSAGE)) {
|
||||
if (state != CONNECTED) {
|
||||
if (state.get() != CONNECTED) {
|
||||
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
|
||||
return;
|
||||
}
|
||||
|
@ -187,19 +189,19 @@ public class ClientConnectionHandler implements Runnable {
|
|||
}
|
||||
|
||||
public void connect() throws ChatProtocolException {
|
||||
if (state != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
||||
if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
||||
this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null);
|
||||
this.setState(CONFIRM_CONNECT);
|
||||
}
|
||||
|
||||
public void disconnect() throws ChatProtocolException {
|
||||
if (state != NEW && state != 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, USER_NONE, DATA_TYPE_DISCONNECT,null);
|
||||
this.setState(CONFIRM_DISCONNECT);
|
||||
}
|
||||
|
||||
public boolean message(String messageString) throws ChatProtocolException {
|
||||
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
||||
if (state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
||||
|
||||
Matcher matcher = messagePattern.matcher(messageString);
|
||||
if (matcher.find()) {
|
||||
|
|
Loading…
Reference in New Issue