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
* 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();

View file

@ -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<VisualizerSkin> currentSkins = new ArrayList<VisualizerSkin>();
/* Generic visualization */
private Observer simObserver = null;
private MoteCountListener newMotesListener;
private Observer posObserver = null;
private Observer moteHighligtObserver = null;
private Vector<Mote> highlightedMotes = new Vector<Mote>();
@ -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);
}
}
}