improved code: uses log mote interfaces observers directly, instead of going via the sim event central

This commit is contained in:
Fredrik Osterlind 2012-03-06 14:30:39 +01:00
parent 5288ac12b5
commit bef1a013f1
2 changed files with 60 additions and 39 deletions

View file

@ -48,8 +48,7 @@ import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface;
import se.sics.cooja.SimEventCentral.LogOutputEvent;
import se.sics.cooja.SimEventCentral.LogOutputListener;
import se.sics.cooja.SimEventCentral.MoteCountListener;
/**
* Mote2Mote Relations is used to show mote relations in simulated
@ -83,13 +82,32 @@ public class Mote2MoteRelations extends MoteInterface {
private ArrayList<Mote> relations = new ArrayList<Mote>();
private GUI gui;
private LogOutputListener logListener;
private Observer logObserver = new Observer() {
public void update(Observable o, Object arg) {
String msg = ((Log) o).getLastLogMessage();
handleNewLog(msg);
};
};
private MoteCountListener moteCountListener;
public Mote2MoteRelations(Mote mote) {
this.mote = mote;
this.gui = mote.getSimulation().getGUI();
}
mote.getSimulation().getEventCentral().addLogOutputListener(logListener = new LogOutputListener() {
public void added() {
super.added();
/* Observe log interfaces */
for (MoteInterface mi: mote.getInterfaces().getInterfaces()) {
if (mi instanceof Log) {
((Log)mi).addObserver(logObserver);
}
}
/* Observe other motes: if removed, remove our relations to them too */
mote.getSimulation().getEventCentral().addMoteCountListener(moteCountListener = new MoteCountListener() {
public void moteWasAdded(Mote mote) {
/* Ignored */
}
@ -106,28 +124,28 @@ public class Mote2MoteRelations extends MoteInterface {
relations.remove(mote);
gui.removeMoteRelation(Mote2MoteRelations.this.mote, mote);
}
public void newLogOutput(LogOutputEvent ev) {
if (ev.getMote() != Mote2MoteRelations.this.mote) {
return;
}
handleNewLog(ev.msg);
}
public void removedLogOutput(LogOutputEvent ev) {
/* Ignored */
}
});
}
public void removed() {
super.removed();
/* Stop observing log interfaces */
for (MoteInterface mi: mote.getInterfaces().getInterfaces()) {
if (mi instanceof Log) {
((Log)mi).deleteObserver(logObserver);
}
}
logObserver = null;
/* Remove all relations to other motes */
Mote[] relationsArr = relations.toArray(new Mote[0]);
for (Mote m: relationsArr) {
gui.removeMoteRelation(Mote2MoteRelations.this.mote, m);
}
relations.clear();
mote.getSimulation().getEventCentral().removeLogOutputListener(logListener);
mote.getSimulation().getEventCentral().removeMoteCountListener(moteCountListener);
}
private void handleNewLog(String msg) {

View file

@ -46,8 +46,6 @@ import org.jdom.Element;
import se.sics.cooja.ClassDescription;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface;
import se.sics.cooja.SimEventCentral.LogOutputEvent;
import se.sics.cooja.SimEventCentral.LogOutputListener;
import se.sics.cooja.plugins.skins.AttributeVisualizerSkin;
/**
@ -86,33 +84,38 @@ public class MoteAttributes extends MoteInterface {
private HashMap<String, Object> attributes = new HashMap<String, Object>();
private LogOutputListener logListener;
private Observer logObserver = new Observer() {
public void update(Observable o, Object arg) {
String msg = ((Log) o).getLastLogMessage();
handleNewLog(msg);
};
};
public MoteAttributes(Mote mote) {
this.mote = mote;
}
mote.getSimulation().getEventCentral().addLogOutputListener(logListener = new LogOutputListener() {
public void moteWasAdded(Mote mote) {
/* Ignored */
public void added() {
super.added();
/* Observe log interfaces */
for (MoteInterface mi: mote.getInterfaces().getInterfaces()) {
if (mi instanceof Log) {
((Log)mi).addObserver(logObserver);
}
public void moteWasRemoved(Mote mote) {
/* Ignored */
}
public void newLogOutput(LogOutputEvent ev) {
if (ev.getMote() != MoteAttributes.this.mote) {
return;
}
handleNewLog(ev.msg);
}
public void removedLogOutput(LogOutputEvent ev) {
/* Ignored */
}
});
}
}
public void removed() {
super.removed();
mote.getSimulation().getEventCentral().removeLogOutputListener(logListener);
/* Stop observing log interfaces */
for (MoteInterface mi: mote.getInterfaces().getInterfaces()) {
if (mi instanceof Log) {
((Log)mi).deleteObserver(logObserver);
}
}
logObserver = null;
}
private void handleNewLog(String msg) {