Search in sources :

Example 1 with Message

use of org.fisco.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.

the class Service method sendCheckResultToNode.

public void sendCheckResultToNode(ChannelRequest request, ChannelHandlerContext ctx, short msgtype) {
    try {
        Message msg = new Message();
        msg.setData(request.getContentByteArray());
        msg.setResult(0);
        msg.setSeq(request.getMessageID());
        msg.setType(msgtype);
        msg.setResult(0);
        ByteBuf out = ctx.alloc().buffer();
        msg.writeHeader(out);
        msg.writeExtra(out);
        ctx.writeAndFlush(out);
        logger.debug("response seq:{} length:{}", request.getMessageID(), out.readableBytes());
    } catch (Exception e) {
        logger.error("system error:", e);
    }
}
Also used : BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) Message(org.fisco.bcos.channel.handler.Message) TopicVerifyMessage(org.fisco.bcos.channel.dto.TopicVerifyMessage) ByteBuf(io.netty.buffer.ByteBuf) 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)

Example 2 with Message

use of org.fisco.bcos.channel.handler.Message 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 3 with Message

use of org.fisco.bcos.channel.handler.Message in project web3sdk by FISCO-BCOS.

the class Service method sendResponse2Node.

private void sendResponse2Node(ChannelHandlerContext ctx, TopicVerifyMessage message) {
    try {
        Message response = new Message();
        response.setSeq(message.getSeq());
        response.setResult(0);
        response.setType((short) ChannelMessageType.REQUEST_TOPICCERT.getType());
        response.setData("".getBytes());
        ByteBuf out = ctx.alloc().buffer();
        response.writeHeader(out);
        response.writeExtra(out);
        ctx.writeAndFlush(out);
        logger.info("response seq:{} length:{}", response.getSeq(), out.readableBytes());
    } catch (Exception e) {
        logger.error("response seq:{} send error", message.getSeq());
    }
}
Also used : BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) Message(org.fisco.bcos.channel.handler.Message) TopicVerifyMessage(org.fisco.bcos.channel.dto.TopicVerifyMessage) ByteBuf(io.netty.buffer.ByteBuf) 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)

Example 4 with Message

use of org.fisco.bcos.channel.handler.Message 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)4 ByteBuf (io.netty.buffer.ByteBuf)4 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)4 TopicVerifyMessage (org.fisco.bcos.channel.dto.TopicVerifyMessage)4 Message (org.fisco.bcos.channel.handler.Message)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchProviderException (java.security.NoSuchProviderException)3 CertificateException (java.security.cert.CertificateException)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)3 HeartBeatParser (org.fisco.bcos.channel.protocol.parser.HeartBeatParser)2 BcosHeartbeat (org.fisco.bcos.channel.dto.BcosHeartbeat)1