From 8bd7187491081d6c4b3a3eaf564576f5ad9ca7b9 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Fri, 27 Nov 2009 15:53:10 +0000 Subject: [PATCH] the simulation object now handles the motetype configuration for motes; common functionality that was earlier implemented in each mote. this patch allows the simulator more freedom for hand-picking mote class loaders + some debug output in experimental ImportAppMoteType --- .../src/se/sics/cooja/avrmote/MicaZMote.java | 30 +++++---------- .../src/se/sics/cooja/mspmote/MspMote.java | 30 +++++---------- .../cooja/java/se/sics/cooja/Simulation.java | 37 +++++++++++++++---- .../sics/cooja/contikimote/ContikiMote.java | 20 ++++------ .../java/se/sics/cooja/interfaces/MoteID.java | 6 +-- .../cooja/motes/AbstractApplicationMote.java | 12 ++---- .../sics/cooja/motes/ImportAppMoteType.java | 8 ++++ 7 files changed, 72 insertions(+), 71 deletions(-) diff --git a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java index 3bf397c1e..7e64202ae 100755 --- a/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java +++ b/tools/cooja/apps/avrora/src/se/sics/cooja/avrmote/MicaZMote.java @@ -26,14 +26,14 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MicaZMote.java,v 1.12 2009/11/17 14:30:26 joxe Exp $ + * $Id: MicaZMote.java,v 1.13 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja.avrmote; import java.io.File; +import java.util.ArrayList; import java.util.Collection; -import java.util.Vector; import org.apache.log4j.Logger; import org.jdom.Element; @@ -52,9 +52,7 @@ import avrora.sim.Simulator; import avrora.sim.State; import avrora.sim.mcu.AtmelMicrocontroller; import avrora.sim.mcu.EEPROM; -import avrora.sim.mcu.Microcontroller; import avrora.sim.platform.MicaZ; -import avrora.sim.platform.Platform; import avrora.sim.platform.PlatformFactory; /** @@ -183,6 +181,7 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { } public void setType(MoteType type) { + myMoteType = (MicaZMoteType) type; } public MoteInterfaceHandler getInterfaces() { @@ -227,18 +226,15 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { } public boolean setConfigXML(Simulation simulation, Collection configXML, boolean visAvailable) { + setSimulation(simulation); + initEmulator(myMoteType.getContikiFirmwareFile()); + myMoteInterfaceHandler = createMoteInterfaceHandler(); + for (Element element: configXML) { String name = element.getName(); if (name.equals("motetype_identifier")) { - - setSimulation(simulation); - myMoteType = (MicaZMoteType) simulation.getMoteType(element.getText()); - getType().setIdentifier(element.getText()); - - initEmulator(myMoteType.getContikiFirmwareFile()); - myMoteInterfaceHandler = createMoteInterfaceHandler(); - + /* Ignored: handled by simulation */ } else if (name.equals("interface_config")) { Class moteInterfaceClass = simulation.getGUI().tryLoadClass( this, MoteInterface.class, element.getText().trim()); @@ -259,16 +255,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { } public Collection getConfigXML() { - Vector config = new Vector(); - + ArrayList config = new ArrayList(); Element element; - // Mote type identifier - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - - // Mote interfaces + /* Mote interfaces */ for (MoteInterface moteInterface: getInterfaces().getInterfaces()) { element = new Element("interface_config"); element.setText(moteInterface.getClass().getName()); diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java index a5735fd27..0f753915e 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MspMote.java,v 1.34 2009/10/27 10:02:48 fros4943 Exp $ + * $Id: MspMote.java,v 1.35 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja.mspmote; @@ -40,7 +40,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Hashtable; import java.util.Observable; -import java.util.Vector; import org.apache.log4j.Logger; import org.jdom.Element; @@ -395,21 +394,18 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc } public boolean setConfigXML(Simulation simulation, Collection configXML, boolean visAvailable) { + setSimulation(simulation); + initEmulator(myMoteType.getContikiFirmwareFile()); + myMoteInterfaceHandler = createMoteInterfaceHandler(); + + /* Create watchpoint container */ + breakpointsContainer = new MspBreakpointContainer(this, getFirmwareDebugInfo(this)); + for (Element element: configXML) { String name = element.getName(); if (name.equals("motetype_identifier")) { - - setSimulation(simulation); - myMoteType = (MspMoteType) simulation.getMoteType(element.getText()); - getType().setIdentifier(element.getText()); - - initEmulator(myMoteType.getContikiFirmwareFile()); - myMoteInterfaceHandler = createMoteInterfaceHandler(); - - /* Create watchpoint container */ - breakpointsContainer = new MspBreakpointContainer(this, getFirmwareDebugInfo(this)); - + /* Ignored: handled by simulation */ } else if ("breakpoints".equals(element.getName())) { breakpointsContainer.setConfigXML(element.getChildren(), visAvailable); } else if (name.equals("interface_config")) { @@ -436,15 +432,9 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc } public Collection getConfigXML() { - Vector config = new Vector(); - + ArrayList config = new ArrayList(); Element element; - // Mote type identifier - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - /* Breakpoints */ element = new Element("breakpoints"); element.addContent(breakpointsContainer.getConfigXML()); diff --git a/tools/cooja/java/se/sics/cooja/Simulation.java b/tools/cooja/java/se/sics/cooja/Simulation.java index 38bc18ebe..9704cdb8c 100644 --- a/tools/cooja/java/se/sics/cooja/Simulation.java +++ b/tools/cooja/java/se/sics/cooja/Simulation.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: Simulation.java,v 1.55 2009/11/25 20:47:19 fros4943 Exp $ + * $Id: Simulation.java,v 1.56 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja; @@ -484,10 +484,17 @@ public class Simulation extends Observable implements Runnable { element = new Element("mote"); element.setText(mote.getClass().getName()); - Collection moteXML = mote.getConfigXML(); - if (moteXML != null) { - element.addContent(moteXML); + Collection moteConfig = mote.getConfigXML(); + if (moteConfig == null) { + moteConfig = new ArrayList(); } + + /* Add mote type identifier */ + Element typeIdentifier = new Element("motetype_identifier"); + typeIdentifier.setText(mote.getType().getIdentifier()); + moteConfig.add(typeIdentifier); + + element.addContent(moteConfig); config.add(element); } @@ -606,14 +613,30 @@ public class Simulation extends Observable implements Runnable { } } - // Mote + /* Mote */ if (element.getName().equals("mote")) { - Class moteClass = myGUI.tryLoadClass(this, Mote.class, - element.getText().trim()); + String moteClassName = element.getText().trim(); + + /* Read mote type identifier */ + MoteType moteType = null; + for (Element subElement: (Collection) element.getChildren()) { + if (subElement.getName().equals("motetype_identifier")) { + moteType = getMoteType(subElement.getText()); + break; + } + } + if (moteType == null) { + throw new Exception("No mote type for mote: " + moteClassName); + } + + /* Load mote class using mote type's class loader */ + Class moteClass = myGUI.tryLoadClass(moteType, Mote.class, moteClassName); if (moteClass == null) { throw new Exception("Could not load mote class: " + element.getText().trim()); } + Mote mote = moteClass.getConstructor((Class[]) null).newInstance((Object[]) null); + mote.setType(moteType); if (mote.setConfigXML(this, element.getChildren(), visAvailable)) { addMote(mote); } else { diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java index 0297beedb..38109081f 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java @@ -26,11 +26,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiMote.java,v 1.15 2009/10/27 10:12:33 fros4943 Exp $ + * $Id: ContikiMote.java,v 1.16 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja.contikimote; +import java.util.ArrayList; import java.util.Collection; import java.util.Vector; @@ -174,16 +175,10 @@ public class ContikiMote extends AbstractWakeupMote implements Mote { * @return Current simulation config */ public Collection getConfigXML() { - Vector config = new Vector(); - + ArrayList config = new ArrayList(); Element element; - // Mote type identifier - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - - // Mote interfaces + /* Mote interfaces */ for (MoteInterface moteInterface: getInterfaces().getInterfaces()) { element = new Element("interface_config"); element.setText(moteInterface.getClass().getName()); @@ -200,15 +195,14 @@ public class ContikiMote extends AbstractWakeupMote implements Mote { public boolean setConfigXML(Simulation simulation, Collection configXML, boolean visAvailable) { this.simulation = simulation; + myMemory = myType.createInitialMemory(); + myInterfaceHandler = new MoteInterfaceHandler(this, myType.getMoteInterfaceClasses()); for (Element element: configXML) { String name = element.getName(); if (name.equals("motetype_identifier")) { - myType = (ContikiMoteType) simulation.getMoteType(element.getText()); - myMemory = myType.createInitialMemory(); - myInterfaceHandler = new MoteInterfaceHandler(this, myType.getMoteInterfaceClasses()); - + /* Ignored: handled by simulation */ } else if (name.equals("interface_config")) { Class moteInterfaceClass = simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim()); diff --git a/tools/cooja/java/se/sics/cooja/interfaces/MoteID.java b/tools/cooja/java/se/sics/cooja/interfaces/MoteID.java index 05ab4f33d..846d9469d 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/MoteID.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/MoteID.java @@ -26,18 +26,18 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MoteID.java,v 1.2 2009/10/28 14:35:10 fros4943 Exp $ + * $Id: MoteID.java,v 1.3 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja.interfaces; import java.util.ArrayList; import java.util.Collection; -import java.util.Vector; import org.jdom.Element; -import se.sics.cooja.*; +import se.sics.cooja.ClassDescription; +import se.sics.cooja.MoteInterface; /** * A MoteID represents a mote ID number. An implementation should notify all diff --git a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java index 61b98b45a..e00df375c 100644 --- a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java +++ b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: AbstractApplicationMote.java,v 1.6 2009/10/28 14:38:02 fros4943 Exp $ + * $Id: AbstractApplicationMote.java,v 1.7 2009/11/27 15:53:10 fros4943 Exp $ */ package se.sics.cooja.motes; @@ -130,10 +130,6 @@ public abstract class AbstractApplicationMote extends AbstractWakeupMote impleme ArrayList config = new ArrayList(); Element element; - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - for (MoteInterface moteInterface: moteInterfaces.getInterfaces()) { element = new Element("interface_config"); element.setText(moteInterface.getClass().getName()); @@ -152,14 +148,14 @@ public abstract class AbstractApplicationMote extends AbstractWakeupMote impleme Collection configXML, boolean visAvailable) { this.simulation = simulation; this.memory = new SectionMoteMemory(new Properties()); + moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses()); + moteInterfaces.getRadio().addObserver(radioDataObserver); for (Element element : configXML) { String name = element.getName(); if (name.equals("motetype_identifier")) { - moteType = simulation.getMoteType(element.getText()); - moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses()); - moteInterfaces.getRadio().addObserver(radioDataObserver); + /* Ignored: handled by simulation */ } else if (name.equals("interface_config")) { Class moteInterfaceClass = simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim()); diff --git a/tools/cooja/java/se/sics/cooja/motes/ImportAppMoteType.java b/tools/cooja/java/se/sics/cooja/motes/ImportAppMoteType.java index fcbccce4f..f08dd9d3b 100644 --- a/tools/cooja/java/se/sics/cooja/motes/ImportAppMoteType.java +++ b/tools/cooja/java/se/sics/cooja/motes/ImportAppMoteType.java @@ -36,6 +36,7 @@ import java.io.File; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Collection; +import java.util.Random; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; @@ -75,12 +76,15 @@ public class ImportAppMoteType extends AbstractApplicationMoteType { } private class TestClassLoader extends ClassLoader { + private int id; /* DEBUG */ private File file; public TestClassLoader(File f) { + id = new Random().nextInt(); file = f; } public TestClassLoader(ClassLoader parent, File f) { super(parent); + id = new Random().nextInt(); file = f; } public Class findClass(String name) { @@ -90,6 +94,9 @@ public class ImportAppMoteType extends AbstractApplicationMoteType { private byte[] loadClassData(String name) { return ArrayUtils.readFromFile(file); } + public String toString() { + return "ImportAppMoteType classloader #" + id; + } } public Collection getConfigXML() { @@ -200,6 +207,7 @@ public class ImportAppMoteType extends AbstractApplicationMoteType { new TestClassLoader(simulation.getGUI().projectDirClassLoader, moteClassFile); } + logger.info(moteClass.getClassLoader()); setDescription("Imported Mote Type #" + moteClassFile.getName()); return true;