From 5257195d8253547362e7d827b527334930c4b8e3 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 15 Apr 2022 21:13:28 +0200 Subject: [PATCH] fixed issue #39 --- .../ch/zhaw/pm2/multichat/server/Server.java | 23 +--------------- .../server/ServerConnectionHandler.java | 27 ++++++++----------- 2 files changed, 12 insertions(+), 38 deletions(-) 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 3167691..d7ef2cd 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 @@ -90,32 +90,11 @@ public class Server { NetworkHandler.NetworkConnection connection = networkServer.waitForConnection(); ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete); new Thread(connectionHandler).start(); - mutex.lock(); - try { - nameComplete.await(); - 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(); - } } } catch(SocketException e) { System.out.println("Server connection terminated"); - } - catch (IOException e) { + } catch (IOException e) { System.err.println("Communication error " + e); - } catch (InterruptedException e) { - throw new RuntimeException(e); } // close server System.out.println("Server Stopped."); 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 1ab73ed..9a7cb91 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 @@ -194,28 +194,23 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab private void caseConnect(String sender) throws ChatProtocolException { if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state); if (sender.isBlank()) sender = this.userName; + //if username not valid if (connectionRegistry.containsKey(sender)) { - mutex.lock(); - try { - state = ERROR; - nameComplete.signal(); - } - finally { - mutex.unlock(); - } + state = ERROR; + System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.", + getConnection().getRemoteHost(), + getConnection().getRemotePort())); throw new ChatProtocolException("User name already taken: " + sender); } - mutex.lock(); - try { - this.userName = sender; - nameComplete.signal(); - } - finally { - mutex.unlock(); - } + //if username valid + this.userName = sender; connectionRegistry.put(userName, this); sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName); this.state = CONNECTED; + System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>", + userName, + getConnection().getRemoteHost(), + getConnection().getRemotePort())); } /**