improved code: uses log mote interfaces observers directly, instead of going via the sim event central
This commit is contained in:
parent
5288ac12b5
commit
bef1a013f1
|
@ -48,8 +48,7 @@ import se.sics.cooja.ClassDescription;
|
||||||
import se.sics.cooja.GUI;
|
import se.sics.cooja.GUI;
|
||||||
import se.sics.cooja.Mote;
|
import se.sics.cooja.Mote;
|
||||||
import se.sics.cooja.MoteInterface;
|
import se.sics.cooja.MoteInterface;
|
||||||
import se.sics.cooja.SimEventCentral.LogOutputEvent;
|
import se.sics.cooja.SimEventCentral.MoteCountListener;
|
||||||
import se.sics.cooja.SimEventCentral.LogOutputListener;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mote2Mote Relations is used to show mote relations in simulated
|
* 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 ArrayList<Mote> relations = new ArrayList<Mote>();
|
||||||
private GUI gui;
|
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) {
|
public Mote2MoteRelations(Mote mote) {
|
||||||
this.mote = mote;
|
this.mote = mote;
|
||||||
this.gui = mote.getSimulation().getGUI();
|
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) {
|
public void moteWasAdded(Mote mote) {
|
||||||
/* Ignored */
|
/* Ignored */
|
||||||
}
|
}
|
||||||
|
@ -106,28 +124,28 @@ public class Mote2MoteRelations extends MoteInterface {
|
||||||
relations.remove(mote);
|
relations.remove(mote);
|
||||||
gui.removeMoteRelation(Mote2MoteRelations.this.mote, 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() {
|
public void removed() {
|
||||||
super.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]);
|
Mote[] relationsArr = relations.toArray(new Mote[0]);
|
||||||
for (Mote m: relationsArr) {
|
for (Mote m: relationsArr) {
|
||||||
gui.removeMoteRelation(Mote2MoteRelations.this.mote, m);
|
gui.removeMoteRelation(Mote2MoteRelations.this.mote, m);
|
||||||
}
|
}
|
||||||
relations.clear();
|
relations.clear();
|
||||||
|
|
||||||
mote.getSimulation().getEventCentral().removeLogOutputListener(logListener);
|
mote.getSimulation().getEventCentral().removeMoteCountListener(moteCountListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleNewLog(String msg) {
|
private void handleNewLog(String msg) {
|
||||||
|
|
|
@ -46,8 +46,6 @@ import org.jdom.Element;
|
||||||
import se.sics.cooja.ClassDescription;
|
import se.sics.cooja.ClassDescription;
|
||||||
import se.sics.cooja.Mote;
|
import se.sics.cooja.Mote;
|
||||||
import se.sics.cooja.MoteInterface;
|
import se.sics.cooja.MoteInterface;
|
||||||
import se.sics.cooja.SimEventCentral.LogOutputEvent;
|
|
||||||
import se.sics.cooja.SimEventCentral.LogOutputListener;
|
|
||||||
import se.sics.cooja.plugins.skins.AttributeVisualizerSkin;
|
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 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) {
|
public MoteAttributes(Mote mote) {
|
||||||
this.mote = mote;
|
this.mote = mote;
|
||||||
|
}
|
||||||
|
|
||||||
mote.getSimulation().getEventCentral().addLogOutputListener(logListener = new LogOutputListener() {
|
public void added() {
|
||||||
public void moteWasAdded(Mote mote) {
|
super.added();
|
||||||
/* Ignored */
|
|
||||||
|
/* 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() {
|
public void removed() {
|
||||||
super.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) {
|
private void handleNewLog(String msg) {
|
||||||
|
|
Loading…
Reference in a new issue