📄 simdriver.java
字号:
// $Id: SimDriver.java,v 1.15 2004/06/11 21:30:14 mikedemmer Exp $/* * * * "Copyright (c) 2004 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice and the following two paragraphs appear in all copies of * this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors: Michael Demmer * Date: January 9, 2004 * Desc: Main simulation driver * *//** * @author Michael Demmer */package net.tinyos.sim;import net.tinyos.sim.event.*;import net.tinyos.sim.script.*;import net.tinyos.sim.plugins.*;import java.io.*;import java.util.*;import java.net.URL;public class SimDriver { protected SimDebug debug = SimDebug.get("driver"); protected TinyViz tv; protected AutoRun autoRun; protected SimState simState; protected SimComm simComm; protected SimCommands simCommands; protected SimExec simExec; protected SimEventBus simEventBus; protected SimRandom simRandom; protected MoteVariables moteVariables; protected PluginManager pluginManager; protected ScriptInterpreter interp; protected ScriptInteractor interactor; protected RadioModelPlugin radioModel; protected MoteLayoutPlugin moteLayout; protected PacketLoggerPlugin packets; protected String scriptPath = null; protected String scriptArgs = null; protected int pauseCount = 0; protected long tossimTime = 0; protected Hashtable options = new Hashtable(); protected long randomSeed; private void help(String error) { if (error != null) { System.err.println("Error parsing arguments: " + error + "\n"); } System.err.println("SimDriver: Usage:"); System.err.println(" java net.tinyos.sim.SimDriver [options] script"); System.err.println("Options:"); System.err.println(" -help\n\tPrint this help"); System.err.println(" -gui\n\tRun the TinyViz GUI"); System.err.println(" -noconsole\n\tDo not run a python interpreter console"); System.err.println(" -listen <port>\n\t" + "Listen for telnet connections and spawn an interpreter"); System.err.println(" -run <executable> <nummotes>\n\tRun simulation"); System.err.println(" -autorun <config>\n\tRun in batch mode"); System.err.println(" -simargs <args>\n\tSimulator arguments"); System.err.println(" -script <script file>\n\tRun Python Script"); System.err.println(" -scriptargs <args>\n\tPython script arguments"); System.err.println(" -nosf\n\tDo not start SerialForwarder"); System.err.println(" -nopause\n\tDo not pause the simulator at init time"); System.err.println(" -plugins [path]\n\tSpecify colon-delimited " + "directories to search for plugin classes"); System.err.println(" -scriptpath [path]\n\tDitto to search for scripts"); System.err.println(" -nolaf\n\tUse default look and feel (ugly!)"); System.err.println(" -echodbg\n\tPrint out TOSSIM debug messages"); System.err.println(" -seed <seed>\n\tSet random number generator seed"); System.err.println(" <name=value>\n\tSet name=value plugin options"); System.err.println(""); System.err.println("Known SIMDBG modes: " + SimDebug.listAllModes()); System.exit(1); } public SimDriver(String initargs[]) { /* Parse options */ boolean gui = false; boolean console = true; int listen_port = -1; String plugin_path = null; String autorun_exec = null; int autorun_nummotes = 1; String autorun_config = null; String autorun_args = null; String simargs = null; String script = null; boolean lookandfeel = true; boolean run_serial_forward = true; boolean pause_on_init = true; boolean echo_dbg = false; boolean seed_set = false; try { for (int n = 0; n < initargs.length; n++) { if (initargs[n].equals("-help") || initargs[n].equals("-h")) { help(null); } else if (initargs[n].equals("-gui")) { gui = true; } else if (initargs[n].equals("-noconsole")) { console = false; } else if (initargs[n].equals("-listen")) { listen_port = Integer.parseInt(initargs[n+1]); n++; } else if (initargs[n].equals("-plugins")) { plugin_path = initargs[n+1]; n++; } else if (initargs[n].equals("-scriptpath")) { scriptPath = initargs[n+1]; n++; } else if (initargs[n].equals("-run")) { autorun_exec = initargs[n+1]; autorun_nummotes = Integer.parseInt(initargs[n+2]); n += 2; } else if (initargs[n].equals("-autorun")) { autorun_config = initargs[n+1]; n++; } else if (initargs[n].equals("-simargs")) { simargs = initargs[n + 1]; n++; } else if (initargs[n].equals("-script")) { script = initargs[n + 1]; n++; } else if (initargs[n].equals("-scriptargs")) { scriptArgs = initargs[n + 1]; n++; } else if (initargs[n].equals("-nolaf")) { lookandfeel = false; } else if (initargs[n].equals("-nosf")) { run_serial_forward = false; } else if (initargs[n].equals("-nopause")) { pause_on_init = false; } else if (initargs[n].equals("-echodbg")) { echo_dbg = true; } else if (initargs[n].equals("-seed")) { seed_set = true; randomSeed = Integer.parseInt(initargs[n + 1]); n++; } else if (initargs[n].indexOf('-') != 0 && initargs[n].indexOf('=') != 0) { StringTokenizer st = new StringTokenizer(initargs[n],"="); String optionName = st.nextToken(); String optionValue = st.nextToken(); if (optionName == null || optionValue == null) { help("invalid option syntax -- must specify both name and value"); } setOption(optionName, optionValue); } else { help("unrecognized option: "+initargs[n]); } } } catch (Exception e) { e.printStackTrace(); help("got exception parsing arguments: " + e); return; } /* Do option validation */ if (autorun_exec != null && autorun_config != null) help("cannot use -run and -autorun together"); if (script != null && autorun_exec != null) help("cannot use -run and -script together"); if (script != null && autorun_config != null) help("cannot use -autorun and -script together"); if (gui == false && lookandfeel == false) help("-nolaf is meaningless without -gui"); if (script != null) { try { FileInputStream file = new FileInputStream(script); } catch (FileNotFoundException e) { help("can't read script file " + script); } } /* First of all, seed and create the random number generator */ if (!seed_set) { Random getSeed = new Random(); randomSeed = getSeed.nextInt(); if (randomSeed < 0) randomSeed = -randomSeed; } simRandom = new SimRandom(randomSeed); System.out.println("Simulation random seed " + randomSeed); /* Create the core sim driver objects */ if (autorun_exec != null) { autoRun = new AutoRun(this, autorun_exec, autorun_nummotes, simargs); } else if (autorun_config != null) { try { autoRun = new AutoRun(this, autorun_config); } catch (IOException e) { System.err.println("Error parsing autorun config. Exiting"); exit(1); } } System.out.println("Initializing simulator objects..."); simEventBus = new SimEventBus(this); simState = new SimState(this); simComm = new SimComm(this, run_serial_forward, pause_on_init); simCommands = new SimCommands(this); simExec = new SimExec(this); moteVariables = new MoteVariables(this); /* * Handle plugins */ System.out.println("Loading simulator plugins..."); pluginManager = new PluginManager(this); pluginManager.loadPlugins(plugin_path); radioModel = (RadioModelPlugin)pluginManager.getPlugin("RadioModelPlugin"); moteLayout = (MoteLayoutPlugin)pluginManager.getPlugin("MoteLayoutPlugin"); packets = (PacketLoggerPlugin)pluginManager.getPlugin("PacketLoggerPlugin"); pluginManager.register(radioModel); pluginManager.register(moteLayout); pluginManager.register(packets); /* * A plugin to keep track of the tossim time. */ Plugin p; p = new TimeUpdatePlugin(); pluginManager.addPlugin(p); pluginManager.register(p); /* * And one to echo debug message output, if requested. */ if (echo_dbg) { p = new EchoDebugPlugin(); pluginManager.addPlugin(p); pluginManager.register(p); } /* Now create the gui if requested */ if (gui) { System.out.println("Creating TinyViz GUI..."); tv = new TinyViz(this, lookandfeel); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -