Search in sources :

Example 6 with ChannelResponse

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

the class Service method sendChannelMessage.

public ChannelResponse sendChannelMessage(ChannelRequest request) {
    class Callback extends ChannelResponseCallback {

        Callback() {
            try {
                semaphore.acquire(1);
            } catch (InterruptedException e) {
                logger.error("错误:", e);
            }
        }

        @Override
        public void onResponseMessage(ChannelResponse response) {
            channelResponse = response;
            logger.debug("收到响应: {}", response.getContent());
            semaphore.release();
        }

        public ChannelResponse channelResponse;

        public Semaphore semaphore = new Semaphore(1, true);
    }
    ;
    Callback callback = new Callback();
    asyncSendChannelMessage(request, callback);
    try {
        callback.semaphore.acquire(1);
    } catch (InterruptedException e) {
        logger.error("系统错误:", e);
    }
    return callback.channelResponse;
}
Also used : ChannelResponse(org.bcos.channel.dto.ChannelResponse) Semaphore(java.util.concurrent.Semaphore)

Example 7 with ChannelResponse

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

the class Service method sendChannelMessage2.

public ChannelResponse sendChannelMessage2(ChannelRequest request) {
    class Callback extends ChannelResponseCallback2 {

        Callback() {
            try {
                semaphore.acquire(1);
            } catch (InterruptedException e) {
                logger.error("错误:", e);
            }
        }

        @Override
        public void onResponseMessage(ChannelResponse response) {
            channelResponse = response;
            logger.debug("收到响应: {}", response.getContent());
            semaphore.release();
        }

        public ChannelResponse channelResponse;

        public Semaphore semaphore = new Semaphore(1, true);
    }
    ;
    Callback callback = new Callback();
    asyncSendChannelMessage2(request, callback);
    try {
        callback.semaphore.acquire(1);
    } catch (InterruptedException e) {
        logger.error("系统错误:", e);
    }
    return callback.channelResponse;
}
Also used : ChannelResponse(org.bcos.channel.dto.ChannelResponse) Semaphore(java.util.concurrent.Semaphore)

Example 8 with ChannelResponse

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

the class Service method onReceiveChannelMessage.

public void onReceiveChannelMessage(ChannelHandlerContext ctx, ChannelMessage message) {
    ChannelResponseCallback callback = (ChannelResponseCallback) seq2Callback.get(message.getSeq());
    logger.debug("收到消息 seq:{}", message.getSeq());
    if (message.getType() == 0x20) {
        // 链上链下请求
        logger.debug("channel请求消息 PUSH");
        if (callback != null) {
            // 清空callback再处理
            logger.debug("seq已存在,清除:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        try {
            ChannelPush push = new ChannelPush();
            if (pushCallback != null) {
                push.setService(this);
                push.setCtx(ctx);
                push.setMessageID(message.getSeq());
                push.setFromNode(message.getFromNode());
                push.setToNode(message.getToNode());
                push.setSeq(message.getSeq());
                push.setMessageID(message.getSeq());
                push.setContent(new String(message.getData(), 0, message.getData().length));
                pushCallback.onPush(push);
            } else {
                logger.error("无法push消息,未设置push callback");
            }
        } catch (Exception e) {
            logger.error("处理PUSH消息失败:", e);
        }
    } else if (message.getType() == 0x21) {
        // 链上链下回包
        logger.debug("channel回包消息:{}", message.getSeq());
        if (callback != null) {
            logger.debug("已找到callback 回包消息");
            ChannelResponse response = new ChannelResponse();
            if (message.getResult() != 0) {
                response.setErrorCode(message.getResult());
                response.setErrorMessage("回包错误");
            }
            response.setErrorCode(message.getResult());
            response.setMessageID(message.getSeq());
            if (message.getData() != null) {
                response.setContent(new String(message.getData()));
            }
            callback.onResponse(response);
        } else {
            logger.error("未找到回包callback,可能已超时:{}", message.getData());
            return;
        }
    }
}
Also used : ChannelPush(org.bcos.channel.dto.ChannelPush) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with ChannelResponse

use of org.bcos.channel.dto.ChannelResponse 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());
    logger.debug("收到消息 seq:{}", message.getSeq());
    if (message.getType() == 0x30) {
        // 链上链下请求
        logger.debug("channel请求消息 PUSH");
        if (callback != null) {
            // 清空callback再处理
            logger.debug("seq已存在,清除:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        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());
                push.setContent(new String(message.getData(), 0, message.getData().length));
                pushCallback.onPush(push);
            } else {
                logger.error("无法push消息,未设置push callback");
            }
        } catch (Exception e) {
            logger.error("处理PUSH消息失败:", e);
        }
    } else if (message.getType() == 0x31) {
        // 链上链下回包
        logger.debug("channel回包消息:{}", message.getSeq());
        if (callback != null) {
            logger.debug("已找到callback 回包消息");
            ChannelResponse response = new ChannelResponse();
            if (message.getResult() != 0) {
                response.setErrorCode(message.getResult());
                response.setErrorMessage("回包错误");
            }
            response.setErrorCode(message.getResult());
            response.setMessageID(message.getSeq());
            if (message.getData() != null) {
                response.setContent(new String(message.getData()));
            }
            callback.onResponse(response);
        } else {
            logger.error("未找到回包callback,可能已超时:{}", message.getData());
            return;
        }
    }
}
Also used : ChannelPush2(org.bcos.channel.dto.ChannelPush2) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with ChannelResponse

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

