Search in sources :

Example 1 with EthereumResponse

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

the class Service method onReceiveTransactionMessage.

public void onReceiveTransactionMessage(ChannelHandlerContext ctx, EthereumMessage message) {
    TransactionSucCallback callback = (TransactionSucCallback) seq2TransactionCallback.get(message.getSeq());
    logger.info("receive transaction success seq:{}", message.getSeq());
    if (callback != null) {
        logger.info("found callback transaction callback");
        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);
        seq2TransactionCallback.remove(message.getSeq());
    } else {
        logger.info("callback is null");
    }
}
Also used : EthereumResponse(org.bcos.channel.dto.EthereumResponse)

Example 2 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, TransactionSucCallback transactionSucCallback) {
    class Callback extends EthereumResponseCallback {

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

        @Override
        public void onResponse(EthereumResponse response) {
            ethereumResponse = response;
            logger.info("收到响应: {}", response.getContent());
            semaphore.release();
        }

        public EthereumResponse ethereumResponse;

        public Semaphore semaphore = new Semaphore(1, true);
    }
    Callback callback = new Callback();
    asyncSendEthereumMessage(request, callback, transactionSucCallback);
    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 3 with EthereumResponse

use of org.bcos.channel.dto.EthereumResponse 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 4 with EthereumResponse

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

the class TransactionSucCallback method onTimeout.

public void onTimeout() {
    logger.error("transactionSuc timeout");
    EthereumResponse response = new EthereumResponse();
    response.setErrorCode(102);
    response.setErrorMessage("transactionSuc timeout");
    response.setContent("");
    onResponse(response);
}
Also used : EthereumResponse(org.bcos.channel.dto.EthereumResponse)

Example 5 with EthereumResponse

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

the class ChannelEthereumService method send.

@Override
public <T extends Response> T send(Request request, Class<T> responseType) throws IOException {
    byte[] payload = objectMapper.writeValueAsBytes(request);
    EthereumRequest ethereumRequest = new EthereumRequest();
    ethereumRequest.setKeyID(channelService.getOrgID());
    ethereumRequest.setBankNO("");
    ethereumRequest.setContent(new String(payload));
    ethereumRequest.setMessageID(channelService.newSeq());
    if (timeout != 0) {
        ethereumRequest.setTimeout(timeout);
    }
    EthereumResponse response;
    if (!request.isNeedTransCallback()) {
        response = channelService.sendEthereumMessage(ethereumRequest);
    } else {
        response = channelService.sendEthereumMessage(ethereumRequest, request.getTransactionSucCallback());
    }
    logger.debug("发送ethereum请求:{} {}", ethereumRequest.getMessageID(), objectMapper.writeValueAsString(request));
    logger.debug("收到ethereum响应:{} {} {}", ethereumRequest.getMessageID(), response.getErrorCode(), response.getContent());
    return objectMapper.readValue(response.getContent(), responseType);
}
Also used : EthereumRequest(org.bcos.channel.dto.EthereumRequest) 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