changed mote state to enum type instead of int

This commit is contained in:
fros4943 2006-09-26 13:08:05 +00:00
parent 7ef9271567
commit 17431ba16c
13 changed files with 58 additions and 63 deletions

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: Mote.java,v 1.1 2006/08/21 12:12:56 fros4943 Exp $ * $Id: Mote.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -50,21 +50,15 @@ import org.jdom.Element;
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public interface Mote { public interface Mote {
/**
* Active state.
*/
public static int STATE_ACTIVE = 1;
/** /**
* Sleeping state. * Possible mote states
*/ */
public static int STATE_LPM = 2; public static enum State {
ACTIVE, // Active state
/** LPM, // Low power mode (sleeping)
* Dead state (may be out of batteries). DEAD // Dead (for example out of batteries)
*/ }
public static int STATE_DEAD = 3;
/** /**
* Tries to change state to given argument. * Tries to change state to given argument.
@ -72,12 +66,12 @@ public interface Mote {
* *
* @param newState New state of mote. * @param newState New state of mote.
*/ */
public void setState(int newState); public void setState(State newState);
/** /**
* @return Current mote state * @return Current mote state
*/ */
public int getState(); public State getState();
/** /**
* Adds new state observer. * Adds new state observer.

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: ContikiMote.java,v 1.2 2006/08/23 12:18:27 fros4943 Exp $ * $Id: ContikiMote.java,v 1.3 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote; package se.sics.cooja.contikimote;
@ -64,7 +64,7 @@ public class ContikiMote implements Mote {
// Time to wake up if sleeping // Time to wake up if sleeping
private int wakeUpTime = 0; private int wakeUpTime = 0;
private int myState = STATE_ACTIVE; private State myState = State.ACTIVE;
// State observable // State observable
private class StateObservable extends Observable { private class StateObservable extends Observable {
@ -99,26 +99,26 @@ public class ContikiMote implements Mote {
this.myMemory = moteType.createInitialMemory(); this.myMemory = moteType.createInitialMemory();
this.myInterfaceHandler = new MoteInterfaceHandler((Mote) this, moteType.getMoteInterfaces()); this.myInterfaceHandler = new MoteInterfaceHandler((Mote) this, moteType.getMoteInterfaces());
myState = STATE_ACTIVE; myState = State.ACTIVE;
} }
public void setState(int newState) { public void setState(State newState) {
if (myState == STATE_DEAD) { if (myState == State.DEAD) {
return; return;
} }
if (myState == STATE_ACTIVE && newState != STATE_ACTIVE) { if (myState == State.ACTIVE && newState != State.ACTIVE) {
myState = newState; myState = newState;
stateObservable.stateChanged(); stateObservable.stateChanged();
} }
if (myState == STATE_LPM && newState != STATE_LPM) { if (myState == State.LPM && newState != State.LPM) {
myState = newState; myState = newState;
stateObservable.stateChanged(); stateObservable.stateChanged();
} }
} }
public int getState() { public State getState() {
return myState; return myState;
} }
@ -175,17 +175,17 @@ public class ContikiMote implements Mote {
public void tick(int simTime) { public void tick(int simTime) {
// If mote is dead, do nothing at all // If mote is dead, do nothing at all
if (getState() == STATE_DEAD) if (getState() == State.DEAD)
return; return;
// If mote is sleeping and has a wake up time, should it wake up now? // If mote is sleeping and has a wake up time, should it wake up now?
if (getState() == STATE_LPM && wakeUpTime > 0 && wakeUpTime <= simTime) { if (getState() == State.LPM && wakeUpTime > 0 && wakeUpTime <= simTime) {
setState(STATE_ACTIVE); setState(State.ACTIVE);
wakeUpTime = 0; wakeUpTime = 0;
} }
// If mote is active.. // If mote is active..
if (getState() == STATE_ACTIVE) { if (getState() == State.ACTIVE) {
// Let all active interfaces act before tick // Let all active interfaces act before tick
// Observe that each interface may put the mote to sleep at this point // Observe that each interface may put the mote to sleep at this point
myInterfaceHandler.doActiveActionsBeforeTick(); myInterfaceHandler.doActiveActionsBeforeTick();
@ -196,7 +196,7 @@ public class ContikiMote implements Mote {
// If mote is still active, complete this tick // If mote is still active, complete this tick
if (getState() == STATE_ACTIVE) { if (getState() == State.ACTIVE) {
// Copy mote memory to core // Copy mote memory to core
myType.setCoreMemory(myMemory); myType.setCoreMemory(myMemory);
@ -216,19 +216,19 @@ public class ContikiMote implements Mote {
myInterfaceHandler.doPassiveActionsAfterTick(); myInterfaceHandler.doPassiveActionsAfterTick();
// If mote is awake, should it go to sleep? // If mote is awake, should it go to sleep?
if (getState() == STATE_ACTIVE) { if (getState() == State.ACTIVE) {
// Check if this mote should sleep (no more pending timers or processes to poll) // Check if this mote should sleep (no more pending timers or processes to poll)
int processRunValue = myMemory.getIntValueOf("simProcessRunValue"); int processRunValue = myMemory.getIntValueOf("simProcessRunValue");
int etimersPending = myMemory.getIntValueOf("simEtimerPending"); int etimersPending = myMemory.getIntValueOf("simEtimerPending");
int nextExpirationTime = myMemory.getIntValueOf("simNextExpirationTime"); int nextExpirationTime = myMemory.getIntValueOf("simNextExpirationTime");
if (processRunValue == 0 && etimersPending == 0) { if (processRunValue == 0 && etimersPending == 0) {
setState(STATE_LPM); setState(State.LPM);
wakeUpTime = 0; wakeUpTime = 0;
} }
if (processRunValue == 0 && etimersPending == 1 && nextExpirationTime > 0) { if (processRunValue == 0 && etimersPending == 1 && nextExpirationTime > 0) {
setState(STATE_LPM); setState(State.LPM);
wakeUpTime = nextExpirationTime; wakeUpTime = nextExpirationTime;
} }
@ -280,7 +280,7 @@ public class ContikiMote implements Mote {
public boolean setConfigXML(Simulation simulation, Collection<Element> configXML) { public boolean setConfigXML(Simulation simulation, Collection<Element> configXML) {
mySim = simulation; mySim = simulation;
myState = STATE_ACTIVE; myState = State.ACTIVE;
for (Element element: configXML) { for (Element element: configXML) {
String name = element.getName(); String name = element.getName();

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: ContikiButton.java,v 1.1 2006/08/21 12:13:05 fros4943 Exp $ * $Id: ContikiButton.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -106,7 +106,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
// If mote is inactive, wake it up // If mote is inactive, wake it up
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
mote.setState(Mote.STATE_ACTIVE); mote.setState(Mote.State.ACTIVE);
setChanged(); setChanged();
notifyObservers(); notifyObservers();
@ -121,7 +121,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
// If mote is inactive, wake it up // If mote is inactive, wake it up
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
mote.setState(Mote.STATE_ACTIVE); mote.setState(Mote.State.ACTIVE);
setChanged(); setChanged();
notifyObservers(); notifyObservers();

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: ContikiPIR.java,v 1.1 2006/08/21 12:13:05 fros4943 Exp $ * $Id: ContikiPIR.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -109,7 +109,7 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface {
// If mote is inactive, wake it up // If mote is inactive, wake it up
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
mote.setState(Mote.STATE_ACTIVE); mote.setState(Mote.State.ACTIVE);
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();

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: ContikiRS232.java,v 1.1 2006/08/21 12:13:04 fros4943 Exp $ * $Id: ContikiRS232.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -181,7 +181,7 @@ public class ContikiRS232 extends MoteInterface implements ContikiMoteInterface
moteMem.setByteArray("simSerialReceivingData", newData); moteMem.setByteArray("simSerialReceivingData", newData);
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
mote.setState(Mote.STATE_ACTIVE); mote.setState(Mote.State.ACTIVE);
} }

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: ContikiRadio.java,v 1.1 2006/08/21 12:13:05 fros4943 Exp $ * $Id: ContikiRadio.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -151,7 +151,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface {
// If mote is inactive, wake it up // If mote is inactive, wake it up
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
myMote.setState(Mote.STATE_ACTIVE); myMote.setState(Mote.State.ACTIVE);
} }
public void advanceListenState() { public void advanceListenState() {

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: ContikiVib.java,v 1.1 2006/08/21 12:13:05 fros4943 Exp $ * $Id: ContikiVib.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -112,7 +112,7 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface {
// If mote is inactive, wake it up // If mote is inactive, wake it up
if (RAISES_EXTERNAL_INTERRUPT) if (RAISES_EXTERNAL_INTERRUPT)
mote.setState(Mote.STATE_ACTIVE); mote.setState(Mote.State.ACTIVE);
} }
} }

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: UserPlatformsDialog.java,v 1.5 2006/09/06 12:26:33 fros4943 Exp $ * $Id: UserPlatformsDialog.java,v 1.6 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.dialogs; package se.sics.cooja.dialogs;
@ -36,7 +36,6 @@ import java.awt.event.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Vector; import java.util.Vector;
import javax.swing.*; import javax.swing.*;

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: Battery.java,v 1.2 2006/09/26 12:47:06 fros4943 Exp $ * $Id: Battery.java,v 1.3 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -133,11 +133,11 @@ public class Battery extends MoteInterface implements PassiveMoteInterface {
return; return;
// If mote is dead, do nothing // If mote is dead, do nothing
if (mote.getState() == Mote.STATE_DEAD) if (mote.getState() == Mote.State.DEAD)
return; return;
// Check mote state // Check mote state
if (mote.getState() == Mote.STATE_LPM) { if (mote.getState() == Mote.State.LPM) {
// Mote is sleeping. Sum up energy usage. // Mote is sleeping. Sum up energy usage.
double totalEnergyConsumption = 0.0; double totalEnergyConsumption = 0.0;
totalEnergyConsumption += energyConsumptionLPMPerTick; totalEnergyConsumption += energyConsumptionLPMPerTick;
@ -171,7 +171,7 @@ public class Battery extends MoteInterface implements PassiveMoteInterface {
if (getCurrentEnergy() <= 0.0) { if (getCurrentEnergy() <= 0.0) {
setChanged(); setChanged();
notifyObservers(); notifyObservers();
mote.setState(Mote.STATE_DEAD); mote.setState(Mote.State.DEAD);
} }
} }

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: DummyMote.java,v 1.1 2006/08/21 12:13:12 fros4943 Exp $ * $Id: DummyMote.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.motes; package se.sics.cooja.motes;
@ -100,12 +100,12 @@ public class DummyMote implements Mote {
myInterfaceHandler.addPassiveInterface(myPosition); myInterfaceHandler.addPassiveInterface(myPosition);
} }
public void setState(int newState) { public void setState(State newState) {
logger.fatal("Dummy mote can not change state"); logger.fatal("Dummy mote can not change state");
} }
public int getState() { public State getState() {
return Mote.STATE_ACTIVE; return Mote.State.ACTIVE;
} }
public void addStateObserver(Observer newObserver) { public void addStateObserver(Observer newObserver) {

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: MoteInformation.java,v 1.1 2006/08/21 12:13:07 fros4943 Exp $ * $Id: MoteInformation.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -39,6 +39,7 @@ import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.Mote.State;
/** /**
* MoteInformation is a simple information window for motes. * MoteInformation is a simple information window for motes.
@ -102,9 +103,9 @@ public class MoteInformation extends VisPlugin {
label = new JLabel("-- STATE --"); label = new JLabel("-- STATE --");
label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT)); label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT));
smallPane.add(BorderLayout.WEST, label); smallPane.add(BorderLayout.WEST, label);
if (mote.getState() == Mote.STATE_ACTIVE) if (mote.getState() == Mote.State.ACTIVE)
label = new JLabel("active"); label = new JLabel("active");
else if (mote.getState() == Mote.STATE_LPM) else if (mote.getState() == State.LPM)
label = new JLabel("low power mode"); label = new JLabel("low power mode");
else else
label = new JLabel("dead"); label = new JLabel("dead");
@ -207,9 +208,9 @@ public class MoteInformation extends VisPlugin {
// Register as state observer to detect if mote changes state // Register as state observer to detect if mote changes state
mote.addStateObserver(stateObserver = new Observer() { mote.addStateObserver(stateObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
if (mote.getState() == Mote.STATE_ACTIVE) if (mote.getState() == State.ACTIVE)
stateLabel.setText("active"); stateLabel.setText("active");
else if (mote.getState() == Mote.STATE_LPM) else if (mote.getState() == Mote.State.LPM)
stateLabel.setText("low power mode"); stateLabel.setText("low power mode");
else else
stateLabel.setText("dead"); stateLabel.setText("dead");

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: VisBattery.java,v 1.1 2006/08/21 12:13:08 fros4943 Exp $ * $Id: VisBattery.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -96,7 +96,7 @@ public class VisBattery extends Visualizer2D {
} }
public Color[] getColorOf(Mote mote) { public Color[] getColorOf(Mote mote) {
if (mote.getState() == Mote.STATE_DEAD) if (mote.getState() == Mote.State.DEAD)
return new Color[]{Color.RED}; return new Color[]{Color.RED};
Battery battery = mote.getInterfaces().getBattery(); Battery battery = mote.getInterfaces().getBattery();

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: VisState.java,v 1.1 2006/08/21 12:13:07 fros4943 Exp $ * $Id: VisState.java,v 1.2 2006/09/26 13:08:05 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -36,6 +36,7 @@ import java.util.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.Mote.State;
/** /**
* A State Visualizer indicates mote states by painting them in different colors. * A State Visualizer indicates mote states by painting them in different colors.
@ -107,11 +108,11 @@ public class VisState extends Visualizer2D {
Color[] returnColors = new Color[2]; Color[] returnColors = new Color[2];
// If mote is sleeping, make outer circle blue // If mote is sleeping, make outer circle blue
if (mote.getState() == Mote.STATE_LPM) if (mote.getState() == Mote.State.LPM)
returnColors[1] = Color.GRAY; returnColors[1] = Color.GRAY;
// If mote is dead, make outer circle red // If mote is dead, make outer circle red
else if (mote.getState() == Mote.STATE_DEAD) else if (mote.getState() == State.DEAD)
returnColors[1] = Color.RED; returnColors[1] = Color.RED;
else else