Search in sources :

Example 1 with ChannelConnections

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

the class Service method asyncSendChannelMessage.

public void asyncSendChannelMessage(ChannelRequest request, ChannelResponseCallback callback) {
    try {
        logger.debug("处理链上链下请求: " + request.getMessageID());
        callback.setService(this);
        ChannelMessage channelMessage = new ChannelMessage();
        channelMessage.setSeq(request.getMessageID());
        channelMessage.setResult(0);
        // 链上链下请求0x20
        channelMessage.setType((short) 0x20);
        channelMessage.setData(request.getContent().getBytes());
        try {
            List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
            List<ConnectionInfo> toConnectionInfos = new ArrayList<ConnectionInfo>();
            // 设置发送节点
            ChannelConnections fromChannelConnections = allChannelConnections.get(orgID);
            if (fromChannelConnections == null) {
                // 没有找到对应的链
                // 返回错误
                logger.error("没有找到本机构:{}", request.getFromOrg());
                throw new Exception("未找到本机构");
            }
            fromConnectionInfos.addAll(fromChannelConnections.getConnections());
            logger.debug("发送结构:{} 节点数:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
            callback.setFromChannelConnections(fromChannelConnections);
            callback.setFromConnectionInfos(fromConnectionInfos);
            // 设置目的节点
            ChannelConnections toChannelConnections = allChannelConnections.get(request.getToOrg());
            if (toChannelConnections == null) {
                logger.error("未找到目的机构: {}", request.getToOrg());
                throw new Exception("未找到目标机构");
            }
            toConnectionInfos.addAll(toChannelConnections.getConnections());
            logger.debug("机构:{} 节点数:{}", request.getToOrg(), toChannelConnections.getConnections().size());
            callback.setToConnectionInfos(toConnectionInfos);
            // 设置消息内容
            callback.setRequest(channelMessage);
            seq2Callback.put(request.getMessageID(), callback);
            if (request.getTimeout() > 0) {
                final ChannelResponseCallback callbackInner = callback;
                callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {

                    ChannelResponseCallback _callback = callbackInner;

                    @Override
                    public void run(Timeout timeout) throws Exception {
                        // 处理超时逻辑
                        _callback.onTimeout();
                    }
                }, request.getTimeout(), TimeUnit.MILLISECONDS));
            }
            callback.retrySendMessage(0);
        } catch (Exception e) {
            logger.error("发送消息异常 消息未发出", e);
            ChannelResponse response = new ChannelResponse();
            response.setErrorCode(100);
            response.setMessageID(request.getMessageID());
            response.setErrorMessage(e.getMessage());
            response.setContent("");
            callback.onResponse(response);
            return;
        }
    } catch (Exception e) {
        logger.error("系统错误", e);
    }
}
Also used : ChannelConnections(org.bcos.channel.handler.ChannelConnections) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) ArrayList(java.util.ArrayList) ConnectionInfo(org.bcos.channel.handler.ConnectionInfo) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ChannelMessage(org.bcos.channel.dto.ChannelMessage)

Example 2 with ChannelConnections

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

the class Service method asyncSendEthereumMessage.

