From 902200f12a9e18ea3291812fd85cda4e6d31d0b2 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Fri, 9 Mar 2012 14:57:10 +0100 Subject: [PATCH] using memory monitor instead of previously used time events --- .../se/sics/cooja/interfaces/RimeAddress.java | 54 +++++++++---------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/interfaces/RimeAddress.java b/tools/cooja/java/se/sics/cooja/interfaces/RimeAddress.java index 478b9a6a5..b7a8907ff 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/RimeAddress.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/RimeAddress.java @@ -41,13 +41,12 @@ import javax.swing.JPanel; import org.apache.log4j.Logger; import org.jdom.Element; -import se.sics.cooja.AddressMemory; import se.sics.cooja.ClassDescription; import se.sics.cooja.Mote; import se.sics.cooja.MoteInterface; -import se.sics.cooja.MoteTimeEvent; -import se.sics.cooja.Simulation; -import se.sics.cooja.TimeEvent; +import se.sics.cooja.MoteMemory; +import se.sics.cooja.MoteMemory.MemoryEventType; +import se.sics.cooja.MoteMemory.MemoryMonitor; /** * 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") public class RimeAddress extends MoteInterface { private static Logger logger = Logger.getLogger(RimeAddress.class); - private AddressMemory moteMem; + private MoteMemory moteMem; public static final int RIME_ADDR_LENGTH = 2; - + + private MemoryMonitor memMonitor = null; + public RimeAddress(final Mote mote) { - moteMem = (AddressMemory) mote.getMemory(); - - /* Detect startup address (only zeroes) */ - TimeEvent updateWhenAddressReady = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - if (!hasRimeAddress()) { - return; - } - - String addrString = getAddressString(); - addrString = addrString.replace(".", ""); - addrString = addrString.replace("0", ""); - if (!addrString.isEmpty()) { + moteMem = mote.getMemory(); + if (hasRimeAddress()) { + memMonitor = new MemoryMonitor() { + public void memoryChanged(MoteMemory memory, MemoryEventType type, int address) { + if (type != MemoryEventType.WRITE) { + return; + } setChanged(); 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() { @@ -140,6 +129,13 @@ public class RimeAddress extends MoteInterface { this.deleteObserver(observer); } + public void removed() { + super.removed(); + if (memMonitor != null) { + moteMem.removeMemoryMonitor(moteMem.getVariableAddress("rimeaddr_node_addr"), RIME_ADDR_LENGTH, memMonitor); + } + } + public Collection getConfigXML() { return null; }