From 40e3e146662143c6f90c5779e09772aab39bb27b Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sat, 16 Apr 2022 22:09:56 +0200 Subject: [PATCH] fixed #49 clean up --- .../pm2/multichat/client/ChatWindowController.java | 1 - .../pm2/multichat/client/ClientConnectionHandler.java | 5 +++-- .../zhaw/pm2/multichat/client/ClientMessageList.java | 7 +++---- .../zhaw/pm2/multichat/protocol/ConnectionHandler.java | 10 +++++----- .../java/ch/zhaw/pm2/multichat/protocol/Message.java | 2 +- .../main/java/ch/zhaw/pm2/multichat/server/Server.java | 2 +- .../pm2/multichat/server/ServerConnectionHandler.java | 6 ++---- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java index 8b0e625..df225eb 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java @@ -242,7 +242,6 @@ public class ChatWindowController { class WindowCloseHandler implements EventHandler { /** - * * @param event the event which occurred when Windows is closed */ public void handle(WindowEvent event) { diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java index eab747b..6fa0034 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java @@ -50,8 +50,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab * Called to initialize the ClientConnectionHandler when trying to start a connection * * @param serverAddress to connect to - * @param serverPort to connect to - * @param userName to connect as + * @param serverPort to connect to + * @param userName to connect as * @throws IOException if connection to Server not possible */ public void initialize(String serverAddress, int serverPort, String userName) throws IOException { @@ -214,6 +214,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab /** * Initiates the procedure to send a new message and sends one as such. + * * @param data Data which has been transmitted */ private void caseMessage(Message data) { diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 9061a4a..6151e97 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -17,6 +17,7 @@ public class ClientMessageList { /** * Adds a new message to ArrayList and also informs Listener. + * * @param message that should be added */ public void addMessage(Message message) { @@ -37,12 +38,10 @@ public class ClientMessageList { for (Message message : messages) { if (showAll || message.matchesFilter(filter)) { switch (message.getType()) { - case DATA_TYPE_MESSAGE -> - result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText())); + case DATA_TYPE_MESSAGE -> 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 DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_CONNECT -> 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())); } } } 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 ae586b0..1878f62 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 @@ -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 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) { diff --git a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/Message.java b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/Message.java index 9509b0f..b4837d3 100644 --- a/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/Message.java +++ b/protocol/src/main/java/ch/zhaw/pm2/multichat/protocol/Message.java @@ -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; diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java index be26302..a367fc4 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/Server.java @@ -85,7 +85,7 @@ public class Server { private void start() { System.out.println("Server started."); try { - while (true) { + while (networkServer.isAvailable()) { NetworkHandler.NetworkConnection connection = networkServer.waitForConnection(); ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections); new Thread(connectionHandler).start(); diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java index 0dc8aee..47924ee 100644 --- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java +++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java @@ -62,10 +62,8 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab /** * Constructor to initialize the connection * - * @param connection representing the socket connection between server and client - * @param registry map containing all active connections between server and clients - - + * @param connection representing the socket connection between server and client + * @param registry map containing all active connections between server and clients */ public ServerConnectionHandler(NetworkHandler.NetworkConnection connection, Map registry) {