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
This commit is contained in:
fros4943 2009-11-27 15:53:10 +00:00
parent a1b4597b95
commit 8bd7187491
7 changed files with 72 additions and 71 deletions

View File

@ -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<Element> 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<? extends MoteInterface> moteInterfaceClass = simulation.getGUI().tryLoadClass(
this, MoteInterface.class, element.getText().trim());
@ -259,16 +255,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote {
}
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
ArrayList<Element> config = new ArrayList<Element>();
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());

View File

@ -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<Element> 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<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
ArrayList<Element> config = new ArrayList<Element>();
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());

View File

@ -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<Element> moteXML = mote.getConfigXML();
if (moteXML != null) {
element.addContent(moteXML);
Collection<Element> moteConfig = mote.getConfigXML();
if (moteConfig == null) {
moteConfig = new ArrayList<Element>();
}
/* 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<? extends Mote> 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>) 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<? extends Mote> 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 {

View File

@ -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<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
ArrayList<Element> config = new ArrayList<Element>();
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<Element> 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<? extends MoteInterface> moteInterfaceClass =
simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim());

View File

@ -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

View File

@ -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<Element> config = new ArrayList<Element>();
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<Element> 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<? extends MoteInterface> moteInterfaceClass =
simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim());

View File

@ -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<Element> 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;