From c9aed1affdab9ba3686490ca9b252b79a496754f Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 15 Apr 2022 22:06:22 +0200 Subject: [PATCH] javadoc --- .../multichat/protocol/ConnectionHandler.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java index b0ed994..21eacc8 100644 --- a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java +++ b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/ConnectionHandler.java @@ -5,6 +5,11 @@ 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 aswell {@link ConnectionHandler#sendData(String, String, String, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)} + */ public abstract class ConnectionHandler { private NetworkHandler.NetworkConnection connection; @@ -18,38 +23,77 @@ public abstract class ConnectionHandler { public static final String USER_NONE = ""; public static final String USER_ALL = "*"; + // State of the connection public enum State { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR; } + /** + * + * @return {@link ConnectionHandler#DATA_TYPE_CONNECT} + */ public static String getDataTypeConnect() { return DATA_TYPE_CONNECT; } + /** + * + * @return {@link ConnectionHandler#DATA_TYPE_CONFIRM} + */ public static String getDataTypeConfirm() { return DATA_TYPE_CONFIRM; } + /** + * + * @return {@link ConnectionHandler#DATA_TYPE_DISCONNECT} + */ public static String getDataTypeDisconnect() { return DATA_TYPE_DISCONNECT; } + /** + * + * @return {@link ConnectionHandler#DATA_TYPE_MESSAGE + */ public static String getDataTypeMessage() { return DATA_TYPE_MESSAGE; } + /** + * + * @return {@link ConnectionHandler#DATA_TYPE_ERROR} + */ public static String getDataTypeError() { return DATA_TYPE_ERROR; } + /** + * + * @return {@link NetworkHandler.NetworkConnection} + */ public NetworkHandler.NetworkConnection getConnection() { 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 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 { // parse data content 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) { if (connection.isAvailable()) { new StringBuilder(); @@ -82,7 +133,7 @@ public abstract class ConnectionHandler { .append(payload+"\n") .toString(); try { - getConnection().send(data); + connection.send(data); } catch (SocketException e) { System.err.println("Connection closed: " + e.getMessage()); } catch (EOFException e) {