Search in sources :

Example 1 with ChannelPush2

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

the class Service method onReceiveChannelMessage2.

public void onReceiveChannelMessage2(ChannelHandlerContext ctx, ChannelMessage2 message) {
    ChannelResponseCallback2 callback = (ChannelResponseCallback2) seq2Callback.get(message.getSeq());
    if (message.getType() == ChannelMessageType.AMOP_REQUEST.getType() || message.getType() == ChannelMessageType.AMOP_MULBROADCAST.getType()) {
        logger.debug("channel PUSH");
        if (callback != null) {
            logger.debug("seq already existed,clear:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        if (message.getTopic().length() > verifyChannelPrefix.length() && verifyChannelPrefix.equals(message.getTopic().substring(0, verifyChannelPrefix.length()))) {
            try {
                signForAmop(ctx, message);
            } catch (IOException e) {
                logger.error("sign for amop failed:{}", e);
            }
        } else {
            try {
                ChannelPush2 push = new ChannelPush2();
                if (pushCallback != null) {
                    // pushCallback.setInfo(info);
                    push.setSeq(message.getSeq());
                    push.setService(this);
                    push.setCtx(ctx);
                    push.setTopic(message.getTopic());
                    push.setSeq(message.getSeq());
                    push.setMessageID(message.getSeq());
                    logger.debug("msg:{}", Arrays.toString(message.getData()));
                    push.setContent(message.getData());
                    pushCallback.onPush(push);
                } else {
                    logger.error("can not push,unset push callback");
                }
            } catch (Exception e) {
                logger.error("push error:{}", e);
            }
        }
    } else if (message.getType() == ChannelMessageType.AMOP_RESPONSE.getType()) {
        logger.debug("channel message:{}", message.getSeq());
        if (callback != null) {
            logger.debug("found callback response");
            ChannelResponse response = new ChannelResponse();
            if (message.getResult() != 0) {
                response.setErrorMessage("response errors");
            }
            // over bandwidth limit
            if (message.getResult() == ChannelMessageError.REJECT_AMOP_REQ_FOR_OVER_BANDWIDTHLIMIT.getError()) {
                logger.error("AMOP request was rejected due to over bandwidth limit, message: {}", message.getSeq());
                response.setErrorMessage("AMOP request was rejected due to over bandwidth limit");
            }
            response.setErrorCode(message.getResult());
            response.setMessageID(message.getSeq());
            if (message.getData() != null) {
                response.setContent(message.getData());
            }
            callback.onResponse(response);
        } else {
            logger.error("can not found response callback,timeout:{}", message.getData());
            return;
        }
    }
}
Also used : ChannelPush2(org.fisco.bcos.channel.dto.ChannelPush2) IOException(java.io.IOException) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) 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 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 ChannelPush2 (org.fisco.bcos.channel.dto.ChannelPush2)1 ChannelResponse (org.fisco.bcos.channel.dto.ChannelResponse)1 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)1