using memory monitor instead of previously used time events

This commit is contained in:
Fredrik Osterlind 2012-03-09 14:57:10 +01:00
parent 85323adc1e
commit 902200f12a

View file

@ -41,13 +41,12 @@ import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.AddressMemory;
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.MoteTimeEvent; import se.sics.cooja.MoteMemory;
import se.sics.cooja.Simulation; import se.sics.cooja.MoteMemory.MemoryEventType;
import se.sics.cooja.TimeEvent; import se.sics.cooja.MoteMemory.MemoryMonitor;
/** /**
* Read-only interface to Rime address read from Contiki variable: rimeaddr_node_addr. * Read-only interface to Rime address read from Contiki variable: rimeaddr_node_addr.
@ -59,37 +58,27 @@ import se.sics.cooja.TimeEvent;
@ClassDescription("Rime address") @ClassDescription("Rime address")
public class RimeAddress extends MoteInterface { public class RimeAddress extends MoteInterface {
private static Logger logger = Logger.getLogger(RimeAddress.class); private static Logger logger = Logger.getLogger(RimeAddress.class);
private AddressMemory moteMem; private MoteMemory moteMem;
public static final int RIME_ADDR_LENGTH = 2; public static final int RIME_ADDR_LENGTH = 2;
private MemoryMonitor memMonitor = null;
public RimeAddress(final Mote mote) { public RimeAddress(final Mote mote) {
moteMem = (AddressMemory) mote.getMemory(); moteMem = mote.getMemory();
if (hasRimeAddress()) {
/* Detect startup address (only zeroes) */ memMonitor = new MemoryMonitor() {
TimeEvent updateWhenAddressReady = new MoteTimeEvent(mote, 0) { public void memoryChanged(MoteMemory memory, MemoryEventType type, int address) {
public void execute(long t) { if (type != MemoryEventType.WRITE) {
if (!hasRimeAddress()) { return;
return; }
}
String addrString = getAddressString();
addrString = addrString.replace(".", "");
addrString = addrString.replace("0", "");
if (!addrString.isEmpty()) {
setChanged(); setChanged();
notifyObservers(); notifyObservers();
return;
} }
};
/* Postpone until address has been set */ /* TODO XXX Timeout? */
mote.getSimulation().scheduleEvent( moteMem.addMemoryMonitor(moteMem.getVariableAddress("rimeaddr_node_addr"), RIME_ADDR_LENGTH, memMonitor);
this, }
mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
return;
}
};
updateWhenAddressReady.execute(0);
} }
public boolean hasRimeAddress() { public boolean hasRimeAddress() {
@ -140,6 +129,13 @@ public class RimeAddress extends MoteInterface {
this.deleteObserver(observer); this.deleteObserver(observer);
} }
public void removed() {
super.removed();
if (memMonitor != null) {
moteMem.removeMemoryMonitor(moteMem.getVariableAddress("rimeaddr_node_addr"), RIME_ADDR_LENGTH, memMonitor);
}
}
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {
return null; return null;
} }