javadoc
This commit is contained in:
parent
119cd1eb52
commit
a5069e938f
|
@ -5,6 +5,11 @@ import java.io.IOException;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Scanner;
|
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 aswell {@link ConnectionHandler#sendData(String, String, String, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
||||||
|
*/
|
||||||
public abstract class ConnectionHandler {
|
public abstract class ConnectionHandler {
|
||||||
private NetworkHandler.NetworkConnection<String> connection;
|
private NetworkHandler.NetworkConnection<String> connection;
|
||||||
|
|
||||||
|
@ -18,38 +23,77 @@ public abstract class ConnectionHandler {
|
||||||
public static final String USER_NONE = "";
|
public static final String USER_NONE = "";
|
||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
|
// State of the connection
|
||||||
public enum State {
|
public enum State {
|
||||||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link ConnectionHandler#DATA_TYPE_CONNECT}
|
||||||
|
*/
|
||||||
public static String getDataTypeConnect() {
|
public static String getDataTypeConnect() {
|
||||||
return DATA_TYPE_CONNECT;
|
return DATA_TYPE_CONNECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link ConnectionHandler#DATA_TYPE_CONFIRM}
|
||||||
|
*/
|
||||||
public static String getDataTypeConfirm() {
|
public static String getDataTypeConfirm() {
|
||||||
return DATA_TYPE_CONFIRM;
|
return DATA_TYPE_CONFIRM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link ConnectionHandler#DATA_TYPE_DISCONNECT}
|
||||||
|
*/
|
||||||
public static String getDataTypeDisconnect() {
|
public static String getDataTypeDisconnect() {
|
||||||
return DATA_TYPE_DISCONNECT;
|
return DATA_TYPE_DISCONNECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link ConnectionHandler#DATA_TYPE_MESSAGE
|
||||||
|
*/
|
||||||
public static String getDataTypeMessage() {
|
public static String getDataTypeMessage() {
|
||||||
return DATA_TYPE_MESSAGE;
|
return DATA_TYPE_MESSAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link ConnectionHandler#DATA_TYPE_ERROR}
|
||||||
|
*/
|
||||||
public static String getDataTypeError() {
|
public static String getDataTypeError() {
|
||||||
return DATA_TYPE_ERROR;
|
return DATA_TYPE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return {@link NetworkHandler.NetworkConnection}
|
||||||
|
*/
|
||||||
public NetworkHandler.NetworkConnection<String> getConnection() {
|
public NetworkHandler.NetworkConnection<String> getConnection() {
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the NetworkConnection used for the server <-> client connection
|
||||||
|
* @param connection NetworkConnection used for the server <-> client connection
|
||||||
|
*/
|
||||||
protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
|
protected void setConnection(NetworkHandler.NetworkConnection<String> connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method reads the data when a ConnectionHandler recieves it. It tries to read out the sender, reciever, 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 reciever 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 reciever, StringBuilder type, StringBuilder payload) throws ChatProtocolException {
|
protected void processData(Scanner scanner, StringBuilder sender, StringBuilder reciever, StringBuilder type, StringBuilder payload) throws ChatProtocolException {
|
||||||
// parse data content
|
// parse data content
|
||||||
if (scanner.hasNextLine()) {
|
if (scanner.hasNextLine()) {
|
||||||
|
@ -72,6 +116,13 @@ public abstract class ConnectionHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets called to send data via the socket defined in the {@link NetworkHandler.NetworkConnection}
|
||||||
|
* @param sender of the data
|
||||||
|
* @param receiver of the data
|
||||||
|
* @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, String type, String payload) {
|
||||||
if (connection.isAvailable()) {
|
if (connection.isAvailable()) {
|
||||||
new StringBuilder();
|
new StringBuilder();
|
||||||
|
@ -82,7 +133,7 @@ public abstract class ConnectionHandler {
|
||||||
.append(payload+"\n")
|
.append(payload+"\n")
|
||||||
.toString();
|
.toString();
|
||||||
try {
|
try {
|
||||||
getConnection().send(data);
|
connection.send(data);
|
||||||
} 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) {
|
||||||
|
|
Loading…
Reference in New Issue