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());
}
}
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;
}
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;
}
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);
}
}
}
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);
}
}
}
Aggregations