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;
public RimeAddress(final Mote mote) { private MemoryMonitor memMonitor = null;
moteMem = (AddressMemory) mote.getMemory();
/* Detect startup address (only zeroes) */ public RimeAddress(final Mote mote) {
TimeEvent updateWhenAddressReady = new MoteTimeEvent(mote, 0) { moteMem = mote.getMemory();
public void execute(long t) { if (hasRimeAddress()) {
if (!hasRimeAddress()) { memMonitor = new MemoryMonitor() {
public void memoryChanged(MoteMemory memory, MemoryEventType type, int address) {
if (type != MemoryEventType.WRITE) {
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 */
mote.getSimulation().scheduleEvent(
this,
mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
return;
} }
}; };
updateWhenAddressReady.execute(0); /* TODO XXX Timeout? */
moteMem.addMemoryMonitor(moteMem.getVariableAddress("rimeaddr_node_addr"), RIME_ADDR_LENGTH, memMonitor);
}
} }
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;
} }