From 47bf1de4ca4b2b42e0a16a244eab5b8a776e3de1 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 2 Jul 2009 12:05:24 +0000 Subject: [PATCH] using event central for keeping track of simulation motes --- .../java/se/sics/cooja/plugins/TimeLine.java | 49 +++++------------- .../se/sics/cooja/plugins/Visualizer.java | 50 +++++++++++-------- 2 files changed, 42 insertions(+), 57 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/plugins/TimeLine.java b/tools/cooja/java/se/sics/cooja/plugins/TimeLine.java index 2f3f8b343..e58e754a8 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/TimeLine.java +++ b/tools/cooja/java/se/sics/cooja/plugins/TimeLine.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: TimeLine.java,v 1.10 2009/06/25 16:44:08 fros4943 Exp $ + * $Id: TimeLine.java,v 1.11 2009/07/02 12:05:24 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -81,6 +81,7 @@ import se.sics.cooja.Simulation; import se.sics.cooja.VisPlugin; import se.sics.cooja.Watchpoint; import se.sics.cooja.WatchpointMote; +import se.sics.cooja.SimEventCentral.MoteCountListener; import se.sics.cooja.interfaces.LED; import se.sics.cooja.interfaces.Radio; import se.sics.cooja.interfaces.Radio.RadioEvent; @@ -114,8 +115,7 @@ public class TimeLine extends VisPlugin { private int paintedMoteHeight = EVENT_PIXEL_HEIGHT; private Simulation simulation; - private Observer simulationObserver; - private int simulationObserverLast = -1; + private MoteCountListener newMotesListener; private JScrollPane timelineScrollPane; private MoteRuler timelineMoteRuler; @@ -242,39 +242,17 @@ public class TimeLine extends VisPlugin { numberMotesWasUpdated(); /* Automatically add/delete motes */ - simulation.addObserver(simulationObserver = new Observer() { - public void update(Observable obs, Object obj) { - if (simulation.getMotesCount() == simulationObserverLast) { - /* TODO Detect added/removed motes by event types, not mote counts */ - return; - } - simulationObserverLast = simulation.getMotesCount(); - - /* Unregister removed motes */ - Mote[] simMotes = simulation.getMotes(); - MoteEvents[] allMoteEventsArr = allMoteEvents.toArray(new MoteEvents[0]); - for (MoteEvents moteEvents: allMoteEventsArr) { - /* Check that mote still exists in simulation */ - boolean exists = false; - for (Mote existing: simMotes) { - if (existing == moteEvents.mote) { - exists = true; - break; - } - } - - if (!exists) { - removeMote(moteEvents.mote); - } - } - - /* Add all simulation motes */ - for (Mote m: simulation.getMotes()) { - addMote(m); - } + simulation.getEventCentral().addMoteCountListener(newMotesListener = new MoteCountListener() { + public void moteWasAdded(Mote mote) { + addMote(mote); + } + public void moteWasRemoved(Mote mote) { + removeMote(mote); } }); - simulationObserver.update(null, null); + for (Mote m: simulation.getMotes()) { + addMote(m); + } /* Update timeline for the duration of the plugin */ repaintTimelineTimer.start(); @@ -665,7 +643,7 @@ public class TimeLine extends VisPlugin { /* Remove repaint timer */ repaintTimelineTimer.stop(); - simulation.deleteObserver(simulationObserver); + simulation.getEventCentral().removeMoteCountListener(newMotesListener); /* Remove active mote interface observers */ for (MoteObservation o: activeMoteObservers) { @@ -740,7 +718,6 @@ public class TimeLine extends VisPlugin { for (MoteEvents moteEvents: allMoteEventsArr) { removeMote(moteEvents.mote); } - simulationObserverLast = simulation.getMotesCount(); for (Element element : configXML) { String name = element.getName(); diff --git a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java index ef2b04c7b..106591108 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Visualizer.java,v 1.7 2009/06/10 15:57:08 fros4943 Exp $ + * $Id: Visualizer.java,v 1.8 2009/07/02 12:05:54 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -55,6 +55,7 @@ import org.jdom.Element; import se.sics.cooja.*; import se.sics.cooja.GUI.MoteRelation; +import se.sics.cooja.SimEventCentral.MoteCountListener; import se.sics.cooja.interfaces.*; import se.sics.cooja.plugins.skins.AddressVisualizerSkin; import se.sics.cooja.plugins.skins.IDVisualizerSkin; @@ -117,7 +118,7 @@ public class Visualizer extends VisPlugin { private ArrayList currentSkins = new ArrayList(); /* Generic visualization */ - private Observer simObserver = null; + private MoteCountListener newMotesListener; private Observer posObserver = null; private Observer moteHighligtObserver = null; private Vector highlightedMotes = new Vector(); @@ -239,22 +240,32 @@ public class Visualizer extends VisPlugin { repaint(); } }; - simulation.addObserver(simObserver = new Observer() { - public void update(Observable obs, Object obj) { - - /* Observe mote positions */ - for (Mote mote: Visualizer.this.simulation.getMotes()) { - Position posIntf = mote.getInterfaces().getPosition(); - if (posIntf != null) { - posIntf.addObserver(posObserver); - } + simulation.getEventCentral().addMoteCountListener(newMotesListener = new MoteCountListener() { + public void moteWasAdded(Mote mote) { + Position pos = mote.getInterfaces().getPosition(); + if (pos != null) { + pos.addObserver(posObserver); + } + calculateTransformations(); + repaint(); + } + public void moteWasRemoved(Mote mote) { + Position pos = mote.getInterfaces().getPosition(); + if (pos != null) { + pos.deleteObserver(posObserver); } - calculateTransformations(); repaint(); } }); - simObserver.update(null, null); + for (Mote mote: simulation.getMotes()) { + Position pos = mote.getInterfaces().getPosition(); + if (pos != null) { + pos.addObserver(posObserver); + } + } + calculateTransformations(); + repaint(); /* Observe mote highlights */ gui.addMoteHighlightObserver(moteHighligtObserver = new Observer() { @@ -908,14 +919,11 @@ public class Visualizer extends VisPlugin { gui.deleteMoteRelationsObserver(moteRelationsObserver); } - if (simObserver != null) { - simulation.deleteObserver(simObserver); - - for (int i = 0; i < simulation.getMotesCount(); i++) { - Position posIntf = simulation.getMote(i).getInterfaces().getPosition(); - if (posIntf != null) { - posIntf.deleteObserver(posObserver); - } + simulation.getEventCentral().removeMoteCountListener(newMotesListener); + for (Mote mote: simulation.getMotes()) { + Position pos = mote.getInterfaces().getPosition(); + if (pos != null) { + pos.deleteObserver(posObserver); } } }