use of org.fisco.bcos.channel.protocol.ChannelHandshake in project web3sdk by FISCO-BCOS.
the class ConnectionCallback method queryChannelProtocolVersion.
private void queryChannelProtocolVersion(ChannelHandlerContext ctx) throws ChannelPrococolExceiption, IOException {
final String host = ChannelHandlerContextHelper.getPeerHost(ctx);
ChannelHandshake channelHandshake = new ChannelHandshake();
String seq = UUID.randomUUID().toString().replaceAll("-", "");
byte[] payload = ObjectMapperFactory.getObjectMapper().writeValueAsBytes(channelHandshake);
String content = new String(payload);
logger.debug(" channel protocol handshake, host: {}, seq: {}, content: {}", host, seq, content);
BcosMessage bcosMessage = new BcosMessage();
bcosMessage.setType((short) ChannelMessageType.CLIENT_HANDSHAKE.getType());
bcosMessage.setSeq(seq);
bcosMessage.setResult(0);
bcosMessage.setData(payload);
ByteBuf byteBuf = ctx.alloc().buffer();
bcosMessage.writeHeader(byteBuf);
bcosMessage.writeExtra(byteBuf);
channelService.getSeq2Callback().put(seq, new BcosResponseCallback() {
@Override
public void onResponse(BcosResponse response) {
try {
if (response.getErrorCode() != 0) {
logger.error(" channel protocol handshake request failed, code: {}, message: {}", response.getErrorCode(), response.getErrorMessage());
throw new ChannelPrococolExceiption(" channel protocol handshake request failed, code: " + response.getErrorCode() + ", message: " + response.getErrorMessage());
}
ChannelProtocol channelProtocol = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), ChannelProtocol.class);
EnumChannelProtocolVersion enumChannelProtocolVersion = EnumChannelProtocolVersion.toEnum(channelProtocol.getProtocol());
channelProtocol.setEnumProtocol(enumChannelProtocolVersion);
logger.info(" channel protocol handshake success, set socket channel protocol, host: {}, channel protocol: {}", host, channelProtocol);
ctx.channel().attr(AttributeKey.valueOf(EnumSocketChannelAttributeKey.CHANNEL_PROTOCOL_KEY.getKey())).set(channelProtocol);
//
sendUpdateTopicMessage(ctx);
queryBlockNumber(ctx);
// channelService.getEventLogFilterManager().sendFilter();
} catch (Exception e) {
logger.error(" channel protocol handshake failed, exception: {}", e.getMessage());
ctx.writeAndFlush("").addListener(ChannelFutureListener.CLOSE);
}
}
});
ctx.writeAndFlush(byteBuf);
}
Aggregations