use of io.nuls.network.entity.NodeGroup 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);
}
}
}
use of io.nuls.network.entity.NodeGroup in project nuls by nuls-io.
the class NodesManager method init.
/**
* Check if is a consensus node,add consensusNodeGroup
*/
public void init() {
// init default NodeGroup
NodeGroup inNodes = new NodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
NodeGroup outNodes = new NodeGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP);
nodeGroups.put(inNodes.getName(), inNodes);
nodeGroups.put(outNodes.getName(), outNodes);
boolean isConsensus = NulsContext.MODULES_CONFIG.getCfgValue(PocConsensusConstant.CFG_CONSENSUS_SECTION, PocConsensusConstant.PROPERTY_PARTAKE_PACKING, false);
if (isConsensus) {
NodeGroup consensusNodes = new NodeGroup(NetworkConstant.NETWORK_NODE_CONSENSUS_GROUP);
nodeGroups.put(consensusNodes.getName(), consensusNodes);
}
}
use of io.nuls.network.entity.NodeGroup in project nuls by nuls-io.
the class ServerChannelHandler method channelRegistered.
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
SocketChannel socketChannel = (SocketChannel) ctx.channel();
String remoteIP = socketChannel.remoteAddress().getHostString();
Node node = getNetworkService().getNode(remoteIP);
if (node != null) {
if (node.getStatus() == Node.CONNECT) {
ctx.channel().close();
return;
}
// When nodes try to connect to each other but not connected, select one of the smaller IP addresses as the server
if (node.getType() == Node.OUT) {
String localIP = InetAddress.getLocalHost().getHostAddress();
boolean isLocalServer = IpUtil.judgeIsLocalServer(localIP, remoteIP);
if (!isLocalServer) {
ctx.channel().close();
return;
} else {
getNetworkService().removeNode(remoteIP);
}
}
}
NodeGroup group = getNetworkService().getNodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
if (group.size() > getNetworkService().getNetworkParam().maxInCount()) {
ctx.channel().close();
return;
}
}
use of io.nuls.network.entity.NodeGroup in project nuls by nuls-io.
the class BroadcastHandler method broadcastToGroup.
public BroadcastResult broadcastToGroup(BaseEvent event, String groupName, String excludeNodeId, boolean asyn) {
NodeGroup group = nodesManager.getNodeGroup(groupName);
if (group == null) {
return new BroadcastResult(false, "NodeGroup not found");
}
if (group.size() == 0) {
return new BroadcastResult(false, "no node can be broadcast");
}
List<Node> nodeList = new ArrayList<>(group.getNodes().values());
return broadcastToList(nodeList, event, excludeNodeId, asyn);
}
use of io.nuls.network.entity.NodeGroup in project nuls by nuls-io.
the class NetworkMessageResource method getInfo.
@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getInfo() {
RpcResult result = RpcResult.getSuccess();
InfoDto info = new InfoDto(NulsContext.getInstance().getBestBlock().getHeader().getHeight(), NulsContext.getInstance().getNetBestBlockHeight(), TimeService.getNetTimeOffset());
NodeGroup inGroup = networkService.getNodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
NodeGroup outGroup = networkService.getNodeGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP);
int count = 0;
for (Node node : inGroup.getNodes().values()) {
if (node.getStatus() == Node.HANDSHAKE) {
count += 1;
}
}
info.setInCount(count);
count = 0;
for (Node node : outGroup.getNodes().values()) {
if (node.getStatus() == Node.HANDSHAKE) {
count += 1;
}
}
info.setOutCount(count);
result.setData(info);
return result;
}
Aggregations