From dfdaae163b7de23fa180938f3a879fd7aa0cbfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 12 Mar 2025 23:16:39 +0100 Subject: [PATCH] documentation for all major modules --- .../main/java/client/ClientGuiController.java | 53 ++++++++++++++----- .../app/src/main/java/client/ClientLogin.java | 4 ++ .../main/java/client/UsernameSingleton.java | 7 +++ .../src/main/java/server/ServerConnexion.java | 43 +++++++++++++++ 4 files changed, 94 insertions(+), 13 deletions(-) diff --git a/ChatApp/app/src/main/java/client/ClientGuiController.java b/ChatApp/app/src/main/java/client/ClientGuiController.java index 3a3f148..6883157 100644 --- a/ChatApp/app/src/main/java/client/ClientGuiController.java +++ b/ChatApp/app/src/main/java/client/ClientGuiController.java @@ -45,25 +45,28 @@ public class ClientGuiController implements ClientListener { this.client = client; } -@FXML -public void initialize() throws SocketException { - client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername()); - Platform.runLater(() -> { - Stage stage = (Stage) vueContainer.getScene().getWindow(); -// stage.setResizable(true); // Fixed a bug that made the close button disappear under my Wayland setup (don't know if it's the same for x11) - stage.setOnCloseRequest(event -> { - connected = false; - client.close(); + @FXML + public void initialize() throws SocketException { + client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername()); + Platform.runLater(() -> { + Stage stage = (Stage) vueContainer.getScene().getWindow(); + stage.setResizable(true); // Fixed a bug that made the close button disappear under my Wayland setup (don't know if it's the same for x11) + stage.setOnCloseRequest(event -> { + connected = false; + client.close(); + }); + chatList.heightProperty().addListener((obs, oldVal, newVal) -> chatPane.setVvalue(1.0)); }); - chatList.heightProperty().addListener((obs, oldVal, newVal) -> chatPane.setVvalue(1.0)); - }); -} + } @Override public void handleDisconnect() { System.out.println("Disconnected"); } + /** + * Create an alert dialog when the connection to the server fails. + */ @Override public void handleConnexionError() { if(connected) { @@ -79,6 +82,9 @@ public void initialize() throws SocketException { } } + /** + * Create an alert dialog when the connection to the server is successful. + */ @Override public void handleConnect() { Platform.runLater(() -> { @@ -88,6 +94,13 @@ public void initialize() throws SocketException { alert.showAndWait(); }); } + + /** + * Format and insert the received chat message into the chat list. + * @param time the time the message was sent + * @param chatter the name of the person who sent the message + * @param content the content of the message + */ @Override public void handleChatMessage(Instant time, String chatter, String content) { String untagged = untag(content); @@ -129,7 +142,10 @@ public void initialize() throws SocketException { return result.toString(); } - + /** + * Display the list of rooms in the room list. + * @param roomNames the list of room names + */ @Override public void handleRoomList(List roomNames) { Platform.runLater(() -> { @@ -145,6 +161,10 @@ public void initialize() throws SocketException { }); } + /** + * Create the chat (message wall + input) environment. + * @param roomName the name of the room + */ private void createChatEnv(String roomName) { Platform.runLater(() -> { chatList.getChildren().clear(); @@ -177,8 +197,15 @@ public void initialize() throws SocketException { }); } + /** + * Colorize the message according to the color codes. + * @param chatter the name of the person who sent the message + * @param content the content of the message + * @return the formatted message + */ private TextFlow formatMessage(String chatter, String content) { TextFlow textFlow = new TextFlow(); + // Evil regex : match every sequence starting with a color code ending with the next color code Pattern pattern = Pattern.compile("&([rbgyn])([^&]*)"); Matcher matcher = pattern.matcher(content); int lastIndex = 0; diff --git a/ChatApp/app/src/main/java/client/ClientLogin.java b/ChatApp/app/src/main/java/client/ClientLogin.java index a292624..e717209 100644 --- a/ChatApp/app/src/main/java/client/ClientLogin.java +++ b/ChatApp/app/src/main/java/client/ClientLogin.java @@ -13,6 +13,10 @@ public class ClientLogin { @FXML public TextField usernameField; + /** + * Sets the username and switches to the loading screen. + * @throws IOException if the FXML file is not found. + */ @FXML private void login() throws IOException { String username = usernameField.getText(); diff --git a/ChatApp/app/src/main/java/client/UsernameSingleton.java b/ChatApp/app/src/main/java/client/UsernameSingleton.java index 53d1b84..eaf6eb1 100644 --- a/ChatApp/app/src/main/java/client/UsernameSingleton.java +++ b/ChatApp/app/src/main/java/client/UsernameSingleton.java @@ -1,5 +1,8 @@ package client; +/** + * This class aims to make the username available to all controllers. (Recommended by JavaFX, because otherwise, FXMLLoader bugs and doesn't switch scenes properly.) + */ public class UsernameSingleton { private static UsernameSingleton instance; private String username; @@ -7,6 +10,10 @@ public class UsernameSingleton { private UsernameSingleton() { } + /** + * Get the instance of the singleton. + * @return the instance + */ public static UsernameSingleton getInstance() { if (instance == null) { instance = new UsernameSingleton(); diff --git a/ChatApp/app/src/main/java/server/ServerConnexion.java b/ChatApp/app/src/main/java/server/ServerConnexion.java index 67fdb87..65d0d93 100644 --- a/ChatApp/app/src/main/java/server/ServerConnexion.java +++ b/ChatApp/app/src/main/java/server/ServerConnexion.java @@ -36,6 +36,10 @@ public class ServerConnexion implements PacketVisitor { } } + /** + * Check if the client is logged in + * @return true if the client is logged in + */ private boolean checkLogin() { if (this.chatterName != null && this.chatterName.isEmpty()) { sendPacket(new ServerResponsePacket(Response.AuthError)); @@ -44,6 +48,11 @@ public class ServerConnexion implements PacketVisitor { return true; } + + /** + * Make the server create a room & enter it + * @param packet the packet containing the room name + */ @Override public void visitPacket(CreateRoomPacket packet) { if (!checkLogin()) @@ -54,6 +63,10 @@ public class ServerConnexion implements PacketVisitor { onRoomJoin(); } + /** + * Make the server join a room + * @param packet the packet containing the room name + */ @Override public void visitPacket(JoinRoomPacket packet) { if (!checkLogin()) @@ -68,6 +81,10 @@ public class ServerConnexion implements PacketVisitor { onRoomJoin(); } + /** + * Make the server leave a room + * @param packet the packet containing the room name + */ @Override public void visitPacket(LeaveRoomPacket packet) { if (!checkLogin()) @@ -79,6 +96,10 @@ public class ServerConnexion implements PacketVisitor { onRoomLeave(roomName); } + /** + * Checks if the pseudo is available (nobody connected with the same pseudo) + * @param packet the packet containing the pseudo + */ @Override public void visitPacket(LoginPacket packet) { if (packet.getPseudo().isEmpty() || server.hasChatterName(packet.getPseudo())) { @@ -92,6 +113,10 @@ public class ServerConnexion implements PacketVisitor { System.out.println("[Server] Chatter " + packet.getPseudo() + " connected !"); } + /** + * Get the list of rooms + * @param packet the packet containing the request + */ @Override public void visitPacket(RequestRoomListPacket packet) { if (!checkLogin()) @@ -99,6 +124,10 @@ public class ServerConnexion implements PacketVisitor { sendPacket(new RoomListPacket(server.getRoomNames())); } + /** + * Send a message to the room + * @param packet the packet containing the message + */ @Override public void visitPacket(SendChatMessagePacket packet) { if (!checkLogin()) @@ -107,11 +136,18 @@ public class ServerConnexion implements PacketVisitor { sendPacket(new ServerResponsePacket(messageSent ? Response.MessageSent : Response.MessageNotSent)); } + /** + * Disconnect the client + * @param packet the packet containing the request + */ @Override public void visitPacket(DisconnectPacket packet) { this.onDisconnect(); } + /** + * Remove all room connections while disconnecting the client + */ private void onDisconnect() { if (this.server.isInRoom(this)) { this.onRoomLeave(this.server.getRoomName(this)); @@ -121,11 +157,18 @@ public class ServerConnexion implements PacketVisitor { System.out.println("[Server] Chatter " + chatterName + " disconnected !"); } + /** + * Send a message to the room when a client joins + */ private void onRoomJoin() { String joinMessage = "Chatter " + this.chatterName + " joined the room !"; this.server.sendToRoom(this, new ChatMessagePacket(Instant.now(), "", joinMessage)); } + /** + * Send a message to the room when a client leaves + * @param roomName the name of the room + */ private void onRoomLeave(String roomName) { String joinMessage = "Chatter " + this.chatterName + " left the room !"; this.server.sendToRoom(roomName, new ChatMessagePacket(Instant.now(), "", joinMessage));