Search in sources :

Example 1 with EthereumMessage

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

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

the class ChannelConnections method reconnect.

public void reconnect() {
    for (Entry<String, ChannelHandlerContext> ctx : networkConnections.entrySet()) {
        if (ctx.getValue() == null || !ctx.getValue().channel().isActive()) {
            String[] split = ctx.getKey().split(":");
            String host = split[0];
            Integer port = Integer.parseInt(split[1]);
            logger.debug("尝试连接到: {}:{}", host, port);
            bootstrap.connect(host, port);
        } else {
            logger.trace("发送心跳至 {}", ctx.getKey());
            // 连接还在,发送心跳
            EthereumMessage ethereumMessage = new EthereumMessage();
            ethereumMessage.setSeq(UUID.randomUUID().toString().replaceAll("-", ""));
            ethereumMessage.setResult(0);
            ethereumMessage.setType((short) 0x13);
            ethereumMessage.setData("0".getBytes());
            ByteBuf out = ctx.getValue().alloc().buffer();
            ethereumMessage.writeHeader(out);
            ethereumMessage.writeExtra(out);
            ctx.getValue().writeAndFlush(out);
        }
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) EthereumMessage(org.bcos.channel.dto.EthereumMessage)

Aggregations

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