tick method returns true if mote can be ticked again immediately

This commit is contained in:
fros4943 2008-03-31 15:22:41 +00:00
parent 7579904492
commit 43ef84ad85
4 changed files with 93 additions and 83 deletions

View file

@ -1,7 +1,7 @@
/*
* 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,
@ -12,7 +12,7 @@
* 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
@ -23,8 +23,8 @@
* 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: Mote.java,v 1.4 2007/01/10 14:57:42 fros4943 Exp $
*
* $Id: Mote.java,v 1.5 2008/03/31 15:22:42 fros4943 Exp $
*/
package se.sics.cooja;
@ -35,17 +35,17 @@ import org.jdom.Element;
/**
* A simulated mote.
*
*
* A mote is always in some state, describing the status of the CPU etc. Motes
* in different states may be handled differently by for example the simulation
* loop and plugins.
*
*
* All motes must also have an interface handler, a mote type and a mote memory.
*
*
* @see se.sics.cooja.MoteInterfaceHandler
* @see se.sics.cooja.MoteMemory
* @see se.sics.cooja.MoteType
*
*
* @author Fredrik Osterlind
*/
public interface Mote {
@ -65,7 +65,7 @@ public interface Mote {
/**
* Tries to change state to given argument. A dead mote can typically not
* change state, while a sleeping or active mote can.
*
*
* @param newState
* New state of mote.
*/
@ -78,7 +78,7 @@ public interface Mote {
/**
* Adds new state observer. This observer is notified if mote changes state.
*
*
* @see #deleteStateObserver(Observer)
* @param newObserver
* New observer
@ -87,7 +87,7 @@ public interface Mote {
/**
* Delete existing state observer.
*
*
* @see #addStateObserver(Observer)
* @param newObserver
* Registered state observer
@ -96,7 +96,7 @@ public interface Mote {
/**
* Returns the interface handler of this mote.
*
*
* @see #setInterfaces(MoteInterfaceHandler)
* @return Mote interface handler
*/
@ -104,7 +104,7 @@ public interface Mote {
/**
* Sets the interface handler of this mote.
*
*
* @param moteInterfaceHandler
* New interface handler
* @see #getInterfaces()
@ -113,7 +113,7 @@ public interface Mote {
/**
* Returns the memory of this mote.
*
*
* @see #setMemory(MoteMemory)
* @return Mote memory
*/
@ -121,7 +121,7 @@ public interface Mote {
/**
* Sets the memory of this mote.
*
*
* @see #getMemory()
* @param memory
* Mote memory
@ -130,7 +130,7 @@ public interface Mote {
/**
* Returns mote type.
*
*
* @see #setType(MoteType)
* @return Mote type
*/
@ -138,7 +138,7 @@ public interface Mote {
/**
* Sets mote type to given argument.
*
*
* @see #getType()
* @param type
* New type
@ -147,7 +147,7 @@ public interface Mote {
/**
* Returns simulation which holds this mote.
*
*
* @see #setSimulation(Simulation)
* @return Simulation
*/
@ -155,7 +155,7 @@ public interface Mote {
/**
* Sets the simulation which holds this mote.
*
*
* @see #getSimulation()
* @param simulation
* Simulation
@ -164,21 +164,22 @@ public interface Mote {
/**
* Ticks this mote and increases any internal time to given argument.
*
*
* Each mote implementation may handle calls to this method differently, but
* typically the simulated mote should at least handle one event.
*
*
* This method is responsible for updating the mote interfaces, the memory and
* the mote state.
*
*
* A call to this method typically polls all interfaces, activates the memory,
* lets the underlying mote software handle one event, fetches the updated
* memory and finally polls all interfaces again.
*
*
* @param simTime
* New simulation time
* @return True is mote accepts another immediate tick
*/
public void tick(int simTime);
public boolean tick(int simTime);
/**
* Returns XML elements representing the current config of this mote. This is
@ -186,7 +187,7 @@ public interface Mote {
* file. For example a mote may return the configs of all its interfaces. This
* method should however not return state specific information such as the
* mote state. (All nodes are restarted when loading a simulation.)
*
*
* @see #setConfigXML(Simulation, Collection, boolean)
* @return XML elements representing the current mote config
*/
@ -194,12 +195,12 @@ public interface Mote {
/**
* Sets the current mote config depending on the given XML elements.
*
*
* @param simulation
* Simulation holding this mote
* @param configXML
* Config XML elements
*
*
* @see #getConfigXML()
*/
public abstract boolean setConfigXML(Simulation simulation,

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ContikiMote.java,v 1.6 2007/07/13 09:08:24 fros4943 Exp $
* $Id: ContikiMote.java,v 1.7 2008/03/31 15:22:43 fros4943 Exp $
*/
package se.sics.cooja.contikimote;
@ -42,15 +42,15 @@ import se.sics.cooja.*;
* a loaded shared library and JNI.
* It contains a section mote memory, a mote interface handler and a
* Contiki mote type.
*
*
* The mote type is responsible for the connection to the loaded
* Contiki system.
*
*
* When ticked a Contiki mote polls all interfaces, copies the mote
* memory to the core, lets the Contiki system handle one event,
* fetches the updated memory and finally polls all interfaces again.
* memory to the core, lets the Contiki system handle one event,
* fetches the updated memory and finally polls all interfaces again.
* The mote state is also updated during a mote tick.
*
*
* @author Fredrik Osterlind
*/
public class ContikiMote implements Mote {
@ -75,21 +75,21 @@ public class ContikiMote implements Mote {
}
private StateObservable stateObservable = new StateObservable();
/**
* Creates a new uninitialized Contiki mote.
*
*
* This mote needs at least a type, a memory, a mote interface handler
* and to be connected to a simulation.
*/
public ContikiMote() {
}
/**
* Creates a new mote of given type.
* Both the initial mote memory and the interface handler
* are supplied from the mote type.
*
*
* @param moteType Mote type
* @param sim Mote's simulation
*/
@ -97,8 +97,8 @@ public class ContikiMote implements Mote {
this.mySim = sim;
this.myType = moteType;
this.myMemory = moteType.createInitialMemory();
this.myInterfaceHandler = new MoteInterfaceHandler((Mote) this, moteType.getMoteInterfaces());
this.myInterfaceHandler = new MoteInterfaceHandler(this, moteType.getMoteInterfaces());
myState = State.ACTIVE;
}
@ -116,7 +116,7 @@ public class ContikiMote implements Mote {
myState = newState;
stateObservable.stateChanged();
}
if (myState == State.DEAD) {
mySim.getRadioMedium().unregisterMote(this, mySim);
}
@ -133,11 +133,11 @@ public class ContikiMote implements Mote {
public void deleteStateObserver(Observer newObserver) {
stateObservable.deleteObserver(newObserver);
}
public MoteInterfaceHandler getInterfaces() {
return myInterfaceHandler;
}
public void setInterfaces(MoteInterfaceHandler newInterfaces) {
myInterfaceHandler = newInterfaces;
}
@ -176,12 +176,13 @@ public class ContikiMote implements Mote {
*
* @param simTime Current simulation time
*/
public void tick(int simTime) {
public boolean tick(int simTime) {
State currentState = getState();
// If mote is dead, do nothing at all
if (currentState == State.DEAD)
return;
if (currentState == State.DEAD) {
return false;
}
// If mote is sleeping and has a wake up time, should it wake up now?
if (currentState == State.LPM && wakeUpTime > 0 && wakeUpTime <= simTime) {
@ -204,7 +205,7 @@ public class ContikiMote implements Mote {
// If mote is still active, complete this tick
currentState = getState();
if (currentState == State.ACTIVE) {
// Copy mote memory to core
myType.setCoreMemory(myMemory);
@ -228,18 +229,20 @@ public class ContikiMote implements Mote {
int processRunValue = myMemory.getIntValueOf("simProcessRunValue");
int etimersPending = myMemory.getIntValueOf("simEtimerPending");
int nextExpirationTime = myMemory.getIntValueOf("simNextExpirationTime");
if (processRunValue == 0 && etimersPending == 0) {
setState(State.LPM);
wakeUpTime = 0;
}
if (processRunValue == 0 && etimersPending == 1 && nextExpirationTime > 0) {
setState(State.LPM);
wakeUpTime = nextExpirationTime;
}
}
return false;
}
/**
@ -250,9 +253,9 @@ public class ContikiMote implements Mote {
*/
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
Element element;
// Mote type identifier
element = new Element("motetype_identifier");
element.setText(getType().getIdentifier());
@ -269,7 +272,7 @@ public class ContikiMote implements Mote {
config.add(element);
}
}
// Passive interface configs (if any)
for (MoteInterface moteInterface: getInterfaces().getAllPassiveInterfaces()) {
element = new Element("interface_config");
@ -281,47 +284,49 @@ public class ContikiMote implements Mote {
config.add(element);
}
}
return config;
}
public boolean setConfigXML(Simulation simulation, Collection<Element> configXML, boolean visAvailable) {
mySim = simulation;
myState = State.ACTIVE;
for (Element element: configXML) {
String name = element.getName();
if (name.equals("motetype_identifier")) {
myType = (ContikiMoteType) simulation.getMoteType(element.getText());
myMemory = myType.createInitialMemory();
myInterfaceHandler = new MoteInterfaceHandler((Mote) this, myType.getMoteInterfaces());
myInterfaceHandler = new MoteInterfaceHandler(this, myType.getMoteInterfaces());
} else if (name.equals("interface_config")) {
Class<? extends MoteInterface> moteInterfaceClass =
Class<? extends MoteInterface> moteInterfaceClass =
simulation.getGUI().tryLoadClass(this, MoteInterface.class, element.getText().trim());
if (moteInterfaceClass == null) {
logger.fatal("Could not load mote interface class: " + element.getText().trim());
return false;
}
MoteInterface moteInterface = myInterfaceHandler.getInterfaceOfType(moteInterfaceClass);
if (moteInterface != null)
if (moteInterface != null) {
moteInterface.setConfigXML(element.getChildren(), visAvailable);
else
} else {
logger.warn("Can't restore configuration for non-existing interface: " + moteInterfaceClass.getName());
}
}
}
return true;
}
public String toString() {
if (getInterfaces().getMoteID() != null) {
return "Contiki Mote, ID=" + getInterfaces().getMoteID().getMoteID();
} else
} else {
return "Contiki Mote, ID=null";
}
}
}

View file

@ -1,7 +1,7 @@
/*
* 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,
@ -12,7 +12,7 @@
* 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
@ -23,8 +23,8 @@
* 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.2 2007/01/09 10:01:14 fros4943 Exp $
*
* $Id: DisturberMote.java,v 1.3 2008/03/31 15:22:42 fros4943 Exp $
*/
package se.sics.cooja.motes;
@ -42,7 +42,7 @@ 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.
*
*
* @author Fredrik Osterlind, Thiemo Voigt
*/
public class DisturberMote implements Mote {
@ -70,7 +70,7 @@ public class DisturberMote implements Mote {
/**
* 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
@ -141,11 +141,12 @@ public class DisturberMote implements Mote {
this.mySim = simulation;
}
public void tick(int simTime) {
public boolean tick(int simTime) {
myInterfaceHandler.doPassiveActionsBeforeTick();
myInterfaceHandler.doActiveActionsBeforeTick();
myInterfaceHandler.doActiveActionsAfterTick();
myInterfaceHandler.doPassiveActionsAfterTick();
return false;
}
public Collection<Element> getConfigXML() {
@ -173,7 +174,7 @@ public class DisturberMote implements Mote {
config.add(element);
}
}
// Passive interface configs (if any)
for (MoteInterface moteInterface: getInterfaces().getAllPassiveInterfaces()) {
element = new Element("interface_config");
@ -201,7 +202,7 @@ public class DisturberMote implements Mote {
myDisturberRadio = new DisturberRadio(this);
myInterfaceHandler.addActiveInterface(myDisturberRadio);
for (Element element : configXML) {
String name = element.getName();
@ -228,8 +229,9 @@ public class DisturberMote implements Mote {
public String toString() {
if (getInterfaces().getMoteID() != null) {
return "Disturber Mote, ID=" + getInterfaces().getMoteID().getMoteID();
} else
} else {
return "Disturber Mote, ID=null";
}
}
}

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: DummyMote.java,v 1.3 2007/01/09 10:01:14 fros4943 Exp $
* $Id: DummyMote.java,v 1.4 2008/03/31 15:22:41 fros4943 Exp $
*/
package se.sics.cooja.motes;
@ -46,17 +46,17 @@ 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 {
@ -79,7 +79,7 @@ public class DummyMote implements Mote {
/**
* 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
@ -146,7 +146,7 @@ public class DummyMote implements Mote {
this.mySim = simulation;
}
public void tick(int simTime) {
public boolean tick(int simTime) {
// Perform some dummy task
if (myRandom.nextDouble() > 0.9) {
@ -157,6 +157,7 @@ public class DummyMote implements Mote {
+ myRandom.nextDouble() - 0.5, myPosition.getZCoordinate()
+ myRandom.nextDouble() - 0.5);
}
return false;
}
public Collection<Element> getConfigXML() {
@ -215,8 +216,9 @@ public class DummyMote implements Mote {
public String toString() {
if (getInterfaces().getMoteID() != null) {
return "Dummy Mote, ID=" + getInterfaces().getMoteID().getMoteID();
} else
} else {
return "Dummy Mote, ID=null";
}
}
}