Search in sources :

Example 6 with BcosMessage

use of org.fisco.bcos.channel.dto.BcosMessage in project web3sdk by FISCO-BCOS.

the class ConnectionCallback method queryBlockNumber.

private void queryBlockNumber(ChannelHandlerContext ctx) throws JsonProcessingException {
    final String host = ChannelHandlerContextHelper.getPeerHost(ctx);
    String seq = channelService.newSeq();
    BcosMessage bcosMessage = new BcosMessage();
    bcosMessage.setType((short) ChannelMessageType.CHANNEL_RPC_REQUEST.getType());
    bcosMessage.setSeq(seq);
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(channelService);
    Request<Integer, BlockNumber> request = new Request<>("getBlockNumber", Arrays.asList(channelService.getGroupId()), channelEthereumService, BlockNumber.class);
    bcosMessage.setData(ObjectMapperFactory.getObjectMapper().writeValueAsBytes(request));
    ByteBuf byteBuf = ctx.alloc().buffer();
    bcosMessage.writeHeader(byteBuf);
    bcosMessage.writeExtra(byteBuf);
    String content = new String(bcosMessage.getData());
    logger.info(" query block number host: {}, seq: {}, content: {}", host, seq, content);
    channelService.getSeq2Callback().put(seq, new BcosResponseCallback() {

        @Override
        public void onResponse(BcosResponse response) {
            try {
                BlockNumber blockNumber = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), BlockNumber.class);
                SocketChannel socketChannel = (SocketChannel) ctx.channel();
                InetSocketAddress socketAddress = socketChannel.remoteAddress();
                channelService.getNodeToBlockNumberMap().put(socketAddress.getAddress().getHostAddress() + socketAddress.getPort(), blockNumber.getBlockNumber());
                logger.info(" query blocknumer, host:{}, blockNumber: {} ", host, blockNumber.getBlockNumber());
            } catch (Exception e) {
                logger.error(" query blocknumer failed, host: {}, message: {} ", host, e.getMessage());
                throw new MessageDecodingException(response.getContent());
            }
        }
    });
    ctx.writeAndFlush(byteBuf);
}
Also used : BcosResponseCallback(org.fisco.bcos.channel.client.BcosResponseCallback) SocketChannel(io.netty.channel.socket.SocketChannel) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) InetSocketAddress(java.net.InetSocketAddress) Request(org.fisco.bcos.web3j.protocol.core.Request) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ByteBuf(io.netty.buffer.ByteBuf) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)6 ByteBuf (io.netty.buffer.ByteBuf)5 IOException (java.io.IOException)5 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)4 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)4 SocketChannel (io.netty.channel.socket.SocketChannel)3 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)3 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 BigInteger (java.math.BigInteger)2 InetSocketAddress (java.net.InetSocketAddress)2 TopicVerifyMessage (org.fisco.bcos.channel.dto.TopicVerifyMessage)2 ChannelPrococolExceiption (org.fisco.bcos.channel.protocol.ChannelPrococolExceiption)2 EnumChannelProtocolVersion (org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion)2 Request (org.fisco.bcos.web3j.protocol.core.Request)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1