Create CommandRouter and CommandHandler to execute request #206

Merged
lars.winzer merged 12 commits from feat/14-commandrouter-and-commandhandler into main 2026-03-26 18:23:19 +01:00
5 changed files with 14 additions and 9 deletions
Showing only changes of commit a9ffb4daeb - Show all commits
@@ -4,7 +4,6 @@ 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.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.NetworkManager; import ch.unibas.dmi.dbis.cs108.casono.server.network.NetworkManager;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.CommandRouter; import ch.unibas.dmi.dbis.cs108.casono.server.network.command.CommandRouter;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.TestCommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent; 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.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.CommandParserDispatcher; import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.CommandParserDispatcher;
@@ -1,10 +1,9 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.command; package ch.unibas.dmi.dbis.cs108.casono.server.network.command;
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.Request;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.Request;
public class CommandRouter { public class CommandRouter {
private final Map<Class<? extends Request>, CommandHandler<?>> handlers = new HashMap<>(); private final Map<Class<? extends Request>, CommandHandler<?>> handlers = new HashMap<>();
@@ -12,14 +11,17 @@ public class CommandRouter {
handlers.put(request, handler); handlers.put(request, handler);
} }
// Safe, because during registration, it's ensured that the provided CommandHandler only receives requests it can handle. // Safe, because during registration, it's ensured that the provided CommandHandler only
// receives requests it can handle.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void execute(Request request) { public void execute(Request request) {
CommandHandler<Request> handler = (CommandHandler<Request>) handlers.get(request.getClass()); CommandHandler<Request> handler =
(CommandHandler<Request>) handlers.get(request.getClass());
if (handler == null) { if (handler == null) {
String requestName = request.getClass().toString(); String requestName = request.getClass().toString();
throw new UnknownRequestException("Unable to execute request " + requestName + ". Type unknown", requestName); throw new UnknownRequestException(
"Unable to execute request " + requestName + ". Type unknown", requestName);
} }
handler.execute(request); handler.execute(request);
} }
@@ -26,7 +26,10 @@ public class Session {
* @throws IOException if an I/O error occurs during initialization * @throws IOException if an I/O error occurs during initialization
*/ */
public Session( public Session(
TransportLayer transport, EventBus eventBus, CommandParserDispatcher dispatcher, CommandRouter router) { TransportLayer transport,
EventBus eventBus,
CommandParserDispatcher dispatcher,
CommandRouter router) {
this.id = new SessionId(); this.id = new SessionId();
this.transport = transport; this.transport = transport;
this.dispatcher = dispatcher; this.dispatcher = dispatcher;
@@ -20,7 +20,8 @@ public class SessionManager {
private final CommandRouter router; private final CommandRouter router;
/** Constructs a new SessionManager. */ /** Constructs a new SessionManager. */
public SessionManager(EventBus eventBus, CommandParserDispatcher dispatcher, CommandRouter router) { public SessionManager(
EventBus eventBus, CommandParserDispatcher dispatcher, CommandRouter router) {
this.sessions = new ConcurrentHashMap<>(); this.sessions = new ConcurrentHashMap<>();
this.eventBus = eventBus; this.eventBus = eventBus;
this.logger = LogManager.getLogger(SessionManager.class); this.logger = LogManager.getLogger(SessionManager.class);