Merge pull request #51 from PM2-IT21bWIN-ruiz-mach-krea/useDataObjectToSend
Use data object to send
This commit is contained in:
commit
a603f05135
|
@ -1,7 +1,9 @@
|
||||||
package ch.zhaw.pm2.multichat.client;
|
package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State;
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.Message;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.beans.value.ChangeListener;
|
import javafx.beans.value.ChangeListener;
|
||||||
import javafx.beans.value.ObservableValue;
|
import javafx.beans.value.ObservableValue;
|
||||||
|
@ -239,7 +241,7 @@ public class ChatWindowController {
|
||||||
* @param message String to be added as Error
|
* @param message String to be added as Error
|
||||||
*/
|
*/
|
||||||
public void addError(String message) {
|
public void addError(String message) {
|
||||||
messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message));
|
messages.addMessage(new Message(ConnectionHandler.DATA_TYPE.DATA_TYPE_ERROR, null, null, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -248,7 +250,6 @@ public class ChatWindowController {
|
||||||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param event the event which occurred when Windows is closed
|
* @param event the event which occurred when Windows is closed
|
||||||
*/
|
*/
|
||||||
public void handle(WindowEvent event) {
|
public void handle(WindowEvent event) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.Message;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
import javafx.beans.property.SimpleIntegerProperty;
|
import javafx.beans.property.SimpleIntegerProperty;
|
||||||
import javafx.beans.property.SimpleObjectProperty;
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
@ -49,8 +50,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
* Called to initialize the ClientConnectionHandler when trying to start a connection
|
* Called to initialize the ClientConnectionHandler when trying to start a connection
|
||||||
*
|
*
|
||||||
* @param serverAddress to connect to
|
* @param serverAddress to connect to
|
||||||
* @param serverPort to connect to
|
* @param serverPort to connect to
|
||||||
* @param userName to connect as
|
* @param userName to connect as
|
||||||
* @throws IOException if connection to Server not possible
|
* @throws IOException if connection to Server not possible
|
||||||
*/
|
*/
|
||||||
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
|
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
|
||||||
|
@ -122,7 +123,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
try {
|
try {
|
||||||
System.out.println("Start receiving data...");
|
System.out.println("Start receiving data...");
|
||||||
while (getConnection().isAvailable()) {
|
while (getConnection().isAvailable()) {
|
||||||
String data = getConnection().receive();
|
Message data = getConnection().receive();
|
||||||
processData(data);
|
processData(data);
|
||||||
}
|
}
|
||||||
System.out.println("Stopped receiving data");
|
System.out.println("Stopped receiving data");
|
||||||
|
@ -162,95 +163,77 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
*
|
*
|
||||||
* @param data that is received in a form of a String and then used depending on its determined cause.
|
* @param data that is received in a form of a String and then used depending on its determined cause.
|
||||||
*/
|
*/
|
||||||
private void processData(String data) {
|
private void processData(Message data) {
|
||||||
try {
|
// dispatch operation based on type parameter
|
||||||
Scanner scanner = new Scanner(data);
|
if (data.getType() == DATA_TYPE.DATA_TYPE_CONNECT) {
|
||||||
StringBuilder sender = new StringBuilder();
|
System.err.println("Illegal connect request from server");
|
||||||
StringBuilder receiver = new StringBuilder();
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_CONFIRM) {
|
||||||
StringBuilder type = new StringBuilder();
|
caseConfirm(data);
|
||||||
StringBuilder payload = new StringBuilder();
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_DISCONNECT) {
|
||||||
super.processData(scanner, sender, receiver, type, payload);
|
caseDisconnect(data);
|
||||||
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_MESSAGE) {
|
||||||
// dispatch operation based on type parameter
|
caseMessage(data);
|
||||||
if (type.toString().equals(getDataTypeConnect())) {
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_ERROR) {
|
||||||
System.err.println("Illegal connect request from server");
|
caseError(data);
|
||||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
} else {
|
||||||
caseConfirm(sender.toString(), receiver.toString(), payload.toString());
|
System.out.println("Unknown data type received: " + data.getType());
|
||||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
|
||||||
caseDisconnect(sender.toString(), receiver.toString(), payload.toString());
|
|
||||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
|
||||||
caseMessage(sender.toString(), receiver.toString(), payload.toString());
|
|
||||||
} else if (type.toString().equals(getDataTypeError())) {
|
|
||||||
caseError(sender.toString(), receiver.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 receiver, String payload) {
|
private void caseConfirm(Message data) {
|
||||||
if (state.get() == CONFIRM_CONNECT) {
|
if (state.get() == CONFIRM_CONNECT) {
|
||||||
this.userName.set(receiver);
|
this.userName.set(data.getReceiver());
|
||||||
this.serverPort.set(getConnection().getRemotePort());
|
this.serverPort.set(getConnection().getRemotePort());
|
||||||
this.serverAddress.set(getConnection().getRemoteHost());
|
this.serverAddress.set(getConnection().getRemoteHost());
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
messages.addMessage(data);
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + data.getText());
|
||||||
this.setState(CONNECTED);
|
this.setState(CONNECTED);
|
||||||
} else if (state.get() == CONFIRM_DISCONNECT) {
|
} else if (state.get() == CONFIRM_DISCONNECT) {
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
messages.addMessage(data);
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + data.getText());
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Got unexpected confirm message: " + payload);
|
System.err.println("Got unexpected confirm message: " + data.getText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates the disconnect sequence and sends the message with all its info.
|
* Initiates the disconnect sequence and sends the message with all its info.
|
||||||
*
|
*
|
||||||
* @param sender the sender name of the message
|
* @param data Data which has been transmitted
|
||||||
* @param receiver the receiver name of the message
|
|
||||||
* @param payload the added payload that corresponds to the message
|
|
||||||
*/
|
*/
|
||||||
private void caseDisconnect(String sender, String receiver, String payload) {
|
private void caseDisconnect(Message data) {
|
||||||
if (state.get() == DISCONNECTED) {
|
if (state.get() == DISCONNECTED) {
|
||||||
System.out.println("DISCONNECT: Already in disconnected: " + payload);
|
System.out.println("DISCONNECT: Already in disconnected: " + data.getText());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
messages.addMessage(data);
|
||||||
System.out.println("DISCONNECT: " + payload);
|
System.out.println("DISCONNECT: " + data.getText());
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates the procedure to send a new message and sends one as such.
|
* Initiates the procedure to send a new message and sends one as such.
|
||||||
*
|
*
|
||||||
* @param sender the sender name of the message
|
* @param data Data which has been transmitted
|
||||||
* @param receiver the receiver name of the message
|
|
||||||
* @param payload the added payload that corresponds to the message
|
|
||||||
*/
|
*/
|
||||||
private void caseMessage(String sender, String receiver, String payload) {
|
private void caseMessage(Message data) {
|
||||||
if (state.get() != CONNECTED) {
|
if (state.get() != CONNECTED) {
|
||||||
System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload);
|
System.out.println("MESSAGE: Illegal state " + state + " for message: " + data.getText());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, payload));
|
messages.addMessage(data);
|
||||||
System.out.println("MESSAGE: From " + sender + " to " + receiver + ": " + payload);
|
System.out.println("MESSAGE: From " + data.getSender() + " to " + data.getReceiver() + ": " + data.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores the message as an error message and displays it as such as well.
|
* Stores the message as an error message and displays it as such as well.
|
||||||
*
|
*
|
||||||
* @param sender the sender name of the message
|
* @param data Data which has been transmitted
|
||||||
* @param receiver the receiver name of the message
|
|
||||||
* @param payload the added payload that corresponds to the message
|
|
||||||
*/
|
*/
|
||||||
private void caseError(String sender, String receiver, String payload) {
|
private void caseError(Message data) {
|
||||||
messages.addMessage(new Message(Message.MessageType.ERROR, sender, receiver, payload));
|
messages.addMessage(data);
|
||||||
System.out.println("ERROR: " + payload);
|
System.out.println("ERROR: " + data.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package ch.zhaw.pm2.multichat.client;
|
package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.Message;
|
||||||
import javafx.beans.property.SimpleBooleanProperty;
|
import javafx.beans.property.SimpleBooleanProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -15,6 +17,7 @@ public class ClientMessageList {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new message to ArrayList and also informs Listener.
|
* Adds a new message to ArrayList and also informs Listener.
|
||||||
|
*
|
||||||
* @param message that should be added
|
* @param message that should be added
|
||||||
*/
|
*/
|
||||||
public void addMessage(Message message) {
|
public void addMessage(Message message) {
|
||||||
|
@ -35,12 +38,10 @@ public class ClientMessageList {
|
||||||
for (Message message : messages) {
|
for (Message message : messages) {
|
||||||
if (showAll || message.matchesFilter(filter)) {
|
if (showAll || message.matchesFilter(filter)) {
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case MESSAGE ->
|
case DATA_TYPE_MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
|
||||||
result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
|
case DATA_TYPE_ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
|
||||||
case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
|
case DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_CONNECT -> result.append(String.format("[INFO] %s\n", message.getText()));
|
||||||
case INFO -> result.append(String.format("[INFO] %s\n", message.getText()));
|
default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
|
||||||
default ->
|
|
||||||
result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,23 +3,15 @@ package ch.zhaw.pm2.multichat.protocol;
|
||||||
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.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This abstract class is the superclass for ClientConnectionHandler and ServerConnectionHandler
|
* This abstract class is the superclass for ClientConnectionHandler and ServerConnectionHandler
|
||||||
* It offers the DATA_TYPE Strings and a {@link State} enum for all valid connection states.
|
* It offers the DATA_TYPE for message and a {@link State} enum for all valid connection states.
|
||||||
* Shared methods are implemented in this class as well {@link ConnectionHandler#sendData(String, String, String, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
* Shared methods are implemented in this class as well {@link ConnectionHandler#sendData(String, String, DATA_TYPE, String)}
|
||||||
*/
|
*/
|
||||||
public abstract class ConnectionHandler {
|
public abstract class ConnectionHandler {
|
||||||
private NetworkHandler.NetworkConnection<String> connection;
|
private NetworkHandler.NetworkConnection<String> connection;
|
||||||
|
|
||||||
// Data types used for the Chat Protocol
|
|
||||||
private static final String DATA_TYPE_CONNECT = "CONNECT";
|
|
||||||
private static final String DATA_TYPE_CONFIRM = "CONFIRM";
|
|
||||||
private static final String DATA_TYPE_DISCONNECT = "DISCONNECT";
|
|
||||||
private static final String DATA_TYPE_MESSAGE = "MESSAGE";
|
|
||||||
private static final String DATA_TYPE_ERROR = "ERROR";
|
|
||||||
|
|
||||||
public static final String USER_NONE = "";
|
public static final String USER_NONE = "";
|
||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
|
@ -28,39 +20,44 @@ public abstract class ConnectionHandler {
|
||||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// DATA_TYPE of the messages
|
||||||
* @return {@link ConnectionHandler#DATA_TYPE_CONNECT}
|
public enum DATA_TYPE {
|
||||||
*/
|
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_MESSAGE, DATA_TYPE_ERROR
|
||||||
public static String getDataTypeConnect() {
|
|
||||||
return DATA_TYPE_CONNECT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@link ConnectionHandler#DATA_TYPE_CONFIRM}
|
* @return {@link DATA_TYPE}
|
||||||
*/
|
*/
|
||||||
public static String getDataTypeConfirm() {
|
public static DATA_TYPE getDataTypeConnect() {
|
||||||
return DATA_TYPE_CONFIRM;
|
return DATA_TYPE.DATA_TYPE_CONNECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@link ConnectionHandler#DATA_TYPE_DISCONNECT}
|
* @return {@link DATA_TYPE}
|
||||||
*/
|
*/
|
||||||
public static String getDataTypeDisconnect() {
|
public static DATA_TYPE getDataTypeConfirm() {
|
||||||
return DATA_TYPE_DISCONNECT;
|
return DATA_TYPE.DATA_TYPE_CONFIRM;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@link ConnectionHandler#DATA_TYPE_MESSAGE
|
* @return {@link DATA_TYPE}
|
||||||
*/
|
*/
|
||||||
public static String getDataTypeMessage() {
|
public static DATA_TYPE getDataTypeDisconnect() {
|
||||||
return DATA_TYPE_MESSAGE;
|
return DATA_TYPE.DATA_TYPE_DISCONNECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@link ConnectionHandler#DATA_TYPE_ERROR}
|
* @return {@link DATA_TYPE}
|
||||||
*/
|
*/
|
||||||
public static String getDataTypeError() {
|
public static DATA_TYPE getDataTypeMessage() {
|
||||||
return DATA_TYPE_ERROR;
|
return DATA_TYPE.DATA_TYPE_MESSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@link DATA_TYPE}
|
||||||
|
*/
|
||||||
|
public static DATA_TYPE getDataTypeError() {
|
||||||
|
return DATA_TYPE.DATA_TYPE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,40 +76,6 @@ public abstract class ConnectionHandler {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method reads the data when a ConnectionHandler receives it. It tries to read out the sender, receiver, type and payload.
|
|
||||||
* If the data does not contain the expected number of lines, it throws a {@link ChatProtocolException}
|
|
||||||
*
|
|
||||||
* @param scanner to read data
|
|
||||||
* @param sender of the data
|
|
||||||
* @param receiver for the data
|
|
||||||
* @param type of data
|
|
||||||
* @param payload the data sent
|
|
||||||
* @throws ChatProtocolException if the data does not contain the expected number of lines
|
|
||||||
*/
|
|
||||||
protected void processData(Scanner scanner, StringBuilder sender, StringBuilder receiver, StringBuilder type, StringBuilder payload) throws ChatProtocolException {
|
|
||||||
// parse data content
|
|
||||||
if (scanner.hasNextLine()) {
|
|
||||||
sender.append(scanner.nextLine());
|
|
||||||
} else if (scanner.hasNextLine()) {
|
|
||||||
receiver.append(scanner.nextLine());
|
|
||||||
} else {
|
|
||||||
throw new ChatProtocolException("No Sender found");
|
|
||||||
}
|
|
||||||
if (scanner.hasNextLine()) {
|
|
||||||
receiver.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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method gets called to send data via the socket defined in the {@link NetworkHandler.NetworkConnection}
|
* This method gets called to send data via the socket defined in the {@link NetworkHandler.NetworkConnection}
|
||||||
|
@ -122,14 +85,10 @@ public abstract class ConnectionHandler {
|
||||||
* @param type of the data
|
* @param type of the data
|
||||||
* @param payload of the data
|
* @param payload of the data
|
||||||
*/
|
*/
|
||||||
protected void sendData(String sender, String receiver, String type, String payload) {
|
protected void sendData(String sender, String receiver, DATA_TYPE type, String payload) {
|
||||||
if (connection.isAvailable()) {
|
if (connection.isAvailable()) {
|
||||||
String data = sender + "\n" +
|
|
||||||
receiver + "\n" +
|
|
||||||
type + "\n" +
|
|
||||||
payload + "\n";
|
|
||||||
try {
|
try {
|
||||||
connection.send(data);
|
connection.send(new Message(type, sender, receiver, payload));
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.err.println("Connection closed: " + e.getMessage());
|
System.err.println("Connection closed: " + e.getMessage());
|
||||||
} catch (EOFException e) {
|
} catch (EOFException e) {
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package ch.zhaw.pm2.multichat.client;
|
package ch.zhaw.pm2.multichat.protocol;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Message object represents one Message of a client. Can be stored in ClientMessageList.
|
* A Message object represents one Message of a client. Can be stored in ClientMessageList.
|
||||||
*/
|
*/
|
||||||
public class Message {
|
public class Message implements Serializable {
|
||||||
private final MessageType type;
|
private final ConnectionHandler.DATA_TYPE type;
|
||||||
private final String sender;
|
private final String sender;
|
||||||
private final String receiver;
|
private final String receiver;
|
||||||
private final String text;
|
private final String text;
|
||||||
|
@ -17,7 +19,7 @@ public class Message {
|
||||||
* @param receiver The User who should receive the message.
|
* @param receiver The User who should receive the message.
|
||||||
* @param text The Text of the message.
|
* @param text The Text of the message.
|
||||||
*/
|
*/
|
||||||
public Message(MessageType type, String sender, String receiver, String text) {
|
public Message(ConnectionHandler.DATA_TYPE type, String sender, String receiver, String text) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.receiver = receiver;
|
this.receiver = receiver;
|
||||||
|
@ -39,7 +41,7 @@ public class Message {
|
||||||
/**
|
/**
|
||||||
* @return The type of the message.
|
* @return The type of the message.
|
||||||
*/
|
*/
|
||||||
public MessageType getType() {
|
public ConnectionHandler.DATA_TYPE getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +66,4 @@ public class Message {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Enumeration of Message Types.
|
|
||||||
*/
|
|
||||||
public enum MessageType {
|
|
||||||
INFO, MESSAGE, ERROR;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -38,7 +38,7 @@ import java.util.Objects;
|
||||||
* on the client side, usually the result is displayed to the user). After processing is finished the
|
* on the client side, usually the result is displayed to the user). After processing is finished the
|
||||||
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
|
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
|
||||||
* </li>
|
* </li>
|
||||||
* <li>sending data: call {@link NetworkConnection#send(Serializable data)}, which sends the given data
|
* <li>sending data: call {@link NetworkConnection#send(Message)}, which sends the given data
|
||||||
* object to the remote side. The method returns as soon the object has been transmitted.
|
* object to the remote side. The method returns as soon the object has been transmitted.
|
||||||
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
|
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
|
||||||
* at a time is sending data.</li>
|
* at a time is sending data.</li>
|
||||||
|
@ -270,7 +270,7 @@ public class NetworkHandler {
|
||||||
* on the client side, usually the result is displayed to the user). After processing is finished the
|
* on the client side, usually the result is displayed to the user). After processing is finished the
|
||||||
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
|
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
|
||||||
* </li>
|
* </li>
|
||||||
* <li>sending data: call {@link NetworkConnection#send(Serializable data)}, which sends the given data
|
* <li>sending data: call {@link NetworkConnection#send(Message)}, which sends the given data
|
||||||
* object to the remote side. The method returns as soon the object has been transmitted.
|
* object to the remote side. The method returns as soon the object has been transmitted.
|
||||||
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
|
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
|
||||||
* at a time is sending data.
|
* at a time is sending data.
|
||||||
|
@ -305,7 +305,7 @@ public class NetworkHandler {
|
||||||
* @param data data object of type T to be submitted through the connection.
|
* @param data data object of type T to be submitted through the connection.
|
||||||
* @throws IOException if an error occurs (e.g. connection interrupted while sending, ...)
|
* @throws IOException if an error occurs (e.g. connection interrupted while sending, ...)
|
||||||
*/
|
*/
|
||||||
public synchronized void send(T data) throws IOException {
|
public synchronized void send(Message data) throws IOException {
|
||||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||||
outputStream.writeObject(data);
|
outputStream.writeObject(data);
|
||||||
}
|
}
|
||||||
|
@ -320,9 +320,9 @@ public class NetworkHandler {
|
||||||
* @throws IOException if an error occours. (e.g. terminated locally/remotely) see above.
|
* @throws IOException if an error occours. (e.g. terminated locally/remotely) see above.
|
||||||
* @throws ClassNotFoundException if the data object received does not match any class in the local classpath
|
* @throws ClassNotFoundException if the data object received does not match any class in the local classpath
|
||||||
*/
|
*/
|
||||||
public T receive() throws IOException, ClassNotFoundException {
|
public Message receive() throws IOException, ClassNotFoundException {
|
||||||
ObjectInputStream inputStream = new ObjectInputStream(this.socket.getInputStream());
|
ObjectInputStream inputStream = new ObjectInputStream(this.socket.getInputStream());
|
||||||
return (T) inputStream.readObject();
|
return (Message) inputStream.readObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -85,7 +85,7 @@ public class Server {
|
||||||
private void start() {
|
private void start() {
|
||||||
System.out.println("Server started.");
|
System.out.println("Server started.");
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (networkServer.isAvailable()) {
|
||||||
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
||||||
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
|
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
|
||||||
new Thread(connectionHandler).start();
|
new Thread(connectionHandler).start();
|
||||||
|
|
|
@ -5,6 +5,7 @@ import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
|
|
||||||
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
|
|
||||||
|
import ch.zhaw.pm2.multichat.protocol.Message;
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
|
@ -61,10 +62,8 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
/**
|
/**
|
||||||
* Constructor to initialize the connection
|
* Constructor to initialize the connection
|
||||||
*
|
*
|
||||||
* @param connection representing the socket connection between server and client
|
* @param connection representing the socket connection between server and client
|
||||||
* @param registry map containing all active connections between server and clients
|
* @param registry map containing all active connections between server and clients
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
||||||
Map<String, ServerConnectionHandler> registry) {
|
Map<String, ServerConnectionHandler> registry) {
|
||||||
|
@ -84,7 +83,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
try {
|
try {
|
||||||
System.out.println("Start receiving data...");
|
System.out.println("Start receiving data...");
|
||||||
while (getConnection().isAvailable() && !(state == ERROR)) {
|
while (getConnection().isAvailable() && !(state == ERROR)) {
|
||||||
String data = getConnection().receive();
|
Message data = getConnection().receive();
|
||||||
processData(data);
|
processData(data);
|
||||||
}
|
}
|
||||||
System.out.println("Stopped receiving data");
|
System.out.println("Stopped receiving data");
|
||||||
|
@ -136,28 +135,21 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
*
|
*
|
||||||
* @param data received by the server
|
* @param data received by the server
|
||||||
*/
|
*/
|
||||||
private void processData(String data) {
|
private void processData(Message data) {
|
||||||
try {
|
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
|
// dispatch operation based on type parameter
|
||||||
if (type.toString().equals(getDataTypeConnect())) {
|
if (data.getType() == DATA_TYPE.DATA_TYPE_CONNECT) {
|
||||||
caseConnect(sender.toString());
|
caseConnect(data);
|
||||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_CONFIRM) {
|
||||||
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 (data.getType() == DATA_TYPE.DATA_TYPE_DISCONNECT) {
|
||||||
caseDisconnect();
|
caseDisconnect();
|
||||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_MESSAGE) {
|
||||||
caseMessage(sender.toString(), receiver.toString(), type.toString(), payload.toString());
|
caseMessage(data);
|
||||||
} else if (type.toString().equals(getDataTypeError())) {
|
} else if (data.getType() == DATA_TYPE.DATA_TYPE_ERROR) {
|
||||||
System.err.println("Received error from client (" + sender + "): " + payload);
|
System.err.println("Received error from client (" + data.getSender() + "): " + data.getText());
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Unknown data type received: " + type);
|
System.err.println("Unknown data type received: " + data.getType());
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (ChatProtocolException e) {
|
} catch (ChatProtocolException e) {
|
||||||
|
@ -167,25 +159,26 @@ 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.
|
* 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
|
* @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 (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
|
||||||
if (sender.isBlank()) sender = this.userName;
|
|
||||||
//if username not valid
|
//if username not valid
|
||||||
if (connectionRegistry.containsKey(sender)) {
|
if (connectionRegistry.containsKey(data.getSender())) {
|
||||||
state = ERROR;
|
state = ERROR;
|
||||||
System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.",
|
System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.",
|
||||||
getConnection().getRemoteHost(),
|
getConnection().getRemoteHost(),
|
||||||
getConnection().getRemotePort()));
|
getConnection().getRemotePort()));
|
||||||
throw new ChatProtocolException("User name already taken: " + sender);
|
throw new ChatProtocolException("User name already taken: " + data.getSender());
|
||||||
}
|
}
|
||||||
//if username valid
|
//if username valid
|
||||||
this.userName = sender;
|
if (!data.getSender().isBlank()) {
|
||||||
|
this.userName = data.getSender();
|
||||||
|
}
|
||||||
connectionRegistry.put(userName, this);
|
connectionRegistry.put(userName, this);
|
||||||
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successful for " + userName);
|
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successful for " + userName);
|
||||||
this.state = CONNECTED;
|
this.state = CONNECTED;
|
||||||
|
@ -196,7 +189,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.
|
* Disconnects connection by removing connection from registry and calling method {@link ServerConnectionHandler#stopReceiving()} to terminate socket.
|
||||||
*
|
*
|
||||||
* @throws ChatProtocolException if state already DISCONNECTED.
|
* @throws ChatProtocolException if state already DISCONNECTED.
|
||||||
|
@ -213,30 +206,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
|
* Checks if broadcast or unicast. Sends data accordingly
|
||||||
*
|
*
|
||||||
* @param sender who sent data
|
* @param data to transmit
|
||||||
* @param receiver to receive data
|
|
||||||
* @param type of message
|
|
||||||
* @param payload data to transmit
|
|
||||||
* @throws ChatProtocolException if state not equal to CONNECT
|
* @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 (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()) {
|
for (ServerConnectionHandler handler : connectionRegistry.values()) {
|
||||||
handler.sendData(sender, receiver, type, payload);
|
handler.sendData(data.getSender(), data.getReceiver(), data.getType(), data.getText());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ServerConnectionHandler handler = connectionRegistry.get(receiver);
|
ServerConnectionHandler handler = connectionRegistry.get(data.getReceiver());
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.sendData(sender, receiver, type, payload);
|
handler.sendData(data.getSender(), data.getReceiver(), data.getType(), data.getText());
|
||||||
if (!receiver.equals(sender)) {
|
if (!data.getReceiver().equals(data.getSender())) {
|
||||||
sendData(sender, receiver, type, payload); //send message to sender if it's a direct message and sender is not receiver.
|
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 {
|
} else {
|
||||||
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + receiver);
|
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + data.getReceiver());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue