Search in sources :

Example 1 with ChannelProtocol

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);
}
Also used : ChannelProtocol(org.fisco.bcos.channel.protocol.ChannelProtocol)

Example 2 with 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);
}
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)

Example 3 with ChannelProtocol

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;
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) ChannelProtocol(org.fisco.bcos.channel.protocol.ChannelProtocol)

Aggregations

ChannelProtocol (org.fisco.bcos.channel.protocol.ChannelProtocol)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 SocketChannel (io.netty.channel.socket.SocketChannel)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 EnumChannelProtocolVersion (org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion)1 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)1