📄 autorun.java
字号:
if (cur_arc.logfile != null) { try { arPlugin.openLogfile(); } catch (IOException ioe) { System.err.println("AUTORUN: Unable to open logfile: "+ioe); return; } } /* Run precmd */ if (cur_arc.precmd != null) { try { System.err.println("AUTORUN: Running precmd: "+cur_arc.precmd); Process precmd = Runtime.getRuntime().exec(cur_arc.precmd); precmd.waitFor(); } catch (InterruptedException ie) { // Ignore } catch (Exception ex) { System.err.println("AUTORUN: Unable to run precmd: "+ex); return; } } /* Set up command and run it */ String cmdLine; if (cur_arc.niceval != 0) { cmdLine = "nice -n "+cur_arc.niceval+" "+cur_arc.executable+" -gui"; } else { cmdLine = cur_arc.executable+" -gui"; } if (cur_arc.numsec > 0) { cmdLine += " -t="+cur_arc.numsec; } cmdLine += " -r=lossy"; cmdLine += " "+cur_arc.numMotes; String env[] = null; if (cur_arc.dbgflags != null) { env = new String[1]; env[0] = "DBG="+cur_arc.dbgflags; } System.err.println("AUTORUN: Running simulation: "+cmdLine); try { tv.reset(); simProcess = Runtime.getRuntime().exec(cmdLine, env); while ((tv.getSimComm().isStopped() || tv.getSimComm().isPaused()) && !stopProcess) { System.err.println("AUTORUN: Connecting..."); Thread.currentThread().sleep(500); if (tv.getSimComm().isStopped()) tv.getSimComm().start(); if (tv.getSimComm().isPaused()) tv.resume(); tv.getSimComm().waitUntilInit(); } if (cur_arc.pauseatstart) { System.err.println("AUTORUN: Click play to start simulation."); tv.getSimComm().pause(); } } catch (Exception ex) { System.err.println("AUTORUN: Unable to run simulation: "+ex); return; } while (!stopProcess) { try { exited = false; System.err.println("AUTORUN: Simulation running."); simProcess.waitFor(); System.err.println("AUTORUN: Process exited."); exited = true; break; } catch (InterruptedException ie) { continue; } } System.err.println("AUTORUN: Done with run."); // Done running simulation tv.pause(); try { tv.getEventBus().processAll(); } catch (InterruptedException ie) { // Ignore } tv.getMotePanel().refreshAndWait(); // Capture screenshot if (cur_arc.screenshot != null) { System.err.println("AUTORUN: Capturing screenshot to "+cur_arc.screenshot+"..."); try { Thread.currentThread().sleep(1000); } catch (InterruptedException ie) { // Ignore } java.awt.Robot robot = null; try { robot = new java.awt.Robot(); OutputStream f = new FileOutputStream(cur_arc.screenshot); java.awt.image.BufferedImage bi = robot.createScreenCapture(new java.awt.Rectangle(java.awt.Toolkit.getDefaultToolkit().getScreenSize())); PngEncoder pnge = new PngEncoder(bi); byte imgbytes[] = pnge.pngEncode(); f.write(imgbytes); f.close(); } catch(Exception exp) { System.err.println("AUTORUN: Can't capture screenshot: "+exp); } } /* Run post cmd */ if (cur_arc.postcmd != null) { try { System.err.println("AUTORUN: Running postcmd: "+cur_arc.postcmd); Process postcmd = Runtime.getRuntime().exec(cur_arc.postcmd, env); postcmd.waitFor(); } catch (InterruptedException ie) { // Ignore } catch (Exception ex) { System.err.println("AUTORUN: Unable to run postcmd: "+ex); return; } } if (cur_arc.stopmode == STOPMODE_PAUSE && !exited) { // Wait until user resumes try { synchronized(tv) { while (tv.isPaused()) { tv.wait(); } } } catch (InterruptedException ie) { // Just keep going } } // Sleeps are in here to give process enough time to exit and // SimComm enough time to reset. Probably could use better // synchronization. tv.getSimComm().stop(); try { Thread.currentThread().sleep(2000); } catch (InterruptedException ie) { // Ignore } simProcess.destroy(); System.err.println("AUTORUN: Destroyed process."); stopProcess = false; try { Thread.currentThread().sleep(500); } catch (InterruptedException ie) { // Ignore } try { arPlugin.closeLogfile(); } catch (IOException ioe) { // Ignore } } synchronized (this) { this.notifyAll(); return; } } } class autoRunPlugin extends Plugin { private Writer logfw = null; private long time_start; void closeLogfile() throws IOException { double totalsec = (System.currentTimeMillis() - time_start) / 1000.0; int min = (int)(totalsec / 60); int sec = (int)(totalsec % 60); if (logfw != null) { logfw.write("# Run ended, time elapsed: "+min+":"+sec+" min:sec\n"); logfw.flush(); logfw.close(); logfw = null; } } void openLogfile() throws IOException { logfw = new BufferedWriter(new FileWriter(cur_arc.logfile)); logfw.write("# AutoRun logfile\n"); logfw.write("# Executable: "+cur_arc.executable+"\n"); logfw.write("# Num motes: "+cur_arc.numMotes+"\n"); logfw.write("# DBG flags: "+cur_arc.dbgflags+"\n"); logfw.write("# Total sec: "+cur_arc.numsec+"\n"); logfw.write("# Stop string: "+cur_arc.stopstring+"\n"); Enumeration e = cur_arc.options.keys(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); logfw.write("# Option "+key+": "+cur_arc.options.get(e)+"\n"); } e = cur_arc.plugins.elements(); while (e.hasMoreElements()) { String plugin = (String)e.nextElement(); logfw.write("# Plugin: "+plugin+"\n"); } logfw.write("#\n"); logfw.write("# Format: <mote> <time> <data>\n"); time_start = System.currentTimeMillis(); } public void register() {} public void deregister() {} public void reset() {} public void draw(java.awt.Graphics graphics) {} public String toString() { return "AutoRun logger (do not disable)"; } private synchronized void log(String msg) { if (logfw == null) return; String logs = "NONE NONE "+msg.replace('\n', ' ')+"\n"; try { logfw.write(logs); } catch (IOException ioe) { System.err.println("AUTORUN: Cannot log message"); } if (cur_arc.stopstring != null && msg.indexOf(cur_arc.stopstring) != -1) { if (!cur_arc.stopstringall || ++stopstringall_count == cur_arc.numMotes) { try { logfw.write("# Stopping due to match of stopstring: "+cur_arc.stopstring+"\n"); } catch (IOException ioe2) { // Ignore } stopstringall_count = 0; stop(); } } } private synchronized void log(SimEvent event) { if (logfw == null) return; String logs; if (event instanceof TossimEvent) { TossimEvent tev = (TossimEvent)event; logs = tev.getMoteID()+" "+tev.getTime(); logs = logs+" "+event.toString().replace('\n', ' ')+"\n"; } else { logs = "NONE NONE "+event.toString().replace('\n', ' ')+"\n"; } try { logfw.write(logs); } catch (IOException ioe) { System.err.println("AUTORUN: Cannot log message"); } if (cur_arc.stopstring != null && event.toString().indexOf(cur_arc.stopstring) != -1) { if (!cur_arc.stopstringall || ++stopstringall_count == cur_arc.numMotes) { try { logfw.write("# Stopping due to match of stopstring: "+cur_arc.stopstring+"\n"); } catch (IOException ioe2) { // Ignore } stopstringall_count = 0; stop(); } } } public void handleEvent(SimEvent event) { log(event); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -