Merge pull request #44 from PM2-IT21bWIN-ruiz-mach-krea/continue_connectionhandler

Continue connectionhandler
This commit is contained in:
Roman Schenk 2022-04-15 17:41:12 +02:00 committed by GitHub Enterprise
commit 84e59effec
3 changed files with 167 additions and 172 deletions

View File

@ -99,66 +99,24 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
private void processData(String data) { private void processData(String data) {
try { try {
// parse data content
Scanner scanner = new Scanner(data); Scanner scanner = new Scanner(data);
String sender = null; StringBuilder sender = new StringBuilder();
String reciever = null; StringBuilder reciever = new StringBuilder();
String type = null; StringBuilder type = new StringBuilder();
String payload = null; StringBuilder payload = new StringBuilder();
if (scanner.hasNextLine()) { super.processData(scanner,sender,reciever,type,payload);
sender = scanner.nextLine();
} else {
throw new ChatProtocolException("No Sender found");
}
if (scanner.hasNextLine()) {
reciever = scanner.nextLine();
} else {
throw new ChatProtocolException("No Reciever found");
}
if (scanner.hasNextLine()) {
type = scanner.nextLine();
} else {
throw new ChatProtocolException("No Type found");
}
if (scanner.hasNextLine()) {
payload = scanner.nextLine();
}
// dispatch operation based on type parameter // dispatch operation based on type parameter
if (type.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.equals(getDataTypeConfirm())) { } else if (type.toString().equals(getDataTypeConfirm())) {
if (state.get() == CONFIRM_CONNECT) { caseConfirm(sender.toString(), reciever.toString(), payload.toString());
this.userName.set(reciever); } else if (type.toString().equals(getDataTypeDisconnect())) {
this.serverPort.set(getConnection().getRemotePort()); caseDisconnect(sender.toString(),reciever.toString(),payload.toString());
this.serverAddress.set(getConnection().getRemoteHost()); } else if (type.toString().equals(getDataTypeMessage())) {
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload)); caseMessage(sender.toString(),reciever.toString(),payload.toString());
System.out.println("CONFIRM: " + payload); } else if (type.toString().equals(getDataTypeError())) {
this.setState(CONNECTED); caseError(sender.toString(), reciever.toString(), payload.toString());
} 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);
}
} else if (type.equals(getDataTypeDisconnect())) {
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);
} else if (type.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,reciever,payload));
System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload);
} else if (type.equals(getDataTypeError())) {
messages.addMessage(new Message(Message.MessageType.ERROR,sender,reciever,payload));
System.out.println("ERROR: " + payload);
} else { } else {
System.out.println("Unknown data type received: " + type); System.out.println("Unknown data type received: " + type);
} }
@ -168,27 +126,47 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
} }
} }
private void sendData(String sender, String receiver, String type, String payload) { private void caseConfirm(String sender, String reciever, String payload) {
if (getConnection().isAvailable()) { if (state.get() == CONFIRM_CONNECT) {
new StringBuilder(); this.userName.set(reciever);
String data = new StringBuilder() this.serverPort.set(getConnection().getRemotePort());
.append(sender+"\n") this.serverAddress.set(getConnection().getRemoteHost());
.append(receiver+"\n") messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
.append(type+"\n") System.out.println("CONFIRM: " + payload);
.append(payload+"\n") this.setState(CONNECTED);
.toString(); } else if (state.get() == CONFIRM_DISCONNECT) {
try { messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
getConnection().send(data); System.out.println("CONFIRM: " + payload);
} catch (SocketException e) { this.setState(DISCONNECTED);
System.err.println("Connection closed: " + e.getMessage()); } else {
} catch (EOFException e) { System.err.println("Got unexpected confirm message: " + payload);
System.out.println("Connection terminated by remote");
} catch(IOException e) {
System.err.println("Communication error: " + e.getMessage());
}
} }
} }
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);
this.sendData(userName.get(), USER_NONE, getDataTypeConnect(),null); this.sendData(userName.get(), USER_NONE, getDataTypeConnect(),null);
@ -218,5 +196,4 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
return false; return false;
} }
} }
} }

View File

