Search in sources :

Example 1 with ConnectionInfo

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

the class Service method asyncSendChannelMessage2.

public void asyncSendChannelMessage2(ChannelRequest request, ChannelResponseCallback2 callback) {
    try {
        if (request.getContentByteArray().length >= 32 * 1024 * 1024) {
            logger.error("send byte length should not greater than 32M now length:{}", request.getContentByteArray().length);
            throw new AmopException("send byte length should not greater than 32M");
        }
        logger.debug("ChannelRequest:{} ", request.getMessageID());
        callback.setService(this);
        ChannelMessage2 channelMessage = new ChannelMessage2();
        channelMessage.setSeq(request.getMessageID());
        channelMessage.setResult(0);
        // channelMessage.setType((short) ChannelMessageType.AMOP_REQUEST.getType());
        if (request.getType() == 0) {
            channelMessage.setType((short) ChannelMessageType.AMOP_REQUEST.getType());
        } else {
            channelMessage.setType(request.getType());
        }
        channelMessage.setData(request.getContentByteArray());
        channelMessage.setTopic(request.getToTopic());
        logger.info("msgid:{} type:{} topic:{}", request.getMessageID(), channelMessage.getType(), request.getToTopic());
        try {
            List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
            // select send node
            ChannelConnections fromChannelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
            if (fromChannelConnections == null) {
                if (orgID != null) {
                    logger.error("not found:{}", orgID);
                    throw new Exception("not found orgID");
                } else {
                    logger.error("not found:{}", agencyName);
                    throw new Exception("not found agencyName");
                }
            }
            fromConnectionInfos.addAll(fromChannelConnections.getConnections());
            logger.debug("FromOrg:{} nodes:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
            callback.setFromChannelConnections(fromChannelConnections);
            callback.setFromConnectionInfos(fromConnectionInfos);
            // set request content
            callback.setRequest(channelMessage);
            logger.info("put msgid:{} into callback map", request.getMessageID());
            seq2Callback.put(request.getMessageID(), callback);
            if (request.getTimeout() > 0) {
                logger.info("timeoutms:{}", request.getTimeout());
                final ChannelResponseCallback2 callbackInner = callback;
                callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {

                    ChannelResponseCallback2 _callback = callbackInner;

                    @Override
                    public void run(Timeout timeout) throws Exception {
                        // process timeout logic
                        _callback.onTimeout();
                        logger.error("process channel message timeout, seq: {}, timeout: {}", channelMessage.getSeq(), request.getTimeout());
                    }
                }, request.getTimeout(), TimeUnit.MILLISECONDS));
            }
            callback.retrySendMessage();
        } catch (Exception e) {
            logger.error("send message fail:", e);
            ChannelResponse response = new ChannelResponse();
            response.setErrorCode(ChannelMessageError.MESSAGE_SEND_EXCEPTION.getError());
            response.setMessageID(request.getMessageID());
            response.setErrorMessage(e.getMessage());
            response.setContent("");
            callback.onResponse(response);
            return;
        }
    } catch (Exception e) {
        logger.error("system error", e);
    }
}
Also used : ChannelConnections(org.fisco.bcos.channel.handler.ChannelConnections) ChannelMessage2(org.fisco.bcos.channel.dto.ChannelMessage2) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) ArrayList(java.util.ArrayList) ConnectionInfo(org.fisco.bcos.channel.handler.ConnectionInfo) 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)

Example 2 with ConnectionInfo

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

the class Service method asyncMulticastChannelMessage2.

public void asyncMulticastChannelMessage2(ChannelRequest request) {
    try {
        logger.debug("ChannelRequest:{} ", request.getMessageID());
        ChannelMessage2 channelMessage = new ChannelMessage2();
        channelMessage.setSeq(request.getMessageID());
        channelMessage.setResult(0);
        channelMessage.setType((short) ChannelMessageType.AMOP_MULBROADCAST.getType());
        channelMessage.setData(request.getContentByteArray());
        channelMessage.setTopic(request.getToTopic());
        try {
            // set request content
            ChannelConnections fromChannelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
            if (fromChannelConnections == null) {
                if (orgID != null) {
                    logger.error("not found:{}", orgID);
                    throw new Exception("not found orgID");
                } else {
                    logger.error("not found:{}", agencyName);
                    throw new Exception("not found agencyName");
                }
            }
            logger.debug("FromOrg:{} nodes:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
            for (ConnectionInfo connectionInfo : fromChannelConnections.getConnections()) {
                ChannelHandlerContext ctx = fromChannelConnections.getNetworkConnectionByHost(connectionInfo.getHost(), connectionInfo.getPort());
                if (ctx != null && ChannelHandlerContextHelper.isChannelAvailable(ctx)) {
                    ByteBuf out = ctx.alloc().buffer();
                    channelMessage.writeHeader(out);
                    channelMessage.writeExtra(out);
                    ctx.writeAndFlush(out);
                    logger.debug("send message to{}:{} success ", connectionInfo.getHost(), connectionInfo.getPort());
                } else {
                    logger.error("sending node unavailable, {}:{}", connectionInfo.getHost(), connectionInfo.getPort());
                }
            }
        } catch (Exception e) {
            logger.error("send message fail:{}", e);
            ChannelResponse response = new ChannelResponse();
            response.setErrorCode(ChannelMessageError.MESSAGE_SEND_EXCEPTION.getError());
            response.setMessageID(request.getMessageID());
            response.setErrorMessage(e.getMessage());
            response.setContent("");
            return;
        }
    } catch (Exception e) {
        logger.error("system error:{}", e);
    }
}
Also used : ChannelConnections(org.fisco.bcos.channel.handler.ChannelConnections) ChannelMessage2(org.fisco.bcos.channel.dto.ChannelMessage2) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ConnectionInfo(org.fisco.bcos.channel.handler.ConnectionInfo) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 CertificateException (java.security.cert.CertificateException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ChannelMessage2 (org.fisco.bcos.channel.dto.ChannelMessage2)2 ChannelResponse (org.fisco.bcos.channel.dto.ChannelResponse)2 ChannelConnections (org.fisco.bcos.channel.handler.ChannelConnections)2 ConnectionInfo (org.fisco.bcos.channel.handler.ConnectionInfo)2 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 ArrayList (java.util.ArrayList)1