documentation for all major modules

This commit is contained in:
Clément
2025-03-12 23:16:39 +01:00
parent 0b6f5193a0
commit dfdaae163b
4 changed files with 94 additions and 13 deletions

View File

@@ -50,7 +50,7 @@ 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.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();
@@ -64,6 +64,9 @@ public void initialize() throws SocketException {
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<String> 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;

View File

@@ -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();

View File

@@ -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();

View File

@@ -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));