Attempt to send obj
This commit is contained in:
@@ -5,6 +5,7 @@ import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||
|
||||
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||
|
||||
import ch.zhaw.pm2.multichat.protocol.Message;
|
||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||
|
||||
import java.io.EOFException;
|
||||
@@ -84,7 +85,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||
try {
|
||||
System.out.println("Start receiving data...");
|
||||
while (getConnection().isAvailable() && !(state == ERROR)) {
|
||||
String data = getConnection().receive();
|
||||
Message data = getConnection().receive();
|
||||
processData(data);
|
||||
}
|
||||
System.out.println("Stopped receiving data");
|
||||
@@ -136,28 +137,21 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||
*
|
||||
* @param data received by the server
|
||||
*/
|
||||
private void processData(String data) {
|
||||
private void processData(Message data) {
|
||||
try {
|
||||
Scanner scanner = new Scanner(data);
|
||||
StringBuilder sender = new StringBuilder();
|
||||
StringBuilder receiver = new StringBuilder();
|
||||
StringBuilder type = new StringBuilder();
|
||||
StringBuilder payload = new StringBuilder();
|
||||
super.processData(scanner, sender, receiver, type, payload);
|
||||
|
||||
// dispatch operation based on type parameter
|
||||
if (type.toString().equals(getDataTypeConnect())) {
|
||||
caseConnect(sender.toString());
|
||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||
if (data.getType() == DATA_TYPE.DATA_TYPE_CONNECT) {
|
||||
caseConnect(data);
|
||||
} else if (data.getType() == DATA_TYPE.DATA_TYPE_CONFIRM) {
|
||||
System.out.println("Not expecting to receive a CONFIRM request from client");
|
||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||
} else if (data.getType() == DATA_TYPE.DATA_TYPE_DISCONNECT) {
|
||||
caseDisconnect();
|
||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||
caseMessage(sender.toString(), receiver.toString(), type.toString(), payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeError())) {
|
||||
System.err.println("Received error from client (" + sender + "): " + payload);
|
||||
} else if (data.getType() == DATA_TYPE.DATA_TYPE_MESSAGE) {
|
||||
caseMessage(data);
|
||||
} else if (data.getType() == DATA_TYPE.DATA_TYPE_ERROR) {
|
||||
System.err.println("Received error from client (" + data.getSender() + "): " + data.getText());
|
||||
} else {
|
||||
System.err.println("Unknown data type received: " + type);
|
||||
System.err.println("Unknown data type received: " + data.getType());
|
||||
|
||||
}
|
||||
} catch (ChatProtocolException e) {
|
||||
@@ -167,25 +161,25 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(Message)}
|
||||
* Checks if username is valid. if valid sends response to client with confirmation.
|
||||
*
|
||||
* @param sender of the payload
|
||||
* @param data sent
|
||||
* @throws ChatProtocolException if username not valid
|
||||
*/
|
||||
private void caseConnect(String sender) throws ChatProtocolException {
|
||||
private void caseConnect(Message data) throws ChatProtocolException {
|
||||
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
|
||||
if (sender.isBlank()) sender = this.userName;
|
||||
if (data.getSender().isBlank()) data.setSender(this.userName);
|
||||
//if username not valid
|
||||
if (connectionRegistry.containsKey(sender)) {
|
||||
if (connectionRegistry.containsKey(data.getSender())) {
|
||||
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);
|
||||
throw new ChatProtocolException("User name already taken: " + data.getSender());
|
||||
}
|
||||
//if username valid
|
||||
this.userName = sender;
|
||||
this.userName = data.getSender();
|
||||
connectionRegistry.put(userName, this);
|
||||
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successful for " + userName);
|
||||
this.state = CONNECTED;
|
||||
@@ -196,7 +190,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(Message)}
|
||||
* Disconnects connection by removing connection from registry and calling method {@link ServerConnectionHandler#stopReceiving()} to terminate socket.
|
||||
*
|
||||
* @throws ChatProtocolException if state already DISCONNECTED.
|
||||
@@ -213,30 +207,27 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
||||
* This method is called by method {@link ServerConnectionHandler#processData(Message)}
|
||||
* Checks if broadcast or unicast. Sends data accordingly
|
||||
*
|
||||
* @param sender who sent data
|
||||
* @param receiver to receive data
|
||||
* @param type of message
|
||||
* @param payload data to transmit
|
||||
* @param data to transmit
|
||||
* @throws ChatProtocolException if state not equal to CONNECT
|
||||
*/
|
||||
private void caseMessage(String sender, String receiver, String type, String payload) throws ChatProtocolException {
|
||||
private void caseMessage(Message data) throws ChatProtocolException {
|
||||
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state);
|
||||
if (USER_ALL.equals(receiver)) {
|
||||
if (USER_ALL.equals(data.getReceiver())) {
|
||||
for (ServerConnectionHandler handler : connectionRegistry.values()) {
|
||||
handler.sendData(sender, receiver, type, payload);
|
||||
handler.sendData(data.getSender(), data.getReceiver(), data.getType(), data.getText());
|
||||
}
|
||||
} else {
|
||||
ServerConnectionHandler handler = connectionRegistry.get(receiver);
|
||||
ServerConnectionHandler handler = connectionRegistry.get(data.getReceiver());
|
||||
if (handler != null) {
|
||||
handler.sendData(sender, receiver, type, payload);
|
||||
if (!receiver.equals(sender)) {
|
||||
sendData(sender, receiver, type, payload); //send message to sender if it's a direct message and sender is not receiver.
|
||||
handler.sendData(data.getSender(), data.getReceiver(), data.getType(), data.getText());
|
||||
if (!data.getReceiver().equals(data.getSender())) {
|
||||
sendData(data.getSender(), data.getReceiver(), data.getType(), data.getText()); //send message to sender if it's a direct message and sender is not receiver.
|
||||
}
|
||||
} else {
|
||||
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + receiver);
|
||||
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + data.getReceiver());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user