Search in sources :

Example 1 with BroadcastResult

use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.

the class MessageBusServiceImpl method getNodeIdListResult.

private Result<List<String>> getNodeIdListResult(BroadcastResult result) {
    List<String> list = new ArrayList<>();
    if (!result.isSuccess() || result.getBroadcastNodes() == null || result.getBroadcastNodes().isEmpty()) {
        return Result.getFailed(result.getErrorCode()).setData(list);
    }
    for (Node node : result.getBroadcastNodes()) {
        list.add(node.getId());
    }
    Result rs = new Result();
    rs.setSuccess(true);
    rs.setData(list);
    return rs;
}
Also used : Node(io.nuls.network.model.Node) ArrayList(java.util.ArrayList) BroadcastResult(io.nuls.network.model.BroadcastResult) Result(io.nuls.kernel.model.Result)

Example 2 with BroadcastResult

use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.

the class BroadcastHandler method broadcastToANode.

public BroadcastResult broadcastToANode(BaseMessage message, Node node, boolean asyn) {
    if (!node.isAlive()) {
        return new BroadcastResult(false, NetworkErrorCode.NET_NODE_DEAD);
    }
    if (node.getChannel() == null || !node.getChannel().isActive()) {
        return new BroadcastResult(false, NetworkErrorCode.NET_NODE_MISS_CHANNEL);
    }
    try {
        MessageHeader header = message.getHeader();
        header.setMagicNumber(networkParam.getPacketMagic());
        BaseNulsData body = message.getMsgBody();
        header.setLength(body.size());
        if (asyn) {
            node.getChannel().eventLoop().execute(() -> {
                try {
                    Channel channel = node.getChannel();
                    if (channel != null) {
                        channel.writeAndFlush(Unpooled.wrappedBuffer(message.serialize()));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } else {
            ChannelFuture future = node.getChannel().writeAndFlush(Unpooled.wrappedBuffer(message.serialize()));
            future.await();
            boolean success = future.isSuccess();
            if (!success) {
                return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_FAIL);
            }
        }
    } catch (Exception e) {
        Log.error(e);
        return new BroadcastResult(false, NetworkErrorCode.NET_MESSAGE_ERROR);
    }
    return new BroadcastResult(true, KernelErrorCode.SUCCESS);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) BroadcastResult(io.nuls.network.model.BroadcastResult) Channel(io.netty.channel.Channel) MessageHeader(io.nuls.protocol.message.base.MessageHeader) IOException(java.io.IOException) BaseNulsData(io.nuls.kernel.model.BaseNulsData) IOException(java.io.IOException)

Example 3 with BroadcastResult

use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.

the class BroadcastHandler method broadcastToHalfNode.

public BroadcastResult broadcastToHalfNode(BaseMessage msg, Node excludeNode, boolean asyn) {
    Collection<Node> nodes = nodeManager.getAvailableNodes();
    if (nodes.isEmpty()) {
        return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_NODE_EMPTY);
    }
    List<Node> nodeList = new ArrayList<>();
    int i = 0;
    for (Node node : nodes) {
        i++;
        if (i % 2 == 1) {
            nodeList.add(node);
        }
    }
    return broadcastToList(nodeList, msg, excludeNode, asyn, 50);
}
Also used : BroadcastResult(io.nuls.network.model.BroadcastResult) Node(io.nuls.network.model.Node)

Example 4 with BroadcastResult

use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.

the class BroadcastHandler method broadcastToList.

private BroadcastResult broadcastToList(Collection<Node> nodeList, BaseMessage message, Node excludeNode, boolean asyn, int percent) {
    BroadcastResult result = new BroadcastResult();
    try {
        int successCount = 0;
        int minCount = 5;
        // 根据百分比决定直接广播给多少个节点
        if (nodeList.size() > minCount && percent < 100) {
            int needCount = nodeList.size() * percent / 100;
            if (needCount < minCount) {
                needCount = minCount;
            }
            Set<Integer> set = new HashSet<>();
            while (true) {
                Random rand = new Random();
                int ran = rand.nextInt(nodeList.size());
                set.add(ran);
                if (set.size() == needCount + 1) {
                    break;
                }
            }
            int nodeListIndex = 0;
            Collection<Node> nodeBroadcastList = new ArrayList<>();
            for (Node node : nodeList) {
                if (set.contains(nodeListIndex)) {
                    if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
                        nodeListIndex++;
                        continue;
                    }
                    nodeBroadcastList.add(node);
                    if (nodeBroadcastList.size() == needCount) {
                        break;
                    }
                }
                nodeListIndex++;
            }
            nodeList = nodeBroadcastList;
        }
        for (Node node : nodeList) {
            if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
                continue;
            }
            BroadcastResult br = broadcastToNode(message, node, asyn);
            if (br.isSuccess()) {
                successCount++;
                result.getBroadcastNodes().add(node);
            } else if (br.getErrorCode().equals(NetworkErrorCode.NET_MESSAGE_ERROR)) {
                return br;
            }
        }
        if (successCount == 0) {
            return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_FAIL);
        }
    } catch (Exception e) {
        return new BroadcastResult(false, NetworkErrorCode.NET_MESSAGE_ERROR);
    }
    result.setSuccess(true);
    result.setErrorCode(KernelErrorCode.SUCCESS);
    return result;
}
Also used : BroadcastResult(io.nuls.network.model.BroadcastResult) Node(io.nuls.network.model.Node) IOException(java.io.IOException)

Aggregations

BroadcastResult (io.nuls.network.model.BroadcastResult)4 Node (io.nuls.network.model.Node)3 IOException (java.io.IOException)2 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 BaseNulsData (io.nuls.kernel.model.BaseNulsData)1 Result (io.nuls.kernel.model.Result)1 MessageHeader (io.nuls.protocol.message.base.MessageHeader)1 ArrayList (java.util.ArrayList)1