// $Id: sim.java,v 1.29 2007/11/07 10:42:45 zahorjan Exp $ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.Timer; import org.jgrapht.graph.*; /** * This is the mainline that drives the simulation. * Invoke something like this: *
java -Xmv300M -cp '.;jgrapht-jdk1.5.jar' sim 100 1 20 40 * */ public class sim { private static int slot = 0; private static int nNodeFailures = 0; private static int nnodes = 0; private static int nedges = 0; private static int maxTimesteps = 0; private static long time; public static int getTime() { return slot; } public static int getNumInitialFailures() { return nNodeFailures; } public static void usage() { System.out.println("Usage: java -Xmx300m -cp '.;jgrapht-jdk1.5.jar' sim nsteps nfail nnodes nedges"); System.out.println(" java -Xmx300m -cp '.;jgrapht-jdk1.5.jar' sim nsteps nfail graphFileName"); System.out.println("\nThe first creates a random topology, the second uses the one"); System.out.println("described in the file."); System.out.println("\n\tnsteps is the number of timesteps to run."); System.out.println("\tnfail is the number of nodes to fail."); System.out.println("\tnnodes is the number of nodes in the network."); System.out.println("\tnedges is the number of edges in the network."); System.out.println("\tgraphFileName is the name of a file containing a description"); System.out.println("\t of the network topology: #nodes on a single line followed by"); System.out.println("\t pairs of integers representing edges (i.e., the IDs of the"); System.out.println("\t two nodes, where IDs are assigned consecutively starting"); System.out.println("\t at zero)."); } /** * This is THE mainline -- invoke this app by invoking it. */ public static void main(String[] args) { String graphFileName = null; Network net = null; // The number of nodes to fail is required as a command line argument. // There are two more, optional args. if ( args.length != 3 && args.length != 4 ) { usage(); System.exit(1); } // suck in command line arguments try { maxTimesteps = Integer.parseInt(args[0]); System.out.println("Time steps: \t" + maxTimesteps); nNodeFailures = Integer.parseInt(args[1]); System.out.println("Num initial failures:\t" + nNodeFailures); if ( args.length == 4) { nnodes = Integer.parseInt(args[2]); System.out.println("Num nodes:\t" + nnodes); nedges = Integer.parseInt(args[3]); System.out.println("Num edges:\t" + nedges); // sanity check the args if ( nnodes < 1 || nNodeFailures >= nnodes || nedges < nnodes-1 || nedges > nnodes*(nnodes-1) ) { usage(); System.exit(2); } } else { graphFileName = args[2]; System.out.println("Graph file:\t" + graphFileName); } } catch (Exception e) { usage(); System.exit(3); } // Here's where all the work is... try { // create the score keeper ScoreKeeper scorekeeper = new ScoreKeeper(); time = System.currentTimeMillis(); // construct and the graph and initialize routing tables if ( graphFileName == null ) { // want a randomly constructed graph System.out.println("Constructing and initializing graph with " + nnodes + " nodes and " + nedges + " edges..."); net = new Network(nnodes, nedges, new DVNodeFactory()); } else { // want to use topology in file System.out.println("Constructing and initializing graph from file " + graphFileName); net = new Network(graphFileName, new DVNodeFactory()); nnodes = net.numNodes(); nedges = net.numEdges(); } net.initialize(); reportPerformanceFor("Constructed and initialized graph", time); time = System.currentTimeMillis(); if ( ConstConfig.DEBUG ) { System.out.println("Network ([nodes],[links]):"); System.out.println(net); } // Now fail a random set of nodes of edges. MyRandom rand = new MyRandom(); for (int f=0; f