use of io.nuls.network.message.entity.GetNodesIpEvent 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);
}
}
}
Aggregations