Search in sources :

Example 1 with BcosHeartbeat

use of org.fisco.bcos.channel.dto.BcosHeartbeat in project web3sdk by FISCO-BCOS.

the class Service method onReceiveHeartbeat.

public void onReceiveHeartbeat(ChannelHandlerContext ctx, Message msg) {
    String content = "";
    HeartBeatParser heartBeatParser = new HeartBeatParser(ChannelHandlerContextHelper.getProtocolVersion(ctx));
    String data = new String(msg.getData());
    try {
        BcosHeartbeat bcosHeartbeat = heartBeatParser.decode(data);
        // logger.trace(" heartbeat packet, heartbeat is {} ", bcosHeartbeat);
        int heartBeat = bcosHeartbeat.getHeartBeat();
        content = String.valueOf(heartBeat);
    } catch (UnsupportedEncodingException e) {
        logger.error("heartbeat packet cannot be parsed, data: {}", data);
    } catch (Exception e) {
        logger.error("heartbeat packet exception, data: {}", data);
    }
    if ("0".equals(content)) {
        logger.trace("heartbeat packet,send heartbeat packet back");
        Message response = new Message();
        response.setSeq(msg.getSeq());
        response.setResult(0);
        response.setType((short) ChannelMessageType.CLIENT_HEARTBEAT.getType());
        try {
            response.setData(heartBeatParser.encode("1"));
        } catch (JsonProcessingException e) {
            logger.error(" write json failed, message is {} ", e.getMessage());
            return;
        }
        ByteBuf out = ctx.alloc().buffer();
        response.writeHeader(out);
        response.writeExtra(out);
        ctx.writeAndFlush(out);
    } else if ("1".equals(content)) {
        logger.trace("heartbeat response");
    } else {
        logger.trace(" unknown heartbeat message , do nothing, data: {}", data);
    }
}
Also used : BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) Message(org.fisco.bcos.channel.handler.Message) TopicVerifyMessage(org.fisco.bcos.channel.dto.TopicVerifyMessage) HeartBeatParser(org.fisco.bcos.channel.protocol.parser.HeartBeatParser) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BcosHeartbeat(org.fisco.bcos.channel.dto.BcosHeartbeat) ByteBuf(io.netty.buffer.ByteBuf) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 CertificateException (java.security.cert.CertificateException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 BcosHeartbeat (org.fisco.bcos.channel.dto.BcosHeartbeat)1 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)1 TopicVerifyMessage (org.fisco.bcos.channel.dto.TopicVerifyMessage)1 Message (org.fisco.bcos.channel.handler.Message)1 HeartBeatParser (org.fisco.bcos.channel.protocol.parser.HeartBeatParser)1 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)1