diff --git a/tools/cooja/java/se/sics/cooja/plugins/LogListener.java b/tools/cooja/java/se/sics/cooja/plugins/LogListener.java index 5e6338239..8d2f5774f 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/LogListener.java +++ b/tools/cooja/java/se/sics/cooja/plugins/LogListener.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: LogListener.java,v 1.12 2008/10/03 14:30:51 fros4943 Exp $ + * $Id: LogListener.java,v 1.13 2009/05/26 14:27:00 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -88,7 +88,7 @@ public class LogListener extends VisPlugin { if (lastMessage.length() > 0 && lastMessage.charAt(lastMessage.length() - 1) == '\n') { lastMessage = lastMessage.substring(0, lastMessage.length() - 1); } - String outputString = "TIME:" + simulation.getSimulationTime() + "\t"; + String outputString = "TIME:" + simulation.getSimulationTimeMillis() + "\t"; if (mote != null && mote.getInterfaces().getMoteID() != null) { outputString += "ID:" + mote.getInterfaces().getMoteID().getMoteID() + "\t"; } @@ -247,8 +247,8 @@ public class LogListener extends VisPlugin { setTitle("Log Listener - Listening on " + nrLogs + " mote logs"); pack(); - setSize(gui.getDesktopPane().getWidth(), getHeight()); - setLocation(0, gui.getDesktopPane().getHeight() - getHeight()); + setSize(gui.getDesktopPane().getWidth(), 150); + setLocation(0, gui.getDesktopPane().getHeight() - 300); try { setSelected(true); diff --git a/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java b/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java index 2061df12c..0ef07be75 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java +++ b/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ScriptParser.java,v 1.4 2009/01/15 13:11:56 fros4943 Exp $ + * $Id: ScriptParser.java,v 1.5 2009/05/26 14:27:00 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -38,6 +38,8 @@ import javax.script.ScriptException; import org.apache.log4j.Logger; +import se.sics.cooja.Simulation; + public class ScriptParser { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(ScriptParser.class); @@ -130,7 +132,7 @@ public class ScriptParser { throw new ScriptSyntaxErrorException("Only one timeout handler allowed"); } - timeoutTime = Long.parseLong(matcher.group(1)); + timeoutTime = Long.parseLong(matcher.group(1))*Simulation.MILLISECOND; timeoutCode = ";"; matcher.reset(code); @@ -161,7 +163,7 @@ public class ScriptParser { throw new ScriptSyntaxErrorException("Only one timeout handler allowed"); } - timeoutTime = Long.parseLong(matcher.group(1)); + timeoutTime = Long.parseLong(matcher.group(1))*Simulation.MILLISECOND; timeoutCode = matcher.group(2); matcher.reset(code); @@ -228,7 +230,7 @@ public class ScriptParser { Matcher matcher = pattern.matcher(code); while (matcher.find()) { - long time = Long.parseLong(matcher.group(1)); + long time = Long.parseLong(matcher.group(1))*Simulation.MILLISECOND; String msg = matcher.group(2); code = matcher.replaceFirst( diff --git a/tools/cooja/java/se/sics/cooja/plugins/ScriptRunner.java b/tools/cooja/java/se/sics/cooja/plugins/ScriptRunner.java index ce96cc75a..ab0c4537e 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/ScriptRunner.java +++ b/tools/cooja/java/se/sics/cooja/plugins/ScriptRunner.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ScriptRunner.java,v 1.17 2009/04/23 08:54:10 fros4943 Exp $ + * $Id: ScriptRunner.java,v 1.18 2009/05/26 14:27:00 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -86,8 +86,8 @@ public class ScriptRunner implements Plugin { " */\n" + "\n" + "/* Make test automatically fail (timeout) after 100 simulated seconds */\n" + - "//TIMEOUT(100000); /* no action at timeout */\n" + - "TIMEOUT(100000, log.log(\"last msg: \" + msg + \"\\n\")); /* print last msg at timeout */\n" + + "//TIMEOUT(100000); /* milliseconds. no action at timeout */\n" + + "TIMEOUT(100000, log.log(\"last msg: \" + msg + \"\\n\")); /* milliseconds. print last msg at timeout */\n" + "\n" + "log.log(\"first mote output: '\" + msg + \"'\\n\");\n" + "\n" + diff --git a/tools/cooja/java/se/sics/cooja/plugins/SimControl.java b/tools/cooja/java/se/sics/cooja/plugins/SimControl.java index 11b4b5d4f..f8888a10b 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/SimControl.java +++ b/tools/cooja/java/se/sics/cooja/plugins/SimControl.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: SimControl.java,v 1.13 2009/04/23 08:48:01 fros4943 Exp $ + * $Id: SimControl.java,v 1.14 2009/05/26 14:27:00 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -38,6 +38,7 @@ import java.beans.PropertyChangeListener; import java.text.NumberFormat; import java.util.*; import javax.swing.*; +import javax.swing.Timer; import javax.swing.event.*; import org.apache.log4j.Logger; @@ -59,16 +60,14 @@ public class SimControl extends VisPlugin { private static final int SLIDE_MAX = 921; // e^9.21 => ~10000 private static final int TIME_MAX = 10000; + private static final int LABEL_UPDATE_INTERVAL = 100; + private JSlider sliderDelay; private JLabel simulationTime, delayLabel; private JButton startButton, stopButton; private JFormattedTextField stopTimeTextField; - private int simulationStopTime = -1; private Observer simObserver; - private Observer tickObserver; - - private long lastTextUpdateTime = -1; /** * Create a new simulation control panel. @@ -83,55 +82,42 @@ public class SimControl extends VisPlugin { JButton button; JPanel smallPanel; - SwingUtilities.invokeLater(new Runnable() { - public void run() { - // Register as tickobserver - simulation.addTickObserver(tickObserver = new Observer() { - public void update(Observable obs, Object obj) { - if (simulation == null || simulationTime == null) { - return; - } + /* Update current time label when simulation is running */ + if (simulation.isRunning()) { + updateLabelTimer.start(); + } - // During simulation running, only update text 10 times each second - if (lastTextUpdateTime < System.currentTimeMillis() - 100) { - lastTextUpdateTime = System.currentTimeMillis(); - simulationTime.setText("Current simulation time: " + simulation.getSimulationTime()); - } - - if (simulationStopTime > 0 && simulationStopTime <= simulation.getSimulationTime() && simulation.isRunning()) { - // Time to stop simulation now - simulation.stopSimulation(); - simulationStopTime = -1; - stopTimeTextField.setValue(simulation.getSimulationTime()); - } + /* Observe current simulation */ + simulation.addObserver(simObserver = new Observer() { + public void update(Observable obs, Object obj) { + if (simulation.isRunning()) { + startButton.setEnabled(false); + stopButton.setEnabled(true); + + /* Start label update timer */ + if (!updateLabelTimer.isRunning()) { + updateLabelTimer.start(); } - }); + } else { + startButton.setEnabled(true); + stopButton.setEnabled(false); - // Register as simulation observer - simulation.addObserver(simObserver = new Observer() { - public void update(Observable obs, Object obj) { - if (simulation.isRunning()) { - startButton.setEnabled(false); - stopButton.setEnabled(true); - } else { - startButton.setEnabled(true); - stopButton.setEnabled(false); + /* Update simulation stop text field */ + if (!stopEvent.isScheduled()) { + stopTimeTextField.setValue(simulation.getSimulationTimeMillis()); + } + } - if (simulationStopTime < 0) { - stopTimeTextField.setValue(simulation.getSimulationTime()); - } - } - - if (sliderDelay != null) { - sliderDelay.setValue(convertTimeToSlide(simulation.getDelayTime())); - simulationTime.setText("Current simulation time: " + simulation.getSimulationTime()); - } + SwingUtilities.invokeLater(new Runnable() { + public void run() { + sliderDelay.setValue(convertTimeToSlide(simulation.getDelayTime())); + simulationTime.setText("Current simulation time: " + simulation.getSimulationTimeMillis()); + simulationTime.setToolTipText("Simulation time in microseconds: " + simulation.getSimulationTime()); } }); } }); - - + // Main panel JPanel controlPanel = new JPanel(); controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS)); @@ -177,20 +163,26 @@ public class SimControl extends VisPlugin { stopTimeTextField = new JFormattedTextField(integerFormat); stopTimeTextField.addPropertyChangeListener("value", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { + /* Remove already scheduled stop event */ + if (stopEvent.isScheduled()) { + stopEvent.remove(); + } + JFormattedTextField numberTextField = (JFormattedTextField) e.getSource(); - int untilTime = ((Number) numberTextField.getValue()).intValue(); - if (untilTime <= simulation.getSimulationTime()) { + long stopTime = ((Number) numberTextField.getValue()).intValue()*Simulation.MILLISECOND; + if (stopTime <= simulation.getSimulationTime()) { + /* No simulation stop scheduled */ numberTextField.setBackground(Color.LIGHT_GRAY); numberTextField.setToolTipText("Enter future simulation time"); - simulationStopTime = -1; } else { + /* Schedule simulation stop */ numberTextField.setBackground(Color.WHITE); - numberTextField.setToolTipText("Simulation will stop at time: " + untilTime); - simulationStopTime = untilTime; + numberTextField.setToolTipText("Simulation will stop at time (us): " + stopTime); + simulation.scheduleEvent(stopEvent, stopTime); } } }); - stopTimeTextField.setValue(simulation.getSimulationTime()); + stopTimeTextField.setValue(simulation.getSimulationTimeMillis()); stopTimeTextField.setSize(100, stopTimeTextField.getHeight()); smallPanel.add(stopTimeTextField); @@ -204,7 +196,7 @@ public class SimControl extends VisPlugin { smallPanel.setLayout(new BoxLayout(smallPanel, BoxLayout.X_AXIS)); smallPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); - label = new JLabel("Current simulation time: " + simulation.getSimulationTime()); + label = new JLabel("Current simulation time: " + simulation.getSimulationTimeMillis()); smallPanel.add(label); simulationTime = label; @@ -296,7 +288,7 @@ public class SimControl extends VisPlugin { simulation.stopSimulation(); } } else if (e.getActionCommand().equals("single_ms")) { - simulation.tickSimulation(); + simulation.stepMillisecondSimulation(); } else { logger.debug("Unhandled action: " + e.getActionCommand()); } @@ -304,14 +296,35 @@ public class SimControl extends VisPlugin { } MyEventHandler myEventHandler = new MyEventHandler(); public void closePlugin() { - // Remove log observer from all log interfaces + /* Remove simulation observer */ if (simObserver != null) { simulation.deleteObserver(simObserver); } - if (tickObserver != null) { - simulation.deleteTickObserver(tickObserver); + /* Remove stop event */ + if (stopEvent.isScheduled()) { + stopEvent.remove(); } + + /* Remove label update timer */ + updateLabelTimer.stop(); } + private TimeEvent stopEvent = new TimeEvent(0) { + public void execute(long t) { + /* Stop simulation */ + simulation.stopSimulation(); + } + }; + + private Timer updateLabelTimer = new Timer(LABEL_UPDATE_INTERVAL, new ActionListener() { + public void actionPerformed(ActionEvent e) { + simulationTime.setText("Current simulation time: " + simulation.getSimulationTimeMillis()); + + /* Automatically stop if simulation is no longer running */ + if (!simulation.isRunning()) { + updateLabelTimer.stop(); + } + } + }); } diff --git a/tools/cooja/java/se/sics/cooja/plugins/SimInformation.java b/tools/cooja/java/se/sics/cooja/plugins/SimInformation.java index 0c80c96d0..e12a5c93d 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/SimInformation.java +++ b/tools/cooja/java/se/sics/cooja/plugins/SimInformation.java @@ -26,14 +26,17 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: SimInformation.java,v 1.5 2009/03/10 21:20:30 fros4943 Exp $ + * $Id: SimInformation.java,v 1.6 2009/05/26 14:27:00 fros4943 Exp $ */ package se.sics.cooja.plugins; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; +import javax.swing.Timer; import se.sics.cooja.*; @@ -45,9 +48,10 @@ import se.sics.cooja.*; @ClassDescription("Simulation Information") @PluginType(PluginType.SIM_PLUGIN) public class SimInformation extends VisPlugin { - private static final long serialVersionUID = 1L; private Simulation simulation; + private static final int LABEL_UPDATE_INTERVAL = 100; + private final static int LABEL_WIDTH = 170; private final static int LABEL_HEIGHT = 15; @@ -57,7 +61,6 @@ public class SimInformation extends VisPlugin { private JLabel labelNrMoteTypes; private Observer simObserver; - private Observer tickObserver; /** * Create a new simulation information window. @@ -112,7 +115,7 @@ public class SimInformation extends VisPlugin { smallPane.add(Box.createHorizontalGlue()); label = new JLabel(); - label.setText("" + simulation.getSimulationTime()); + label.setText("" + simulation.getSimulationTimeMillis()); labelSimTime = label; smallPane.add(label); @@ -199,24 +202,20 @@ public class SimInformation extends VisPlugin { } if (simulation.isRunning()) { labelStatus.setText("RUNNING"); + updateLabelTimer.start(); } else { labelStatus.setText("STOPPED"); } labelNrMotes.setText("" + simulation.getMotesCount()); labelNrMoteTypes.setText("" + simulation.getMoteTypes().length); - - } - }); - - // Register as tick observer - simulation.addTickObserver(tickObserver = new Observer() { - public void update(Observable obs, Object obj) { - if (labelSimTime != null) { - labelSimTime.setText("" + simulation.getSimulationTime()); - } } }); + /* Update current time label when simulation is running */ + if (simulation.isRunning()) { + updateLabelTimer.start(); + } + try { setSelected(true); } catch (java.beans.PropertyVetoException e) { @@ -231,9 +230,19 @@ public class SimInformation extends VisPlugin { simulation.deleteObserver(simObserver); } - if (tickObserver != null) { - simulation.deleteTickObserver(tickObserver); - } + /* Remove label update timer */ + updateLabelTimer.stop(); } + private Timer updateLabelTimer = new Timer(LABEL_UPDATE_INTERVAL, new ActionListener() { + public void actionPerformed(ActionEvent e) { + labelSimTime.setText("" + simulation.getSimulationTimeMillis()); + + /* Automatically stop if simulation is no longer running */ + if (!simulation.isRunning()) { + updateLabelTimer.stop(); + } + } + }); + }