using event central for keeping track of simulation motes

This commit is contained in:
fros4943 2009-07-02 12:05:24 +00:00
parent 9b164adc35
commit 47bf1de4ca
2 changed files with 42 additions and 57 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.plugins;
@ -81,6 +81,7 @@ import se.sics.cooja.Simulation;
import se.sics.cooja.VisPlugin; import se.sics.cooja.VisPlugin;
import se.sics.cooja.Watchpoint; import se.sics.cooja.Watchpoint;
import se.sics.cooja.WatchpointMote; import se.sics.cooja.WatchpointMote;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.interfaces.LED; import se.sics.cooja.interfaces.LED;
import se.sics.cooja.interfaces.Radio; import se.sics.cooja.interfaces.Radio;
import se.sics.cooja.interfaces.Radio.RadioEvent; import se.sics.cooja.interfaces.Radio.RadioEvent;
@ -114,8 +115,7 @@ public class TimeLine extends VisPlugin {
private int paintedMoteHeight = EVENT_PIXEL_HEIGHT; private int paintedMoteHeight = EVENT_PIXEL_HEIGHT;
private Simulation simulation; private Simulation simulation;
private Observer simulationObserver; private MoteCountListener newMotesListener;
private int simulationObserverLast = -1;
private JScrollPane timelineScrollPane; private JScrollPane timelineScrollPane;
private MoteRuler timelineMoteRuler; private MoteRuler timelineMoteRuler;
@ -242,39 +242,17 @@ public class TimeLine extends VisPlugin {
numberMotesWasUpdated(); numberMotesWasUpdated();
/* Automatically add/delete motes */ /* Automatically add/delete motes */
simulation.addObserver(simulationObserver = new Observer() { simulation.getEventCentral().addMoteCountListener(newMotesListener = new MoteCountListener() {
public void update(Observable obs, Object obj) { public void moteWasAdded(Mote mote) {
if (simulation.getMotesCount() == simulationObserverLast) { addMote(mote);
/* TODO Detect added/removed motes by event types, not mote counts */ }
return; public void moteWasRemoved(Mote mote) {
} removeMote(mote);
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);
}
} }
}); });
simulationObserver.update(null, null); for (Mote m: simulation.getMotes()) {
addMote(m);
}
/* Update timeline for the duration of the plugin */ /* Update timeline for the duration of the plugin */
repaintTimelineTimer.start(); repaintTimelineTimer.start();
@ -665,7 +643,7 @@ public class TimeLine extends VisPlugin {
/* Remove repaint timer */ /* Remove repaint timer */
repaintTimelineTimer.stop(); repaintTimelineTimer.stop();
simulation.deleteObserver(simulationObserver); simulation.getEventCentral().removeMoteCountListener(newMotesListener);
/* Remove active mote interface observers */ /* Remove active mote interface observers */
for (MoteObservation o: activeMoteObservers) { for (MoteObservation o: activeMoteObservers) {
@ -740,7 +718,6 @@ public class TimeLine extends VisPlugin {
for (MoteEvents moteEvents: allMoteEventsArr) { for (MoteEvents moteEvents: allMoteEventsArr) {
removeMote(moteEvents.mote); removeMote(moteEvents.mote);
} }
simulationObserverLast = simulation.getMotesCount();
for (Element element : configXML) { for (Element element : configXML) {
String name = element.getName(); String name = element.getName();

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.plugins;
@ -55,6 +55,7 @@ import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.GUI.MoteRelation; import se.sics.cooja.GUI.MoteRelation;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.interfaces.*; import se.sics.cooja.interfaces.*;
import se.sics.cooja.plugins.skins.AddressVisualizerSkin; import se.sics.cooja.plugins.skins.AddressVisualizerSkin;
import se.sics.cooja.plugins.skins.IDVisualizerSkin; import se.sics.cooja.plugins.skins.IDVisualizerSkin;
@ -117,7 +118,7 @@ public class Visualizer extends VisPlugin {
private ArrayList<VisualizerSkin> currentSkins = new ArrayList<VisualizerSkin>(); private ArrayList<VisualizerSkin> currentSkins = new ArrayList<VisualizerSkin>();
/* Generic visualization */ /* Generic visualization */
private Observer simObserver = null; private MoteCountListener newMotesListener;
private Observer posObserver = null; private Observer posObserver = null;
private Observer moteHighligtObserver = null; private Observer moteHighligtObserver = null;
private Vector<Mote> highlightedMotes = new Vector<Mote>(); private Vector<Mote> highlightedMotes = new Vector<Mote>();
@ -239,22 +240,32 @@ public class Visualizer extends VisPlugin {
repaint(); repaint();
} }
}; };
simulation.addObserver(simObserver = new Observer() { simulation.getEventCentral().addMoteCountListener(newMotesListener = new MoteCountListener() {
public void update(Observable obs, Object obj) { public void moteWasAdded(Mote mote) {
Position pos = mote.getInterfaces().getPosition();
/* Observe mote positions */ if (pos != null) {
for (Mote mote: Visualizer.this.simulation.getMotes()) { pos.addObserver(posObserver);
Position posIntf = mote.getInterfaces().getPosition(); }
if (posIntf != null) { calculateTransformations();
posIntf.addObserver(posObserver); repaint();
} }
public void moteWasRemoved(Mote mote) {
Position pos = mote.getInterfaces().getPosition();
if (pos != null) {
pos.deleteObserver(posObserver);
} }
calculateTransformations(); calculateTransformations();
repaint(); 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 */ /* Observe mote highlights */
gui.addMoteHighlightObserver(moteHighligtObserver = new Observer() { gui.addMoteHighlightObserver(moteHighligtObserver = new Observer() {
@ -908,14 +919,11 @@ public class Visualizer extends VisPlugin {
gui.deleteMoteRelationsObserver(moteRelationsObserver); gui.deleteMoteRelationsObserver(moteRelationsObserver);
} }
if (simObserver != null) { simulation.getEventCentral().removeMoteCountListener(newMotesListener);
simulation.deleteObserver(simObserver); for (Mote mote: simulation.getMotes()) {
Position pos = mote.getInterfaces().getPosition();
for (int i = 0; i < simulation.getMotesCount(); i++) { if (pos != null) {
Position posIntf = simulation.getMote(i).getInterfaces().getPosition(); pos.deleteObserver(posObserver);
if (posIntf != null) {
posIntf.deleteObserver(posObserver);
}
} }
} }
} }