// $Id: Network.java,v 1.31 2007/11/08 01:57:01 zahorjan Exp $ import java.util.*; import java.io.*; import org.jgrapht.*; import org.jgrapht.graph.*; /** * Class representing an entire network. */ public class Network extends SimpleGraph { /** * Used by getNetwork(). */ private static Network theNetwork = null; /** * There can be only one network in an application. * There is a static accessor function to find it. */ public static Network getNetwork() { return theNetwork; } /** * An array of all nodes in the network. */ private Node[] node; /** * Set to true if the topology changes because * one or more nodes fail or recover. Reset to false * by takeStep(). */ private boolean hasChanged; private MyRandom rand; private int nNodes = 0; private int nEdges = 0; public Node[] getNodeArray() { return node; } // the values returned include any failed nodes and their edges public int numNodes() { return nNodes; } public int numEdges() { return nEdges; } /** * Returns true if the network has changed (nodes added or deleted) * since the last invocation of takeStep(), false otherwise. */ public boolean hasChanged() { return hasChanged; } /** * Constructor leading to a randomly generated topology. First construct * the network object, then call initialize(). *

* There can be at most one Network object in the application. * An IllegalStateException is thrown otherwise. */ public Network(int nnodes, int nedges, VertexFactory nodeFactory ) throws IllegalArgumentException, IllegalStateException { super(DefaultEdge.class); nNodes = nnodes; nEdges = nedges; commonInit(); // sanity check args if ( nnodes < 1 || nedges < nnodes-1 || nedges > (nnodes*(nnodes-1))/2 ) { throw new IllegalArgumentException( "Bad arguments to Network constructor(" + nnodes + ", " + nedges + ")" ); } // Now construct the random topology. // First create a chain, so we know graph is connected. // Then add additional edges, at random. node[0] = nodeFactory.createVertex(); addVertex( node[0] ); for (int n=1; n * #nodes * src dst * src dst * ... * *

* First construct the network object, then call initialize(). *

* There can be at most one Network object in the application. * An IllegalStateException is thrown otherwise. */ public Network(String graphFile, VertexFactory nodeFactory ) throws IllegalStateException, FileNotFoundException, IOException { super(DefaultEdge.class); // prepare to read the file -- get #nodes FileInputStream inStream = new FileInputStream(graphFile); Scanner scanner = new Scanner(inStream); nNodes = scanner.nextInt(); // now that we know how many nodes there are we can all commonInit() commonInit(); // create the nodes for (int n=0; nReturns prior failed status of node (i.e., true if it was already failed). */ public boolean failNode(int n) { return node[n].setHasFailed( true ); } /** * Take a time step. Go throught nodes in a random order, and invoke * each node's update routine with only some fixed probability. */ public void takeStep() { // reset boolean indicating whether topology has been changed int numVertices = vertexSet().size(); hasChanged = false; // Cause each node to swap its current and future in queues. // (Not done in the next loop because I have an idea that might cause // each node to be run more than once.) for (Node next : node ) { next.swapInQueues(); } // okay, now cause each node to take a step. (Skipping nodes, // with probability ConstSysProperties.UPDATE_PROB, is done in the Node code.) for ( Node next : node ) { next.takeStep(); } // topology changes iff number of vertices has changed hasChanged = numVertices != vertexSet().size(); } }