Search in sources :

Example 1 with HeartBeatParser

use of org.fisco.bcos.channel.protocol.parser.HeartBeatParser in project web3sdk by FISCO-BCOS.

the class Service method sendHeartbeatMessage.

public void sendHeartbeatMessage(ChannelHandlerContext ctx) {
    Message message = new BcosMessage();
    message.setSeq(UUID.randomUUID().toString().replaceAll("-", ""));
    message.setResult(0);
    message.setType((short) ChannelMessageType.CLIENT_HEARTBEAT.getType());
    HeartBeatParser heartBeatParser = new HeartBeatParser(ChannelHandlerContextHelper.getProtocolVersion(ctx));
    try {
        message.setData(heartBeatParser.encode("0"));
    } catch (JsonProcessingException e) {
        logger.error(" write json failed, message: {}", e.getMessage());
        return;
    }
    ByteBuf out = ctx.alloc().buffer();
    message.writeHeader(out);
    message.writeExtra(out);
    ctx.writeAndFlush(out);
}
Also used : BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) 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) ByteBuf(io.netty.buffer.ByteBuf) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with HeartBeatParser

use of org.fisco.bcos.channel.protocol.parser.HeartBeatParser 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)2 ByteBuf (io.netty.buffer.ByteBuf)2 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)2 TopicVerifyMessage (org.fisco.bcos.channel.dto.TopicVerifyMessage)2 Message (org.fisco.bcos.channel.handler.Message)2 HeartBeatParser (org.fisco.bcos.channel.protocol.parser.HeartBeatParser)2 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 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)1