Add: Pass CommandRouter to Session and SessionReader

This commit is contained in:
Lars Simon Winzer
2026-03-26 17:58:25 +01:00
parent d25f533ee9
commit 628e86a964
2 changed files with 14 additions and 1 deletions
@@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.CommandRouter;
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.response.PrimitiveResponse;
@@ -14,6 +15,7 @@ public class Session {
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;
/**
@@ -24,10 +26,11 @@ public class Session {
* @throws IOException if an I/O error occurs during initialization
*/
public Session(
TransportLayer transport, EventBus eventBus, CommandParserDispatcher dispatcher) {
TransportLayer transport, EventBus eventBus, CommandParserDispatcher dispatcher, CommandRouter router) {
this.id = new SessionId();
this.transport = transport;
this.dispatcher = dispatcher;
this.router = router;
this.responseQueue = new ArrayBlockingQueue<>(RESPOND_QUEUE_SIZE);
}
@@ -66,4 +69,8 @@ public class Session {
public CommandParserDispatcher getDispatcher() {
return dispatcher;
}
public CommandRouter getRouter() {
return router;
}
}
@@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.CommandRouter;
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.parser.CommandParserDispatcher;
@@ -22,6 +23,7 @@ public class SessionReader implements Runnable {
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) {
@@ -29,6 +31,7 @@ public class SessionReader implements Runnable {
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());
@@ -50,8 +53,11 @@ public class SessionReader implements Runnable {
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()));