Search in sources :

Example 1 with JsonRpcIdentifiableMessage

use of co.rsk.jsonrpc.JsonRpcIdentifiableMessage in project rskj by rsksmart.

the class RskWebSocketJsonRpcHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
    ByteBuf content = msg.copy().content();
    try (ByteBufInputStream source = new ByteBufInputStream(content)) {
        final JsonNode jsonNodeRequest = mapper.readTree(source);
        RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);
        RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);
        JsonRpcResultOrError resultOrError = null;
        if (validationResult.isValid()) {
            // TODO(mc) we should support the ModuleDescription method filters
            resultOrError = request.accept(this, ctx);
        } else {
            resultOrError = new JsonRpcError(JsonRpcError.INVALID_PARAMS, validationResult.getMessage());
        }
        JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
        ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));
        return;
    } catch (IOException e) {
        LOGGER.trace("Not a known or valid JsonRpcRequest", e);
        // We need to release this resource, netty only takes care about 'ByteBufHolder msg'
        content.release(content.refCnt());
    }
    // delegate to the next handler if the message can't be matched to a known JSON-RPC request
    ctx.fireChannelRead(msg);
}
Also used : JsonRpcIdentifiableMessage(co.rsk.jsonrpc.JsonRpcIdentifiableMessage) RskJsonRpcRequest(co.rsk.rpc.modules.RskJsonRpcRequest) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonRpcError(co.rsk.jsonrpc.JsonRpcError) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) JsonRpcResultOrError(co.rsk.jsonrpc.JsonRpcResultOrError)

Aggregations

JsonRpcError (co.rsk.jsonrpc.JsonRpcError)1 JsonRpcIdentifiableMessage (co.rsk.jsonrpc.JsonRpcIdentifiableMessage)1 JsonRpcResultOrError (co.rsk.jsonrpc.JsonRpcResultOrError)1 RskJsonRpcRequest (co.rsk.rpc.modules.RskJsonRpcRequest)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 IOException (java.io.IOException)1