public void asyncSendEthereumMessage(EthereumRequest request, EthereumResponseCallback callback) {
    logger.debug("处理Ethereum请求: " + request.getMessageID());
    Boolean sended = false;
    EthereumMessage ethereumMessage = new EthereumMessage();
    ethereumMessage.setSeq(request.getMessageID());
    ethereumMessage.setResult(0);
    ethereumMessage.setType((short) 0x12);
    ethereumMessage.setData(request.getContent().getBytes());
    // 选取发送节点
    try {
        ChannelConnections fromChannelConnections = allChannelConnections.get(orgID);
        if (fromChannelConnections == null) {
            // 没有找到对应的链
            // 返回错误
            logger.error("没有找到本机构:{}", orgID);
            throw new Exception("未找到本机构");
        }
        ChannelHandlerContext ctx = fromChannelConnections.randomNetworkConnection();
        ByteBuf out = ctx.alloc().buffer();
        ethereumMessage.writeHeader(out);
        ethereumMessage.writeExtra(out);
        seq2Callback.put(request.getMessageID(), callback);
        if (request.getTimeout() > 0) {
            // ethereum名字可能会搞混,换成channel
            final EthereumResponseCallback callbackInner = callback;
            callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {

                EthereumResponseCallback _callback = callbackInner;

                @Override
                public void run(Timeout timeout) throws Exception {
                    // 处理超时逻辑
                    _callback.onTimeout();
                }
            }, request.getTimeout(), TimeUnit.MILLISECONDS));
        }
        ctx.writeAndFlush(out);
        logger.debug("发送Ethereum消息至 " + ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress() + ":" + ((SocketChannel) ctx.channel()).remoteAddress().getPort() + " 成功");
        sended = true;
    } catch (Exception e) {
        logger.error("系统错误", e);
        EthereumResponse response = new EthereumResponse();
        response.setErrorCode(-1);
        response.setErrorMessage("系统错误");
        response.setContent("");
        response.setMessageID(request.getMessageID());
        if (callback.getTimeout() != null) {
            callback.getTimeout().cancel();
        }
        callback.onResponse(response);
    }
}
Also used : ChannelConnections(org.bcos.channel.handler.ChannelConnections) SocketChannel(io.netty.channel.socket.SocketChannel) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) EthereumResponse(org.bcos.channel.dto.EthereumResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EthereumMessage(org.bcos.channel.dto.EthereumMessage)

Example 3 with ChannelConnections

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

the class Service method asyncSendChannelMessage2.

public void asyncSendChannelMessage2(ChannelRequest request, ChannelResponseCallback2 callback) {
    try {
        logger.debug("处理链上链下请求: " + request.getMessageID());
        callback.setService(this);
        ChannelMessage2 channelMessage = new ChannelMessage2();
        channelMessage.setSeq(request.getMessageID());
        channelMessage.setResult(0);
        // 链上链下请求0x30
        channelMessage.setType((short) 0x30);
        channelMessage.setData(request.getContent().getBytes());
        channelMessage.setTopic(request.getToTopic());
        try {
            List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
            // 设置发送节点
            ChannelConnections fromChannelConnections = allChannelConnections.get(orgID);
            if (fromChannelConnections == null) {
                // 没有找到对应的链
                // 返回错误
                logger.error("没有找到本机构:{}", orgID);
                throw new Exception("未找到本机构");
            }
            fromConnectionInfos.addAll(fromChannelConnections.getConnections());
            logger.debug("发送机构:{} 节点数:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
            callback.setFromChannelConnections(fromChannelConnections);
            callback.setFromConnectionInfos(fromConnectionInfos);
            // 设置消息内容
            callback.setRequest(channelMessage);
            seq2Callback.put(request.getMessageID(), callback);
            if (request.getTimeout() > 0) {
                final ChannelResponseCallback2 callbackInner = callback;
                callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {

                    ChannelResponseCallback2 _callback = callbackInner;

                    @Override
                    public void run(Timeout timeout) throws Exception {
                        // 处理超时逻辑
                        _callback.onTimeout();
                    }
                }, request.getTimeout(), TimeUnit.MILLISECONDS));
            }
            callback.retrySendMessage();
        } catch (Exception e) {
            logger.error("发送消息异常 消息未发出", e);
            ChannelResponse response = new ChannelResponse();
            response.setErrorCode(100);
            response.setMessageID(request.getMessageID());
            response.setErrorMessage(e.getMessage());
            response.setContent("");
            callback.onResponse(response);
            return;
        }
    } catch (Exception e) {
        logger.error("系统错误", e);
    }
}
Also used : ChannelConnections(org.bcos.channel.handler.ChannelConnections) ChannelMessage2(org.bcos.channel.dto.ChannelMessage2) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) ArrayList(java.util.ArrayList) ConnectionInfo(org.bcos.channel.handler.ConnectionInfo) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Timeout (io.netty.util.Timeout)3 TimerTask (io.netty.util.TimerTask)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ChannelConnections (org.bcos.channel.handler.ChannelConnections)3 ArrayList (java.util.ArrayList)2 ChannelResponse (org.bcos.channel.dto.ChannelResponse)2 ConnectionInfo (org.bcos.channel.handler.ConnectionInfo)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 ChannelMessage (org.bcos.channel.dto.ChannelMessage)1 ChannelMessage2 (org.bcos.channel.dto.ChannelMessage2)1 EthereumMessage (org.bcos.channel.dto.EthereumMessage)1 EthereumResponse (org.bcos.channel.dto.EthereumResponse)1