application-level mote type implementing new mote type methods

+ radio disturber mote extending abstract application mote
This commit is contained in:
fros4943 2009-03-09 15:38:10 +00:00
parent dadc67abd7
commit 74647ce2de
7 changed files with 177 additions and 780 deletions

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; package se.sics.cooja.motes;
@ -35,8 +35,6 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.interfaces.ApplicationRadio;
import se.sics.cooja.interfaces.Position;
import se.sics.cooja.interfaces.Radio; 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 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; private Simulation simulation = null;
protected ApplicationRadio myApplicationRadio;
private Observer radioDataObserver = new Observer() { private Observer radioDataObserver = new Observer() {
public void update(Observable obs, Object obj) { 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() {
} }
public AbstractApplicationMote(MoteType moteType, Simulation sim) { public AbstractApplicationMote(MoteType moteType, Simulation sim) {
mySim = sim; this.simulation = sim;
myType = moteType; this.moteType = moteType;
// Create memory // Create memory
myMemory = new SectionMoteMemory(new Properties()); this.memory = new SectionMoteMemory(new Properties());
// Create position // Create mote interfaces
myInterfaceHandler = new MoteInterfaceHandler(); this.moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses());
Position myPosition = new Position(this);
myInterfaceHandler.addInterface(myPosition);
// Create radio if (moteInterfaces.getRadio() != null) {
myApplicationRadio = new ApplicationRadio(this); moteInterfaces.getRadio().addObserver(radioDataObserver);
myApplicationRadio.addObserver(radioDataObserver); }
myInterfaceHandler.addInterface(myApplicationRadio);
} }
public void setState(State newState) { public void setState(State newState) {
@ -116,35 +106,46 @@ public abstract class AbstractApplicationMote implements Mote {
} }
public MoteInterfaceHandler getInterfaces() { public MoteInterfaceHandler getInterfaces() {
return myInterfaceHandler; return moteInterfaces;
} }
public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) { public void setInterfaces(MoteInterfaceHandler moteInterfaceHandler) {
myInterfaceHandler = moteInterfaceHandler; moteInterfaces = moteInterfaceHandler;
} }
public MoteMemory getMemory() { public MoteMemory getMemory() {
return myMemory; return memory;
} }
public void setMemory(MoteMemory memory) { public void setMemory(MoteMemory memory) {
myMemory = (SectionMoteMemory) memory; this.memory = (SectionMoteMemory) memory;
} }
public MoteType getType() { public MoteType getType() {
return myType; return moteType;
} }
public void setType(MoteType type) { public void setType(MoteType type) {
myType = type; moteType = type;
} }
public Simulation getSimulation() { public Simulation getSimulation() {
return mySim; return simulation;
} }
public void setSimulation(Simulation 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<Element> getConfigXML() { public Collection<Element> getConfigXML() {
@ -157,12 +158,8 @@ public abstract class AbstractApplicationMote implements Mote {
element.setText(getType().getIdentifier()); element.setText(getType().getIdentifier());
config.add(element); config.add(element);
// The position interface should also save its config
element = new Element("interface_config");
element.setText(myInterfaceHandler.getPosition().getClass().getName());
// Interfaces // Interfaces
for (MoteInterface moteInterface: getInterfaces().getInterfaces()) { for (MoteInterface moteInterface: moteInterfaces.getInterfaces()) {
element = new Element("interface_config"); element = new Element("interface_config");
element.setText(moteInterface.getClass().getName()); element.setText(moteInterface.getClass().getName());
@ -180,22 +177,17 @@ public abstract class AbstractApplicationMote implements Mote {
Collection<Element> configXML, boolean visAvailable) { Collection<Element> configXML, boolean visAvailable) {
// Set initial configuration // Set initial configuration
mySim = simulation; this.simulation = simulation;
myMemory = new SectionMoteMemory(new Properties()); this.memory = 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);
for (Element element : configXML) { for (Element element : configXML) {
String name = element.getName(); String name = element.getName();
if (name.equals("motetype_identifier")) { 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")) { } else if (name.equals("interface_config")) {
Class<? extends MoteInterface> moteInterfaceClass = Class<? extends MoteInterface> moteInterfaceClass =
simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim()); simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim());
if (moteInterfaceClass == null) { if (moteInterfaceClass == null) {
@ -203,8 +195,7 @@ public abstract class AbstractApplicationMote implements Mote {
return false; return false;
} }
MoteInterface moteInterface = myInterfaceHandler MoteInterface moteInterface = moteInterfaces.getInterfaceOfType(moteInterfaceClass);
.getInterfaceOfType(moteInterfaceClass);
moteInterface.setConfigXML(element.getChildren(), visAvailable); moteInterface.setConfigXML(element.getChildren(), visAvailable);
} }

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; package se.sics.cooja.motes;
@ -32,7 +32,9 @@ package se.sics.cooja.motes;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension; import java.awt.Dimension;
import java.io.File;
import java.util.*; import java.util.*;
import javax.swing.*; import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
@ -50,7 +52,7 @@ public abstract class AbstractApplicationMoteType implements MoteType {
private String description = null; private String description = null;
private Vector<Class<? extends MoteInterface>> moteInterfaces = null; private Class<? extends MoteInterface>[] moteInterfaces = null;
// Type specific class configuration // Type specific class configuration
private ProjectConfig myConfig = null; 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) { if (description == null) {
@ -96,33 +92,13 @@ public abstract class AbstractApplicationMoteType implements MoteType {
description = "Application Mote Type #" + identifier; description = "Application Mote Type #" + identifier;
} }
moteInterfaces = new Vector<Class<? extends MoteInterface>>(); moteInterfaces = new Class[2];
moteInterfaces.add(Position.class); moteInterfaces[0] = Position.class;
moteInterfaces.add(ApplicationRadio.class); moteInterfaces[1] = ApplicationRadio.class;
return true; return true;
} }
/**
* Returns all mote interfaces of this mote type
*
* @return All mote interfaces
*/
public Vector<Class<? extends MoteInterface>> getMoteInterfaces() {
return moteInterfaces;
}
/**
* Set mote interfaces of this mote type
*
* @param moteInterfaces
* New mote interfaces
*/
public void setMoteInterfaces(
Vector<Class<? extends MoteInterface>> moteInterfaces) {
this.moteInterfaces = moteInterfaces;
}
public String getIdentifier() { public String getIdentifier() {
return identifier; return identifier;
} }
@ -139,8 +115,15 @@ public abstract class AbstractApplicationMoteType implements MoteType {
this.description = description; this.description = description;
} }
public JPanel getTypeVisualizer() { public Class<? extends MoteInterface>[] getMoteInterfaceClasses() {
return moteInterfaces;
}
public void setMoteInterfaceClasses(Class<? extends MoteInterface>[] moteInterfaces) {
this.moteInterfaces = moteInterfaces;
}
public JPanel getTypeVisualizer() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
JLabel label = new JLabel(); JLabel label = new JLabel();
JPanel smallPane; JPanel smallPane;
@ -180,6 +163,31 @@ public abstract class AbstractApplicationMoteType implements MoteType {
return panel; 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() { public ProjectConfig getConfig() {
return myConfig; return myConfig;
} }
@ -200,7 +208,7 @@ public abstract class AbstractApplicationMoteType implements MoteType {
config.add(element); config.add(element);
// Mote interfaces // Mote interfaces
for (Class moteInterface : getMoteInterfaces()) { for (Class moteInterface : getMoteInterfaceClasses()) {
element = new Element("moteinterface"); element = new Element("moteinterface");
element.setText(moteInterface.getName()); element.setText(moteInterface.getName());
config.add(element); config.add(element);
@ -211,8 +219,8 @@ public abstract class AbstractApplicationMoteType implements MoteType {
public boolean setConfigXML(Simulation simulation, public boolean setConfigXML(Simulation simulation,
Collection<Element> configXML, boolean visAvailable) { Collection<Element> configXML, boolean visAvailable) {
ArrayList<Class<? extends MoteInterface>> moteInterfacesList = new ArrayList<Class<? extends MoteInterface>>();
for (Element element : configXML) { for (Element element : configXML) {
moteInterfaces = new Vector<Class<? extends MoteInterface>>();
String name = element.getName(); String name = element.getName();
@ -221,19 +229,23 @@ public abstract class AbstractApplicationMoteType implements MoteType {
} else if (name.equals("description")) { } else if (name.equals("description")) {
description = element.getText(); description = element.getText();
} else if (name.equals("moteinterface")) { } else if (name.equals("moteinterface")) {
Class<? extends MoteInterface> moteInterfaceClass = simulation.getGUI() Class<? extends MoteInterface> moteInterfaceClass =
.tryLoadClass(this, MoteInterface.class, element.getText().trim()); simulation.getGUI().tryLoadClass(
this, MoteInterface.class, element.getText().trim());
if (moteInterfaceClass == null) { if (moteInterfaceClass == null) {
logger.warn("Can't find mote interface class: " + element.getText()); logger.warn("Can't find mote interface class: " + element.getText());
} else { } else {
moteInterfaces.add(moteInterfaceClass); moteInterfacesList.add(moteInterfaceClass);
} }
} else { } else {
logger.fatal("Unrecognized entry in loaded configuration: " + name); logger.fatal("Unrecognized entry in loaded configuration: " + name);
} }
} }
moteInterfaces = new Class[moteInterfacesList.size()];
moteInterfacesList.toArray(moteInterfaces);
boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable); boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable);
return createdOK; return createdOK;
} }

View File

@ -24,196 +24,31 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; package se.sics.cooja.motes;
import java.util.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*; 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 * Simple application-level mote that periodically transmits dummy radio packets
* transmission of other nodes on a certain channel (currently this is * on a configurable radio channel, interfering all surrounding radio communication.
* hard-coded in the DisturberRadio.
* *
* @see DisturberMoteType
* @author Fredrik Osterlind, Thiemo Voigt * @author Fredrik Osterlind, Thiemo Voigt
*/ */
public class DisturberMote implements Mote { public class DisturberMote extends AbstractApplicationMote {
private static Logger logger = Logger.getLogger(DisturberMote.class); 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() { public DisturberMote() {
super();
} }
/** public DisturberMote(MoteType moteType, Simulation simulation) {
* Creates a new dummy mote of the given type in the given simulation. An super(moteType, simulation);
* 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<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
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<Element> 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<? extends MoteInterface> 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() { public String toString() {
@ -223,5 +58,4 @@ public class DisturberMote implements Mote {
return "Disturber Mote, ID=null"; return "Disturber Mote, ID=null";
} }
} }
} }

View File

@ -31,46 +31,30 @@
package se.sics.cooja.motes; package se.sics.cooja.motes;
import java.awt.BorderLayout;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension;
import java.util.*;
import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position; 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 * @author Fredrik Osterlind, Thiemo Voigt
*/ */
@ClassDescription("Disturber Mote Type") @ClassDescription("Disturber Mote Type")
@AbstractionLevelDescription("Application level") @AbstractionLevelDescription("Application level")
public class DisturberMoteType implements MoteType { public class DisturberMoteType extends AbstractApplicationMoteType {
private static Logger logger = Logger.getLogger(DisturberMoteType.class); private static Logger logger = Logger.getLogger(DisturberMoteType.class);
// Mote type specific data
private String identifier = null;
private String description = null;
private Vector<Class<? extends MoteInterface>> moteInterfaces = null;
// Type specific class configuration
private ProjectConfig myConfig = null;
// Simulation holding this mote type
private Simulation mySimulation = null;
public DisturberMoteType() { public DisturberMoteType() {
super();
} }
public DisturberMoteType(String identifier) { public DisturberMoteType(String identifier) {
this.identifier = identifier; super(identifier);
description = "Disturber Mote Type #" + identifier; setDescription("Disturber Mote Type #" + getIdentifier());
} }
public Mote generateMote(Simulation simulation) { public Mote generateMote(Simulation simulation) {
@ -78,182 +62,14 @@ public class DisturberMoteType implements MoteType {
} }
public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) { public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) {
super.configureAndInit(parentContainer, simulation, visAvailable);
setDescription("Disturber Mote Type #" + getIdentifier());
if (identifier == null) { Class<? extends MoteInterface>[] moteInterfaces = new Class[2];
// Create unique identifier moteInterfaces[0] = Position.class;
int counter = 0; moteInterfaces[1] = DisturberRadio.class;
boolean identifierOK = false; setMoteInterfaceClasses(moteInterfaces);
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<Class<? extends MoteInterface>>();
moteInterfaces.add(Position.class);
moteInterfaces.add(DisturberRadio.class);
return true; return true;
} }
/* TV: add next two for interfaces */
/**
* Returns all mote interfaces of this mote type
*
* @return All mote interfaces
*/
public Vector<Class<? extends MoteInterface>> getMoteInterfaces() {
return moteInterfaces;
}
/**
* Set mote interfaces of this mote type
*
* @param moteInterfaces
* New mote interfaces
*/
public void setMoteInterfaces(
Vector<Class<? extends MoteInterface>> 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<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
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<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
moteInterfaces = new Vector<Class<? extends MoteInterface>>(); /* 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<? extends MoteInterface> 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;
}
} }

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.motes;
@ -47,7 +47,7 @@ import se.sics.cooja.interfaces.*;
* *
* @author Fredrik Osterlind, Thiemo Voigt * @author Fredrik Osterlind, Thiemo Voigt
*/ */
public class DisturberRadio extends Radio { public class DisturberRadio extends Radio implements PolledBeforeAllTicks {
private Mote myMote; private Mote myMote;
private static Logger logger = Logger.getLogger(DisturberRadio.class); private static Logger logger = Logger.getLogger(DisturberRadio.class);
@ -155,6 +155,10 @@ public class DisturberRadio extends Radio {
long currentTime = myMote.getSimulation().getSimulationTime(); long currentTime = myMote.getSimulation().getSimulationTime();
if (!transmitting && currentTime % TRANSMISSION_INTERVAL == 0) { if (!transmitting && currentTime % TRANSMISSION_INTERVAL == 0) {
if (distChannel < 0) {
return;
}
transmitting = true; transmitting = true;
lastEvent = RadioEvent.TRANSMISSION_STARTED; lastEvent = RadioEvent.TRANSMISSION_STARTED;
lastEventTime = currentTime; lastEventTime = currentTime;
@ -163,6 +167,10 @@ public class DisturberRadio extends Radio {
this.notifyObservers(); this.notifyObservers();
} else if (transmitting && currentTime >= transEndTime) { } else if (transmitting && currentTime >= transEndTime) {
transmitting = false; transmitting = false;
lastEvent = RadioEvent.PACKET_TRANSMITTED;
lastEventTime = currentTime;
this.setChanged();
this.notifyObservers();
lastEvent = RadioEvent.TRANSMISSION_FINISHED; lastEvent = RadioEvent.TRANSMISSION_FINISHED;
lastEventTime = currentTime; lastEventTime = currentTime;
this.setChanged(); this.setChanged();
@ -170,9 +178,6 @@ public class DisturberRadio extends Radio {
} }
} }
public void doActionsAfterTick() {
}
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
// Location // Location
JPanel panel = new JPanel(); JPanel panel = new JPanel();
@ -212,9 +217,7 @@ public class DisturberRadio extends Radio {
channelPicker.addPropertyChangeListener("value", new PropertyChangeListener() { channelPicker.addPropertyChangeListener("value", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) { public void propertyChange(PropertyChangeEvent e) {
distChannel = ((Number) channelPicker.getValue()).intValue(); 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<Element> config = new Vector<Element>(); Vector<Element> config = new Vector<Element>();
Element element; Element element;
// We need to save the mote type identifier
element = new Element("channel"); element = new Element("channel");
element.setText(Integer.toString(distChannel)); element.setText(Integer.toString(distChannel));
config.add(element); config.add(element);

View File

@ -1,229 +1,69 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2006, Swedish Institute of Computer Science. All rights
* All rights reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions are met:
* are met: * 1. Redistributions of source code must retain the above copyright notice,
* 1. Redistributions of source code must retain the above copyright * this list of conditions and the following disclaimer. 2. Redistributions in
* notice, this list of conditions and the following disclaimer. * binary form must reproduce the above copyright notice, this list of
* 2. Redistributions in binary form must reproduce the above copyright * conditions and the following disclaimer in the documentation and/or other
* notice, this list of conditions and the following disclaimer in the * materials provided with the distribution. 3. Neither the name of the
* documentation and/or other materials provided with the distribution. * Institute nor the names of its contributors may be used to endorse or promote
* 3. Neither the name of the Institute nor the names of its contributors * products derived from this software without specific prior written
* may be used to endorse or promote products derived from this software * permission.
* without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 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; package se.sics.cooja.motes;
import java.util.Collection;
import java.util.Observer;
import java.util.Properties;
import java.util.Random; import java.util.Random;
import java.util.Vector;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Position;
/** public class DummyMote extends AbstractApplicationMote {
* 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 {
private static Logger logger = Logger.getLogger(DummyMote.class); 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() { public DummyMote() {
super();
} }
/** public DummyMote(MoteType moteType, Simulation simulation) {
* Creates a new dummy mote of the given type in the given simulation. An super(moteType, simulation);
* 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 boolean tick(long simTime) { public boolean tick(long simTime) {
// Perform some dummy task /* Dummy task: move randomly */
if (random.nextDouble() > 0.9) { if (random.nextDouble() > 0.9) {
// Move mote randomly Position pos = getInterfaces().getPosition();
Position myPosition = myInterfaceHandler.getPosition(); pos.setCoordinates(
myPosition.setCoordinates(myPosition.getXCoordinate() pos.getXCoordinate() + random.nextDouble() - 0.5,
+ random.nextDouble() - 0.5, myPosition.getYCoordinate() pos.getYCoordinate() + random.nextDouble() - 0.5,
+ random.nextDouble() - 0.5, myPosition.getZCoordinate() pos.getZCoordinate() + random.nextDouble() - 0.5
+ random.nextDouble() - 0.5); );
} }
return false; return false;
} }
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
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<Element> 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<? extends MoteInterface> 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() { public String toString() {
if (getInterfaces().getMoteID() != null) { return "Dummy Mote";
return "Dummy Mote, ID=" + getInterfaces().getMoteID().getMoteID();
} else {
return "Dummy Mote, ID=null";
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2009, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: DummyMoteType.java,v 1.5 2008/02/12 15:10:49 fros4943 Exp $ *
*/ */
package se.sics.cooja.motes; package se.sics.cooja.motes;
import java.awt.Container; import java.awt.Container;
import java.util.*;
import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position;
@ClassDescription("Dummy Mote Type") @ClassDescription("Dummy Mote Type")
@AbstractionLevelDescription("Application level") @AbstractionLevelDescription("Application level")
public class DummyMoteType implements MoteType { public class DummyMoteType extends AbstractApplicationMoteType {
private static Logger logger = Logger.getLogger(DummyMoteType.class); private static Logger logger = Logger.getLogger(DummyMoteType.class);
// Mote type specific data
private String identifier = null;
private String description = null;
public DummyMoteType() { public DummyMoteType() {
super();
} }
public DummyMoteType(String identifier) { public DummyMoteType(String identifier) {
this.identifier = identifier; super(identifier);
description = "Dummy Mote Type #" + identifier; setDescription("Dummy Mote Type #" + getIdentifier());
} }
public Mote generateMote(Simulation simulation) { public Mote generateMote(Simulation simulation) {
@ -63,98 +55,8 @@ public class DummyMoteType implements MoteType {
} }
public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) { public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) {
super.configureAndInit(parentContainer, simulation, visAvailable);
if (identifier == null) { setDescription("Dummy Mote Type #" + getIdentifier());
// 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;
}
return true; 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<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
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<Element> 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;
}
} }