From 74647ce2de265b95759f1d39bf41c76f18180214 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Mon, 9 Mar 2009 15:38:10 +0000 Subject: [PATCH] application-level mote type implementing new mote type methods + radio disturber mote extending abstract application mote --- .../cooja/motes/AbstractApplicationMote.java | 111 ++++----- .../motes/AbstractApplicationMoteType.java | 86 ++++--- .../se/sics/cooja/motes/DisturberMote.java | 182 +------------- .../sics/cooja/motes/DisturberMoteType.java | 210 +--------------- .../se/sics/cooja/motes/DisturberRadio.java | 20 +- .../java/se/sics/cooja/motes/DummyMote.java | 232 +++--------------- .../se/sics/cooja/motes/DummyMoteType.java | 116 +-------- 7 files changed, 177 insertions(+), 780 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMote.java index e74ae0750..d58da3d17 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.3 2008/10/28 13:38:55 fros4943 Exp $ + * $Id: AbstractApplicationMote.java,v 1.4 2009/03/09 15:38:10 fros4943 Exp $ */ package se.sics.cooja.motes; @@ -35,8 +35,6 @@ import org.apache.log4j.Logger; import org.jdom.Element; import se.sics.cooja.*; -import se.sics.cooja.interfaces.ApplicationRadio; -import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Radio; /** @@ -50,55 +48,47 @@ public abstract class AbstractApplicationMote implements Mote { private static Logger logger = Logger.getLogger(AbstractApplicationMote.class); - private MoteType myType = null; + private MoteType moteType = null; - private SectionMoteMemory myMemory = null; + private SectionMoteMemory memory = null; - protected MoteInterfaceHandler myInterfaceHandler = null; + protected MoteInterfaceHandler moteInterfaces = null; - private Simulation mySim = null; - - protected ApplicationRadio myApplicationRadio; + private Simulation simulation = null; private Observer radioDataObserver = new Observer() { public void update(Observable obs, Object obj) { - handleNewRadioData(obs, obj); + if (getInterfaces().getRadio().getLastEvent() != Radio.RadioEvent.RECEPTION_FINISHED) { + return; + } + + /* Called at incoming data packets */ + logger.info("Application mote received radio data:"); + byte[] packet = getInterfaces().getRadio().getLastPacketReceived().getPacketData(); + String data = ""; + for (byte b: packet) { + data += (char)b; + } + logger.info(data); } }; - public void handleNewRadioData(Observable obs, Object obj) { - if (myApplicationRadio.getLastEvent() != Radio.RadioEvent.RECEPTION_FINISHED) { - return; - } - - logger.info("Application mote received radio data:"); - byte[] packet = myApplicationRadio.getLastPacketReceived().getPacketData(); - String data = ""; - for (byte b: packet) { - data += (char)b; - } - logger.info(data); - } - public AbstractApplicationMote() { } public AbstractApplicationMote(MoteType moteType, Simulation sim) { - mySim = sim; - myType = moteType; + this.simulation = sim; + this.moteType = moteType; // Create memory - myMemory = new SectionMoteMemory(new Properties()); + this.memory = new SectionMoteMemory(new Properties()); - // Create position - myInterfaceHandler = new MoteInterfaceHandler(); - Position myPosition = new Position(this); - myInterfaceHandler.addInterface(myPosition); + // Create mote interfaces + this.moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses()); - // Create radio - myApplicationRadio = new ApplicationRadio(this); - myApplicationRadio.addObserver(radioDataObserver); - myInterfaceHandler.addInterface(myApplicationRadio); + if (moteInterfaces.getRadio() != null) { + moteInterfaces.getRadio().addObserver(radioDataObserver); + } } public void setState(State newState) { @@ -116,35 +106,46 @@ public abstract class AbstractApplicationMote implements Mote { } public MoteInterfaceHandler getInterfaces() { - return myInterfaceHandler; + return moteInterfaces; } public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) { - myInterfaceHandler = moteInterfaceHandler; + moteInterfaces = moteInterfaceHandler; } public MoteMemory getMemory() { - return myMemory; + return memory; } public void setMemory(MoteMemory memory) { - myMemory = (SectionMoteMemory) memory; + this.memory = (SectionMoteMemory) memory; } public MoteType getType() { - return myType; + return moteType; } public void setType(MoteType type) { - myType = type; + moteType = type; } public Simulation getSimulation() { - return mySim; + return simulation; } public void setSimulation(Simulation simulation) { - this.mySim = simulation; + this.simulation = simulation; + } + + public boolean tick(long simTime) { + moteInterfaces.doPassiveActionsBeforeTick(); + moteInterfaces.doActiveActionsBeforeTick(); + + /* TODO Implement application functionality here */ + + moteInterfaces.doActiveActionsAfterTick(); + moteInterfaces.doPassiveActionsAfterTick(); + return false; } public Collection getConfigXML() { @@ -157,12 +158,8 @@ public abstract class AbstractApplicationMote implements Mote { element.setText(getType().getIdentifier()); config.add(element); - // The position interface should also save its config - element = new Element("interface_config"); - element.setText(myInterfaceHandler.getPosition().getClass().getName()); - // Interfaces - for (MoteInterface moteInterface: getInterfaces().getInterfaces()) { + for (MoteInterface moteInterface: moteInterfaces.getInterfaces()) { element = new Element("interface_config"); element.setText(moteInterface.getClass().getName()); @@ -180,22 +177,17 @@ public abstract class AbstractApplicationMote implements Mote { Collection configXML, boolean visAvailable) { // Set initial configuration - mySim = simulation; - myMemory = new SectionMoteMemory(new Properties()); - myInterfaceHandler = new MoteInterfaceHandler(); - Position myPosition = new Position(this); - myInterfaceHandler.addInterface(myPosition); - myApplicationRadio = new ApplicationRadio(this); - myApplicationRadio.addObserver(radioDataObserver); - myInterfaceHandler.addInterface(myApplicationRadio); + this.simulation = simulation; + this.memory = new SectionMoteMemory(new Properties()); for (Element element : configXML) { String name = element.getName(); if (name.equals("motetype_identifier")) { - myType = simulation.getMoteType(element.getText()); + moteType = simulation.getMoteType(element.getText()); + this.moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses()); } else if (name.equals("interface_config")) { - Class moteInterfaceClass = + Class moteInterfaceClass = simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim()); if (moteInterfaceClass == null) { @@ -203,8 +195,7 @@ public abstract class AbstractApplicationMote implements Mote { return false; } - MoteInterface moteInterface = myInterfaceHandler - .getInterfaceOfType(moteInterfaceClass); + MoteInterface moteInterface = moteInterfaces.getInterfaceOfType(moteInterfaceClass); moteInterface.setConfigXML(element.getChildren(), visAvailable); } diff --git a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java index 3ed37cc1d..82ac112d5 100644 --- a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java +++ b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.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: AbstractApplicationMoteType.java,v 1.2 2008/02/12 15:10:49 fros4943 Exp $ + * $Id: AbstractApplicationMoteType.java,v 1.3 2009/03/09 15:38:10 fros4943 Exp $ */ package se.sics.cooja.motes; @@ -32,7 +32,9 @@ package se.sics.cooja.motes; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; +import java.io.File; import java.util.*; + import javax.swing.*; import org.apache.log4j.Logger; import org.jdom.Element; @@ -50,7 +52,7 @@ public abstract class AbstractApplicationMoteType implements MoteType { private String description = null; - private Vector> moteInterfaces = null; + private Class[] moteInterfaces = null; // Type specific class configuration private ProjectConfig myConfig = null; @@ -83,12 +85,6 @@ public abstract class AbstractApplicationMoteType implements MoteType { } } } - - if (description == null) { - // Create description - description = "Application Mote Type #" + counter; - } - } if (description == null) { @@ -96,33 +92,13 @@ public abstract class AbstractApplicationMoteType implements MoteType { description = "Application Mote Type #" + identifier; } - moteInterfaces = new Vector>(); - moteInterfaces.add(Position.class); - moteInterfaces.add(ApplicationRadio.class); + moteInterfaces = new Class[2]; + moteInterfaces[0] = Position.class; + moteInterfaces[1] = ApplicationRadio.class; return true; } - /** - * Returns all mote interfaces of this mote type - * - * @return All mote interfaces - */ - public Vector> getMoteInterfaces() { - return moteInterfaces; - } - - /** - * Set mote interfaces of this mote type - * - * @param moteInterfaces - * New mote interfaces - */ - public void setMoteInterfaces( - Vector> moteInterfaces) { - this.moteInterfaces = moteInterfaces; - } - public String getIdentifier() { return identifier; } @@ -139,8 +115,15 @@ public abstract class AbstractApplicationMoteType implements MoteType { this.description = description; } - public JPanel getTypeVisualizer() { + public Class[] getMoteInterfaceClasses() { + return moteInterfaces; + } + public void setMoteInterfaceClasses(Class[] moteInterfaces) { + this.moteInterfaces = moteInterfaces; + } + + public JPanel getTypeVisualizer() { JPanel panel = new JPanel(); JLabel label = new JLabel(); JPanel smallPane; @@ -180,6 +163,31 @@ public abstract class AbstractApplicationMoteType implements MoteType { return panel; } + public File getContikiSourceFile() { + return null; /* Contiki-independent */ + } + + public File getContikiFirmwareFile() { + return null; /* Contiki-independent */ + } + + public void setContikiSourceFile(File file) { + /* Contiki-independent */ + } + + public void setContikiFirmwareFile(File file) { + /* Contiki-independent */ + } + + public String getCompileCommands() { + /* Contiki-independent */ + return null; + } + + public void setCompileCommands(String commands) { + /* Contiki-independent */ + } + public ProjectConfig getConfig() { return myConfig; } @@ -200,7 +208,7 @@ public abstract class AbstractApplicationMoteType implements MoteType { config.add(element); // Mote interfaces - for (Class moteInterface : getMoteInterfaces()) { + for (Class moteInterface : getMoteInterfaceClasses()) { element = new Element("moteinterface"); element.setText(moteInterface.getName()); config.add(element); @@ -211,8 +219,8 @@ public abstract class AbstractApplicationMoteType implements MoteType { public boolean setConfigXML(Simulation simulation, Collection configXML, boolean visAvailable) { + ArrayList> moteInterfacesList = new ArrayList>(); for (Element element : configXML) { - moteInterfaces = new Vector>(); String name = element.getName(); @@ -221,19 +229,23 @@ public abstract class AbstractApplicationMoteType implements MoteType { } else if (name.equals("description")) { description = element.getText(); } else if (name.equals("moteinterface")) { - Class moteInterfaceClass = simulation.getGUI() - .tryLoadClass(this, MoteInterface.class, element.getText().trim()); + Class moteInterfaceClass = + simulation.getGUI().tryLoadClass( + this, MoteInterface.class, element.getText().trim()); if (moteInterfaceClass == null) { logger.warn("Can't find mote interface class: " + element.getText()); } else { - moteInterfaces.add(moteInterfaceClass); + moteInterfacesList.add(moteInterfaceClass); } } else { logger.fatal("Unrecognized entry in loaded configuration: " + name); } } + moteInterfaces = new Class[moteInterfacesList.size()]; + moteInterfacesList.toArray(moteInterfaces); + boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable); return createdOK; } diff --git a/tools/cooja/java/se/sics/cooja/motes/DisturberMote.java b/tools/cooja/java/se/sics/cooja/motes/DisturberMote.java index e694e19ab..7cf4b7dae 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DisturberMote.java +++ b/tools/cooja/java/se/sics/cooja/motes/DisturberMote.java @@ -24,196 +24,31 @@ * (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: DisturberMote.java,v 1.6 2009/02/18 11:49:54 fros4943 Exp $ + * $Id: DisturberMote.java,v 1.7 2009/03/09 15:38:10 fros4943 Exp $ */ package se.sics.cooja.motes; -import java.util.*; - import org.apache.log4j.Logger; -import org.jdom.Element; - import se.sics.cooja.*; -import se.sics.cooja.interfaces.Position; -import se.sics.cooja.motes.DisturberRadio; /** - * A disturber mote is a purely Java-based mote. It is used to disturb - * transmission of other nodes on a certain channel (currently this is - * hard-coded in the DisturberRadio. + * Simple application-level mote that periodically transmits dummy radio packets + * on a configurable radio channel, interfering all surrounding radio communication. * + * @see DisturberMoteType * @author Fredrik Osterlind, Thiemo Voigt */ -public class DisturberMote implements Mote { +public class DisturberMote extends AbstractApplicationMote { private static Logger logger = Logger.getLogger(DisturberMote.class); - private MoteType myType = null; - - private SectionMoteMemory myMemory = null; - - private MoteInterfaceHandler myInterfaceHandler = null; - - private Simulation mySim = null; - - private DisturberRadio myDisturberRadio; - - /** - * Creates a new uninitialized dummy mote. - */ public DisturberMote() { + super(); } - /** - * Creates a new dummy mote of the given type in the given simulation. An - * empty mote memory and a position interface is added to this mote. - * - * @param moteType - * Mote type - * @param sim - * Simulation - */ - public DisturberMote(MoteType moteType, Simulation sim) { - mySim = sim; - myType = moteType; - Random random = new Random(); /* Do not use main random generator for positioning */ - - // Create memory - myMemory = new SectionMoteMemory(new Properties()); - - // Create interface handler - myInterfaceHandler = new MoteInterfaceHandler(); - Position myPosition = new Position(this); - myPosition.setCoordinates( - random.nextDouble() * 100, - random.nextDouble() * 100, - random.nextDouble() * 100 - ); - myInterfaceHandler.addInterface(myPosition); - - // create interface handler for radio - myDisturberRadio = new DisturberRadio(this); - myInterfaceHandler.addInterface(myDisturberRadio); - } - - public void setState(State newState) { - logger.fatal("Disturber mote can not change state"); - } - - public State getState() { - return Mote.State.ACTIVE; - } - - public void addStateObserver(Observer newObserver) { - } - - public void deleteStateObserver(Observer newObserver) { - } - - public MoteInterfaceHandler getInterfaces() { - return myInterfaceHandler; - } - - public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) { - myInterfaceHandler = moteInterfaceHandler; - } - - public MoteMemory getMemory() { - return myMemory; - } - - public void setMemory(MoteMemory memory) { - myMemory = (SectionMoteMemory) memory; - } - - public MoteType getType() { - return myType; - } - - public void setType(MoteType type) { - myType = type; - } - - public Simulation getSimulation() { - return mySim; - } - - public void setSimulation(Simulation simulation) { - this.mySim = simulation; - } - - public boolean tick(long simTime) { - myInterfaceHandler.doPassiveActionsBeforeTick(); - myInterfaceHandler.doActiveActionsBeforeTick(); - myInterfaceHandler.doActiveActionsAfterTick(); - myInterfaceHandler.doPassiveActionsAfterTick(); - return false; - } - - public Collection getConfigXML() { - Vector config = new Vector(); - - Element element; - - // We need to save the mote type identifier - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - - // The position interface should also save its config - element = new Element("interface_config"); - element.setText(myInterfaceHandler.getPosition().getClass().getName()); - - // Interfaces - for (MoteInterface moteInterface: getInterfaces().getInterfaces()) { - element = new Element("interface_config"); - element.setText(moteInterface.getClass().getName()); - - Collection interfaceXML = moteInterface.getConfigXML(); - if (interfaceXML != null) { - element.addContent(interfaceXML); - config.add(element); - } - } - - return config; - } - - public boolean setConfigXML(Simulation simulation, - Collection configXML, boolean visAvailable) { - - // Set initial configuration - mySim = simulation; - myMemory = new SectionMoteMemory(new Properties()); - myInterfaceHandler = new MoteInterfaceHandler(); - Position myPosition = new Position(this); - myInterfaceHandler.addInterface(myPosition); - myDisturberRadio = new DisturberRadio(this); - myInterfaceHandler.addInterface(myDisturberRadio); - - - for (Element element : configXML) { - String name = element.getName(); - - if (name.equals("motetype_identifier")) { - myType = simulation.getMoteType(element.getText()); - } else if (name.equals("interface_config")) { - Class moteInterfaceClass = simulation.getGUI() - .tryLoadClass(this, MoteInterface.class, element.getText().trim()); - - if (moteInterfaceClass == null) { - logger.warn("Can't find mote interface class: " + element.getText()); - return false; - } - - MoteInterface moteInterface = myInterfaceHandler - .getInterfaceOfType(moteInterfaceClass); - moteInterface.setConfigXML(element.getChildren(), visAvailable); - } - - } - return true; + public DisturberMote(MoteType moteType, Simulation simulation) { + super(moteType, simulation); } public String toString() { @@ -223,5 +58,4 @@ public class DisturberMote implements Mote { return "Disturber Mote, ID=null"; } } - } diff --git a/tools/cooja/java/se/sics/cooja/motes/DisturberMoteType.java b/tools/cooja/java/se/sics/cooja/motes/DisturberMoteType.java index 3c84cabad..eef788f66 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DisturberMoteType.java +++ b/tools/cooja/java/se/sics/cooja/motes/DisturberMoteType.java @@ -31,46 +31,30 @@ package se.sics.cooja.motes; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.util.*; -import javax.swing.*; import org.apache.log4j.Logger; -import org.jdom.Element; - import se.sics.cooja.*; import se.sics.cooja.interfaces.Position; /** + * Simple application-level mote that periodically transmits dummy radio packets + * on a configurable radio channel, interfering all surrounding radio communication. * - * + * @see DisturberMote * @author Fredrik Osterlind, Thiemo Voigt */ @ClassDescription("Disturber Mote Type") @AbstractionLevelDescription("Application level") -public class DisturberMoteType implements MoteType { +public class DisturberMoteType extends AbstractApplicationMoteType { private static Logger logger = Logger.getLogger(DisturberMoteType.class); - // Mote type specific data - private String identifier = null; - - private String description = null; - - private Vector> moteInterfaces = null; - - // Type specific class configuration - private ProjectConfig myConfig = null; - - // Simulation holding this mote type - private Simulation mySimulation = null; - public DisturberMoteType() { + super(); } public DisturberMoteType(String identifier) { - this.identifier = identifier; - description = "Disturber Mote Type #" + identifier; + super(identifier); + setDescription("Disturber Mote Type #" + getIdentifier()); } public Mote generateMote(Simulation simulation) { @@ -78,182 +62,14 @@ public class DisturberMoteType implements MoteType { } public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) { + super.configureAndInit(parentContainer, simulation, visAvailable); + setDescription("Disturber Mote Type #" + getIdentifier()); - if (identifier == null) { - // Create unique identifier - int counter = 0; - boolean identifierOK = false; - while (!identifierOK) { - counter++; - identifier = "dist" + counter; - identifierOK = true; - - // Check if identifier is already used by some other type - for (MoteType existingMoteType : simulation.getMoteTypes()) { - if (existingMoteType != this - && existingMoteType.getIdentifier().equals(identifier)) { - identifierOK = false; - break; - } - } - } - - if (description == null) { - // Create description - description = "Disturber Mote Type #" + counter; - } - - } - - if (description == null) { - // Create description - description = "Disturber Mote Type #" + identifier; - } - - moteInterfaces = new Vector>(); - moteInterfaces.add(Position.class); - moteInterfaces.add(DisturberRadio.class); + Class[] moteInterfaces = new Class[2]; + moteInterfaces[0] = Position.class; + moteInterfaces[1] = DisturberRadio.class; + setMoteInterfaceClasses(moteInterfaces); return true; } - - /* TV: add next two for interfaces */ - /** - * Returns all mote interfaces of this mote type - * - * @return All mote interfaces - */ - public Vector> getMoteInterfaces() { - return moteInterfaces; - } - - /** - * Set mote interfaces of this mote type - * - * @param moteInterfaces - * New mote interfaces - */ - public void setMoteInterfaces( - Vector> moteInterfaces) { - this.moteInterfaces = moteInterfaces; - } - - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - /* TV replaced return null with this */ - public JPanel getTypeVisualizer() { - - JPanel panel = new JPanel(); - JLabel label = new JLabel(); - JPanel smallPane; - - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - // Identifier - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Identifier"); - smallPane.add(BorderLayout.WEST, label); - label = new JLabel(identifier); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); - - // Description - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Description"); - smallPane.add(BorderLayout.WEST, label); - label = new JLabel(description); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); - - // Mote Interfaces - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Mote interfaces"); - smallPane.add(BorderLayout.WEST, label); - panel.add(smallPane); - - for (Class moteInterface : moteInterfaces) { - smallPane = new JPanel(new BorderLayout()); - label = new JLabel(moteInterface.getSimpleName()); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); - } - - panel.add(Box.createRigidArea(new Dimension(0, 5))); - return panel; - } - - public ProjectConfig getConfig() { - return myConfig; - //return null; /* TV */ - } - - public Collection getConfigXML() { - Vector config = new Vector(); - - Element element; - - // Identifier - element = new Element("identifier"); - element.setText(getIdentifier()); - config.add(element); - - // Description - element = new Element("description"); - element.setText(getDescription()); - config.add(element); - - // Mote interfaces - for (Class moteInterface : getMoteInterfaces()) { - element = new Element("moteinterface"); - element.setText(moteInterface.getName()); - config.add(element); - } - - return config; - } - - public boolean setConfigXML(Simulation simulation, - Collection configXML, boolean visAvailable) { - for (Element element : configXML) { - moteInterfaces = new Vector>(); /* TV */ - mySimulation = simulation; /* TV */ - - String name = element.getName(); - - if (name.equals("identifier")) { - identifier = element.getText(); - } else if (name.equals("description")) { - description = element.getText(); - } else if (name.equals("moteinterface")) { /* TV */ - Class moteInterfaceClass = simulation.getGUI() - .tryLoadClass(this, MoteInterface.class, element.getText().trim()); - - if (moteInterfaceClass == null) { - logger.warn("Can't find mote interface class: " + element.getText()); - } else { - moteInterfaces.add(moteInterfaceClass); - } - } else { - logger.fatal("Unrecognized entry in loaded configuration: " + name); - } - } - - boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable); - return createdOK; - } - } diff --git a/tools/cooja/java/se/sics/cooja/motes/DisturberRadio.java b/tools/cooja/java/se/sics/cooja/motes/DisturberRadio.java index bba9db13e..5fe54c320 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DisturberRadio.java +++ b/tools/cooja/java/se/sics/cooja/motes/DisturberRadio.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: DisturberRadio.java,v 1.9 2008/12/04 14:03:42 joxe Exp $ + * $Id: DisturberRadio.java,v 1.10 2009/03/09 15:38:10 fros4943 Exp $ */ package se.sics.cooja.motes; @@ -47,7 +47,7 @@ import se.sics.cooja.interfaces.*; * * @author Fredrik Osterlind, Thiemo Voigt */ -public class DisturberRadio extends Radio { +public class DisturberRadio extends Radio implements PolledBeforeAllTicks { private Mote myMote; private static Logger logger = Logger.getLogger(DisturberRadio.class); @@ -155,6 +155,10 @@ public class DisturberRadio extends Radio { long currentTime = myMote.getSimulation().getSimulationTime(); if (!transmitting && currentTime % TRANSMISSION_INTERVAL == 0) { + if (distChannel < 0) { + return; + } + transmitting = true; lastEvent = RadioEvent.TRANSMISSION_STARTED; lastEventTime = currentTime; @@ -163,6 +167,10 @@ public class DisturberRadio extends Radio { this.notifyObservers(); } else if (transmitting && currentTime >= transEndTime) { transmitting = false; + lastEvent = RadioEvent.PACKET_TRANSMITTED; + lastEventTime = currentTime; + this.setChanged(); + this.notifyObservers(); lastEvent = RadioEvent.TRANSMISSION_FINISHED; lastEventTime = currentTime; this.setChanged(); @@ -170,9 +178,6 @@ public class DisturberRadio extends Radio { } } - public void doActionsAfterTick() { - } - public JPanel getInterfaceVisualizer() { // Location JPanel panel = new JPanel(); @@ -212,9 +217,7 @@ public class DisturberRadio extends Radio { channelPicker.addPropertyChangeListener("value", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { distChannel = ((Number) channelPicker.getValue()).intValue(); - if (observer != null) { - observer.update(null, null); - } + observer.update(null, null); } }); @@ -244,7 +247,6 @@ public class DisturberRadio extends Radio { Vector config = new Vector(); Element element; - // We need to save the mote type identifier element = new Element("channel"); element.setText(Integer.toString(distChannel)); config.add(element); diff --git a/tools/cooja/java/se/sics/cooja/motes/DummyMote.java b/tools/cooja/java/se/sics/cooja/motes/DummyMote.java index 2eb2e4d05..c814b01fe 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DummyMote.java +++ b/tools/cooja/java/se/sics/cooja/motes/DummyMote.java @@ -1,229 +1,69 @@ /* - * Copyright (c) 2006, Swedish Institute of Computer Science. - * All rights reserved. + * Copyright (c) 2006, Swedish Institute of Computer Science. All rights + * reserved. * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of the + * Institute nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (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: DummyMote.java,v 1.7 2009/02/18 10:41:50 fros4943 Exp $ + * $Id: DummyMote.java,v 1.8 2009/03/09 15:38:10 fros4943 Exp $ */ package se.sics.cooja.motes; -import java.util.Collection; -import java.util.Observer; -import java.util.Properties; import java.util.Random; -import java.util.Vector; import org.apache.log4j.Logger; -import org.jdom.Element; - import se.sics.cooja.*; import se.sics.cooja.interfaces.Position; -/** - * A dummy mote is a purely Java-based mote, and can be used as an example of - * how to implement motes other than the usual Contiki mote. - * - * The dummy mote uses an empty section mote memory without any variable - * mappings. - * - * The mote interface handler has a position interface, added when the mote is - * constructed. - * - * When the dummy mote is ticked all (one!) interfaces are polled and a random - * variable decides if the position should be changed. The node never leaves the - * active state. - * - * @author Fredrik Osterlind - */ -public class DummyMote implements Mote { +public class DummyMote extends AbstractApplicationMote { private static Logger logger = Logger.getLogger(DummyMote.class); + private Random random = new Random(); /* XXX Not using Cooja main random generator */ - private MoteType myType = null; - private SectionMoteMemory myMemory = null; - private MoteInterfaceHandler myInterfaceHandler = null; - private Simulation mySim = null; - - private Random random = null; - - /** - * Creates a new uninitialized dummy mote. - */ public DummyMote() { + super(); } - /** - * Creates a new dummy mote of the given type in the given simulation. An - * empty mote memory and a position interface is added to this mote. - * - * @param moteType - * Mote type - * @param sim - * Simulation - */ - public DummyMote(MoteType moteType, Simulation sim) { - mySim = sim; - myType = moteType; - random = mySim.getRandomGenerator(); - - // Create memory - myMemory = new SectionMoteMemory(new Properties()); - - // Create interface handler - myInterfaceHandler = new MoteInterfaceHandler(); - Position myPosition = new Position(this); - myPosition.setCoordinates( - random.nextDouble() * 100, - random.nextDouble() * 100, - random.nextDouble() * 100 - ); - myInterfaceHandler.addInterface(myPosition); - } - - public void setState(State newState) { - logger.fatal("Dummy mote can not change state"); - } - - public State getState() { - return Mote.State.ACTIVE; - } - - public void addStateObserver(Observer newObserver) { - } - - public void deleteStateObserver(Observer newObserver) { - } - - public MoteInterfaceHandler getInterfaces() { - return myInterfaceHandler; - } - - public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) { - myInterfaceHandler = moteInterfaceHandler; - } - - public MoteMemory getMemory() { - return myMemory; - } - - public void setMemory(MoteMemory memory) { - myMemory = (SectionMoteMemory) memory; - } - - public MoteType getType() { - return myType; - } - - public void setType(MoteType type) { - myType = type; - } - - public Simulation getSimulation() { - return mySim; - } - - public void setSimulation(Simulation simulation) { - this.mySim = simulation; + public DummyMote(MoteType moteType, Simulation simulation) { + super(moteType, simulation); } public boolean tick(long simTime) { - // Perform some dummy task + /* Dummy task: move randomly */ if (random.nextDouble() > 0.9) { - // Move mote randomly - Position myPosition = myInterfaceHandler.getPosition(); - myPosition.setCoordinates(myPosition.getXCoordinate() - + random.nextDouble() - 0.5, myPosition.getYCoordinate() - + random.nextDouble() - 0.5, myPosition.getZCoordinate() - + random.nextDouble() - 0.5); + Position pos = getInterfaces().getPosition(); + pos.setCoordinates( + pos.getXCoordinate() + random.nextDouble() - 0.5, + pos.getYCoordinate() + random.nextDouble() - 0.5, + pos.getZCoordinate() + random.nextDouble() - 0.5 + ); } + return false; } - public Collection getConfigXML() { - Vector config = new Vector(); - - Element element; - - // We need to save the mote type identifier - element = new Element("motetype_identifier"); - element.setText(getType().getIdentifier()); - config.add(element); - - // The position interface should also save its config - element = new Element("interface_config"); - element.setText(myInterfaceHandler.getPosition().getClass().getName()); - - Collection interfaceXML = myInterfaceHandler.getPosition().getConfigXML(); - if (interfaceXML != null) { - element.addContent(interfaceXML); - config.add(element); - } - - return config; - } - - public boolean setConfigXML(Simulation simulation, - Collection configXML, boolean visAvailable) { - mySim = simulation; - myMemory = new SectionMoteMemory(new Properties()); - random = mySim.getRandomGenerator(); - myInterfaceHandler = new MoteInterfaceHandler(); - myInterfaceHandler.addInterface(new Position(this)); - - for (Element element : configXML) { - String name = element.getName(); - - if (name.equals("motetype_identifier")) { - myType = simulation.getMoteType(element.getText()); - } else if (name.equals("interface_config")) { - Class moteInterfaceClass = simulation.getGUI() - .tryLoadClass(this, MoteInterface.class, element.getText().trim()); - - if (moteInterfaceClass == null) { - logger.warn("Can't find mote interface class: " + element.getText()); - return false; - } - - MoteInterface moteInterface = myInterfaceHandler - .getInterfaceOfType(moteInterfaceClass); - moteInterface.setConfigXML(element.getChildren(), visAvailable); - } - - } - return true; - } - public String toString() { - if (getInterfaces().getMoteID() != null) { - return "Dummy Mote, ID=" + getInterfaces().getMoteID().getMoteID(); - } else { - return "Dummy Mote, ID=null"; - } + return "Dummy Mote"; } - } diff --git a/tools/cooja/java/se/sics/cooja/motes/DummyMoteType.java b/tools/cooja/java/se/sics/cooja/motes/DummyMoteType.java index f32c1885c..9538892e2 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DummyMoteType.java +++ b/tools/cooja/java/se/sics/cooja/motes/DummyMoteType.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, Swedish Institute of Computer Science. + * Copyright (c) 2009, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,36 +26,28 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: DummyMoteType.java,v 1.5 2008/02/12 15:10:49 fros4943 Exp $ + * */ package se.sics.cooja.motes; import java.awt.Container; -import java.util.*; - -import javax.swing.*; - import org.apache.log4j.Logger; -import org.jdom.Element; - import se.sics.cooja.*; +import se.sics.cooja.interfaces.Position; @ClassDescription("Dummy Mote Type") @AbstractionLevelDescription("Application level") -public class DummyMoteType implements MoteType { +public class DummyMoteType extends AbstractApplicationMoteType { private static Logger logger = Logger.getLogger(DummyMoteType.class); - // Mote type specific data - private String identifier = null; - private String description = null; - public DummyMoteType() { + super(); } public DummyMoteType(String identifier) { - this.identifier = identifier; - description = "Dummy Mote Type #" + identifier; + super(identifier); + setDescription("Dummy Mote Type #" + getIdentifier()); } public Mote generateMote(Simulation simulation) { @@ -63,98 +55,8 @@ public class DummyMoteType implements MoteType { } public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) { - - if (identifier == null) { - // Create unique identifier - int counter = 0; - boolean identifierOK = false; - while (!identifierOK) { - counter++; - identifier = "dummy" + counter; - identifierOK = true; - - // Check if identifier is already used by some other type - for (MoteType existingMoteType : simulation.getMoteTypes()) { - if (existingMoteType != this - && existingMoteType.getIdentifier().equals(identifier)) { - identifierOK = false; - break; - } - } - } - - if (description == null) { - // Create description - description = "Dummy Mote Type #" + counter; - } - - } - - if (description == null) { - // Create description - description = "Dummy Mote Type #" + identifier; - } - + super.configureAndInit(parentContainer, simulation, visAvailable); + setDescription("Dummy Mote Type #" + getIdentifier()); return true; } - - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public JPanel getTypeVisualizer() { - return null; - } - - public ProjectConfig getConfig() { - return null; - } - - public Collection getConfigXML() { - Vector config = new Vector(); - - Element element; - - // Identifier - element = new Element("identifier"); - element.setText(getIdentifier()); - config.add(element); - - // Description - element = new Element("description"); - element.setText(getDescription()); - config.add(element); - - return config; - } - - public boolean setConfigXML(Simulation simulation, Collection configXML, boolean visAvailable) { - for (Element element : configXML) { - String name = element.getName(); - - if (name.equals("identifier")) { - identifier = element.getText(); - } else if (name.equals("description")) { - description = element.getText(); - } else { - logger.fatal("Unrecognized entry in loaded configuration: " + name); - } - } - - boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable); - return createdOK; - } - }