diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatchException.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatchException.java new file mode 100644 index 0000000..6cf4010 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatchException.java @@ -0,0 +1,7 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher; + +public class ResponseDispatchException extends RuntimeException { + public ResponseDispatchException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatcher.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatcher.java index 1be2f27..1895d44 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatcher.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/protocol/response/dispatcher/ResponseDispatcher.java @@ -27,12 +27,17 @@ public class ResponseDispatcher { * 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 + * @throws ResponseDispatchException wraps any exceptions that occur during dispatching, such as + * the {@link InterruptedException} */ - public void dispatch(Response response) throws InterruptedException { + public void dispatch(Response response) { PrimitiveResponse primitiveResponse = ResponseEncoder.encode(response); Session session = sessionManager.getSessionById(response.getSessionId()); - session.getResponseQueue().put(primitiveResponse); + try { + session.getResponseQueue().put(primitiveResponse); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ResponseDispatchException("Interrupted while dispatching response", e); + } } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/sessions/SessionReader.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/sessions/SessionReader.java index 9fba8ae..6fb4378 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/sessions/SessionReader.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/network/sessions/SessionReader.java @@ -14,6 +14,7 @@ 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.protocol.response.ErrorResponse; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.PrimitiveResponse; +import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatchException; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseEncoder; import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket; import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer; @@ -88,6 +89,11 @@ public class SessionReader implements Runnable { "UNKNOWN_COMMAND", "This command is unknown to the server.")); + } catch (ResponseDispatchException e) { + logger.error( + "Unexpected ResponseDispatchException exception while dispatching request", + e); + } catch (IOException e) { logger.error("Unexpected IO exception while reading from transport", e);