Search in sources :

Example 1 with ChannelHandshake

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);
}
Also used : EnumChannelProtocolVersion(org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion) BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) BcosResponseCallback(org.fisco.bcos.channel.client.BcosResponseCallback) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) ChannelPrococolExceiption(org.fisco.bcos.channel.protocol.ChannelPrococolExceiption) ChannelProtocol(org.fisco.bcos.channel.protocol.ChannelProtocol) ByteBuf(io.netty.buffer.ByteBuf) ChannelHandshake(org.fisco.bcos.channel.protocol.ChannelHandshake) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)1 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)1 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)1 ChannelHandshake (org.fisco.bcos.channel.protocol.ChannelHandshake)1 ChannelPrococolExceiption (org.fisco.bcos.channel.protocol.ChannelPrococolExceiption)1 ChannelProtocol (org.fisco.bcos.channel.protocol.ChannelProtocol)1 EnumChannelProtocolVersion (org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion)1 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)1