Attempt to send obj

This commit is contained in:
Andrin Fassbind
2022-04-16 21:04:51 +02:00
parent b67b9c84aa
commit 77e9cf6163
7 changed files with 109 additions and 176 deletions
@@ -8,18 +8,11 @@ import java.util.Scanner;
/**
* 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.
* 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)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
*/
public abstract class ConnectionHandler {
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_ALL = "*";
@@ -28,39 +21,43 @@ public abstract class ConnectionHandler {
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
}
/**
* @return {@link ConnectionHandler#DATA_TYPE_CONNECT}
*/
public static String getDataTypeConnect() {
return DATA_TYPE_CONNECT;
public enum DATA_TYPE {
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT,DATA_TYPE_MESSAGE,DATA_TYPE_ERROR
}
/**
* @return {@link ConnectionHandler#DATA_TYPE_CONFIRM}
* @return {@link DATA_TYPE}
*/
public static String getDataTypeConfirm() {
return DATA_TYPE_CONFIRM;
public static DATA_TYPE getDataTypeConnect() {
return DATA_TYPE.DATA_TYPE_CONNECT;
}
/**
* @return {@link ConnectionHandler#DATA_TYPE_DISCONNECT}
* @return {@link DATA_TYPE}
*/
public static String getDataTypeDisconnect() {
return DATA_TYPE_DISCONNECT;
public static DATA_TYPE getDataTypeConfirm() {
return DATA_TYPE.DATA_TYPE_CONFIRM;
}
/**
* @return {@link ConnectionHandler#DATA_TYPE_MESSAGE
* @return {@link DATA_TYPE}
*/
public static String getDataTypeMessage() {
return DATA_TYPE_MESSAGE;
public static DATA_TYPE getDataTypeDisconnect() {
return DATA_TYPE.DATA_TYPE_DISCONNECT;
}
/**
* @return {@link ConnectionHandler#DATA_TYPE_ERROR}
* @return {@link DATA_TYPE}
*/
public static String getDataTypeError() {
return DATA_TYPE_ERROR;
public static DATA_TYPE getDataTypeMessage() {
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 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}
@@ -122,14 +85,10 @@ public abstract class ConnectionHandler {
* @param type 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()) {
String data = sender + "\n" +
receiver + "\n" +
type + "\n" +
payload + "\n";
try {
connection.send(data);
connection.send(new Message(type,sender,receiver,payload));
} catch (SocketException e) {
System.err.println("Connection closed: " + e.getMessage());
} catch (EOFException e) {
@@ -0,0 +1,70 @@
package ch.zhaw.pm2.multichat.protocol;
/**
* A Message object represents one Message of a client. Can be stored in ClientMessageList.
*/
public class Message {
private final ConnectionHandler.DATA_TYPE type;
private String sender;
private final String receiver;
private final String text;
/**
* Constructor of Message. Needs all Information about a Message to save them.
*
* @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and receiver will be null)
* @param sender The User who has sent the message.
* @param receiver The User who should receive the message.
* @param text The Text of the message.
*/
public Message(ConnectionHandler.DATA_TYPE type, String sender, String receiver, String text) {
this.type = type;
this.sender = sender;
this.receiver = receiver;
this.text = text;
}
/**
* Checks if the Filter String is contained in one of the data fields sender, receiver or text
*
* @param filter The Filter String
* @return true if it is the Filter String is contained in a data field.
*/
public boolean matchesFilter(String filter) {
return (sender != null && sender.contains(filter)) ||
(receiver != null && receiver.contains(filter)) ||
(text != null && text.contains(filter));
}
/**
* @return The type of the message.
*/
public ConnectionHandler.DATA_TYPE getType() {
return type;
}
/**
* @return The User who has sent the Message.
*/
public String getSender() {
return sender;
}
/**
* @return The Receiver who receives the Message.
*/
public String getReceiver() {
return receiver;
}
/**
* @return The Text of the Message.
*/
public String getText() {
return text;
}
public void setSender(String sender) {
this.sender = sender;
}
}
@@ -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
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
* </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.
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
* 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
* process calls {@link NetworkConnection#receive()} again to wait for the next request.
* </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.
* <b>Important: {@link NetworkConnection} is not thread safe</b>, therefore make sure that only one thread
* 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.
* @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());
outputStream.writeObject(data);
}