📄 simcomm.java
字号:
// $Id: SimComm.java,v 1.17.4.7 2003/09/10 03:41:52 mdwelsh Exp $/* tab:2 * * "Copyright (c) 2000 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: Dennis Chi, Nelson Lee * Date: October 16 2002 * Desc: * *//** * @author Dennis Chi * @author Nelson Lee */package net.tinyos.sim;import java.io.*;import java.util.*;import java.net.*;import net.tinyos.message.*;import net.tinyos.sf.*;import net.tinyos.sim.msg.*;import net.tinyos.sim.event.*;public class SimComm { private static final boolean DEBUG = false; private TinyViz tv; private Thread packetThread = null; private long delay = 0; private Socket eventSocket, cmdSocket; private int state; private static final int STATE_STOPPED = 0; private static final int STATE_CONNECTING = 1; private static final int STATE_PAUSED = 2; private static final int STATE_RUNNING = 3; private SimProtocol eventProtocol, cmdProtocol; private SimEventBus eventBus; private int eventPort, cmdPort; private boolean run_sf, sf_started = false; private boolean pauseOnInit = true; private SerialForwarder sf; private boolean seen_init = false; public SimComm(TinyViz tv, boolean run_sf, boolean pauseOnInit) { this(tv, run_sf, pauseOnInit, SimProtocol.TOSSIM_EVENT_PORT, SimProtocol.TOSSIM_COMMAND_PORT); } public SimComm(TinyViz tv, boolean run_sf, boolean pauseOnInit, int eventPort, int cmdPort) { this.tv = tv; this.run_sf = run_sf; this.pauseOnInit = pauseOnInit; this.eventBus = tv.getEventBus(); this.eventPort = eventPort; this.cmdPort = cmdPort; this.state = STATE_STOPPED; } public void start() { if (state != STATE_STOPPED) return; if (DEBUG) System.err.println("SimComm: start() called"); tv.setStatus("Connecting to simulator..."); state = STATE_CONNECTING; seen_init = false; try { if (DEBUG) System.err.println("SimComm: Opening event socket..."); eventSocket = new Socket("127.0.0.1", eventPort); InputStream input = eventSocket.getInputStream(); OutputStream output = eventSocket.getOutputStream(); eventProtocol = new SimProtocol(input, output, false); } catch (Exception e) { if (DEBUG) System.err.println("SimComm: Socket connection failed: "+e); tv.setStatus("Connection to simulator failed"); state = STATE_STOPPED; return; } tv.setStatus("Connection to simulator established"); state = STATE_PAUSED; try { packetThread = new PacketThread(); packetThread.start(); } catch (Exception exception) { System.err.println(exception); System.exit(-1); } if (run_sf) { if (!sf_started) { try { String args[] = { "-quiet", "-no-gui", "-comm", "tossim-serial" }; sf = new SerialForwarder(args); sf_started = true; } catch (IOException ioe) { if (DEBUG) System.err.println("SimComm: Can't start SerialForward: "+ioe); tv.setStatus("Unable to start serial forwarder"); } } else { sf.stopListenServer(); sf.startListenServer(); } } } public synchronized void stop() { if (state == STATE_STOPPED) return; if (state == STATE_CONNECTING) return; state = STATE_STOPPED; seen_init = false; if (eventSocket != null) { try { if (DEBUG) System.err.println("SimComm: Closing event socket..."); eventSocket.close(); } catch (Exception e) { // Ignore } } if (cmdSocket != null) { try { if (DEBUG) System.err.println("SimComm: Closing command socket..."); cmdSocket.close(); cmdSocket = null; } catch (Exception e) { // Ignore } } this.notify(); try { // Don't wait forever this.wait(500); } catch (InterruptedException ie) { // Ignore } tv.simCommUpdate(); if (DEBUG) System.err.println("SimComm: Stopped."); return; } public synchronized boolean isStopped() { return (state == STATE_STOPPED); } public synchronized boolean isPaused() { return (state == STATE_PAUSED || state == STATE_CONNECTING); } public synchronized void pause() { if (state != STATE_RUNNING) return; state = STATE_PAUSED; // XXX MDW - Don't want this if it prevents selection update // messages from propagating while paused //eventBus.pause(); tv.setStatus("Simulation paused"); tv.simCommUpdate(); } public synchronized void resume() { if (state != STATE_PAUSED && state != STATE_CONNECTING) return; switch (state) { case STATE_PAUSED: state = STATE_RUNNING; if (DEBUG) System.err.println("SimComm: resuming"); this.notify(); tv.setStatus("Simulation resume"); tv.simCommUpdate(); break; case STATE_CONNECTING: // Do nothing break; } } /** Wait until the TossimInitEvent has been read, or we stop. */ public synchronized void waitUntilInit() throws InterruptedException { while (state != STATE_STOPPED && !seen_init) { this.wait(); } } public synchronized void setSimDelay(long delay) { this.delay = delay; } public synchronized void sendCommand(TossimCommand cmd) throws IOException { int trycount = 0; while (trycount < 2) { trycount++; try { if (cmdSocket == null) { if (DEBUG) System.err.println("SimComm: Opening command socket..."); cmdSocket = new Socket("127.0.0.1", cmdPort); if (DEBUG) System.err.println("SimComm: Got command socket: "+cmdSocket); InputStream input = cmdSocket.getInputStream(); OutputStream output = cmdSocket.getOutputStream(); cmdProtocol = new SimProtocol(input, output); if (DEBUG) System.err.println("SimComm: Opened socket to simulator command port."); } cmdProtocol.writeCommand(cmd); tv.setStatus("Wrote command: "+cmd.toString()); if (DEBUG) System.err.println("Wrote command: "+cmd.toString()); return; } catch (IOException ioe) { tv.setStatus("Command send failed: "+ioe.getMessage()); if (DEBUG) System.err.println("Command send failed: "+ioe); try { cmdSocket.close(); cmdSocket = null; } catch (Exception e) { // Ignore } } } throw new IOException("Giving up on sending command: "+cmd); } public void ackEventRead() { // cannot use synchronized method isStopped(); // race condition: PacketReadThread holds onto the lock while trying to read from // event stream; simulator won't send an event until its last event was acked // resulting in deadlock --nalee 3/25/03 if (state != STATE_STOPPED) { //if (DEBUG) System.err.println("SimComm: Acking Event Read"); try { eventProtocol.ackEventRead(); } catch (Exception e) { if (DEBUG) { System.err.println("SimComm: Got exception: "+e); e.printStackTrace(); } } } } protected class PacketThread extends Thread { public PacketThread() throws IOException { setPriority(Thread.MIN_PRIORITY); } public void run() { try{ while (true) { synchronized (SimComm.this) { while (state != STATE_RUNNING) { if (state == STATE_STOPPED) { if (DEBUG) System.err.println("SimComm: State is stopped, resetting...\n"); // XXX MDW - Don't think I want this here // tv.reset(); return; } try { if (DEBUG) System.err.println("SimComm: Waiting..."); SimComm.this.wait(); if (DEBUG) System.err.println("SimComm: Woke up"); } catch (InterruptedException ie) { // Ignore } } } SimEvent event = eventProtocol.readEvent(delay); if (event instanceof net.tinyos.sim.event.TossimInitEvent) { //if (DEBUG) System.err.println("SimComm: TossimInitEvent received, pausing system..."); tv.pause(); eventBus.addEvent(event); try { // Handle all pending events (e.g., initializations) // before reading more from the sim eventBus.processAll(); } catch (InterruptedException ie) { // Just keep going } //if (DEBUG) System.err.println("SimComm: Continuing"); synchronized (SimComm.this) { seen_init = true; SimComm.this.notifyAll(); } } else { eventBus.addEvent(event); } } } catch (Exception e) { if (DEBUG) System.err.println("SimComm: Got exception: "+e); if (DEBUG) e.printStackTrace(); SimComm.this.stop(); // XXX MDW - Don't think I want this here //tv.reset(); return; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -