/** * Initializes the state of this node system. This includes generating and connecting the nodes in * the system. */ public void initialize() { // Determine the number of nodes in the network int numNodes = rand.nextInt(MAX_NUM_NODES + 1); while (numNodes < MIN_NUM_NODES) numNodes = rand.nextInt(MAX_NUM_NODES + 1); System.out.println("Number of nodes: " + numNodes); // Create the nodes for (int i = 0; i < numNodes; i++) { Node node = new Node(new EmptyStrategy(), SystemTime.getTime()); // TODO // Determine this node's peer table size node.setPeerTableSize(10); nodes.add(node); } // Randomly connect nodes for (Node node : nodes) { for (int index = 0; index < node.getPeerTableSize(); index++) { // Find a random node not in this node's peer table int nextNeighbor = rand.nextInt(nodes.size()); // Peer should not know the neighbor and the neighbor should // be this peer while (node.knowsPeer(nextNeighbor) || nextNeighbor == node.getID()) { nextNeighbor = rand.nextInt(nodes.size()); } node.setPeer(index, nodes.get(nextNeighbor)); } } }
/** Infect a random node with this strategy. */ public void infect(StructureStrategy strategy) { int infectedPeer = rand.nextInt(nodes.size()); System.out.println("Infecting peer: " + infectedPeer); nodes.get(infectedPeer).setStrategy(strategy, SystemTime.getTime()); }