Merge pull request #44 from PM2-IT21bWIN-ruiz-mach-krea/continue_connectionhandler
Continue connectionhandler
This commit is contained in:
commit
84e59effec
|
@ -99,34 +99,34 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
|
||||
private void processData(String data) {
|
||||
try {
|
||||
// parse data content
|
||||
Scanner scanner = new Scanner(data);
|
||||
String sender = null;
|
||||
String reciever = null;
|
||||
String type = null;
|
||||
String payload = null;
|
||||
if (scanner.hasNextLine()) {
|
||||
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();
|
||||
}
|
||||
StringBuilder sender = new StringBuilder();
|
||||
StringBuilder reciever = new StringBuilder();
|
||||
StringBuilder type = new StringBuilder();
|
||||
StringBuilder payload = new StringBuilder();
|
||||
super.processData(scanner,sender,reciever,type,payload);
|
||||
|
||||
// dispatch operation based on type parameter
|
||||
if (type.equals(getDataTypeConnect())) {
|
||||
if (type.toString().equals(getDataTypeConnect())) {
|
||||
System.err.println("Illegal connect request from server");
|
||||
} else if (type.equals(getDataTypeConfirm())) {
|
||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||
caseDisconnect(sender.toString(),reciever.toString(),payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||
caseMessage(sender.toString(),reciever.toString(),payload.toString());
|
||||
} else if (type.toString().equals(getDataTypeError())) {
|
||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
||||
} else {
|
||||
System.out.println("Unknown data type received: " + type);
|
||||
}
|
||||
} catch (ChatProtocolException e) {
|
||||
System.err.println("Error while processing data: " + e.getMessage());
|
||||
sendData(USER_NONE, userName.get(), getDataTypeError(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void caseConfirm(String sender, String reciever, String payload) {
|
||||
if (state.get() == CONFIRM_CONNECT) {
|
||||
this.userName.set(reciever);
|
||||
this.serverPort.set(getConnection().getRemotePort());
|
||||
|
@ -141,7 +141,9 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
} else {
|
||||
System.err.println("Got unexpected confirm message: " + payload);
|
||||
}
|
||||
} else if (type.equals(getDataTypeDisconnect())) {
|
||||
}
|
||||
|
||||
private void caseDisconnect(String sender, String reciever, String payload) {
|
||||
if (state.get() == DISCONNECTED) {
|
||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
||||
return;
|
||||
|
@ -149,44 +151,20 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload));
|
||||
System.out.println("DISCONNECT: " + payload);
|
||||
this.setState(DISCONNECTED);
|
||||
} else if (type.equals(getDataTypeMessage())) {
|
||||
}
|
||||
|
||||
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);
|
||||
} else if (type.equals(getDataTypeError())) {
|
||||
messages.addMessage(new Message(Message.MessageType.ERROR,sender,reciever,payload));
|
||||
System.out.println("ERROR: " + payload);
|
||||
} else {
|
||||
System.out.println("Unknown data type received: " + type);
|
||||
}
|
||||
} catch (ChatProtocolException e) {
|
||||
System.err.println("Error while processing data: " + e.getMessage());
|
||||
sendData(USER_NONE, userName.get(), getDataTypeError(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void sendData(String sender, String receiver, String type, String payload) {
|
||||
if (getConnection().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());
|
||||
}
|
||||
}
|
||||
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 {
|
||||
|
@ -218,5 +196,4 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
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 {
|
||||
private NetworkHandler.NetworkConnection<String> connection;
|
||||
|
||||
|
@ -44,4 +49,47 @@ public abstract class ConnectionHandler {
|
|||
protected void setConnection(NetworkHandler.NetworkConnection<String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
|||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
@ -91,42 +90,44 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
getConnection().close();
|
||||
System.out.println("Stopped receiving data.");
|
||||
} 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);
|
||||
}
|
||||
|
||||
private void processData(String data) {
|
||||
try {
|
||||
// parse data content
|
||||
Scanner scanner = new Scanner(data);
|
||||
String sender = null;
|
||||
String reciever = null;
|
||||
String type = null;
|
||||
String payload = null;
|
||||
if (scanner.hasNextLine()) {
|
||||
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();
|
||||
}
|
||||
StringBuilder sender = new StringBuilder();
|
||||
StringBuilder reciever = new StringBuilder();
|
||||
StringBuilder type = new StringBuilder();
|
||||
StringBuilder payload = new StringBuilder();
|
||||
super.processData(scanner,sender,reciever,type,payload);
|
||||
|
||||
// dispatch operation based on type parameter
|
||||
if (type.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 (sender == null || sender.isBlank()) sender = this.userName;
|
||||
if (sender.isBlank()) sender = this.userName;
|
||||
if (connectionRegistry.containsKey(sender)) {
|
||||
mutex.lock();
|
||||
try {
|
||||
|
@ -140,7 +141,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
}
|
||||
mutex.lock();
|
||||
try {
|
||||
this.userName = sender;
|
||||
this.userName = sender.toString();
|
||||
nameComplete.signal();
|
||||
}
|
||||
finally {
|
||||
|
@ -149,9 +150,9 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
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");
|
||||
} else if (type.equals(getDataTypeDisconnect())) {
|
||||
}
|
||||
|
||||
private void caseDisconnect() throws ChatProtocolException {
|
||||
if (state == DISCONNECTED)
|
||||
throw new ChatProtocolException("Illegal state for disconnect request: " + state);
|
||||
if (state == CONNECTED) {
|
||||
|
@ -160,7 +161,9 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
sendData(USER_NONE, userName, getDataTypeConfirm(), "Confirm disconnect of " + userName);
|
||||
this.state = DISCONNECTED;
|
||||
this.stopReceiving();
|
||||
} else if (type.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 (USER_ALL.equals(reciever)) {
|
||||
for (ServerConnectionHandler handler : connectionRegistry.values()) {
|
||||
|
@ -177,38 +180,5 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
|
||||
}
|
||||
}
|
||||
} else if (type.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 sendData(String sender, String receiver, String type, String payload) {
|
||||
if (getConnection().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.out.println("Connection closed: " + e.getMessage());
|
||||
} catch (EOFException e) {
|
||||
System.out.println("Connection terminated by remote");
|
||||
} catch(IOException e) {
|
||||
System.out.println("Communication error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue