abstract application mote, used for implementing application-level motes, now extends wakeup mote.

+ reimplemented disturber mote using abstract application mote
+ removed obsolete dummy mote example
This commit is contained in:
fros4943 2009-10-28 14:38:02 +00:00
parent cb04423a57
commit 5650e818ec
8 changed files with 216 additions and 632 deletions

View file

@ -26,32 +26,46 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ApplicationRadio.java,v 1.9 2009/04/16 14:26:36 fros4943 Exp $
* $Id: ApplicationRadio.java,v 1.10 2009/10/28 14:38:02 fros4943 Exp $
*/
package se.sics.cooja.interfaces;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteTimeEvent;
import se.sics.cooja.RadioPacket;
import se.sics.cooja.Simulation;
/**
* Application radio. May for example be used by Java-based mote to implement
* radio functionality. Supports radio channels and output power functionality.
* The mote should observe the radio for incoming radio packet data.
* Application radio.
*
* May be used by Java-based mote to implement radio functionality.
* Supports radio channels and output power functionality.
* The mote itself should observe the radio for incoming radio packet data.
*
* @author Fredrik Osterlind
*/
public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
private Mote myMote;
public class ApplicationRadio extends Radio {
private static Logger logger = Logger.getLogger(ApplicationRadio.class);
private Simulation simulation;
private Mote mote;
private RadioPacket packetFromMote = null;
private RadioPacket packetToMote = null;
@ -64,17 +78,14 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
private RadioEvent lastEvent = RadioEvent.UNKNOWN;
private long lastEventTime = 0;
private boolean outPacketExists = false;
private RadioPacket outPacket = null;
private int outPacketDuration = -1;
private double signalStrength = -100;
private int radioChannel = 1;
private int radioChannel = -1;
private double outputPower = 0;
private int outputPowerIndicator = 100;
public ApplicationRadio(Mote mote) {
this.myMote = mote;
this.mote = mote;
this.simulation = mote.getSimulation();
}
/* Packet radio support */
@ -99,7 +110,7 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
}
isReceiving = true;
lastEventTime = myMote.getSimulation().getSimulationTime();
lastEventTime = simulation.getSimulationTime();
lastEvent = RadioEvent.RECEPTION_STARTED;
this.setChanged();
this.notifyObservers();
@ -107,16 +118,13 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
public void signalReceptionEnd() {
if (isInterfered() || packetToMote == null) {
// Reset interfered flag
isInterfered = false;
// Reset data
packetToMote = null;
return;
}
isReceiving = false;
lastEventTime = myMote.getSimulation().getSimulationTime();
lastEventTime = simulation.getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED;
this.setChanged();
this.notifyObservers();
@ -139,7 +147,7 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
}
public Position getPosition() {
return myMote.getInterfaces().getPosition();
return mote.getInterfaces().getPosition();
}
public RadioEvent getLastEvent() {
@ -151,7 +159,7 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
isInterfered = true;
lastEvent = RadioEvent.RECEPTION_INTERFERED;
lastEventTime = myMote.getSimulation().getSimulationTime();
lastEventTime = simulation.getSimulationTime();
this.setChanged();
this.notifyObservers();
}
@ -189,10 +197,48 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
* @param packet Packet data
* @param duration Duration to transmit
*/
public void startTransmittingPacket(RadioPacket packet, int duration) {
outPacketExists = true;
outPacketDuration = duration;
outPacket = packet;
public void startTransmittingPacket(final RadioPacket packet, final long duration) {
Runnable startTransmission = new Runnable() {
public void run() {
if (isTransmitting) {
logger.warn("Already transmitting, aborting new transmission");
return;
}
/* Start transmission */
isTransmitting = true;
lastEvent = RadioEvent.TRANSMISSION_STARTED;
lastEventTime = simulation.getSimulationTime();
ApplicationRadio.this.setChanged();
ApplicationRadio.this.notifyObservers();
/* Deliver data */
packetFromMote = packet;
lastEvent = RadioEvent.PACKET_TRANSMITTED;
ApplicationRadio.this.setChanged();
ApplicationRadio.this.notifyObservers();
/*logger.info("Transmission started");*/
/* Finish transmission */
simulation.scheduleEvent(new MoteTimeEvent(mote, 0) {
public void execute(long t) {
isTransmitting = false;
lastEvent = RadioEvent.TRANSMISSION_FINISHED;
lastEventTime = t;
ApplicationRadio.this.setChanged();
ApplicationRadio.this.notifyObservers();
/*logger.info("Transmission finished");*/
}
}, simulation.getSimulationTime() + duration);
}
};
if (simulation.isSimulationThread()) {
startTransmission.run();
} else {
simulation.invokeSimulationThread(startTransmission);
}
}
/**
@ -216,34 +262,6 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
radioChannel = channel;
}
public void doActionsBeforeTick() {
long currentTime = myMote.getSimulation().getSimulationTime();
if (outPacketExists) {
outPacketExists = false;
isTransmitting = true;
lastEvent = RadioEvent.TRANSMISSION_STARTED;
lastEventTime = currentTime;
transmissionEndTime = currentTime + outPacketDuration;
this.setChanged();
this.notifyObservers();
// Deliver packet right away
packetFromMote = outPacket;
lastEvent = RadioEvent.PACKET_TRANSMITTED;
this.setChanged();
this.notifyObservers();
}
if (isTransmitting && currentTime >= transmissionEndTime) {
isTransmitting = false;
lastEvent = RadioEvent.TRANSMISSION_FINISHED;
lastEventTime = currentTime;
this.setChanged();
this.notifyObservers();
}
}
public JPanel getInterfaceVisualizer() {
// Location
JPanel panel = new JPanel();
@ -318,7 +336,7 @@ public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks {
}
public Mote getMote() {
return myMote;
return mote;
}
public boolean isReceiverOn() {

View file

@ -24,17 +24,29 @@
* (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.5 2009/09/17 11:12:25 fros4943 Exp $
* $Id: AbstractApplicationMote.java,v 1.6 2009/10/28 14:38:02 fros4943 Exp $
*/
package se.sics.cooja.motes;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface;
import se.sics.cooja.MoteInterfaceHandler;
import se.sics.cooja.MoteMemory;
import se.sics.cooja.MoteType;
import se.sics.cooja.RadioPacket;
import se.sics.cooja.SectionMoteMemory;
import se.sics.cooja.Simulation;
import se.sics.cooja.interfaces.ApplicationRadio;
import se.sics.cooja.interfaces.Radio;
/**
@ -44,8 +56,7 @@ import se.sics.cooja.interfaces.Radio;
*
* @author Fredrik Osterlind
*/
public abstract class AbstractApplicationMote implements Mote {
public abstract class AbstractApplicationMote extends AbstractWakeupMote implements Mote {
private static Logger logger = Logger.getLogger(AbstractApplicationMote.class);
private MoteType moteType = null;
@ -56,39 +67,31 @@ public abstract class AbstractApplicationMote implements Mote {
private Simulation simulation = null;
/* Observe our own radio for incoming radio packets */
private Observer radioDataObserver = new Observer() {
public void update(Observable obs, Object obj) {
if (getInterfaces().getRadio().getLastEvent() != Radio.RadioEvent.RECEPTION_FINISHED) {
return;
ApplicationRadio radio = (ApplicationRadio) obs;
if (radio.getLastEvent() == Radio.RadioEvent.RECEPTION_FINISHED) {
receivedPacket(radio.getLastPacketReceived());
} else if (radio.getLastEvent() == Radio.RadioEvent.TRANSMISSION_FINISHED) {
sentPacket(radio.getLastPacketTransmitted());
}
/* 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 abstract void receivedPacket(RadioPacket p);
public abstract void sentPacket(RadioPacket p);
public AbstractApplicationMote() {
}
public AbstractApplicationMote(MoteType moteType, Simulation sim) {
this.simulation = sim;
this.moteType = moteType;
// Create memory
this.memory = new SectionMoteMemory(new Properties());
// Create mote interfaces
this.moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses());
if (moteInterfaces.getRadio() != null) {
moteInterfaces.getRadio().addObserver(radioDataObserver);
}
this.moteInterfaces.getRadio().addObserver(radioDataObserver);
requestImmediateWakeup();
}
public MoteInterfaceHandler getInterfaces() {
@ -123,28 +126,14 @@ public abstract class AbstractApplicationMote implements Mote {
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() {
Vector<Element> config = new Vector<Element>();
ArrayList<Element> config = new ArrayList<Element>();
Element element;
// We need to save the mote type identifier
element = new Element("motetype_identifier");
element.setText(getType().getIdentifier());
config.add(element);
// Interfaces
for (MoteInterface moteInterface: moteInterfaces.getInterfaces()) {
element = new Element("interface_config");
element.setText(moteInterface.getClass().getName());
@ -161,8 +150,6 @@ public abstract class AbstractApplicationMote implements Mote {
public boolean setConfigXML(Simulation simulation,
Collection<Element> configXML, boolean visAvailable) {
// Set initial configuration
this.simulation = simulation;
this.memory = new SectionMoteMemory(new Properties());
@ -171,7 +158,8 @@ public abstract class AbstractApplicationMote implements Mote {
if (name.equals("motetype_identifier")) {
moteType = simulation.getMoteType(element.getText());
this.moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses());
moteInterfaces = new MoteInterfaceHandler(this, moteType.getMoteInterfaceClasses());
moteInterfaces.getRadio().addObserver(radioDataObserver);
} else if (name.equals("interface_config")) {
Class<? extends MoteInterface> moteInterfaceClass =
simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim());
@ -184,17 +172,17 @@ public abstract class AbstractApplicationMote implements Mote {
MoteInterface moteInterface = moteInterfaces.getInterfaceOfType(moteInterfaceClass);
moteInterface.setConfigXML(element.getChildren(), visAvailable);
}
}
requestImmediateWakeup();
return true;
}
public int getID() {
return -1;
return moteInterfaces.getMoteID().getMoteID();
}
public String toString() {
return "Application Mote, ID=" + getID();
return "AppMote " + getID();
}
}

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: AbstractApplicationMoteType.java,v 1.3 2009/03/09 15:38:10 fros4943 Exp $
* $Id: AbstractApplicationMoteType.java,v 1.4 2009/10/28 14:38:02 fros4943 Exp $
*/
package se.sics.cooja.motes;
@ -36,39 +36,40 @@ import java.io.File;
import java.util.*;
import javax.swing.*;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.ApplicationRadio;
import se.sics.cooja.interfaces.MoteID;
import se.sics.cooja.interfaces.Position;
@ClassDescription("Application Mote Type")
public abstract class AbstractApplicationMoteType implements MoteType {
private static Logger logger = Logger.getLogger(AbstractApplicationMoteType.class);
// Mote type specific data
private String identifier = null;
private String description = null;
private Class<? extends MoteInterface>[] moteInterfaces = null;
// Type specific class configuration
private ProjectConfig myConfig = null;
private String identifier = null;
private String description = null;
private final Class<? extends MoteInterface>[] moteInterfaceClasses =
new Class[] { SimpleMoteID.class, Position.class, ApplicationRadio.class };
public AbstractApplicationMoteType() {
super();
}
public AbstractApplicationMoteType(String identifier) {
super();
this.identifier = identifier;
description = "Application Mote Type #" + identifier;
this.description = "Application Mote Type #" + identifier;
}
public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) {
if (identifier == null) {
// Create unique identifier
/* Create unique identifier */
int counter = 0;
boolean identifierOK = false;
while (!identifierOK) {
@ -86,16 +87,9 @@ public abstract class AbstractApplicationMoteType implements MoteType {
}
}
}
if (description == null) {
// Create description
description = "Application Mote Type #" + identifier;
}
moteInterfaces = new Class[2];
moteInterfaces[0] = Position.class;
moteInterfaces[1] = ApplicationRadio.class;
return true;
}
@ -116,11 +110,11 @@ public abstract class AbstractApplicationMoteType implements MoteType {
}
public Class<? extends MoteInterface>[] getMoteInterfaceClasses() {
return moteInterfaces;
return moteInterfaceClasses;
}
public void setMoteInterfaceClasses(Class<? extends MoteInterface>[] moteInterfaces) {
this.moteInterfaces = moteInterfaces;
throw new RuntimeException("Can not change the mote interface classes");
}
public JPanel getTypeVisualizer() {
@ -152,7 +146,7 @@ public abstract class AbstractApplicationMoteType implements MoteType {
smallPane.add(BorderLayout.WEST, label);
panel.add(smallPane);
for (Class moteInterface : moteInterfaces) {
for (Class<? extends MoteInterface> moteInterface : moteInterfaceClasses) {
smallPane = new JPanel(new BorderLayout());
label = new JLabel(moteInterface.getSimpleName());
smallPane.add(BorderLayout.EAST, label);
@ -193,61 +187,55 @@ public abstract class AbstractApplicationMoteType implements MoteType {
}
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
ArrayList<Element> config = new ArrayList<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 : getMoteInterfaceClasses()) {
element = new Element("moteinterface");
element.setText(moteInterface.getName());
config.add(element);
}
return config;
}
public boolean setConfigXML(Simulation simulation,
Collection<Element> configXML, boolean visAvailable) {
ArrayList<Class<? extends MoteInterface>> moteInterfacesList = new ArrayList<Class<? extends MoteInterface>>();
for (Element element : configXML) {
String name = element.getName();
if (name.equals("identifier")) {
identifier = element.getText();
} else if (name.equals("description")) {
description = element.getText();
} else if (name.equals("moteinterface")) {
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 {
moteInterfacesList.add(moteInterfaceClass);
}
} else {
logger.fatal("Unrecognized entry in loaded configuration: " + name);
logger.fatal("Unrecognized entry: " + name);
}
}
moteInterfaces = new Class[moteInterfacesList.size()];
moteInterfacesList.toArray(moteInterfaces);
boolean createdOK = configureAndInit(GUI.getTopParentContainer(), simulation, visAvailable);
return createdOK;
}
public static class SimpleMoteID extends MoteID {
private int id = -1;
public SimpleMoteID(Mote mote) {
}
public int getMoteID() {
return id;
}
public void setMoteID(int newID) {
this.id = newID;
}
public double energyConsumption() {
return 0;
}
public JPanel getInterfaceVisualizer() {
return null;
}
public void releaseInterfaceVisualizer(JPanel panel) {
}
}
}

View file

@ -1,61 +0,0 @@
/*
* 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.
*
* 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: DisturberMote.java,v 1.7 2009/03/09 15:38:10 fros4943 Exp $
*/
package se.sics.cooja.motes;
import org.apache.log4j.Logger;
import se.sics.cooja.*;
/**
* 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 extends AbstractApplicationMote {
private static Logger logger = Logger.getLogger(DisturberMote.class);
public DisturberMote() {
super();
}
public DisturberMote(MoteType moteType, Simulation simulation) {
super(moteType, simulation);
}
public String toString() {
if (getInterfaces().getMoteID() != null) {
return "Disturber Mote, ID=" + getInterfaces().getMoteID().getMoteID();
} else {
return "Disturber Mote, ID=null";
}
}
}

View file

@ -32,13 +32,26 @@
package se.sics.cooja.motes;
import java.awt.Container;
import org.apache.log4j.Logger;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position;
import se.sics.cooja.AbstractionLevelDescription;
import se.sics.cooja.COOJARadioPacket;
import se.sics.cooja.ClassDescription;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteTimeEvent;
import se.sics.cooja.MoteType;
import se.sics.cooja.RadioPacket;
import se.sics.cooja.Simulation;
import se.sics.cooja.interfaces.ApplicationRadio;
import se.sics.cooja.interfaces.Radio.RadioEvent;
/**
* Simple application-level mote that periodically transmits dummy radio packets
* on a configurable radio channel, interfering all surrounding radio communication.
* on all radio channels (-1), interfering all surrounding radio communication.
*
* This mote type also implements the mote functionality ("mote software"),
* and can be used as an example of implementing application-level mote.
*
* @see DisturberMote
* @author Fredrik Osterlind, Thiemo Voigt
@ -54,22 +67,67 @@ public class DisturberMoteType extends AbstractApplicationMoteType {
public DisturberMoteType(String identifier) {
super(identifier);
setDescription("Disturber Mote Type #" + getIdentifier());
setDescription("Disturber Mote Type #" + identifier);
}
public boolean configureAndInit(Container parentContainer,
Simulation simulation, boolean visAvailable) {
if (!super.configureAndInit(parentContainer, simulation, visAvailable)) {
return false;
}
setDescription("Disturber Mote Type #" + getIdentifier());
return true;
}
public Mote generateMote(Simulation simulation) {
return new DisturberMote(this, simulation);
}
public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) {
super.configureAndInit(parentContainer, simulation, visAvailable);
setDescription("Disturber Mote Type #" + getIdentifier());
public static class DisturberMote extends AbstractApplicationMote {
private ApplicationRadio radio = null;
private final RadioPacket radioPacket = new COOJARadioPacket(new byte[] {
0x01, 0x02, 0x03, 0x04, 0x05
});
private final static long DELAY = 1*Simulation.MILLISECOND;
private final static long DURATION = 1*Simulation.MILLISECOND;
public DisturberMote() {
super();
}
public DisturberMote(MoteType moteType, Simulation simulation) {
super(moteType, simulation);
}
public void execute(long time) {
if (radio == null) {
radio = (ApplicationRadio) getInterfaces().getRadio();
}
/* Start sending interfering traffic */
/*logger.info("Sending radio packet on channel: " + radio.getChannel());*/
radio.startTransmittingPacket(radioPacket, DURATION);
}
Class<? extends MoteInterface>[] moteInterfaces = new Class[2];
moteInterfaces[0] = Position.class;
moteInterfaces[1] = DisturberRadio.class;
setMoteInterfaceClasses(moteInterfaces);
public boolean tick(long simTime) {
throw new RuntimeException("Obsolete method");
}
return true;
}
public void receivedPacket(RadioPacket p) {
/* Ignore */
}
public void sentPacket(RadioPacket p) {
/* Send another packet after a small pause */
getSimulation().scheduleEvent(new MoteTimeEvent(this, 0) {
public void execute(long t) {
/*logger.info("Sending another radio packet on channel: " + radio.getChannel());*/
radio.startTransmittingPacket(radioPacket, DURATION);
}
}, getSimulation().getSimulationTime() + DELAY);
}
public String toString() {
return "Disturber " + getID();
}
}
}

View file

@ -1,276 +0,0 @@
/*
* 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.
*
* 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: DisturberRadio.java,v 1.11 2009/04/16 14:26:35 fros4943 Exp $
*/
package se.sics.cooja.motes;
import java.text.NumberFormat;
import java.util.*;
import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.*;
/**
* This radio transmits data packet over and over again on a configurable channel.
*
* @author Fredrik Osterlind, Thiemo Voigt
*/
public class DisturberRadio extends Radio implements PolledBeforeAllTicks {
private Mote myMote;
private static Logger logger = Logger.getLogger(DisturberRadio.class);
private final RadioPacket packetFromMote = new COOJARadioPacket(new byte[] { 1, 2, 3, 4, 5 });
private boolean transmitting = false;
private int distChannel = -1; // channel mote is disturbing
private long transEndTime = 0;
private RadioEvent lastEvent = RadioEvent.UNKNOWN;
private long lastEventTime = 0;
public static int TRANSMISSION_INTERVAL = 100;
public static int TRANSMISSION_DURATION = 98;
/**
* Creates an interface to the radio at mote.
*
* @param mote
* Radio's mote.
* @see Mote
* @see se.sics.cooja.MoteInterfaceHandler
*/
public DisturberRadio(Mote mote) {
this.myMote = mote;
}
/* Packet radio support */
public RadioPacket getLastPacketTransmitted() {
return packetFromMote;
}
public RadioPacket getLastPacketReceived() {
return null;
}
public void setReceivedPacket(RadioPacket packet) {
}
/* General radio support */
public void signalReceptionStart() {
}
public void signalReceptionEnd() {
}
public boolean isTransmitting() {
return transmitting;
}
public long getTransmissionEndTime() {
return transEndTime;
}
public boolean isReceiving() {
return false;
}
public int getChannel() {
return distChannel;
}
public Position getPosition() {
return myMote.getInterfaces().getPosition();
}
public RadioEvent getLastEvent() {
return lastEvent;
}
public void interfereAnyReception() {
}
public boolean isInterfered() {
return false;
}
public double getCurrentOutputPower() {
// TODO Implement method
logger.warn("Not implemeted, always returning 1.5 dBm");
return 1.5;
}
public int getOutputPowerIndicatorMax() {
return 100;
}
public int getCurrentOutputPowerIndicator() {
return 100;
}
public double getCurrentSignalStrength() {
return 2.0;
}
public void setCurrentSignalStrength(double signalStrength) {
}
public void doActionsBeforeTick() {
long currentTime = myMote.getSimulation().getSimulationTime();
if (!transmitting && currentTime % TRANSMISSION_INTERVAL == 0) {
if (distChannel < 0) {
return;
}
transmitting = true;
lastEvent = RadioEvent.TRANSMISSION_STARTED;
lastEventTime = currentTime;
transEndTime = currentTime + TRANSMISSION_DURATION;
this.setChanged();
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();
this.notifyObservers();
}
}
public JPanel getInterfaceVisualizer() {
// Location
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel statusLabel = new JLabel("");
final JLabel lastEventLabel = new JLabel("");
final JLabel channelLabel = new JLabel("");
final JFormattedTextField channelPicker = new JFormattedTextField(NumberFormat.getIntegerInstance());
panel.add(statusLabel);
panel.add(lastEventLabel);
panel.add(Box.createVerticalStrut(3));
panel.add(channelLabel);
panel.add(channelPicker);
panel.add(Box.createVerticalGlue());
channelPicker.setValue(distChannel);
channelPicker.setColumns(3);
channelPicker.setText(Integer.toString(distChannel));
final Observer observer = new Observer() {
public void update(Observable obs, Object obj) {
if (isTransmitting()) {
statusLabel.setText("Transmitting now!");
} else {
statusLabel.setText("Disturber resting...");
}
channelLabel.setText("Channel: " + getChannel());
lastEventLabel.setText("Last event (time=" + lastEventTime + "): " + lastEvent);
}
};
this.addObserver(observer);
channelPicker.addPropertyChangeListener("value", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
distChannel = ((Number) channelPicker.getValue()).intValue();
observer.update(null, null);
}
});
observer.update(null, null);
// Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer);
return panel;
}
public void releaseInterfaceVisualizer(JPanel panel) {
Observer observer = (Observer) panel.getClientProperty("intf_obs");
if (observer == null) {
logger.fatal("Error when releasing panel, observer is null");
return;
}
this.deleteObserver(observer);
}
public double energyConsumption() {
return 0;
}
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
Element element;
element = new Element("channel");
element.setText(Integer.toString(distChannel));
config.add(element);
return config;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
String name = element.getName();
if (name.equals("channel")) {
distChannel = Integer.parseInt(element.getText());
} else {
logger.fatal("Read unknown configuration: " + name);
}
}
}
public Mote getMote() {
return myMote;
}
public boolean isReceiverOn() {
return true;
}
}

