Search in sources :

Example 6 with EthereumResponse

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

the class EthereumResponseCallback method onTimeout.

public void onTimeout() {
    logger.error("处理Ethereum消息超时:{}");
    EthereumResponse response = new EthereumResponse();
    response.setErrorCode(102);
    response.setErrorMessage("处理Ethereum消息超时");
    response.setContent("");
    onResponse(response);
}
Also used : EthereumResponse(org.bcos.channel.dto.EthereumResponse)

Example 7 with EthereumResponse

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

the class Service method sendEthereumMessage.

public EthereumResponse sendEthereumMessage(EthereumRequest request) {
    class Callback extends EthereumResponseCallback {

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

        @Override
        public void onResponse(EthereumResponse response) {
            ethereumResponse = response;
            if (ethereumResponse != null && ethereumResponse.getContent() != null) {
                logger.debug("收到响应: {}", response.getContent());
            } else {
                logger.error("ethereum错误");
            }
            semaphore.release();
        }

        public EthereumResponse ethereumResponse;

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

Example 8 with EthereumResponse

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

the class Service method onReceiveEthereumMessage.

public void onReceiveEthereumMessage(ChannelHandlerContext ctx, EthereumMessage message) {
    EthereumResponseCallback callback = (EthereumResponseCallback) seq2Callback.get(message.getSeq());
    logger.debug("收到ethereum消息 seq:{}", message.getSeq());
    if (callback != null) {
        logger.debug("已找到callback ethereum回包消息");
        if (callback.getTimeout() != null) {
            callback.getTimeout().cancel();
        }
        EthereumResponse response = new EthereumResponse();
        if (message.getResult() != 0) {
            response.setErrorMessage("回包错误");
        }
        response.setErrorCode(message.getResult());
        response.setMessageID(message.getSeq());
        response.setContent(new String(message.getData()));
        callback.onResponse(response);
        seq2Callback.remove(message.getSeq());
    } else {
        logger.debug("无callback push消息");
    }
}
Also used : EthereumResponse(org.bcos.channel.dto.EthereumResponse)

Aggregations

EthereumResponse (org.bcos.channel.dto.EthereumResponse)8 Semaphore (java.util.concurrent.Semaphore)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 EthereumMessage (org.bcos.channel.dto.EthereumMessage)1 EthereumRequest (org.bcos.channel.dto.EthereumRequest)1 ChannelConnections (org.bcos.channel.handler.ChannelConnections)1