Shrink code in the ConnectionHandlers
This commit is contained in:
parent
37045ade15
commit
9a534b2465
|
@ -110,38 +110,13 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
if (type.toString().equals(getDataTypeConnect())) {
|
||||
System.err.println("Illegal connect request from server");
|
||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||
if (state.get() == CONFIRM_CONNECT) {
|
||||
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);
|
||||
}
|
||||
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||
if (state.get() == DISCONNECTED) {
|
||||
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);
|
||||
caseDisconnect(sender.toString(),reciever.toString(),payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||
if (state.get() != CONNECTED) {
|
||||
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);
|
||||
caseMessage(sender.toString(),reciever.toString(),payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeError())) {
|
||||
messages.addMessage(new Message(Message.MessageType.ERROR,sender.toString(),reciever.toString(),payload.toString()));
|
||||
System.out.println("ERROR: " + payload);
|
||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else {
|
||||
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 {
|
||||
if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state);
|
||||
|
|
|
@ -106,58 +106,13 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
|
||||
// dispatch operation based on type parameter
|
||||
if (type.toString().equals(getDataTypeConnect())) {
|
||||
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
|
||||
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;
|
||||
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())) {
|
||||
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();
|
||||
caseDisconnect();
|
||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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 {
|
||||
|
@ -169,4 +124,61 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue