Coverage Summary for Class: RskJsonRpcHandler (co.rsk.rpc.netty)
Class |
Class, %
|
Method, %
|
Line, %
|
RskJsonRpcHandler |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/17)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package co.rsk.rpc.netty;
19
20 import co.rsk.jsonrpc.JsonRpcBooleanResult;
21 import co.rsk.jsonrpc.JsonRpcIdentifiableMessage;
22 import co.rsk.jsonrpc.JsonRpcResultOrError;
23 import co.rsk.rpc.EthSubscriptionNotificationEmitter;
24 import co.rsk.rpc.JsonRpcSerializer;
25 import co.rsk.rpc.modules.RskJsonRpcRequest;
26 import co.rsk.rpc.modules.RskJsonRpcRequestVisitor;
27 import co.rsk.rpc.modules.eth.subscribe.EthSubscribeRequest;
28 import co.rsk.rpc.modules.eth.subscribe.EthUnsubscribeRequest;
29 import io.netty.buffer.ByteBufHolder;
30 import io.netty.buffer.ByteBufInputStream;
31 import io.netty.channel.ChannelHandler.Sharable;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.channel.SimpleChannelInboundHandler;
34 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.io.IOException;
39
40 /**
41 * This handler decodes inbound messages and dispatches valid JSON-RPC requests.
42 *
43 * Note that we split JSON-RPC handling in two because jsonrpc4j wasn't able to handle the PUB-SUB model.
44 * Eventually, we might want to implement all methods in this style and remove jsonrpc4j.
45 *
46 * We make this object Sharable so it can be instanced once in the netty pipeline
47 * and since all objects used by this object are thread safe,
48 */
49
50 @Sharable
51 public class RskJsonRpcHandler
52 extends SimpleChannelInboundHandler<ByteBufHolder>
53 implements RskJsonRpcRequestVisitor {
54 private static final Logger LOGGER = LoggerFactory.getLogger(RskJsonRpcHandler.class);
55
56 private final EthSubscriptionNotificationEmitter emitter;
57 private final JsonRpcSerializer serializer;
58
59 public RskJsonRpcHandler(EthSubscriptionNotificationEmitter emitter, JsonRpcSerializer serializer) {
60 this.emitter = emitter;
61 this.serializer = serializer;
62 }
63
64 @Override
65 protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
66 try {
67 RskJsonRpcRequest request = serializer.deserializeRequest(
68 new ByteBufInputStream(msg.copy().content())
69 );
70
71 // TODO(mc) we should support the ModuleDescription method filters
72 JsonRpcResultOrError resultOrError = request.accept(this, ctx);
73 JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
74 ctx.writeAndFlush(new TextWebSocketFrame(serializer.serializeMessage(response)));
75 return;
76 } catch (IOException e) {
77 LOGGER.trace("Not a known or valid JsonRpcRequest", e);
78 }
79
80 // delegate to the next handler if the message can't be matched to a known JSON-RPC request
81 ctx.fireChannelRead(msg.retain());
82 }
83
84 @Override
85 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
86 emitter.unsubscribe(ctx.channel());
87 super.channelInactive(ctx);
88 }
89
90 @Override
91 public JsonRpcResultOrError visit(EthUnsubscribeRequest request, ChannelHandlerContext ctx) {
92 boolean unsubscribed = emitter.unsubscribe(request.getParams().getSubscriptionId());
93 return new JsonRpcBooleanResult(unsubscribed);
94 }
95
96 @Override
97 public JsonRpcResultOrError visit(EthSubscribeRequest request, ChannelHandlerContext ctx) {
98 return request.getParams().accept(emitter, ctx.channel());
99 }
100 }