View file

@ -1,69 +0,0 @@
/*
* 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.
*
* 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.8 2009/03/09 15:38:10 fros4943 Exp $
*/
package se.sics.cooja.motes;
import java.util.Random;
import org.apache.log4j.Logger;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position;
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 */
public DummyMote() {
super();
}
public DummyMote(MoteType moteType, Simulation simulation) {
super(moteType, simulation);
}
public boolean tick(long simTime) {
/* Dummy task: move randomly */
if (random.nextDouble() > 0.9) {
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 String toString() {
return "Dummy Mote";
}
}

View file

@ -1,62 +0,0 @@
/*
* Copyright (c) 2009, 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.
*
* 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.
*
*
*/
package se.sics.cooja.motes;
import java.awt.Container;
import org.apache.log4j.Logger;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.Position;
@ClassDescription("Dummy Mote Type")
@AbstractionLevelDescription("Application level")
public class DummyMoteType extends AbstractApplicationMoteType {
private static Logger logger = Logger.getLogger(DummyMoteType.class);
public DummyMoteType() {
super();
}
public DummyMoteType(String identifier) {
super(identifier);
setDescription("Dummy Mote Type #" + getIdentifier());
}
public Mote generateMote(Simulation simulation) {
return new DummyMote(this, simulation);
}
public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) {
super.configureAndInit(parentContainer, simulation, visAvailable);
setDescription("Dummy Mote Type #" + getIdentifier());
return true;
}
}