Shrink code in the ConnectionHandlers

This commit is contained in:
Andrin Fassbind 2022-04-15 16:39:52 +02:00 committed by Leonardo Brandenberger
parent 5022974427
commit a94a4c81d8
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,58 +106,13 @@ 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())) {
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state); caseConnect(sender.toString());
if (sender.toString().isBlank()) sender.append(this.userName);
if (connectionRegistry.containsKey(sender.toString())) {
mutex.lock();
try {
state = ERROR;
nameComplete.signal();
}
finally {
mutex.unlock();
}
throw new ChatProtocolException("User name already taken: " + sender);
}
mutex.lock();
try {
this.userName = sender.toString();
nameComplete.signal();
}
finally {
mutex.unlock();
}
connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName);
this.state = CONNECTED;
} else if (type.toString().equals(getDataTypeConfirm())) { } else if (type.toString().equals(getDataTypeConfirm())) {
System.out.println("Not expecting to receive a CONFIRM request from client"); System.out.println("Not expecting to receive a CONFIRM request from client");
} else if (type.toString().equals(getDataTypeDisconnect())) { } else if (type.toString().equals(getDataTypeDisconnect())) {
if (state == DISCONNECTED) caseDisconnect();
throw new ChatProtocolException("Illegal state for disconnect request: " + state);
if (state == CONNECTED) {
connectionRegistry.remove(this.userName);
}
sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName);
this.state = DISCONNECTED;
this.stopReceiving();
} else if (type.toString().equals(getDataTypeMessage())) { } else if (type.toString().equals(getDataTypeMessage())) {
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state); caseMessage(sender.toString(), reciever.toString(), type.toString(), payload.toString());
if (USER_ALL.equals(reciever.toString())) {
for (ServerConnectionHandler handler : connectionRegistry.values()) {
handler.sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString());
}
} else {
ServerConnectionHandler handler = connectionRegistry.get(reciever.toString());
if (handler != null) {
handler.sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString());
if(!reciever.toString().equals(sender.toString())){
sendData(sender.toString(), reciever.toString(), type.toString(), payload.toString()); //send message to sender if it's a direct message and sender is not receiver.
}
} else {
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
}
}
} else if (type.toString().equals(getDataTypeError())) { } else if (type.toString().equals(getDataTypeError())) {
System.err.println("Received error from client (" + sender + "): " + payload); System.err.println("Received error from client (" + sender + "): " + payload);
} else { } else {
@ -169,4 +124,61 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
sendData(USER_NONE, userName, getDataTypeError(), 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 (sender.isBlank()) sender = this.userName;
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.toString();
nameComplete.signal();
}
finally {
mutex.unlock();
}
connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName);
this.state = CONNECTED;
}
private void caseDisconnect() throws ChatProtocolException {
if (state == DISCONNECTED)
throw new ChatProtocolException("Illegal state for disconnect request: " + state);
if (state == CONNECTED) {
connectionRegistry.remove(this.userName);
}
sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName);
this.state = DISCONNECTED;
this.stopReceiving();
}
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 (USER_ALL.equals(reciever)) {
for (ServerConnectionHandler handler : connectionRegistry.values()) {
handler.sendData(sender, reciever, type, payload);
}
} else {
ServerConnectionHandler handler = connectionRegistry.get(reciever);
if (handler != null) {
handler.sendData(sender, reciever, type, payload);
if(!reciever.equals(sender)){
sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
}
} else {
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
}
}
}
} }