Compare commits

...
Author SHA1 Message Date
schrom01 bc86c47871 fixed Method Initializing. Setting Datafields serverAddress and serverPort.
#24
2022-04-15 01:26:24 +02:00
Roman SchenkandGitHub Enterprise a75d3466ef Merge pull request #41 from PM2-IT21bWIN-ruiz-mach-krea/Server_Console_Display
Fixes issue #37
2022-04-15 01:07:22 +02:00
Leonardo Brandenberger 97b6fff82e fixes #37 2022-04-15 00:14:22 +02:00
fassbandandGitHub Enterprise 2a8b701f48 Merge pull request #40 from PM2-IT21bWIN-ruiz-mach-krea/Refactoring_ClientConnectionHandler
Refactoring client connection handler
2022-04-14 22:24:13 +02:00
schrom01 86aa801b34 removed Enum State from ServerConnectionHandler.java because it's in ConnectionHandler.java now. 2022-04-14 22:23:49 +02:00
schrom01 2277fee73a merging 2022-04-14 22:12:35 +02:00
schrom01 d8dbd93c15 Merge branch 'main' into Refactoring_ClientConnectionHandler
# Conflicts:
#	client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java
#	client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java
2022-04-14 22:02:39 +02:00
Roman SchenkandGitHub Enterprise 6e8e560d73 Merge pull request #38 from PM2-IT21bWIN-ruiz-mach-krea/ConnectionHandlerClass
Connection handler class
2022-04-14 21:51:59 +02:00
schrom01 615b3844e3 refactoring of ClientConnectionHandler
Instance is now created in ClientUI.java
solved #24 and #15
2022-04-14 21:13:36 +02:00
Roman SchenkandGitHub Enterprise 4a998b0f61 Merge pull request #36 from PM2-IT21bWIN-ruiz-mach-krea/SpecialCharacterUsername
Special character username
2022-04-14 20:39:05 +02:00
Andrin Fassbind b6fd5b569d fixed Issue #28 by checking if username contains space before starting connection 2022-04-14 19:33:18 +02:00
Andrin Fassbind b47d98b960 fixed Issue #19 by changing messagePattern 2022-04-14 12:11:03 +02:00
6 changed files with 76 additions and 48 deletions
@@ -47,6 +47,10 @@ public class ChatWindowController {
messageListener();
}
public void setConnectionHandler(ClientConnectionHandler connectionHandler){
this.connectionHandler = connectionHandler;
}
private void applicationClose() {
disconnect();
}
@@ -105,30 +109,23 @@ public class ChatWindowController {
private void startConnectionHandler() throws IOException {
String userName = userNameField.getText();
String serverAddress = serverAddressField.getText();
int serverPort = Integer.parseInt(serverPortField.getText());
connectionHandler = new ClientConnectionHandler(
NetworkHandler.openConnection(serverAddress, serverPort), userName,
messages);
new Thread(connectionHandler).start();
if(!userName.contains(" ")) {
String serverAddress = serverAddressField.getText();
int serverPort = Integer.parseInt(serverPortField.getText());
connectionHandler.initialize(serverAddress, serverPort, userName);
new Thread(connectionHandler).start();
//register Listener
startListener();
//register Listener
startListener();
// register window close handler
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
}
private void terminateConnectionHandler() {
// unregister window close handler
rootPane.getScene().getWindow().removeEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
if (connectionHandler != null) {
connectionHandler.stopReceiving();
connectionHandler = null;
// register window close handler
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
} else {
addError("It is not allowed to have spaces in username!");
}
}
public void stateChanged(ConnectionHandler.State newState) {
public void stateChanged(State newState) {
// update UI (need to be run in UI thread: see Platform.runLater())
Platform.runLater(new Runnable() {
@Override
@@ -136,8 +133,8 @@ public class ChatWindowController {
connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect");
}
});
if (newState == DISCONNECTED) {
terminateConnectionHandler();
if(newState == DISCONNECTED){
connectionHandler.stopReceiving();
}
}
@@ -17,7 +17,7 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
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 SimpleObjectProperty<State> state;
@@ -25,17 +25,27 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
private SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort;
public ClientConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
String userName,
ClientMessageList messages) {
super(connection);
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
public ClientConnectionHandler(ClientMessageList messages) {
super();
this.messages = messages;
state = new SimpleObjectProperty<>(State.NEW);
serverAddress = new SimpleStringProperty();
serverPort = new SimpleIntegerProperty();
}
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
this.state = new SimpleObjectProperty<>(NEW);
this.serverAddress = new SimpleStringProperty(serverAddress);
this.serverPort = new SimpleIntegerProperty(serverPort);
this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName);
try {
setConnection(NetworkHandler.openConnection(this.serverAddress.get(), this.serverPort.get()));
} catch (IOException e) {
messages.addMessage(new Message(Message.MessageType.ERROR,null,null,e.getMessage()));
System.out.println("ERROR: " + e.getMessage());
}
}
public SimpleStringProperty getServerAddressProperty() { return serverAddress; }
public SimpleIntegerProperty getServerPortProperty() { return serverPort; }
@@ -48,7 +58,6 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
public void setState (State newState) {
state.set(newState);
}
public void run () {
@@ -8,6 +8,7 @@ import javafx.stage.Stage;
public class ClientUI extends Application {
private ClientMessageList clientMessageList = new ClientMessageList();
private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
@Override
public void start(Stage primaryStage) {
@@ -21,6 +22,7 @@ public class ClientUI extends Application {
ChatWindowController chatWindowController = loader.getController();
chatWindowController.setMessages(clientMessageList);
chatWindowController.setConnectionHandler(connectionHandler);
// fill in scene and stage setup
Scene scene = new Scene(rootPane);
@@ -14,11 +14,7 @@ public abstract class ConnectionHandler {
public static final String USER_ALL = "*";
public enum State {
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
}
public ConnectionHandler(NetworkHandler.NetworkConnection<String> connection) {
this.connection = connection;
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
}
public static String getDataTypeConnect() {
@@ -45,7 +41,7 @@ public abstract class ConnectionHandler {
return connection;
}
protected void setConnection() {
protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
this.connection = connection;
}
}
@@ -1,5 +1,6 @@
package ch.zhaw.pm2.multichat.server;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import java.io.IOException;
@@ -75,11 +76,17 @@ public class Server {
mutex.lock();
try {
nameComplete.await();
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
connectionHandler.getUserName(),
connection.getRemoteHost(),
connection.getRemotePort()
));
if(connectionHandler.getState() == ConnectionHandler.State.ERROR) {
System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.",
connection.getRemoteHost(),
connection.getRemotePort()));
}
else {
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
connectionHandler.getUserName(),
connection.getRemoteHost(),
connection.getRemotePort()));
}
}
finally {
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.ConnectionHandler;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
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.ReentrantLock;
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
public class ServerConnectionHandler extends ConnectionHandler implements Runnable{
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
@@ -34,13 +35,10 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
startReceiving();
}
enum State {
NEW, CONNECTED, DISCONNECTED;
}
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
super(connection);
super();
setConnection(connection);
Objects.requireNonNull(connection, "Connection must not be null");
Objects.requireNonNull(registry, "Registry must not be null");
this.connectionRegistry = registry;
@@ -52,11 +50,15 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
return this.userName;
}
public State getState() {
return state;
}
private void startReceiving() {
System.out.println("Starting Connection Handler for new User");
try {
System.out.println("Start receiving data...");
while (getConnection().isAvailable()) {
while (getConnection().isAvailable() && !(state == ERROR)) {
String data = getConnection().receive();
processData(data);
}
@@ -74,7 +76,11 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
} catch (ClassNotFoundException e) {
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);
}
}
@@ -121,8 +127,17 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
if (type.equals(getDataTypeConnect())) {
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
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);
}
mutex.lock();
try {
this.userName = sender;
@@ -169,11 +184,13 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
}
} 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());
}
}
private void sendData(String sender, String receiver, String type, String payload) {
if (getConnection().isAvailable()) {
new StringBuilder();