Search in sources :

Example 1 with ChannelMessage2

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

the class Service method sendResponseMessage2.

// 链上链下二期回包
public void sendResponseMessage2(ChannelResponse response, ChannelHandlerContext ctx, String seq, String topic) {
    try {
        ChannelMessage2 responseMessage = new ChannelMessage2();
        responseMessage.setData(response.getContent().getBytes());
        responseMessage.setResult(response.getErrorCode());
        responseMessage.setSeq(seq);
        // 链上链下二期回包0x31
        responseMessage.setType((short) 0x31);
        responseMessage.setTopic(topic);
        ByteBuf out = ctx.alloc().buffer();
        responseMessage.writeHeader(out);
        responseMessage.writeExtra(out);
        ctx.writeAndFlush(out);
        logger.debug("发送回包成功 seq:{} 长度:{}", response.getMessageID(), out.readableBytes());
    } catch (Exception e) {
        logger.error("系统错误", e);
    }
}
Also used : ChannelMessage2(org.bcos.channel.dto.ChannelMessage2) ByteBuf(io.netty.buffer.ByteBuf) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ChannelMessage2

use of org.bcos.channel.dto.ChannelMessage2 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

UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ChannelMessage2 (org.bcos.channel.dto.ChannelMessage2)2 ByteBuf (io.netty.buffer.ByteBuf)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 ArrayList (java.util.ArrayList)1 ChannelResponse (org.bcos.channel.dto.ChannelResponse)1 ChannelConnections (org.bcos.channel.handler.ChannelConnections)1 ConnectionInfo (org.bcos.channel.handler.ConnectionInfo)1