Search in sources :

Example 11 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse 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

BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)11 IOException (java.io.IOException)7 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ByteBuf (io.netty.buffer.ByteBuf)4 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)4 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)4 BcosRequest (org.fisco.bcos.channel.dto.BcosRequest)3 SocketChannel (io.netty.channel.socket.SocketChannel)2 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 InetSocketAddress (java.net.InetSocketAddress)2 Semaphore (java.util.concurrent.Semaphore)2 EventLogPushCallback (org.fisco.bcos.channel.event.filter.EventLogPushCallback)2 ConnectionCallback (org.fisco.bcos.channel.handler.ConnectionCallback)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 ContractCallException (org.fisco.bcos.web3j.tx.exceptions.ContractCallException)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1