diff --git a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java index f7acd4a..f0e31bb 100644 --- a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java +++ b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java @@ -14,7 +14,7 @@ public abstract class ConnectionHandler { public static final String USER_ALL = "*"; public enum State { - NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; + NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR; } public static String getDataTypeConnect() { diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java index 7e31a36..7c447d6 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java @@ -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(); diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java index c911b7b..c45dfca 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java @@ -50,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); } @@ -72,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); + } } @@ -119,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; @@ -167,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();