the class ConnectionPair method retrySendRemoteMessage.

public void retrySendRemoteMessage() {
    Integer errorCode = 0;
    try {
        // 选取客户端节点
        logger.debug("远端节点数:{}", remoteConnectionInfos.size());
        remoteConnectionInfo = null;
        if (remoteConnectionInfos.size() > 0) {
            Random random = new Random();
            Integer index = random.nextInt(remoteConnectionInfos.size());
            logger.debug("选取:{}", index);
            remoteConnectionInfo = remoteConnectionInfos.get(index);
            remoteConnectionInfos.remove(remoteConnectionInfos.get(index));
        }
        if (remoteConnectionInfo == null) {
            // 所有节点已尝试,无法再重试了
            logger.error("发送消息失败,所有重试均失败");
            errorCode = 99;
            throw new Exception("发送消息失败,所有重试均失败");
        }
        ChannelHandlerContext ctx = remoteChannelConnections.getNetworkConnectionByHost(remoteConnectionInfo.getHost(), remoteConnectionInfo.getPort());
        remoteConnection = ctx;
        if (ctx != null && ctx.channel().isActive()) {
            ByteBuf out = ctx.alloc().buffer();
            message.writeHeader(out);
            message.writeExtra(out);
            ctx.writeAndFlush(out);
            logger.debug("发送消息至  " + remoteConnectionInfo.getHost() + ":" + String.valueOf(remoteConnectionInfo.getPort()) + " 成功");
        } else {
            logger.error("发送节点不可用");
            retrySendRemoteMessage();
        }
    } catch (Exception e) {
        logger.error("发送消息异常", e);
        ChannelResponse response = new ChannelResponse();
        response.setErrorCode(errorCode);
        response.setErrorMessage(e.getMessage());
        // 找不到连接,错误
        logger.error("发送连接异常,返回错误99");
        if (message.getType() == 0x20 || message.getType() == 0x21) {
            message.setType((short) 0x21);
        } else if (message.getType() == 0x30 || message.getType() == 0x31) {
            message.setType((short) 0x31);
        } else {
        // ethereum消息,不用改类型
        }
        message.setResult(99);
        ByteBuf out = localConnection.alloc().buffer();
        message.writeHeader(out);
        message.writeExtra(out);
        localConnection.writeAndFlush(out);
        // 彻底失败后,删掉这个seq
        if (message.getSeq() != null) {
            server.getSeq2Connections().remove(message.getSeq());
        }
        if (timeout != null) {
            timeout.cancel();
        }
        return;
    }
}
Also used : Random(java.util.Random) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelResponse(org.bcos.channel.dto.ChannelResponse) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ChannelResponse (org.bcos.channel.dto.ChannelResponse)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 DateTimeFormatter (java.time.format.DateTimeFormatter)4 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 Random (java.util.Random)3 Service (org.bcos.channel.client.Service)3 ChannelRequest (org.bcos.channel.dto.ChannelRequest)3 ApplicationContext (org.springframework.context.ApplicationContext)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 ChannelConnections (org.bcos.channel.handler.ChannelConnections)2 ConnectionInfo (org.bcos.channel.handler.ConnectionInfo)2 BigInteger (java.math.BigInteger)1 ChannelMessage (org.bcos.channel.dto.ChannelMessage)1 ChannelMessage2 (org.bcos.channel.dto.ChannelMessage2)1 ChannelPush (org.bcos.channel.dto.ChannelPush)1