use of org.fisco.bcos.web3j.protocol.core.Request in project web3sdk by FISCO-BCOS.
the class ConnectionCallback method queryNodeVersion.
private void queryNodeVersion(ChannelHandlerContext ctx) throws JsonProcessingException {
final String host = ChannelHandlerContextHelper.getPeerHost(ctx);
String seq = UUID.randomUUID().toString().replaceAll("-", "");
Request<?, NodeVersion> request = new Request<>("getClientVersion", Arrays.asList(), null, NodeVersion.class);
byte[] payload = ObjectMapperFactory.getObjectMapper().writeValueAsBytes(request);
String content = new String(payload);
logger.info(" query node version host: {}, seq: {}, content: {}", host, seq, content);
BcosMessage bcosMessage = new BcosMessage();
bcosMessage.setType((short) ChannelMessageType.CHANNEL_RPC_REQUEST.getType());
bcosMessage.setSeq(seq);
bcosMessage.setResult(0);
bcosMessage.setData(payload);
ByteBuf byteBuf = ctx.alloc().buffer();
bcosMessage.writeHeader(byteBuf);
bcosMessage.writeExtra(byteBuf);
BcosResponseCallback callback = new BcosResponseCallback() {
@Override
public void onResponse(BcosResponse response) {
try {
if (response.getErrorCode() == ChannelMessageError.MESSAGE_TIMEOUT.getError()) {
// The fisco node version number is below 2.1.0 when request timeout
ChannelHandlerContextHelper.setProtocolVersion(ctx, EnumChannelProtocolVersion.VERSION_1, "below-2.1.0-timeout");
logger.info(" query node version timeout, content: {}", response.getContent());
sendUpdateTopicMessage(ctx);
queryBlockNumber(ctx);
return;
} else if (response.getErrorCode() != 0) {
logger.error(" fisco node version response, code: {}, message: {}", response.getErrorCode(), response.getErrorMessage());
throw new ChannelPrococolExceiption(" query node version failed, code: " + response.getErrorCode() + ", message: " + response.getErrorMessage());
}
Response<NodeVersion.Version> nodeVersion = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), NodeVersion.class);
logger.info(" node: {}, content: {}", nodeVersion.getResult(), response.getContent());
if (EnumNodeVersion.channelProtocolHandleShakeSupport(nodeVersion.getResult().getSupportedVersion())) {
// fisco node support channel protocol handshake, start it
logger.info(" support channel handshake node: {}, content: {}", nodeVersion.getResult(), response.getContent());
queryChannelProtocolVersion(ctx);
} else {
// default channel protocol
ChannelHandlerContextHelper.setProtocolVersion(ctx, EnumChannelProtocolVersion.VERSION_1, nodeVersion.getResult().getSupportedVersion());
logger.info(" not support channel handshake set default ,node: {}, content: {}", nodeVersion.getResult(), response.getContent());
sendUpdateTopicMessage(ctx);
queryBlockNumber(ctx);
// channelService.getEventLogFilterManager().sendFilter();
}
} catch (Exception e) {
logger.error(" query node version failed, message: {}", e.getMessage());
ctx.writeAndFlush("").addListener(ChannelFutureListener.CLOSE);
}
}
};
final BcosResponseCallback callbackInner = callback;
callback.setTimeout(channelService.getTimeoutHandler().newTimeout(new TimerTask() {
BcosResponseCallback _callback = callbackInner;
@Override
public void run(Timeout timeout) throws Exception {
// handle timer
_callback.onTimeout();
logger.error("queryNodeVersion timeout, seq: {}", bcosMessage.getSeq());
}
}, queryNodeVersionTimeoutMS, TimeUnit.MILLISECONDS));
channelService.getSeq2Callback().put(seq, callback);
ctx.writeAndFlush(byteBuf);
}
use of org.fisco.bcos.web3j.protocol.core.Request 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