From cec69dfa51ce80fb33d9c516fd5747879c28ab13 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Wed, 13 Jul 2022 12:55:05 -0700 Subject: Refactor WSFrameHandler to isolate the business logic Signed-off-by: Trumeet --- .../java/moe/ymc/acron/net/WSFrameHandler.java | 65 +++++++++++++--------- 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'src/main/java/moe') diff --git a/src/main/java/moe/ymc/acron/net/WSFrameHandler.java b/src/main/java/moe/ymc/acron/net/WSFrameHandler.java index f03fc36..912e73a 100644 --- a/src/main/java/moe/ymc/acron/net/WSFrameHandler.java +++ b/src/main/java/moe/ymc/acron/net/WSFrameHandler.java @@ -1,6 +1,7 @@ package moe.ymc.acron.net; import com.google.gson.JsonParseException; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.*; @@ -12,6 +13,7 @@ import moe.ymc.acron.c2s.ReqSetConfig; import moe.ymc.acron.c2s.Request; import moe.ymc.acron.cmd.CmdQueue; import moe.ymc.acron.jvav.Pair; +import moe.ymc.acron.s2c.Event; import moe.ymc.acron.s2c.EventQueue; import moe.ymc.acron.s2c.response.EventError; import moe.ymc.acron.s2c.response.EventOk; @@ -69,32 +71,10 @@ public class WSFrameHandler extends SimpleChannelInboundHandler return; } try { - if (request instanceof final ReqCmd reqCmd) { - LOGGER.info("Client {} executed a command: `{}`.", - identification.client().id(), - reqCmd.cmd()); - final Pair res = PolicyChecker.check(identification.client(), - reqCmd.cmd()); - if (res.l() == Action.DENY) { - ctx.channel().writeAndFlush(Serializer.serialize(new EventError(reqCmd.id(), - EventError.Code.FORBIDDEN.value, "This client is not allowed to " + - "execute this command."))); - return; - } - // Write it before enqueueing to prevent potential - // thread safety issues. - ctx.channel().writeAndFlush(Serializer.serialize(new EventOk(id))); - CmdQueue.enqueue(id, - res.r(), - ctx.channel(), - reqCmd.config() == null ? - configuration : - convertConfiguration(reqCmd.config()), - reqCmd.cmd()); - } else if (request instanceof final ReqSetConfig reqSetConfig) { - ctx.channel().attr(Attributes.CONFIGURATION).set(convertConfiguration(reqSetConfig)); - ctx.channel().writeAndFlush(Serializer.serialize(new EventOk(id))); - } + ctx.channel().writeAndFlush(Serializer.serialize(handle(request, + identification, + configuration, + ctx.channel()))); } catch (Throwable e) { LOGGER.info("An error occurred while processing the request. " + "This may just be a malformed request. " + @@ -106,6 +86,39 @@ public class WSFrameHandler extends SimpleChannelInboundHandler } } + @NotNull + private Event handle(@NotNull Request request, + @NotNull ClientIdentification identification, + @NotNull ClientConfiguration configuration, + @NotNull Channel channel) throws Throwable { + if (request instanceof final ReqCmd reqCmd) { + LOGGER.info("Client {} executed a command: `{}`.", + identification.client().id(), + reqCmd.cmd()); + final Pair res = PolicyChecker.check(identification.client(), + reqCmd.cmd()); + if (res.l() == Action.DENY) { + return new EventError(reqCmd.id(), + EventError.Code.FORBIDDEN.value, "This client is not allowed to " + + "execute this command."); + } + // TODO: Ok event may be sent after executing the command. + CmdQueue.enqueue(reqCmd.id(), + res.r(), + channel, + reqCmd.config() == null ? + configuration : + convertConfiguration(reqCmd.config()), + reqCmd.cmd()); + return new EventOk(request.getId()); + } else if (request instanceof final ReqSetConfig reqSetConfig) { + channel.attr(Attributes.CONFIGURATION).set(convertConfiguration(reqSetConfig)); + return new EventOk(request.getId()); + } + // This should not occur. + throw new IllegalStateException("This should not occur."); + } + private ClientConfiguration convertConfiguration(@NotNull ReqSetConfig request) { final ServerWorld world; if (request.world() != null) { -- cgit v1.2.3