Fix: Resolving Git Merge Conflicts #253

Merged
j.kropff merged 69 commits from chore/ui-checkstyle-fixes into main 2026-04-11 15:10:58 +02:00
18 changed files with 136 additions and 151 deletions
Showing only changes of commit 97e5a55042 - Show all commits
@@ -3,10 +3,13 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ChatClient;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatBoxController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.jspecify.annotations.Nullable;
import java.util.*;
/**
* responsible for the transferring of messages from the server to the ChatModel or from the
* ChatViewController to the server
@@ -1,10 +1,9 @@
package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import java.util.ArrayList;
import java.util.function.Consumer;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
/**
* ChatModel, stores the data for a specific chat
@@ -48,6 +47,7 @@ public class ChatModel {
/**
* Returns the type of chat this model represents.
*
* @return The {@link ChatType}.
*/
public ChatType getChattype() {
@@ -55,8 +55,8 @@ public class ChatModel {
}
/**
* Adds a new message to the history and notifies all registered listeners.
* This method is synchronized to ensure thread safety when updating the message list.
* Adds a new message to the history and notifies all registered listeners. This method is
* synchronized to ensure thread safety when updating the message list.
*
* @param msg The {@link Message} to be added.
*/
@@ -76,6 +76,7 @@ public class ChatModel {
/**
* Returns the target user for this chat, primarily used for whispers.
*
* @return The target username or null.
*/
public String getTarget() {
@@ -3,16 +3,13 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBodyBuilder;
import org.jspecify.annotations.NonNull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import org.jspecify.annotations.NonNull;
/**
* Message Object for internal handling of Chat-Messages
*/
/** Message Object for internal handling of Chat-Messages */
public class Message {
private final ChatType type;
private final String message;
@@ -22,8 +19,8 @@ public class Message {
public String target = null;
/**
* Constructs a Message with a provided timestamp. Typically used when
* reconstructing messages received from the server.
* Constructs a Message with a provided timestamp. Typically used when reconstructing messages
* received from the server.
*
* @param type The chat category (e.g., GLOBAL, LOBBY, or WHISPER).
* @param lobbyId The ID of the lobby, or -1 if not applicable.
@@ -48,8 +45,8 @@ public class Message {
}
/**
* Constructs a new Message for the current user. Automatically generates
* a timestamp based on the local system time ("HH:mm").
* Constructs a new Message for the current user. Automatically generates a timestamp based on
* the local system time ("HH:mm").
*
* @param type The chat category (e.g., GLOBAL, LOBBY, or WHISPER).
* @param lobbyId The ID of the lobby, or -1 if not applicable.
@@ -87,8 +84,8 @@ public class Message {
}
/**
* Formats the message object into a string representation compatible with
* the network protocol arguments.
* Formats the message object into a string representation compatible with the network protocol
* arguments.
*
* @return A formatted string containing all message attributes for server transmission.
*/
@@ -110,8 +107,8 @@ public class Message {
}
/**
* Parses a list of network request parameters to reconstruct a Message object.
* Handles different chat types (GLOBAL, LOBBY, WHISPER) and their specific requirements.
* Parses a list of network request parameters to reconstruct a Message object. Handles
* different chat types (GLOBAL, LOBBY, WHISPER) and their specific requirements.
*
* @param parameters A list of {@link RequestParameter} received from the network.
* @return A new {@link Message} instance populated with the parsed data.
@@ -120,21 +117,24 @@ public class Message {
String typeString = getParString(parameters, "TYPE");
ChatType type = ChatType.valueOf(typeString);
return switch (type) {
case GLOBAL -> new Message(
case GLOBAL ->
new Message(
ChatType.GLOBAL,
-1,
getParString(parameters, "USER"),
null,
getParString(parameters, "TIME"),
getParString(parameters, "TEXT"));
case LOBBY -> new Message(
case LOBBY ->
new Message(
ChatType.LOBBY,
Integer.parseInt(getParString(parameters, "GAME")),
getParString(parameters, "USER"),
null,
getParString(parameters, "TIME"),
getParString(parameters, "TEXT"));
case WHISPER -> new Message(
case WHISPER ->
new Message(
ChatType.WHISPER,
Integer.parseInt(getParString(parameters, "GAME", "-1")),
getParString(parameters, "USER"),
@@ -158,7 +158,8 @@ public class Message {
}
/**
* Helper method to extract a specific parameter value by its key, with a fallback default value.
* Helper method to extract a specific parameter value by its key, with a fallback default
* value.
*
* @param parameters The list of parameters to search.
* @param keyString The key to look for.
@@ -3,9 +3,6 @@ package ch.unibas.dmi.dbis.cs108.casono.client.network;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TcpTransport;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
@@ -17,6 +14,8 @@ import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* The ClientService class is responsible for managing the connection to the server, sending
@@ -35,9 +34,9 @@ public class ClientService {
private final Logger logger;
/**
* Constructs a ClientService with the given server IP and port. It establishes
* a socket connection to the server and initializes the TcpTransport and
* ExecutorService for communication.
* Constructs a ClientService with the given server IP and port. It establishes a socket
* connection to the server and initializes the TcpTransport and ExecutorService for
* communication.
*
* @param ip The IP address of the server to connect to.
* @param port The port number of the server to connect to.
@@ -64,8 +63,8 @@ public class ClientService {
"(?<key>\\w+)=(('(?<string>([^']|\\')+)')|(?<primVal>[+-]?[\\d\\w:]+))");
/**
* Removes escape characters from a string, specifically converting escaped
* single quotes (\') back to regular single quotes (').
* Removes escape characters from a string, specifically converting escaped single quotes (\')
* back to regular single quotes (').
*
* @param input The escaped string to process.
* @return The unescaped string.
@@ -75,9 +74,9 @@ public class ClientService {
}
/**
* Converts a list of raw string parameters into a list of {@link RequestParameter} objects.
* It uses a regex matcher to distinguish between quoted strings (which are unescaped)
* and primitive values.
* Converts a list of raw string parameters into a list of {@link RequestParameter} objects. It
* uses a regex matcher to distinguish between quoted strings (which are unescaped) and
* primitive values.
*
* @param input A list of raw strings to be parsed.
* @return A list of parsed {@link RequestParameter} objects.
@@ -102,13 +101,14 @@ public class ClientService {
}
/**
* Sends a command to the server and processes the multi-line response.
* It handles the protocol handshake (expecting +OK), strips leading tabs from
* response lines, and collects them until the "END" marker is reached.
* Sends a command to the server and processes the multi-line response. It handles the protocol
* handshake (expecting +OK), strips leading tabs from response lines, and collects them until
* the "END" marker is reached.
*
* @param message The raw command string to be sent to the transport layer.
* @return A list of response lines received from the server (excluding protocol markers).
* @throws RuntimeException if the server responds with an error or if a communication failure occurs.
* @throws RuntimeException if the server responds with an error or if a communication failure
* occurs.
*/
protected List<String> processCommand(String message) {
List<String> response = new ArrayList<>();
@@ -3,6 +3,9 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
@@ -13,10 +16,6 @@ import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class ChatBoxController {
private String username;
@@ -41,9 +40,9 @@ public class ChatBoxController {
}
/**
* Initializes the chat interface by creating the global chat model and
* adding the corresponding "GLOBAL" tab to the interface.
* It also registers the global chat in the {@link ChatController}'s model map.
* Initializes the chat interface by creating the global chat model and adding the corresponding
* "GLOBAL" tab to the interface. It also registers the global chat in the {@link
* ChatController}'s model map.
*/
@FXML
public void initialize() {
@@ -56,9 +55,8 @@ public class ChatBoxController {
}
/**
* Adds a specific user to the list of available whisper targets.
* Creates a new menu item for the user and defines the action to open
* a private chat tab when selected.
* Adds a specific user to the list of available whisper targets. Creates a new menu item for
* the user and defines the action to open a private chat tab when selected.
*
* @param targetUserName The username of the person to be added to the whisper list.
*/
@@ -70,8 +68,8 @@ public class ChatBoxController {
}
/**
* Creates a new private (whisper) chat model for a specific target user,
* registers it within the chat system, and opens a new chat tab.
* Creates a new private (whisper) chat model for a specific target user, registers it within
* the chat system, and opens a new chat tab.
*
* @param target The username of the recipient for the private messages.
*/
@@ -84,9 +82,9 @@ public class ChatBoxController {
}
/**
* Dynamically loads a new chat tab from an FXML resource and attaches it
* to the TabPane. It initializes a {@link ChatViewController} for the tab
* and sets up a listener to display incoming messages in real-time.
* Dynamically loads a new chat tab from an FXML resource and attaches it to the TabPane. It
* initializes a {@link ChatViewController} for the tab and sets up a listener to display
* incoming messages in real-time.
*
* @param title The title to be displayed on the tab header.
* @param chatModel The {@link ChatModel} containing the data and logic for this specific chat.
@@ -3,6 +3,8 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -13,9 +15,6 @@ import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import java.net.URL;
import java.util.ResourceBundle;
/** Responsible for the presentation of the ChatModel to the Client */
public class ChatViewController implements Initializable {
@@ -44,11 +43,11 @@ public class ChatViewController implements Initializable {
this.username = username;
this.chatModel = chatModel;
}
/**
* Initializes the controller after the FXML root element has been processed.
* Sets up event handlers for sending messages via the input field or button
* and ensures the ScrollPane automatically scrolls to the bottom when new
* messages are added.
* Initializes the controller after the FXML root element has been processed. Sets up event
* handlers for sending messages via the input field or button and ensures the ScrollPane
* automatically scrolls to the bottom when new messages are added.
*
* @param location The location used to resolve relative paths for the root object.
* @param resourceBundle The resources used to localize the root object.
@@ -61,9 +60,9 @@ public class ChatViewController implements Initializable {
}
/**
* Retrieves the text from the input field, creates a new {@link Message} object
* using the current model state, and passes it to the {@link ChatController}
* for network transmission. The input field is cleared after sending.
* Retrieves the text from the input field, creates a new {@link Message} object using the
* current model state, and passes it to the {@link ChatController} for network transmission.
* The input field is cleared after sending.
*/
public void sendMessage() {
String message = inputField.getText().trim();
@@ -81,10 +80,9 @@ public class ChatViewController implements Initializable {
}
/**
* Displays a message in the chat interface. This method creates a new Label
* for the message text and adds it to the message container.
* It uses {@link Platform#runLater(Runnable)} to ensure the UI update
* happens on the JavaFX Application Thread.
* Displays a message in the chat interface. This method creates a new Label for the message
* text and adds it to the message container. It uses {@link Platform#runLater(Runnable)} to
* ensure the UI update happens on the JavaFX Application Thread.
*
* @param msg The {@link Message} object containing the content and metadata to display.
*/
@@ -11,8 +11,8 @@ public class GetMessageCountHandler extends CommandHandler<GetMessageCountReques
private final UserRegistry userRegistry;
/**
* Constructs a new GetMessageCountHandler with the necessary response dispatcher
* and user registry.
* Constructs a new GetMessageCountHandler with the necessary response dispatcher and user
* registry.
*
* @param responseDispatcher The dispatcher used to send the count or error back to the client.
* @param userRegistry The registry used to identify the user and access their message queue.
@@ -24,10 +24,9 @@ public class GetMessageCountHandler extends CommandHandler<GetMessageCountReques
}
/**
* Processes a request to retrieve the number of pending messages for a user.
* It looks up the user by their session ID; if found, it dispatches a
* {@link GetMessageCountResponse} containing the current count. Otherwise,
* it dispatches an {@link ErrorResponse}.
* Processes a request to retrieve the number of pending messages for a user. It looks up the
* user by their session ID; if found, it dispatches a {@link GetMessageCountResponse}
* containing the current count. Otherwise, it dispatches an {@link ErrorResponse}.
*
* @param request The {@link GetMessageCountRequest} containing the session details.
*/
@@ -6,9 +6,9 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.
public class GetMessageCountParser implements CommandParser<GetMessageCountRequest> {
/**
* Parses a raw {@link PrimitiveRequest} into a {@link GetMessageCountRequest}.
* This method wraps the request context from the network layer into a
* structured message count request object.
* Parses a raw {@link PrimitiveRequest} into a {@link GetMessageCountRequest}. This method
* wraps the request context from the network layer into a structured message count request
* object.
*
* @param primitiveRequest The raw request containing parameters and context from the network.
* @return A new {@link GetMessageCountRequest} instance.
@@ -5,9 +5,9 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo
public class GetMessageCountRequest extends Request {
/**
* Constructs a new GetMessageCountRequest with the specified request context.
* This request is used by a client to query the number of pending messages
* currently waiting in their server-side queue.
* Constructs a new GetMessageCountRequest with the specified request context. This request is
* used by a client to query the number of pending messages currently waiting in their
* server-side queue.
*
* @param context The {@link RequestContext} associated with this request.
*/
@@ -6,9 +6,8 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.
public class GetMessageCountResponse extends SuccessResponse {
/**
* Constructs a new GetMessageCountResponse. It creates a response body
* containing the "COUNT" parameter and associates it with the original
* request context.
* Constructs a new GetMessageCountResponse. It creates a response body containing the "COUNT"
* parameter and associates it with the original request context.
*
* @param context The {@link RequestContext} of the original request.
* @param count The number of pending messages to be returned to the client.
@@ -6,7 +6,6 @@ import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
import java.util.Optional;
public class GetNextMessageHandler extends CommandHandler<GetNextMessageRequest> {
@@ -22,11 +21,12 @@ public class GetNextMessageHandler extends CommandHandler<GetNextMessageRequest>
super(responseDispatcher);
this.userRegistry = userRegistry;
}
/**
* Executes the request to retrieve the next message for a specific user.
* It identifies the user via their session ID, dequeues the next available message,
* and dispatches a {@link GetNextMessageResponse}. If the user cannot be identified,
* an {@link ErrorResponse} is sent instead.
* Executes the request to retrieve the next message for a specific user. It identifies the user
* via their session ID, dequeues the next available message, and dispatches a {@link
* GetNextMessageResponse}. If the user cannot be identified, an {@link ErrorResponse} is sent
* instead.
*
* @param request The {@link GetNextMessageRequest} containing the session and context.
*/
@@ -6,10 +6,9 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.
public class GetNextMessageParser implements CommandParser<GetNextMessageRequest> {
/**
* Parses a raw {@link PrimitiveRequest} into a {@link GetNextMessageRequest}.
* This method initializes a parameter accessor (though not currently used
* for extraction) and returns a structured request object containing
* the original request context.
* Parses a raw {@link PrimitiveRequest} into a {@link GetNextMessageRequest}. This method
* initializes a parameter accessor (though not currently used for extraction) and returns a
* structured request object containing the original request context.
*
* @param primitiveRequest The raw request containing parameters and context from the network.
* @return A new {@link GetNextMessageRequest} instance.
@@ -5,9 +5,9 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo
public class GetNextMessageRequest extends Request {
/**
* Constructs a new GetNextMessageRequest with the specified request context.
* This request is typically used by a client to poll or retrieve the next
* available message from the server's queue.
* Constructs a new GetNextMessageRequest with the specified request context. This request is
* typically used by a client to poll or retrieve the next available message from the server's
* queue.
*
* @param context The {@link RequestContext} associated with this request.
*/
@@ -7,9 +7,8 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.
public class GetNextMessageResponse extends SuccessResponse {
/**
* Constructs a new GetNextMessageResponse. It converts the provided {@link Message}
* into a network-compatible response body and associates it with the original
* request context.
* Constructs a new GetNextMessageResponse. It converts the provided {@link Message} into a
* network-compatible response body and associates it with the original request context.
*
* @param context The {@link RequestContext} of the request being answered.
* @param msg The {@link Message} to be sent back to the client.
@@ -22,8 +22,8 @@ public class SendMessageHandler extends CommandHandler<SendMessageRequest> {
/**
* Processes a message send request. This method extracts the message from the request,
* broadcasts it to all connected users, and dispatches a success response (OK)
* back to the sender.
* broadcasts it to all connected users, and dispatches a success response (OK) back to the
* sender.
*
* @param request The {@link SendMessageRequest} containing the message and context.
*/
@@ -36,8 +36,8 @@ public class SendMessageHandler extends CommandHandler<SendMessageRequest> {
}
/**
* Distributes a message to every user currently registered in the system.
* Each user's message queue is updated with the new message.
* Distributes a message to every user currently registered in the system. Each user's message
* queue is updated with the new message.
*
* @param message The {@link Message} object to be broadcast.
*/
@@ -7,9 +7,9 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Primitive
public class SendMessageParser implements CommandParser<SendMessageRequest> {
/**
* Parses a raw {@link PrimitiveRequest} into a specific {@link SendMessageRequest}.
* This method extracts the message details from the request parameters and
* wraps them along with the request context into a structured request object.
* Parses a raw {@link PrimitiveRequest} into a specific {@link SendMessageRequest}. This method
* extracts the message details from the request parameters and wraps them along with the
* request context into a structured request object.
*
* @param primitiveRequest The raw request containing parameters and context from the network.
* @return A structured {@link SendMessageRequest} containing the parsed {@link Message}.
@@ -3,13 +3,12 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.network.CoreClient;
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatBoxController;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class ChatApplication extends Application {
private static final int SCENE_WIDTH = 1200;
@@ -21,30 +20,19 @@ public class ChatApplication extends Application {
public ChatApplication() {}
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ui-structure/components/chatui/chatbox.fxml"));
FXMLLoader fxmlLoader =
new FXMLLoader(
getClass().getResource("/ui-structure/components/chatui/chatbox.fxml"));
ClientService clientService = new ClientService(ip, port);
CoreClient coreClient = new CoreClient(clientService);
// TODO login UI
coreClient.login(username);
ChatController chatController = new ChatController(username, clientService);
ChatBoxController chatBoxController = new ChatBoxController(username, chatController);
fxmlLoader.setController(chatBoxController);
/*fxmlLoader.setControllerFactory(type -> {
if (type == ChatBoxController.class) {
return chatController.getChatBoxController();
} else {
try {
return type.getConstructor().newInstance();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
});*/
Scene scene = new Scene(fxmlLoader.load(), SCENE_WIDTH, SCENE_HEIGHT);
stage.setTitle("Chat");
stage.setScene(scene);
stage.show();
}
}