Shrink code in the ConnectionHandlers

This commit is contained in:
Andrin Fassbind 2022-04-15 16:39:52 +02:00
parent 37045ade15
commit 9a534b2465
2 changed files with 104 additions and 77 deletions

View File

@ -110,38 +110,13 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
if (type.toString().equals(getDataTypeConnect())) { if (type.toString().equals(getDataTypeConnect())) {
System.err.println("Illegal connect request from server"); System.err.println("Illegal connect request from server");
} else if (type.toString().equals(getDataTypeConfirm())) { } else if (type.toString().equals(getDataTypeConfirm())) {
if (state.get() == CONFIRM_CONNECT) { caseConfirm(sender.toString(), reciever.toString(), payload.toString());
this.userName.set(reciever.toString());
this.serverPort.set(getConnection().getRemotePort());
this.serverAddress.set(getConnection().getRemoteHost());
messages.addMessage(new Message(Message.MessageType.INFO,sender.toString(),reciever.toString(),payload.toString()));
System.out.println("CONFIRM: " + payload);
this.setState(CONNECTED);
} else if (state.get() == CONFIRM_DISCONNECT) {
messages.addMessage(new Message(Message.MessageType.INFO,sender.toString(),reciever.toString(),payload.toString()));
System.out.println("CONFIRM: " + payload);
this.setState(DISCONNECTED);
} else {
System.err.println("Got unexpected confirm message: " + payload);
}
} else if (type.toString().equals(getDataTypeDisconnect())) { } else if (type.toString().equals(getDataTypeDisconnect())) {
if (state.get() == DISCONNECTED) { caseDisconnect(sender.toString(),reciever.toString(),payload.toString());
System.out.println("DISCONNECT: Already in disconnected: " + payload);
return;
}
messages.addMessage(new Message(Message.MessageType.INFO,sender.toString(),reciever.toString(),payload.toString()));
System.out.println("DISCONNECT: " + payload);
this.setState(DISCONNECTED);
} else if (type.toString().equals(getDataTypeMessage())) { } else if (type.toString().equals(getDataTypeMessage())) {
if (state.get() != CONNECTED) { caseMessage(sender.toString(),reciever.toString(),payload.toString());
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
return;
}
messages.addMessage(new Message(Message.MessageType.MESSAGE,sender.toString(),reciever.toString(),payload.toString()));
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
} else if (type.toString().equals(getDataTypeError())) { } else if (type.toString().equals(getDataTypeError())) {
messages.addMessage(new Message(Message.MessageType.ERROR,sender.toString(),reciever.toString(),payload.toString())); caseError(sender.toString(), reciever.toString(), payload.toString());
System.out.println("ERROR: " + payload);
} else { } else {
System.out.println("Unknown data type received: " + type); System.out.println("Unknown data type received: " + type);
} }
@ -151,6 +126,46 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
} }
} }
private void caseConfirm(String sender, String reciever, String payload) {
if (state.get() == CONFIRM_CONNECT) {
this.userName.set(reciever);
this.serverPort.set(getConnection().getRemotePort());
this.serverAddress.set(getConnection().getRemoteHost());
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
System.out.println("CONFIRM: " + payload);
this.setState(CONNECTED);
} else if (state.get() == CONFIRM_DISCONNECT) {
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
System.out.println("CONFIRM: " + payload);
this.setState(DISCONNECTED);
} else {
System.err.println("Got unexpected confirm message: " + payload);
}
}
private void caseDisconnect(String sender, String reciever, String payload) {
if (state.get() == DISCONNECTED) {
System.out.println("DISCONNECT: Already in disconnected: " + payload);
return;
}
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
System.out.println("DISCONNECT: " + payload);
this.setState(DISCONNECTED);
}
private void caseMessage(String sender, String reciever, String payload) {
if (state.get() != CONNECTED) {
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
return;
}
messages.addMessage(new Message(Message.MessageType.MESSAGE,sender,reciever,payload));
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
}
private void caseError(String sender, String reciever, String payload) {
messages.addMessage(new Message(Message.MessageType.ERROR,sender,reciever,payload));
System.out.println("ERROR: " + payload);
}
public void connect() throws ChatProtocolException { public void connect() throws ChatProtocolException {
if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);

View File

@ -106,9 +106,29 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
// dispatch operation based on type parameter // dispatch operation based on type parameter
if (type.toString().equals(getDataTypeConnect())) { if (type.toString().equals(getDataTypeConnect())) {
caseConnect(sender.toString());
} else if (type.toString().equals(getDataTypeConfirm())) {
System.out.println("Not expecting to receive a CONFIRM request from client");
} else if (type.toString().equals(getDataTypeDisconnect())) {
caseDisconnect();
} else if (type.toString().equals(getDataTypeMessage())) {
caseMessage(sender.toString(), reciever.toString(), type.toString(), payload.toString());
} else if (type.toString().equals(getDataTypeError())) {
System.err.println("Received error from client (" + sender + "): " + payload);
} else {
System.err.println("Unknown data type received: " + type);
}
} catch(ChatProtocolException e) {
System.out.println("Error while processing data " + e.getMessage());
sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
}
}
private void caseConnect(String sender) throws ChatProtocolException {
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.toString().isBlank()) sender.append(this.userName); if (sender.isBlank()) sender = this.userName;
if (connectionRegistry.containsKey(sender.toString())) { if (connectionRegistry.containsKey(sender)) {
mutex.lock(); mutex.lock();
try { try {
state = ERROR; state = ERROR;
@ -130,9 +150,9 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
connectionRegistry.put(userName, this); connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName); sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName);
this.state = CONNECTED; this.state = CONNECTED;
} else if (type.toString().equals(getDataTypeConfirm())) { }
System.out.println("Not expecting to receive a CONFIRM request from client");
} else if (type.toString().equals(getDataTypeDisconnect())) { private void caseDisconnect() throws ChatProtocolException {
if (state == DISCONNECTED) if (state == DISCONNECTED)
throw new ChatProtocolException("Illegal state for disconnect request: " + state); throw new ChatProtocolException("Illegal state for disconnect request: " + state);
if (state == CONNECTED) { if (state == CONNECTED) {
@ -141,32 +161,24 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName); sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName);
this.state = DISCONNECTED; this.state = DISCONNECTED;
this.stopReceiving(); this.stopReceiving();
} else if (type.toString().equals(getDataTypeMessage())) { }
private void caseMessage(String sender, String reciever, String type, String payload) throws ChatProtocolException{
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state); if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state);
if (USER_ALL.equals(reciever.toString())) { if (USER_ALL.equals(reciever)) {
for (ServerConnectionHandler handler : connectionRegistry.values()) { for (ServerConnectionHandler handler : connectionRegistry.values()) {
handler.sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString()); handler.sendData(sender, reciever, type, payload);
} }
} else { } else {
ServerConnectionHandler handler = connectionRegistry.get(reciever.toString()); ServerConnectionHandler handler = connectionRegistry.get(reciever);
if (handler != null) { if (handler != null) {
handler.sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString()); handler.sendData(sender, reciever, type, payload);
if(!reciever.toString().equals(sender.toString())){ if(!reciever.equals(sender)){
sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString()); //send message to sender if it's a direct message and sender is not receiver. sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
} }
} else { } else {
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever); this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
} }
} }
} else if (type.toString().equals(getDataTypeError())) {
System.err.println("Received error from client (" + sender + "): " + payload);
} else {
System.err.println("Unknown data type received: " + type);
}
} catch(ChatProtocolException e) {
System.out.println("Error while processing data " + e.getMessage());
sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
}
} }
} }