use of org.fisco.bcos.channel.protocol.ChannelProtocol in project web3sdk by FISCO-BCOS.
the class ChannelHandlerContextHelper method setProtocolVersion.
public static void setProtocolVersion(ChannelHandlerContext ctx, EnumChannelProtocolVersion version, String nodeVersion) {
ChannelProtocol channelProtocol = new ChannelProtocol();
channelProtocol.setProtocol(version.getVersionNumber());
channelProtocol.setNodeVersion(nodeVersion);
channelProtocol.setEnumProtocol(version);
ctx.channel().attr(AttributeKey.valueOf(EnumSocketChannelAttributeKey.CHANNEL_PROTOCOL_KEY.getKey())).set(channelProtocol);
}
use of org.fisco.bcos.channel.protocol.ChannelProtocol 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);
}
use of org.fisco.bcos.channel.protocol.ChannelProtocol in project web3sdk by FISCO-BCOS.
the class ChannelHandlerContextHelper method getProtocolVersion.
public static EnumChannelProtocolVersion getProtocolVersion(ChannelHandlerContext ctx) {
SocketChannel socketChannel = (SocketChannel) ctx.channel();
String hostAddress = socketChannel.remoteAddress().getAddress().getHostAddress();
int port = socketChannel.remoteAddress().getPort();
String host = hostAddress + ":" + port;
AttributeKey<ChannelProtocol> attributeKey = AttributeKey.valueOf(EnumSocketChannelAttributeKey.CHANNEL_PROTOCOL_KEY.getKey());
if (ctx.channel().hasAttr(attributeKey)) {
ChannelProtocol channelProtocol = ctx.channel().attr(attributeKey).get();
if (null != channelProtocol) {
return channelProtocol.getEnumProtocol();
} else {
logger.debug(" channel has attr but get null, host: {}", host);
}
}
return null;
}
Aggregations