diff options
author | Trumeet <yuuta@yuuta.moe> | 2022-07-13 12:55:05 -0700 |
---|---|---|
committer | Trumeet <yuuta@yuuta.moe> | 2022-07-13 12:55:05 -0700 |
commit | cec69dfa51ce80fb33d9c516fd5747879c28ab13 (patch) | |
tree | d4a073da75133612f3939e466c7c089a1a1fa1a5 /src/main/java/moe/ymc | |
parent | 4a6706a4a03108008b8859b59b85af83f0182862 (diff) | |
download | acron-cec69dfa51ce80fb33d9c516fd5747879c28ab13.tar acron-cec69dfa51ce80fb33d9c516fd5747879c28ab13.tar.gz acron-cec69dfa51ce80fb33d9c516fd5747879c28ab13.tar.bz2 acron-cec69dfa51ce80fb33d9c516fd5747879c28ab13.zip |
Refactor WSFrameHandler to isolate the business logic
Signed-off-by: Trumeet <yuuta@yuuta.moe>
Diffstat (limited to 'src/main/java/moe/ymc')
-rw-r--r-- | src/main/java/moe/ymc/acron/net/WSFrameHandler.java | 65 |
1 files changed, 39 insertions, 26 deletions
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<WebSocketFrame> return; } try { - if (request instanceof final ReqCmd reqCmd) { - LOGGER.info("Client {} executed a command: `{}`.", - identification.client().id(), - reqCmd.cmd()); - final Pair<Action, Boolean> 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<WebSocketFrame> } } + @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<Action, Boolean> 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) { |