@ -1,5 +1,10 @@
package ch.zhaw.pm2.multichat.protocol; package ch.zhaw.pm2.multichat.protocol;
import java.io.EOFException;
import java.io.IOException;
import java.net.SocketException;
import java.util.Scanner;
public abstract class ConnectionHandler { public abstract class ConnectionHandler {
private NetworkHandler.NetworkConnection<String> connection; private NetworkHandler.NetworkConnection<String> connection;
@ -44,4 +49,47 @@ public abstract class ConnectionHandler {
protected void setConnection(NetworkHandler.NetworkConnection<String> connection) { protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
this.connection = connection; this.connection = connection;
} }
protected void processData(Scanner scanner, StringBuilder sender, StringBuilder reciever, StringBuilder type, StringBuilder payload) throws ChatProtocolException {
// parse data content
if (scanner.hasNextLine()) {
sender.append(scanner.nextLine());
} else {
throw new ChatProtocolException("No Sender found");
}
if (scanner.hasNextLine()) {
reciever.append(scanner.nextLine());
} else {
throw new ChatProtocolException("No Reciever found");
}
if (scanner.hasNextLine()) {
type.append(scanner.nextLine());
} else {
throw new ChatProtocolException("No Type found");
}
if (scanner.hasNextLine()) {
payload.append(scanner.nextLine());
}
}
protected void sendData(String sender, String receiver, String type, String payload) {
if (connection.isAvailable()) {
new StringBuilder();
String data = new StringBuilder()
.append(sender+"\n")
.append(receiver+"\n")
.append(type+"\n")
.append(payload+"\n")
.toString();
try {
getConnection().send(data);
} catch (SocketException e) {
System.err.println("Connection closed: " + e.getMessage());
} catch (EOFException e) {
System.out.println("Connection terminated by remote");
} catch(IOException e) {
System.err.println("Communication error: " + e.getMessage());
}
}
}
} }

View File

@ -8,7 +8,6 @@ import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.net.SocketException; import java.net.SocketException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
@ -91,93 +90,30 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
getConnection().close(); getConnection().close();
System.out.println("Stopped receiving data."); System.out.println("Stopped receiving data.");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Failed to close connection." + e); System.err.println("Failed to close connection." + e.getMessage());
} }
System.out.println("Closed Connection Handler for " + userName); System.out.println("Closed Connection Handler for " + userName);
} }
private void processData(String data) { private void processData(String data) {
try { try {
// parse data content
Scanner scanner = new Scanner(data); Scanner scanner = new Scanner(data);
String sender = null; StringBuilder sender = new StringBuilder();
String reciever = null; StringBuilder reciever = new StringBuilder();
String type = null; StringBuilder type = new StringBuilder();
String payload = null; StringBuilder payload = new StringBuilder();
if (scanner.hasNextLine()) { super.processData(scanner,sender,reciever,type,payload);
sender = scanner.nextLine();
} else {
throw new ChatProtocolException("No Sender found");
}
if (scanner.hasNextLine()) {
reciever = scanner.nextLine();
} else {
throw new ChatProtocolException("No Reciever found");
}
if (scanner.hasNextLine()) {
type = scanner.nextLine();
} else {
throw new ChatProtocolException("No Type found");
}
if (scanner.hasNextLine()) {
payload = scanner.nextLine();
}
// dispatch operation based on type parameter // dispatch operation based on type parameter
if (type.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 == null || sender.isBlank()) sender = this.userName; } else if (type.toString().equals(getDataTypeConfirm())) {
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;
nameComplete.signal();
}
finally {
mutex.unlock();
}
connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName);
this.state = CONNECTED;
} else if (type.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.equals(getDataTypeDisconnect())) { } else if (type.toString().equals(getDataTypeDisconnect())) {
if (state == DISCONNECTED) caseDisconnect();
throw new ChatProtocolException("Illegal state for disconnect request: " + state); } else if (type.toString().equals(getDataTypeMessage())) {
if (state == CONNECTED) { caseMessage(sender.toString(), reciever.toString(), type.toString(), payload.toString());
connectionRegistry.remove(this.userName); } else if (type.toString().equals(getDataTypeError())) {
}
sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName);
this.state = DISCONNECTED;
this.stopReceiving();
} else if (type.equals(getDataTypeMessage())) {
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);
}
}
} else if (type.equals(getDataTypeError())) {
System.err.println("Received error from client (" + sender + "): " + payload); System.err.println("Received error from client (" + sender + "): " + payload);
} else { } else {
System.err.println("Unknown data type received: " + type); System.err.println("Unknown data type received: " + type);
@ -189,25 +125,59 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
} }
} }
private void caseConnect(String sender) throws ChatProtocolException {
if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
private void sendData(String sender, String receiver, String type, String payload) { if (sender.isBlank()) sender = this.userName;
if (getConnection().isAvailable()) { if (connectionRegistry.containsKey(sender)) {
new StringBuilder(); mutex.lock();
String data = new StringBuilder()
.append(sender+"\n")
.append(receiver+"\n")
.append(type+"\n")
.append(payload+"\n")
.toString();
try { try {
getConnection().send(data); state = ERROR;
} catch (SocketException e) { nameComplete.signal();
System.out.println("Connection closed: " + e.getMessage()); }
} catch (EOFException e) { finally {
System.out.println("Connection terminated by remote"); mutex.unlock();
} catch(IOException e) { }
System.out.println("Communication error: " + e.getMessage()); 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);
} }
} }
} }