This commit is contained in:
Leonardo Brandenberger 2022-04-15 00:14:22 +02:00
parent 2a8b701f48
commit 97b6fff82e
3 changed files with 35 additions and 9 deletions

View File

@ -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() {

View File

@ -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();

View File

@ -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();