Search in sources :

Example 16 with Node

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

the class GetNodeEventHandler method getAvailableNodes.

private List<Node> getAvailableNodes(int length, String nodeId) {
    List<Node> nodes = new ArrayList<>();
    int count = 0;
    List<Node> availableNodes = getNetworkService().getAvailableNodes();
    Collections.shuffle(availableNodes);
    for (Node node : availableNodes) {
        if (node.getId().equals(nodeId)) {
            continue;
        }
        nodes.add(node);
        count++;
        if (count == length || count > 20) {
            break;
        }
    }
    return nodes;
}
Also used : Node(io.nuls.network.entity.Node) ArrayList(java.util.ArrayList)

Example 17 with Node

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

the class GetNodesIpEventHandler method process.

@Override
public NetworkEventResult process(BaseEvent event, Node node) {
    List<Node> availableNodes = getNetworkService().getAvailableNodes();
    List<String> ipList = new ArrayList<>();
    for (Node n : availableNodes) {
        ipList.add(n.getIp());
    }
    NodesIpEvent ipEvent = new NodesIpEvent(ipList);
    return new NetworkEventResult(true, ipEvent);
}
Also used : Node(io.nuls.network.entity.Node) ArrayList(java.util.ArrayList) NodesIpEvent(io.nuls.network.message.entity.NodesIpEvent) GetNodesIpEvent(io.nuls.network.message.entity.GetNodesIpEvent) NetworkEventResult(io.nuls.network.message.NetworkEventResult)

Example 18 with Node

use of io.nuls.network.entity.Node 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 19 with Node

use of io.nuls.network.entity.Node 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)

Example 20 with Node

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

the class NodeDiscoverHandler method findOtherNode.

/**
 * Inquire more of the other nodes to the connected nodes
 *
 * @param size
 */
public void findOtherNode(int size) {
    GetNodeEvent event = new GetNodeEvent(size);
    NodeGroup group = nodesManager.getNodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
    if (group.getNodes().size() > 0) {
        List<Node> nodeList = new ArrayList<>(group.getNodes().values());
        Collections.shuffle(nodeList);
        for (Node node : nodeList) {
            if (node.isHandShake()) {
                broadcaster.broadcastToNode(event, node, true);
                break;
            }
        }
    }
    group = nodesManager.getNodeGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP);
    if (group.getNodes().size() > 0) {
        List<Node> nodeList = new ArrayList<>(group.getNodes().values());
        Collections.shuffle(nodeList);
        for (Node node : nodeList) {
            if (node.isHandShake()) {
                broadcaster.broadcastToNode(event, node, true);
                break;
            }
        }
    }
}
Also used : Node(io.nuls.network.entity.Node) GetNodeEvent(io.nuls.network.message.entity.GetNodeEvent) 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