Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97b6fff82e | ||
|
|
2a8b701f48 | ||
|
|
86aa801b34 | ||
|
|
2277fee73a | ||
|
|
d8dbd93c15 | ||
|
|
6e8e560d73 | ||
|
|
615b3844e3 | ||
|
|
4a998b0f61 | ||
|
|
b6fd5b569d | ||
|
|
b47d98b960 |
@@ -47,6 +47,10 @@ public class ChatWindowController {
|
|||||||
messageListener();
|
messageListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setConnectionHandler(ClientConnectionHandler connectionHandler){
|
||||||
|
this.connectionHandler = connectionHandler;
|
||||||
|
}
|
||||||
|
|
||||||
private void applicationClose() {
|
private void applicationClose() {
|
||||||
disconnect();
|
disconnect();
|
||||||
}
|
}
|
||||||
@@ -105,30 +109,23 @@ public class ChatWindowController {
|
|||||||
|
|
||||||
private void startConnectionHandler() throws IOException {
|
private void startConnectionHandler() throws IOException {
|
||||||
String userName = userNameField.getText();
|
String userName = userNameField.getText();
|
||||||
String serverAddress = serverAddressField.getText();
|
if(!userName.contains(" ")) {
|
||||||
int serverPort = Integer.parseInt(serverPortField.getText());
|
String serverAddress = serverAddressField.getText();
|
||||||
connectionHandler = new ClientConnectionHandler(
|
int serverPort = Integer.parseInt(serverPortField.getText());
|
||||||
NetworkHandler.openConnection(serverAddress, serverPort), userName,
|
connectionHandler.initialize(serverAddress, serverPort, userName);
|
||||||
messages);
|
new Thread(connectionHandler).start();
|
||||||
new Thread(connectionHandler).start();
|
|
||||||
|
|
||||||
//register Listener
|
//register Listener
|
||||||
startListener();
|
startListener();
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
} else {
|
||||||
|
addError("It is not allowed to have spaces in username!");
|
||||||
private void terminateConnectionHandler() {
|
|
||||||
// unregister window close handler
|
|
||||||
rootPane.getScene().getWindow().removeEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
|
||||||
if (connectionHandler != null) {
|
|
||||||
connectionHandler.stopReceiving();
|
|
||||||
connectionHandler = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stateChanged(ConnectionHandler.State newState) {
|
public void stateChanged(State newState) {
|
||||||
// update UI (need to be run in UI thread: see Platform.runLater())
|
// update UI (need to be run in UI thread: see Platform.runLater())
|
||||||
Platform.runLater(new Runnable() {
|
Platform.runLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -136,8 +133,8 @@ public class ChatWindowController {
|
|||||||
connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect");
|
connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (newState == DISCONNECTED) {
|
if(newState == DISCONNECTED){
|
||||||
terminateConnectionHandler();
|
connectionHandler.stopReceiving();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
|||||||
|
|
||||||
public class ClientConnectionHandler extends ConnectionHandler implements Runnable {
|
public class ClientConnectionHandler extends ConnectionHandler implements Runnable {
|
||||||
|
|
||||||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
|
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\S*))?\\s*(.*)$" );
|
||||||
|
|
||||||
private SimpleStringProperty userName;
|
private SimpleStringProperty userName;
|
||||||
private SimpleObjectProperty<State> state;
|
private SimpleObjectProperty<State> state;
|
||||||
@@ -25,17 +25,20 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
private SimpleStringProperty serverAddress;
|
private SimpleStringProperty serverAddress;
|
||||||
private SimpleIntegerProperty serverPort;
|
private SimpleIntegerProperty serverPort;
|
||||||
|
|
||||||
public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ClientConnectionHandler(ClientMessageList messages) {
|
||||||
String userName,
|
super();
|
||||||
ClientMessageList messages) {
|
|
||||||
super(connection);
|
|
||||||
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
|
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
state = new SimpleObjectProperty<>(State.NEW);
|
state = new SimpleObjectProperty<>(State.NEW);
|
||||||
serverAddress = new SimpleStringProperty();
|
serverAddress = new SimpleStringProperty();
|
||||||
serverPort = new SimpleIntegerProperty();
|
serverPort = new SimpleIntegerProperty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
|
||||||
|
state = new SimpleObjectProperty<>(NEW);
|
||||||
|
setConnection(NetworkHandler.openConnection(serverAddress, serverPort));
|
||||||
|
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
|
||||||
|
}
|
||||||
|
|
||||||
public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
|
public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
|
||||||
|
|
||||||
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
|
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
|
||||||
@@ -48,7 +51,6 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
|
|
||||||
public void setState (State newState) {
|
public void setState (State newState) {
|
||||||
state.set(newState);
|
state.set(newState);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run () {
|
public void run () {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import javafx.stage.Stage;
|
|||||||
|
|
||||||
public class ClientUI extends Application {
|
public class ClientUI extends Application {
|
||||||
private ClientMessageList clientMessageList = new ClientMessageList();
|
private ClientMessageList clientMessageList = new ClientMessageList();
|
||||||
|
private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
@@ -21,6 +22,7 @@ public class ClientUI extends Application {
|
|||||||
|
|
||||||
ChatWindowController chatWindowController = loader.getController();
|
ChatWindowController chatWindowController = loader.getController();
|
||||||
chatWindowController.setMessages(clientMessageList);
|
chatWindowController.setMessages(clientMessageList);
|
||||||
|
chatWindowController.setConnectionHandler(connectionHandler);
|
||||||
|
|
||||||
// fill in scene and stage setup
|
// fill in scene and stage setup
|
||||||
Scene scene = new Scene(rootPane);
|
Scene scene = new Scene(rootPane);
|
||||||
|
|||||||
@@ -14,11 +14,7 @@ public abstract class ConnectionHandler {
|
|||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
public enum State {
|
public enum State {
|
||||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
|
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
||||||
}
|
|
||||||
|
|
||||||
public ConnectionHandler(NetworkHandler.NetworkConnection<String> connection) {
|
|
||||||
this.connection = connection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getDataTypeConnect() {
|
public static String getDataTypeConnect() {
|
||||||
@@ -45,7 +41,7 @@ public abstract class ConnectionHandler {
|
|||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setConnection() {
|
protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ch.zhaw.pm2.multichat.server;
|
package ch.zhaw.pm2.multichat.server;
|
||||||
|
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -75,11 +76,17 @@ public class Server {
|
|||||||
mutex.lock();
|
mutex.lock();
|
||||||
try {
|
try {
|
||||||
nameComplete.await();
|
nameComplete.await();
|
||||||
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
|
if(connectionHandler.getState() == ConnectionHandler.State.ERROR) {
|
||||||
connectionHandler.getUserName(),
|
System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.",
|
||||||
connection.getRemoteHost(),
|
connection.getRemoteHost(),
|
||||||
connection.getRemotePort()
|
connection.getRemotePort()));
|
||||||
));
|
}
|
||||||
|
else {
|
||||||
|
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
|
||||||
|
connectionHandler.getUserName(),
|
||||||
|
connection.getRemoteHost(),
|
||||||
|
connection.getRemotePort()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.server;
|
|||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
|
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
@@ -15,7 +16,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
import java.util.concurrent.locks.Condition;
|
import java.util.concurrent.locks.Condition;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
|
|
||||||
|
|
||||||
public class ServerConnectionHandler extends ConnectionHandler implements Runnable{
|
public class ServerConnectionHandler extends ConnectionHandler implements Runnable{
|
||||||
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
|
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
|
||||||
@@ -34,13 +35,10 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
startReceiving();
|
startReceiving();
|
||||||
}
|
}
|
||||||
|
|
||||||
enum State {
|
|
||||||
NEW, CONNECTED, DISCONNECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
||||||
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
|
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
|
||||||
super(connection);
|
super();
|
||||||
|
setConnection(connection);
|
||||||
Objects.requireNonNull(connection, "Connection must not be null");
|
Objects.requireNonNull(connection, "Connection must not be null");
|
||||||
Objects.requireNonNull(registry, "Registry must not be null");
|
Objects.requireNonNull(registry, "Registry must not be null");
|
||||||
this.connectionRegistry = registry;
|
this.connectionRegistry = registry;
|
||||||
@@ -52,11 +50,15 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
return this.userName;
|
return this.userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
private void startReceiving() {
|
private void startReceiving() {
|
||||||
System.out.println("Starting Connection Handler for new User");
|
System.out.println("Starting Connection Handler for new User");
|
||||||
try {
|
try {
|
||||||
System.out.println("Start receiving data...");
|
System.out.println("Start receiving data...");
|
||||||
while (getConnection().isAvailable()) {
|
while (getConnection().isAvailable() && !(state == ERROR)) {
|
||||||
String data = getConnection().receive();
|
String data = getConnection().receive();
|
||||||
processData(data);
|
processData(data);
|
||||||
}
|
}
|
||||||
@@ -74,7 +76,11 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
} 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());
|
||||||
}
|
}
|
||||||
|
if (state == ERROR) {
|
||||||
|
System.out.println("Stopping Connection Handler for Rejected Client");
|
||||||
|
} else {
|
||||||
System.out.println("Stopping Connection Handler for " + userName);
|
System.out.println("Stopping Connection Handler for " + userName);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,8 +127,17 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
if (type.equals(getDataTypeConnect())) {
|
if (type.equals(getDataTypeConnect())) {
|
||||||
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
|
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
|
||||||
if (sender == null || sender.isBlank()) sender = this.userName;
|
if (sender == null || sender.isBlank()) sender = this.userName;
|
||||||
if (connectionRegistry.containsKey(sender))
|
if (connectionRegistry.containsKey(sender)) {
|
||||||
|
mutex.lock();
|
||||||
|
try {
|
||||||
|
state = ERROR;
|
||||||
|
nameComplete.signal();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
mutex.unlock();
|
||||||
|
}
|
||||||
throw new ChatProtocolException("User name already taken: " + sender);
|
throw new ChatProtocolException("User name already taken: " + sender);
|
||||||
|
}
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
try {
|
try {
|
||||||
this.userName = sender;
|
this.userName = sender;
|
||||||
@@ -169,11 +184,13 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||||||
|
|
||||||
}
|
}
|
||||||
} catch(ChatProtocolException e) {
|
} catch(ChatProtocolException e) {
|
||||||
System.out.println("Error while processing data" + e.getMessage());
|
System.out.println("Error while processing data " + e.getMessage());
|
||||||
sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
|
sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void sendData(String sender, String receiver, String type, String payload) {
|
private void sendData(String sender, String receiver, String type, String payload) {
|
||||||
if (getConnection().isAvailable()) {
|
if (getConnection().isAvailable()) {
|
||||||
new StringBuilder();
|
new StringBuilder();
|
||||||
|
|||||||
Reference in New Issue
Block a user