Search in sources :

Example 1 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class BlockPersistenceThread method doPersistence.

private void doPersistence(long height) throws IOException {
    Block block = blockManager.getBlock(height);
    if (null == block) {
        List<Node> nodeList = networkService.getAvailableNodes();
        if (nodeList == null || nodeList.isEmpty()) {
            return;
        }
        List<String> nodeIdList = new ArrayList<>();
        for (Node node : nodeList) {
            nodeIdList.add(node.getId());
        }
        try {
            BlockBatchDownloadUtils.getInstance().request(nodeIdList, height, height);
        } catch (InterruptedException e) {
            Log.error(e);
        }
        return;
    }
    if (block.getTxs().isEmpty()) {
        // todo why
        Log.warn("block has no tx!");
        blockManager.removeBlock(block.getHeader().getHash().getDigestHex());
        return;
    }
    boolean isSuccess = blockService.saveBlock(block);
    if (isSuccess) {
        blockManager.removeBlock(block.getHeader().getHash().getDigestHex());
        blockManager.setStoredHeight(height);
        txCacheManager.removeTxList(block.getTxHashList());
    }
}
Also used : Node(io.nuls.network.entity.Node) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block)

Example 2 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NetworkServiceImpl method getNodesIp.

@Override
public Set<String> getNodesIp() {
    Set<String> ipList = new HashSet<>();
    for (String ip : NetworkContext.ipMap.keySet()) {
        ipList.add(ip);
    }
    List<Node> nodeList = getAvailableNodes();
    for (Node node : nodeList) {
        ipList.add(node.getIp());
    }
    return ipList;
}
Also used : Node(io.nuls.network.entity.Node) HashSet(java.util.HashSet)

Example 3 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NodeDiscoverHandler method getLocalNodes.

// get nodes from local database
public List<Node> getLocalNodes(int size) {
    Set<String> keys = nodesManager.getNodes().keySet();
    List<NodePo> nodePos = getNodeDao().getNodePoList(size, keys);
    List<Node> nodes = new ArrayList<>();
    if (nodePos == null || nodePos.isEmpty()) {
        return nodes;
    }
    for (NodePo po : nodePos) {
        Node node = new Node(network);
        NodeTransferTool.toNode(node, po);
        nodes.add(node);
    }
    return nodes;
}
Also used : NodePo(io.nuls.db.entity.NodePo) Node(io.nuls.network.entity.Node)

Example 4 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NodeDiscoverHandler method run.

/**
 * do ping/pong and ask versionMessage
 */
@Override
public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (running) {
        count++;
        List<Node> nodeList = new ArrayList<>(nodesManager.getNodes().values());
        StringBuilder str = new StringBuilder();
        for (Node node : nodeList) {
            if (node.getStatus() == 2) {
                str.append(",");
                str.append(node.getIp());
            }
        }
        if (str.toString().length() == 0) {
            str.append(",");
        }
        Log.info("nodes:" + str.toString().substring(1));
        GetVersionEvent event = new GetVersionEvent(network.getExternalPort());
        GetNodesIpEvent ipEvent = new GetNodesIpEvent();
        for (Node node : nodeList) {
            if (node.isAlive()) {
                broadcaster.broadcastToNode(event, node, true);
                if (count == 10) {
                    broadcaster.broadcastToNode(ipEvent, node, true);
                }
            }
        }
        long now = TimeService.currentTimeMillis();
        if (count == 10) {
            count = 0;
            List<String> list = new ArrayList<>();
            for (Map.Entry<String, Long> entry : NetworkContext.ipMap.entrySet()) {
                if (now - entry.getValue() > DateUtil.MINUTE_TIME * 2) {
                    list.add(entry.getKey());
                }
            }
            for (String ip : list) {
                NetworkContext.ipMap.remove(ip);
            }
        }
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            Log.error(e);
        }
    }
}
Also used : GetVersionEvent(io.nuls.network.message.entity.GetVersionEvent) Node(io.nuls.network.entity.Node) GetNodesIpEvent(io.nuls.network.message.entity.GetNodesIpEvent)

Example 5 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NodesManager method run.

/**
 * check the nodes when closed try to connect other one
 */
@Override
public void run() {
    while (running) {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        if (nodes.isEmpty()) {
            List<Node> nodes = getSeedNodes();
            for (Node node : nodes) {
                node.setType(Node.OUT);
                node.setStatus(Node.WAIT);
                addNodeToGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP, node);
            }
        } else {
            NodeGroup group = nodeGroups.get(NetworkConstant.NETWORK_NODE_OUT_GROUP);
            if (group.size() < network.maxOutCount()) {
                List<Node> nodes = discoverHandler.getLocalNodes(network.maxOutCount() - group.size());
                if (!nodes.isEmpty()) {
                    for (Node node : nodes) {
                        node.setType(Node.OUT);
                        node.setStatus(Node.WAIT);
                        addNodeToGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP, node);
                    }
                } else {
                    discoverHandler.findOtherNode(network.maxOutCount() - group.size());
                }
            }
        }
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            Log.error(e);
        }
    }
}
Also used : Node(io.nuls.network.entity.Node) NodeGroup(io.nuls.network.entity.NodeGroup)

Aggregations

Node (io.nuls.network.entity.Node)25 SocketChannel (io.netty.channel.socket.SocketChannel)8 NodeGroup (io.nuls.network.entity.NodeGroup)5 ArrayList (java.util.ArrayList)4 BroadcastResult (io.nuls.network.entity.BroadcastResult)3 ByteBuf (io.netty.buffer.ByteBuf)2 NulsMessage (io.nuls.core.mesasge.NulsMessage)2 NetworkEventResult (io.nuls.network.message.NetworkEventResult)2 GetNodeEvent (io.nuls.network.message.entity.GetNodeEvent)2 GetNodesIpEvent (io.nuls.network.message.entity.GetNodesIpEvent)2 NodeEvent (io.nuls.network.message.entity.NodeEvent)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Block (io.nuls.core.chain.entity.Block)1 NodePo (io.nuls.db.entity.NodePo)1 GetVersionEvent (io.nuls.network.message.entity.GetVersionEvent)1 NodesIpEvent (io.nuls.network.message.entity.NodesIpEvent)1 InfoDto (io.nuls.rpc.entity.InfoDto)1 RpcResult (io.nuls.rpc.entity.RpcResult)1 HashSet (java.util.HashSet)1