@@ -3,12 +3,11 @@ package ch.zhaw.pm2.multichat.protocol;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
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, DATA_TYPE, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
||||
* 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, DATA_TYPE, String)}
|
||||
*/
|
||||
public abstract class ConnectionHandler {
|
||||
private NetworkHandler.NetworkConnection<String> connection;
|
||||
@@ -21,8 +20,9 @@ public abstract class ConnectionHandler {
|
||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
||||
}
|
||||
|
||||
// DATA_TYPE of the messages
|
||||
public enum DATA_TYPE {
|
||||
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT,DATA_TYPE_MESSAGE,DATA_TYPE_ERROR
|
||||
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_MESSAGE, DATA_TYPE_ERROR
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +88,7 @@ public abstract class ConnectionHandler {
|
||||
protected void sendData(String sender, String receiver, DATA_TYPE type, String payload) {
|
||||
if (connection.isAvailable()) {
|
||||
try {
|
||||
connection.send(new Message(type,sender,receiver,payload));
|
||||
connection.send(new Message(type, sender, receiver, payload));
|
||||
} catch (SocketException e) {
|
||||
System.err.println("Connection closed: " + e.getMessage());
|
||||
} catch (EOFException e) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.io.Serializable;
|
||||
*/
|
||||
public class Message implements Serializable {
|
||||
private final ConnectionHandler.DATA_TYPE type;
|
||||
private String sender;
|
||||
private final String sender;
|
||||
private final String receiver;
|
||||
private final String text;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user