Search in sources :

Example 1 with NodeGroup

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);
        }
    }
}
Also used : Node(io.nuls.network.entity.Node) NodeGroup(io.nuls.network.entity.NodeGroup)

Example 2 with NodeGroup

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);
    }
}
Also used : NodeGroup(io.nuls.network.entity.NodeGroup)

Example 3 with NodeGroup

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;
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) NodeGroup(io.nuls.network.entity.NodeGroup)

Example 4 with NodeGroup

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);
}
Also used : BroadcastResult(io.nuls.network.entity.BroadcastResult) Node(io.nuls.network.entity.Node) ArrayList(java.util.ArrayList) NodeGroup(io.nuls.network.entity.NodeGroup)

Example 5 with NodeGroup

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;
}
Also used : Node(io.nuls.network.entity.Node) RpcResult(io.nuls.rpc.entity.RpcResult) InfoDto(io.nuls.rpc.entity.InfoDto) NodeGroup(io.nuls.network.entity.NodeGroup) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

NodeGroup (io.nuls.network.entity.NodeGroup)8 Node (io.nuls.network.entity.Node)5 SocketChannel (io.netty.channel.socket.SocketChannel)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 BroadcastResult (io.nuls.network.entity.BroadcastResult)1 GetNodeEvent (io.nuls.network.message.entity.GetNodeEvent)1 InfoDto (io.nuls.rpc.entity.InfoDto)1 RpcResult (io.nuls.rpc.entity.RpcResult)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1