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);
}
Aggregations