Merge branch 'main' into 'feat/ui-switch-lobby-game'

This commit is contained in:
Jona Walpert
2026-04-01 11:24:39 +02:00
58 changed files with 1408 additions and 143 deletions
@@ -3,8 +3,11 @@ package ch.unibas.dmi.dbis.cs108.casono.server;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserCleanupJob;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.NetworkManager;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandRouter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParserDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionDisconnectJob;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionManager;
import java.time.Duration;
import java.util.concurrent.Executors;
@@ -15,9 +18,12 @@ import org.apache.logging.log4j.Logger;
/** Application class for starting the server. */
public class ServerApp {
public static final int USER_CLEANUP_JOB_DELAY = 0;
public static final int USER_CLEANUP_JOB_PERIOD = 10;
public static final int USER_CLEANUP_JOB_RECONNECT_THRESHOLD = 10;
private static final int USER_CLEANUP_JOB_DELAY = 0;
private static final int USER_CLEANUP_JOB_PERIOD = 10;
private static final int USER_CLEANUP_JOB_RECONNECT_THRESHOLD = 10;
private static final int SESSION_DISCONNECT_JOB_DELAY = 0;
private static final int SESSION_DISCONNECT_JOB_PERIOD = 2;
private static final int SESSION_DISCONNECT_JOB_TIMEOUT = 5;
public static void start(String arg) {
int port = Integer.parseInt(arg);
@@ -26,10 +32,12 @@ public class ServerApp {
logger.info("Starting server at port {}", port);
EventBus eventBus = new EventBus();
SessionManager sessionManager = new SessionManager();
eventBus.subscribe(
DisconnectEvent.class, event -> sessionManager.removeSession(event.sessionId()));
NetworkManager networkManager = new NetworkManager(port, sessionManager, eventBus);
CommandParserDispatcher dispatcher = new CommandParserDispatcher();
CommandRouter router = new CommandRouter();
SessionManager sessionManager = new SessionManager(eventBus, dispatcher, router);
eventBus.subscribe(DisconnectEvent.class, event -> sessionManager.onDisconnect(event));
NetworkManager networkManager = new NetworkManager(port, sessionManager);
UserRegistry userRegistry = new UserRegistry();
eventBus.subscribe(
@@ -41,6 +49,14 @@ public class ServerApp {
USER_CLEANUP_JOB_DELAY,
USER_CLEANUP_JOB_PERIOD,
TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(
new SessionDisconnectJob(
sessionManager,
eventBus,
Duration.ofSeconds(SESSION_DISCONNECT_JOB_TIMEOUT)),
SESSION_DISCONNECT_JOB_DELAY,
SESSION_DISCONNECT_JOB_PERIOD,
TimeUnit.SECONDS);
networkManager.start();
}
@@ -58,7 +58,6 @@ public class UserRegistry {
*
* @param sessionId the session ID of the disconnected client
*/
// TODO: Add to EventRegistry with DisconnectEvent
public synchronized void onDisconnect(SessionId sessionId) {
User user = bySessionId.remove(sessionId);
if (user == null) {
@@ -1,8 +1,5 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.Session;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionManager;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TcpTransport;
import java.io.IOException;
@@ -18,23 +15,19 @@ public class NetworkManager implements Runnable {
private Thread thread;
private Boolean running;
private SessionManager sessionManager;
private EventBus eventBus;
/**
* Creates a new NetworkManager with the given port, session manager, and event bus.
*
* @param port the port to listen on
* @param sessionManager the session manager to use
* @param eventBus the event bus for events
*/
public NetworkManager(Integer port, SessionManager sessionManager, EventBus eventBus) {
public NetworkManager(Integer port, SessionManager sessionManager) {
this.port = port;
this.logger = LogManager.getLogger(NetworkManager.class);
this.thread = new Thread(this, "networkManager");
this.running = true;
this.sessionManager = sessionManager;
this.eventBus = eventBus;
this.eventBus.subscribe(DisconnectEvent.class, event -> clientDisconnected(event));
}
/** Starts the internal thread to accept new connections. */
@@ -43,15 +36,6 @@ public class NetworkManager implements Runnable {
thread.start();
}
/**
* Handles client disconnection events.
*
* @param event the disconnect event
*/
public void clientDisconnected(DisconnectEvent event) {
logger.info("Session {} disconnected", event.sessionId().value());
}
/** Runs the network manager loop, accepting connections. */
@Override
public void run() {
@@ -61,9 +45,7 @@ public class NetworkManager implements Runnable {
logger.debug("Accepted connection from {}", clientSocket.getRemoteSocketAddress());
Session session = new Session(new TcpTransport(clientSocket), eventBus);
sessionManager.addSession(session);
session.start();
sessionManager.create(new TcpTransport(clientSocket));
}
} catch (IOException e) {
@@ -0,0 +1,7 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
public interface CommandHandler<T extends Request> {
void execute(T request);
}
@@ -0,0 +1,28 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import java.util.HashMap;
import java.util.Map;
public class CommandRouter {
private final Map<Class<? extends Request>, CommandHandler<?>> handlers = new HashMap<>();
public <T extends Request> void register(Class<T> request, CommandHandler<T> handler) {
handlers.put(request, handler);
}
// Safe, because during registration, it's ensured that the provided CommandHandler only
// receives requests it can handle.
@SuppressWarnings("unchecked")
public void execute(Request request) {
CommandHandler<Request> handler =
(CommandHandler<Request>) handlers.get(request.getClass());
if (handler == null) {
String requestName = request.getClass().toString();
throw new UnknownRequestException(
"Unable to execute request " + requestName + ". Type unknown", requestName);
}
handler.execute(request);
}
}
@@ -0,0 +1,14 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution;
public class UnknownRequestException extends RuntimeException {
private final String requestName;
public UnknownRequestException(String message, String requestName) {
super(message);
this.requestName = requestName;
}
public String getRequestName() {
return requestName;
}
}
@@ -1,4 +1,7 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
/**
* Parser to convert the PrimitiveRequest to a Request and performing checks for required fields and
@@ -1,5 +1,7 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import java.util.HashMap;
import java.util.Map;
@@ -0,0 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing;
/** Used in the PrimitiveRequest class to store the key of a parameter with its respective value */
public record RequestParameter(String key, String value) {}
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
package ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing;
/**
* Exception thrown when the CommandParserDispatcher has no registered handler for the provided
@@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/** Used in the PrimitiveRequest class to store the key of a parameter with its respective value */
public record Parameter(String key, String value) {}
@@ -1,6 +0,0 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
import java.util.List;
/** Created by the ProtocolParser to allow easy access to the request contents */
public record PrimitiveRequest(int requestId, String command, List<Parameter> parameters) {}
@@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
/** Request, produced by the CommandParser */
public interface Request {}
@@ -1,11 +1,12 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.RawToken;
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.Token;
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.TokenClassifier;
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.TokenType;
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.Tokenizer;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.RawToken;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.Token;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.TokenClassifier;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.TokenType;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.Tokenizer;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RawRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -19,15 +20,15 @@ public class ProtocolParser {
* @param packet the RawPacket containing the recieved data
* @return created PrimitiveRequest
*/
public static PrimitiveRequest parse(RawPacket packet) {
List<RawToken> rawTokens = Tokenizer.tokenize(packet.payload());
public static RawRequest parse(String payload) {
List<RawToken> rawTokens = Tokenizer.tokenize(payload);
List<Token> tokens = TokenClassifier.classify(rawTokens);
Iterator<Token> iterator = tokens.iterator();
String command = readCommand(iterator);
List<Parameter> parameters = readParameters(iterator);
List<RequestParameter> parameters = readParameters(iterator);
return new PrimitiveRequest(packet.requestId(), command, parameters);
return new RawRequest(command, parameters);
}
/**
@@ -51,8 +52,8 @@ public class ProtocolParser {
* @param iterator
* @return list containing all parsed parameters
*/
private static List<Parameter> readParameters(Iterator<Token> iterator) {
List<Parameter> parameters = new ArrayList<>();
private static List<RequestParameter> readParameters(Iterator<Token> iterator) {
List<RequestParameter> parameters = new ArrayList<>();
try {
while (iterator.hasNext()) {
@@ -66,7 +67,7 @@ public class ProtocolParser {
readSeperator(iterator.next());
String value = readValue(iterator.next());
parameters.add(new Parameter(key, value));
parameters.add(new RequestParameter(key, value));
}
} catch (NoSuchElementException e) {
throw new ProtocolParserException("Ran out of tokens while reading parameter");
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.parser;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser;
public class ProtocolParserException extends RuntimeException {
public ProtocolParserException(String message) {
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
/** Represents a raw (unclassified) token in the tokenizer. */
public record RawToken(RawTokenType type, String value, int line, int column) {}
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
public enum RawTokenType {
WORD,
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
import java.util.ArrayList;
import java.util.List;
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
/** Represents a token in the tokenizer. */
public record Token(TokenType type, String value, int line, int column) {}
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
import java.util.ArrayList;
import java.util.List;
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
/** Enumeration of token types used in the tokenizer. */
public enum TokenType {
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
import java.util.List;
@@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.tokenizer;
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer;
/** Exception thrown during tokenization. */
public class TokenizerException extends RuntimeException {
@@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import java.util.List;
/** Created by the ProtocolParser to allow easy access to the request contents */
public record PrimitiveRequest(
RequestContext context, String command, List<RequestParameter> parameters) {}
@@ -0,0 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import java.util.List;
public record RawRequest(String command, List<RequestParameter> parameters) {}
@@ -0,0 +1,24 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/** Request, produced by the CommandParser */
public abstract class Request {
protected final RequestContext context;
public Request(RequestContext context) {
this.context = context;
}
public RequestContext getContext() {
return context;
}
public SessionId getSessionId() {
return context.sessionId();
}
public int getRequestId() {
return context.requestId();
}
}
@@ -0,0 +1,14 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/**
* Immutable context for a network request.
*
* <p>Contains the originating session's identifier and the request's id. Later used to create
* response.
*
* @param sessionId the identifier of the session that initiated the request
* @param requestId the request's numeric id within the session
*/
public record RequestContext(SessionId sessionId, int requestId) {}
@@ -0,0 +1,26 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor;
/** Exception thrown when a required parameter key is not found. */
public class MissingParameterException extends RuntimeException {
private final String parameterKey;
/**
* Creates a new exception for a missing required parameter.
*
* @param message human-readable description of the missing parameter
* @param parameterKey key of the parameter that could not be found
*/
public MissingParameterException(String message, String parameterKey) {
super(message);
this.parameterKey = parameterKey;
}
/**
* Returns the missing parameter key.
*
* @return key of the parameter that could not be found
*/
public String getParameterKey() {
return parameterKey;
}
}
@@ -0,0 +1,27 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor;
/** Exception thrown when a parameter value cannot be converted to the requested type. */
public class ParameterParseException extends RuntimeException {
private final String parameterKey;
/**
* Creates a new parse exception with a root cause.
*
* @param message human-readable description of the parsing failure
* @param parameterKey key for whose value the error occured
* @param cause original exception thrown during parsing
*/
public ParameterParseException(String message, String parameterKey, Throwable cause) {
super(message, cause);
this.parameterKey = parameterKey;
}
/**
* Returns the missing parameter key.
*
* @return key for whose value the error occured
*/
public String getParameterKey() {
return parameterKey;
}
}
@@ -0,0 +1,116 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Provides typed access to a request's parameters by indexing them by key.
*
* <p>Supports required and optional lookups, with optional conversion from {@link String} values to
* domain-specific types via parser functions.
*/
public class RequestParameterAccessor {
private final Map<String, String> index;
/**
* Creates an accessor
*
* @param parameters to use
*/
public RequestParameterAccessor(List<RequestParameter> parameters) {
this.index =
parameters.stream()
.collect(
Collectors.toUnmodifiableMap(
RequestParameter::key, RequestParameter::value));
}
/**
* Returns the raw value for a required parameter key.
*
* @param key parameter key to look up
* @return raw parameter value
* @throws MissingParameterException if no parameter with the given key exists
*/
public String require(String key) throws MissingParameterException {
String value = index.get(key);
if (value == null) {
throw new MissingParameterException(
"Required parameter with key '" + key + "' is missing.", key);
}
return value;
}
/**
* Returns a parsed value for a required parameter key.
*
* @param key parameter key to look up
* @param parser parser used to convert the raw value
* @param <T> target type returned by the parser
* @return parsed parameter value
* @throws MissingParameterException if no parameter with the given key exists
* @throws ParameterParseException if parsing the raw value fails
*/
public <T> T require(String key, ThrowingParser<T> parser)
throws MissingParameterException, ParameterParseException {
String value = require(key);
try {
return parser.parse(value);
} catch (Exception e) {
throw new ParameterParseException(
"Error while parsing '" + key + "' with specified parser", key, e);
}
}
/**
* Returns the raw value for a parameter key or the provided default value if missing.
*
* @param key parameter key to look up
* @param defaultValue value returned when the key does not exist
* @return found parameter value or {@code defaultValue} if absent
*/
public String optional(String key, String defaultValue) {
String value = index.get(key);
if (value == null) {
return defaultValue;
}
return value;
}
/**
* Returns a parsed value for a parameter key or the provided default value if missing.
*
* @param key parameter key to look up
* @param defaultValue value returned when the key does not exist
* @param parser parser used to convert the raw value
* @param <T> target type returned by the parser
* @return parsed parameter value or {@code defaultValue} if absent
* @throws ParameterParseException if parsing the raw value fails
*/
public <T> T optional(String key, T defaultValue, ThrowingParser<T> parser)
throws ParameterParseException {
String value = index.get(key);
if (value == null) {
return defaultValue;
}
try {
return parser.parse(value);
} catch (Exception e) {
throw new ParameterParseException(
"Error while parsing '" + key + "' with specified parser", key, e);
}
}
/**
* Checks whether a parameter with the given key exists.
*
* @param key parameter key to check
* @return {@code true} if the key exists, otherwise {@code false}
*/
public boolean containsKey(String key) {
return index.containsKey(key);
}
}
@@ -0,0 +1,18 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor;
/**
* Functional parser interface used to convert a raw string parameter into a target type.
*
* @param <T> target type produced by the parser
*/
@FunctionalInterface
interface ThrowingParser<T> {
/**
* Parses the provided raw parameter value.
*
* @param value raw parameter value
* @return parsed value
* @throws Exception if the value cannot be parsed
*/
T parse(String value) throws Exception;
}
@@ -0,0 +1,35 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/** Response representing an error outcome for a client's request. */
public class ErrorResponse extends Response {
/**
* Construct an error response with a code and message.
*
* @param sessionId the target session id
* @param requestId the originating request id
* @param errorCode a short error code identifying the failure
* @param errorMessage a human readable error message
*/
public ErrorResponse(
SessionId sessionId, int requestId, String errorCode, String errorMessage) {
super(
sessionId,
requestId,
ResponseBody.builder().param("CODE", errorCode).param("MSG", errorMessage).build());
}
/**
* {@inheritDoc}
*
* <p>This implementation returns the fixed {@code -ERR} prefix.
*
* @return the {@code -ERR} prefix
*/
@Override
public String prefix() {
return "-ERR";
}
}
@@ -0,0 +1,21 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/**
* A simple success response with an empty body.
*
* <p>Use this to acknowledge successful requests that do not carry additional payload data.
*/
public class OkResponse extends SuccessResponse {
/**
* Create a minimal successful response (no body content).
*
* @param sessionId the target session id
* @param requestId the originating request id
*/
public OkResponse(SessionId sessionId, int requestId) {
super(sessionId, requestId, ResponseBody.builder().build());
}
}
@@ -0,0 +1,12 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/**
* Immutable transport record representing an encoded response ready for delivery to a session.
*
* @param sessionId the target session id
* @param requestId the originating request id
* @param payload the serialized response payload
*/
public record PrimitiveResponse(SessionId sessionId, int requestId, String payload) {}
@@ -0,0 +1,59 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/** Abstract base class for all server responses sent to clients. */
public abstract class Response {
private final SessionId sessionId;
private final int requestId;
private final ResponseBody body;
/**
* Create a new {@code Response}.
*
* @param sessionId the id of the session this response targets
* @param requestId the request identifier this response corresponds to
* @param body the structured response body
*/
protected Response(SessionId sessionId, int requestId, ResponseBody body) {
this.sessionId = sessionId;
this.requestId = requestId;
this.body = body;
}
/**
* Returns the protocol prefix for this response (for example {@code "+OK"} or {@code "-ERR"}).
*
* @return the response prefix string used by the encoder
*/
public abstract String prefix();
/**
* Returns the session id that should receive this response.
*
* @return the target {@link SessionId}
*/
public SessionId getSessionId() {
return sessionId;
}
/**
* Returns the request identifier associated with this response.
*
* @return the numeric request id
*/
public int getRequestId() {
return requestId;
}
/**
* Returns the immutable {@link ResponseBody} that carries the structured payload for this
* response.
*
* @return the response body
*/
public ResponseBody getBody() {
return body;
}
}
@@ -0,0 +1,35 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionId;
/**
* Abstract {@link Response} specialization indicating a successful outcome.
*
* <p>Implementations of this class use the {@code +OK} prefix. It provides a protected constructor
* so subclasses can supply the response body content.
*/
public abstract class SuccessResponse extends Response {
/**
* Create a successful response with the provided body.
*
* @param sessionId the session id this response targets
* @param requestId the originating request id
* @param body the response body
*/
protected SuccessResponse(SessionId sessionId, int requestId, ResponseBody body) {
super(sessionId, requestId, body);
}
/**
* {@inheritDoc}
*
* <p>This implementation returns the fixed {@code +OK} prefix.
*
* @return the {@code +OK} prefix
*/
@Override
public final String prefix() {
return "+OK";
}
}
@@ -0,0 +1,12 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder;
import java.util.List;
/**
* A block node that contains a tag and a list of child {@link ResponseNode} elements. Blocks can be
* nested to build hierarchical response bodies.
*
* @param tag the block tag
* @param children the child nodes contained in this block
*/
public record ResponseBlock(String tag, List<ResponseNode> children) implements ResponseNode {}
@@ -0,0 +1,41 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response;
import java.util.List;
/**
* Immutable container for the structured content of a {@link Response}.
*
* <p>A {@code ResponseBody} holds an ordered list of {@link ResponseNode} items (parameters and
* blocks). Use {@link #builder()} to construct instances.
*/
public class ResponseBody {
private final List<ResponseNode> nodes;
/**
* Package-private constructor used by {@link ResponseBodyBuilder}.
*
* @param nodes the list of response nodes to include in this body
*/
ResponseBody(List<ResponseNode> nodes) {
this.nodes = List.copyOf(nodes);
}
/**
* Create a new {@link ResponseBodyBuilder} for assembling a response body.
*
* @return a fresh builder instance
*/
public static ResponseBodyBuilder builder() {
return new ResponseBodyBuilder();
}
/**
* Returns the ordered list of {@link ResponseNode} elements contained in this body.
*
* @return an immutable list of nodes
*/
public List<ResponseNode> nodes() {
return nodes;
}
}
@@ -0,0 +1,51 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
* Builder for {@link ResponseBody} instances.
*
* <p>Provides methods to append parameter nodes and nested blocks and to produce an immutable
* {@link ResponseBody} via {@link #build()}.
*/
public class ResponseBodyBuilder {
private final List<ResponseNode> nodes = new ArrayList<>();
/**
* Add a key/value parameter to the response body under construction.
*
* @param key the parameter name
* @param value the parameter value (will be converted to string when encoded)
* @return this builder for fluent chaining
*/
public ResponseBodyBuilder param(String key, Object value) {
nodes.add(new ResponseParameter(key, value));
return this;
}
/**
* Add a nested block with the given tag. The provided consumer receives a child builder to
* populate the block content.
*
* @param tag the block tag
* @param content consumer that appends child nodes to the block
* @return this builder for fluent chaining
*/
public ResponseBodyBuilder block(String tag, Consumer<ResponseBodyBuilder> content) {
ResponseBodyBuilder childBuilder = new ResponseBodyBuilder();
content.accept(childBuilder);
nodes.add(new ResponseBlock(tag, childBuilder.build().nodes()));
return this;
}
/**
* Build an immutable {@link ResponseBody} from the accumulated nodes.
*
* @return a new {@link ResponseBody}
*/
public ResponseBody build() {
return new ResponseBody(nodes);
}
}
@@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder;
/**
* Marker interface for elements that may appear in a {@link ResponseBody}.
*
* <p>Implementations include {@link ResponseParameter} and {@link ResponseBlock}.
*/
public interface ResponseNode {}
@@ -0,0 +1,23 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder;
/**
* A parameter node stored in a {@link ResponseBody}.
*
* <p>Represents a simple key/value pair. Callers can use {@link #rawValue()} to obtain the string
* representation of the stored value.
*
* @param key the parameter name
* @param value the parameter value
*/
public record ResponseParameter(String key, Object value) implements ResponseNode {
/**
* Returns the raw string representation of the stored value. This is a convenience wrapper
* around {@code Object#toString()} and may throw {@link NullPointerException} if the stored
* value is {@code null}.
*
* @return the value as string
*/
public String rawValue() {
return value.toString();
}
}
@@ -0,0 +1,38 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.PrimitiveResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.Session;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionManager;
/**
* Helper that dispatches {@link Response} instances to the corresponding {@link Session} by
* encoding them and enqueuing the resulting {@link PrimitiveResponse} into the session's response
* queue.
*/
public class ResponseDispatcher {
private final SessionManager sessionManager;
/**
* Create a dispatcher bound to a {@link SessionManager}.
*
* @param sessionManager manager used to resolve sessions
*/
public ResponseDispatcher(SessionManager sessionManager) {
this.sessionManager = sessionManager;
}
/**
* Encode the given {@link Response} and enqueue the resulting {@link PrimitiveResponse} into
* the target session's response queue.
*
* @param response the response to dispatch
* @throws InterruptedException if the thread is interrupted while waiting to enqueue the
* primitive response
*/
public void dispatch(Response response) throws InterruptedException {
PrimitiveResponse primitiveResponse = ResponseEncoder.encode(response);
Session session = sessionManager.getSessionById(response.getSessionId());
session.getResponseQueue().put(primitiveResponse);
}
}
@@ -0,0 +1,104 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.PrimitiveResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBlock;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseNode;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseParameter;
/**
* Utility responsible for encoding a {@link Response} into a protocol payload string and wrapping
* it into a {@link PrimitiveResponse} suitable for transmission.
*/
public class ResponseEncoder {
private static final String INDENT = "\t";
private static final String NEWLINE = "\n";
/**
* Encode a {@link Response} into a {@link PrimitiveResponse} containing the serialized payload
* string.
*
* @param response the response to encode
* @return a {@link PrimitiveResponse} with encoded payload
*/
public static PrimitiveResponse encode(Response response) {
StringBuilder sb = new StringBuilder();
sb.append(response.prefix());
for (ResponseNode node : response.getBody().nodes()) {
sb.append(NEWLINE);
encodeNode(node, sb, 1);
}
sb.append(NEWLINE).append("END");
return new PrimitiveResponse(
response.getSessionId(), response.getRequestId(), sb.toString());
}
/**
* Internal helper to encode any {@link ResponseNode}.
*
* @param node node to encode
* @param sb string builder to append to
* @param depth current indentation depth
*/
private static void encodeNode(ResponseNode node, StringBuilder sb, int depth) {
if (node instanceof ResponseParameter param) {
encodeParameter(param, sb, depth);
} else if (node instanceof ResponseBlock block) {
encodeBlock(block, sb, depth);
}
}
/**
* Encode a {@link ResponseParameter} into the string builder.
*
* @param param the parameter to encode
* @param sb the output builder
* @param depth indentation depth
*/
private static void encodeParameter(ResponseParameter param, StringBuilder sb, int depth) {
sb.append(INDENT.repeat(depth));
sb.append(param.key());
sb.append("=");
sb.append(maskIfNeeded(param.value().toString()));
}
/**
* Encode a {@link ResponseBlock}, including its children and terminating with an {@code END}
* marker.
*
* @param block the block to encode
* @param sb the output builder
* @param depth current indentation depth
*/
private static void encodeBlock(ResponseBlock block, StringBuilder sb, int depth) {
sb.append(INDENT.repeat(depth));
sb.append(block.tag());
for (ResponseNode child : block.children()) {
sb.append(NEWLINE);
encodeNode(child, sb, depth + 1);
}
sb.append(NEWLINE);
sb.append(INDENT.repeat(depth));
sb.append("END");
}
/**
* Quote or escape the provided value if it contains whitespace or single quotes so the encoded
* payload remains parseable.
*
* @param value the raw string value
* @return quoted/escaped value
*/
private static String maskIfNeeded(String value) {
if (value.contains(" ") || value.contains("'")) {
String escaped = value.replace("'", "\\'");
return "'" + escaped + "'";
}
return value;
}
}
@@ -1,24 +1,24 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandRouter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParserDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.PrimitiveRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.ProtocolParser;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.PrimitiveResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
import java.io.EOFException;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.time.Instant;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/** Represents a client session in the network server. */
public class Session implements Runnable {
private SessionId id;
private Thread thread;
private TransportLayer transport;
private Logger logger;
private Boolean running;
private EventBus eventBus;
public class Session {
private final SessionId id;
private Instant lastActivity;
private final TransportLayer transport;
private final BlockingQueue<PrimitiveResponse> responseQueue;
private final CommandParserDispatcher dispatcher;
private final CommandRouter router;
private static final int RESPOND_QUEUE_SIZE = 32;
/**
* Creates a new Session with the given transport and event bus.
@@ -27,15 +27,17 @@ public class Session implements Runnable {
* @param eventBus the event bus for publishing events
* @throws IOException if an I/O error occurs during initialization
*/
public Session(TransportLayer transport, EventBus eventBus) throws IOException {
public Session(
TransportLayer transport,
EventBus eventBus,
CommandParserDispatcher dispatcher,
CommandRouter router) {
this.id = new SessionId();
this.thread = new Thread(this, "session-" + this.id.value());
this.lastActivity = Instant.now();
this.transport = transport;
this.running = true;
this.eventBus = eventBus;
this.logger = LogManager.getLogger(Session.class.toString() + id.value());
this.logger.info("Created new session");
this.dispatcher = dispatcher;
this.router = router;
this.responseQueue = new ArrayBlockingQueue<>(RESPOND_QUEUE_SIZE);
}
/**
@@ -47,39 +49,48 @@ public class Session implements Runnable {
return this.id;
}
/** Starts the session thread. */
public void start() {
thread.start();
/**
* Gets the timestamp of the last inbound activity for this session.
*
* @return an {@link Instant} representing the time of the last inbound activity
*/
public Instant getLastInboundActivity() {
return lastActivity;
}
/** Updates the timestamp of the last inbound activity for this session. */
public void updateLastInboundActivity() {
this.lastActivity = Instant.now();
}
/**
* Closes the session and its transport.
* Returns the TransportLayer of this session
*
* @throws IOException if an I/O error occurs
* @return the transport layer of the session
*/
public void close() throws IOException {
transport.close();
this.running = false;
public TransportLayer getTransport() {
return transport;
}
/** Runs the session loop, reading from the transport. */
@Override
public void run() {
while (running) {
try {
RawPacket rawPacket = transport.read();
logger.debug("Recieved: {}", rawPacket);
/**
* Returns the BlockingQueue of this session
*
* @return the queue containing outgoing responses
*/
public BlockingQueue<PrimitiveResponse> getResponseQueue() {
return responseQueue;
}
PrimitiveRequest primitiveRequest = ProtocolParser.parse(rawPacket);
logger.debug("Parsed request to {}", primitiveRequest);
} catch (EOFException e) {
logger.info("Client disconnected");
eventBus.publish(new DisconnectEvent(id));
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
/**
* Returns the CommandParserDispatcher of this session
*
* @return the dispatcher to dispatch PrimitiveRequests to for parsing
*/
public CommandParserDispatcher getDispatcher() {
return dispatcher;
}
public CommandRouter getRouter() {
return router;
}
}
@@ -0,0 +1,40 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import java.time.Duration;
import java.time.Instant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SessionDisconnectJob implements Runnable {
private final Logger logger;
private final SessionManager sessionManager;
private final EventBus eventBus;
private final Duration timeoutThreshold;
public SessionDisconnectJob(
SessionManager sessionManager, EventBus eventBus, Duration timeoutThreshold) {
this.logger = LogManager.getLogger(SessionDisconnectJob.class);
this.sessionManager = sessionManager;
this.eventBus = eventBus;
this.timeoutThreshold = timeoutThreshold;
}
@Override
public void run() {
logger.debug("Job started.");
Instant threshold = Instant.now().minus(timeoutThreshold);
for (Session session : sessionManager.getAllSessions()) {
if (session.getLastInboundActivity().isBefore(threshold)) {
eventBus.publish(new DisconnectEvent(session.getId()));
logger.info(
"Initiated disconnect of {}, as it hasn't been active since a while",
session.getId());
}
}
logger.debug("Job finished.");
}
}
@@ -0,0 +1,4 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
/** The SessionHandle stores the session and the two worker threads associated with the session */
record SessionHandle(Session session, Thread reader, Thread writer) {}
@@ -1,44 +1,99 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandRouter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParserDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** Manages active sessions in the server. */
public class SessionManager {
private Map<SessionId, Session> sessions;
private Map<SessionId, SessionHandle> sessions;
private final EventBus eventBus;
private final Logger logger;
private final CommandParserDispatcher dispatcher;
private final CommandRouter router;
/** Constructs a new SessionManager. */
public SessionManager() {
public SessionManager(
EventBus eventBus, CommandParserDispatcher dispatcher, CommandRouter router) {
this.sessions = new ConcurrentHashMap<>();
this.eventBus = eventBus;
this.logger = LogManager.getLogger(SessionManager.class);
this.dispatcher = dispatcher;
this.router = router;
}
/**
* Adds a session to the manager.
* Create new Session from provided transport.
*
* @param session the session to add
* <p>Will create both worker threads and start them.
*
* @param transport to create session from
* @return newly created session
*/
public void addSession(Session session) {
sessions.put(session.getId(), session);
public Session create(TransportLayer transport) {
Session session = new Session(transport, eventBus, dispatcher, router);
SessionReader reader = new SessionReader(session, eventBus);
SessionWriter writer = new SessionWriter(session);
Thread readerThread =
Thread.ofVirtual()
.name("session-" + session.getId().value() + "-reader")
.unstarted(reader);
Thread writerThread =
Thread.ofVirtual()
.name("session-" + session.getId().value() + "-writer")
.unstarted(writer);
sessions.put(session.getId(), new SessionHandle(session, readerThread, writerThread));
readerThread.start();
writerThread.start();
return session;
}
/**
* Removes a session by its ID.
* Disconnect specified client
*
* @param id the ID of the session to remove
* @return the removed session, or null if not found
* <p>WARNING: Client will be uninformed about disconnect. Use with caution.
*
* @param id of the client to disconnect
*/
public Session removeSession(SessionId id) {
return sessions.remove(id);
public void disconnect(SessionId id) {
SessionHandle handle = sessions.remove(id);
if (handle == null) {
logger.warn(
"Requested to disconnect client with id {}. Failed as client is not found",
id.value());
return;
}
logger.debug("Disconnecting session {}", id.value());
handle.reader().interrupt();
handle.writer().interrupt();
try {
handle.session().getTransport().close();
} catch (IOException e) {
logger.error("Unexpected exception while closing transport", e);
}
}
/**
* Removes the specified session.
* Handler for the DisconnectEvent
*
* @param session the session to remove
* @return the removed session, or null if not found
* @param id of the session that disconnected
*/
public Session removeSession(Session session) {
return sessions.remove(session.getId());
public void onDisconnect(DisconnectEvent event) {
logger.debug("Recieved DisconnectEvent event for session {}", event.sessionId().value());
disconnect(event.sessionId());
}
/**
@@ -48,6 +103,16 @@ public class SessionManager {
* @return the session with the specified ID, or null if not found
*/
public Session getSessionById(SessionId id) {
return sessions.get(id);
SessionHandle handle = sessions.get(id);
if (handle == null) {
return null;
}
return handle.session();
}
public Collection<Session> getAllSessions() {
return sessions.values().stream().map(SessionHandle::session).collect(Collectors.toList());
}
}
@@ -0,0 +1,75 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandRouter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParserDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.ProtocolParser;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.ProtocolParserException;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.parser.tokenizer.TokenizerException;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RawRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
import java.io.EOFException;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SessionReader implements Runnable {
private final Session session;
private final TransportLayer transport;
private final EventBus eventBus;
private final CommandParserDispatcher dispatcher;
private final CommandRouter router;
private final Logger logger;
public SessionReader(Session session, EventBus eventBus) {
this.session = session;
this.transport = session.getTransport();
this.eventBus = eventBus;
this.dispatcher = session.getDispatcher();
this.router = session.getRouter();
this.logger =
LogManager.getLogger(
SessionReader.class.toString() + "-" + session.getId().value());
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
RawPacket rawPacket = null;
try {
rawPacket = transport.read();
session.updateLastInboundActivity();
logger.debug("Recieved: {}", rawPacket);
RawRequest rawRequest = ProtocolParser.parse(rawPacket.payload());
logger.debug("Parsed request to {}", rawRequest);
RequestContext requestContext =
new RequestContext(session.getId(), rawPacket.requestId());
PrimitiveRequest primitiveRequest =
new PrimitiveRequest(
requestContext, rawRequest.command(), rawRequest.parameters());
logger.debug("Converted to {}", primitiveRequest);
Request request = dispatcher.parse(primitiveRequest);
router.execute(request);
} catch (EOFException e) {
logger.info("Client disconnected");
eventBus.publish(new DisconnectEvent(session.getId()));
break;
} catch (TokenizerException | ProtocolParserException e) {
logger.error("Error occured while parsing request. RawPacket: {}", rawPacket, e);
// TODO: Send error response to client
} catch (IOException e) {
logger.error("Unexpected exception while reading from transport", e);
}
}
}
}
@@ -0,0 +1,42 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.PrimitiveResponse;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SessionWriter implements Runnable {
private final TransportLayer transport;
private final BlockingQueue<PrimitiveResponse> queue;
private final Logger logger;
public SessionWriter(Session session) {
this.transport = session.getTransport();
this.queue = session.getResponseQueue();
this.logger =
LogManager.getLogger(
SessionReader.class.toString() + "-" + session.getId().value());
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
RawPacket packet = null;
try {
PrimitiveResponse response = queue.take();
packet = new RawPacket(response.requestId(), response.payload());
transport.write(packet);
} catch (IOException e) {
logger.error(
"Unexpected exception while writing to transport. RawPacket: {}",
packet,
e);
} catch (InterruptedException e) {
logger.warn("Thread got interrupted", e);
break;
}
}
}
}