Create CommandRouter and CommandHandler to execute request #206
@@ -1,5 +1,6 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
|
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.events.EventBus;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.CommandParserDispatcher;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.CommandParserDispatcher;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.response.PrimitiveResponse;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.response.PrimitiveResponse;
|
||||||
@@ -14,6 +15,7 @@ public class Session {
|
|||||||
private final TransportLayer transport;
|
private final TransportLayer transport;
|
||||||
private final BlockingQueue<PrimitiveResponse> responseQueue;
|
private final BlockingQueue<PrimitiveResponse> responseQueue;
|
||||||
private final CommandParserDispatcher dispatcher;
|
private final CommandParserDispatcher dispatcher;
|
||||||
|
private final CommandRouter router;
|
||||||
private static final int RESPOND_QUEUE_SIZE = 32;
|
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
|
* @throws IOException if an I/O error occurs during initialization
|
||||||
*/
|
*/
|
||||||
public Session(
|
public Session(
|
||||||
TransportLayer transport, EventBus eventBus, CommandParserDispatcher dispatcher) {
|
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;
|
||||||
|
this.router = router;
|
||||||
this.responseQueue = new ArrayBlockingQueue<>(RESPOND_QUEUE_SIZE);
|
this.responseQueue = new ArrayBlockingQueue<>(RESPOND_QUEUE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,4 +69,8 @@ public class Session {
|
|||||||
public CommandParserDispatcher getDispatcher() {
|
public CommandParserDispatcher getDispatcher() {
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CommandRouter getRouter() {
|
||||||
|
return router;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -1,5 +1,6 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
|
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.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;
|
||||||
@@ -22,6 +23,7 @@ public class SessionReader implements Runnable {
|
|||||||
private final TransportLayer transport;
|
private final TransportLayer transport;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final CommandParserDispatcher dispatcher;
|
private final CommandParserDispatcher dispatcher;
|
||||||
|
private final CommandRouter router;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
public SessionReader(Session session, EventBus eventBus) {
|
public SessionReader(Session session, EventBus eventBus) {
|
||||||
@@ -29,6 +31,7 @@ public class SessionReader implements Runnable {
|
|||||||
this.transport = session.getTransport();
|
this.transport = session.getTransport();
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.dispatcher = session.getDispatcher();
|
this.dispatcher = session.getDispatcher();
|
||||||
|
this.router = session.getRouter();
|
||||||
this.logger =
|
this.logger =
|
||||||
LogManager.getLogger(
|
LogManager.getLogger(
|
||||||
SessionReader.class.toString() + "-" + session.getId().value());
|
SessionReader.class.toString() + "-" + session.getId().value());
|
||||||
@@ -50,8 +53,11 @@ public class SessionReader implements Runnable {
|
|||||||
PrimitiveRequest primitiveRequest =
|
PrimitiveRequest primitiveRequest =
|
||||||
new PrimitiveRequest(
|
new PrimitiveRequest(
|
||||||
requestContext, rawRequest.command(), rawRequest.parameters());
|
requestContext, rawRequest.command(), rawRequest.parameters());
|
||||||
|
logger.debug("Converted to {}", primitiveRequest);
|
||||||
|
|
||||||
Request request = dispatcher.parse(primitiveRequest);
|
Request request = dispatcher.parse(primitiveRequest);
|
||||||
|
|
||||||
|
router.execute(request);
|
||||||
} catch (EOFException e) {
|
} catch (EOFException e) {
|
||||||
logger.info("Client disconnected");
|
logger.info("Client disconnected");
|
||||||
eventBus.publish(new DisconnectEvent(session.getId()));
|
eventBus.publish(new DisconnectEvent(session.getId()));
|
||||||
|
|||||||
Reference in New Issue
Block a user