From 17431ba16ca1620f7af64e1aa8db39d0f53bd5f6 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 26 Sep 2006 13:08:05 +0000 Subject: [PATCH] changed mote state to enum type instead of int --- tools/cooja/java/se/sics/cooja/Mote.java | 24 +++++-------- .../sics/cooja/contikimote/ContikiMote.java | 34 +++++++++---------- .../contikimote/interfaces/ContikiButton.java | 6 ++-- .../contikimote/interfaces/ContikiPIR.java | 4 +-- .../contikimote/interfaces/ContikiRS232.java | 4 +-- .../contikimote/interfaces/ContikiRadio.java | 4 +-- .../contikimote/interfaces/ContikiVib.java | 4 +-- .../cooja/dialogs/UserPlatformsDialog.java | 3 +- .../se/sics/cooja/interfaces/Battery.java | 8 ++--- .../java/se/sics/cooja/motes/DummyMote.java | 8 ++--- .../sics/cooja/plugins/MoteInformation.java | 11 +++--- .../se/sics/cooja/plugins/VisBattery.java | 4 +-- .../java/se/sics/cooja/plugins/VisState.java | 7 ++-- 13 files changed, 58 insertions(+), 63 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/Mote.java b/tools/cooja/java/se/sics/cooja/Mote.java index 2f75cd4cd..ca5f5e79d 100644 --- a/tools/cooja/java/se/sics/cooja/Mote.java +++ b/tools/cooja/java/se/sics/cooja/Mote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -50,21 +50,15 @@ import org.jdom.Element; * @author Fredrik Osterlind */ public interface Mote { - - /** - * Active state. - */ - public static int STATE_ACTIVE = 1; /** - * Sleeping state. + * Possible mote states */ - public static int STATE_LPM = 2; - - /** - * Dead state (may be out of batteries). - */ - public static int STATE_DEAD = 3; + public static enum State { + ACTIVE, // Active state + LPM, // Low power mode (sleeping) + DEAD // Dead (for example out of batteries) + } /** * Tries to change state to given argument. @@ -72,12 +66,12 @@ public interface Mote { * * @param newState New state of mote. */ - public void setState(int newState); + public void setState(State newState); /** * @return Current mote state */ - public int getState(); + public State getState(); /** * Adds new state observer. diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java index afaeef531..980cef086 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMote.java @@ -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.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; @@ -64,7 +64,7 @@ public class ContikiMote implements Mote { // Time to wake up if sleeping private int wakeUpTime = 0; - private int myState = STATE_ACTIVE; + private State myState = State.ACTIVE; // State observable private class StateObservable extends Observable { @@ -99,26 +99,26 @@ public class ContikiMote implements Mote { this.myMemory = moteType.createInitialMemory(); this.myInterfaceHandler = new MoteInterfaceHandler((Mote) this, moteType.getMoteInterfaces()); - myState = STATE_ACTIVE; + myState = State.ACTIVE; } - public void setState(int newState) { - if (myState == STATE_DEAD) { + public void setState(State newState) { + if (myState == State.DEAD) { return; } - if (myState == STATE_ACTIVE && newState != STATE_ACTIVE) { + if (myState == State.ACTIVE && newState != State.ACTIVE) { myState = newState; stateObservable.stateChanged(); } - if (myState == STATE_LPM && newState != STATE_LPM) { + if (myState == State.LPM && newState != State.LPM) { myState = newState; stateObservable.stateChanged(); } } - public int getState() { + public State getState() { return myState; } @@ -175,17 +175,17 @@ public class ContikiMote implements Mote { public void tick(int simTime) { // If mote is dead, do nothing at all - if (getState() == STATE_DEAD) + if (getState() == State.DEAD) return; // If mote is sleeping and has a wake up time, should it wake up now? - if (getState() == STATE_LPM && wakeUpTime > 0 && wakeUpTime <= simTime) { - setState(STATE_ACTIVE); + if (getState() == State.LPM && wakeUpTime > 0 && wakeUpTime <= simTime) { + setState(State.ACTIVE); wakeUpTime = 0; } // If mote is active.. - if (getState() == STATE_ACTIVE) { + if (getState() == State.ACTIVE) { // Let all active interfaces act before tick // Observe that each interface may put the mote to sleep at this point myInterfaceHandler.doActiveActionsBeforeTick(); @@ -196,7 +196,7 @@ public class ContikiMote implements Mote { // If mote is still active, complete this tick - if (getState() == STATE_ACTIVE) { + if (getState() == State.ACTIVE) { // Copy mote memory to core myType.setCoreMemory(myMemory); @@ -216,19 +216,19 @@ public class ContikiMote implements Mote { myInterfaceHandler.doPassiveActionsAfterTick(); // 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) int processRunValue = myMemory.getIntValueOf("simProcessRunValue"); int etimersPending = myMemory.getIntValueOf("simEtimerPending"); int nextExpirationTime = myMemory.getIntValueOf("simNextExpirationTime"); if (processRunValue == 0 && etimersPending == 0) { - setState(STATE_LPM); + setState(State.LPM); wakeUpTime = 0; } if (processRunValue == 0 && etimersPending == 1 && nextExpirationTime > 0) { - setState(STATE_LPM); + setState(State.LPM); wakeUpTime = nextExpirationTime; } @@ -280,7 +280,7 @@ public class ContikiMote implements Mote { public boolean setConfigXML(Simulation simulation, Collection configXML) { mySim = simulation; - myState = STATE_ACTIVE; + myState = State.ACTIVE; for (Element element: configXML) { String name = element.getName(); diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiButton.java index 9e6cd6d17..a605be5ae 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiButton.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -106,7 +106,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface { // If mote is inactive, wake it up if (RAISES_EXTERNAL_INTERRUPT) - mote.setState(Mote.STATE_ACTIVE); + mote.setState(Mote.State.ACTIVE); setChanged(); notifyObservers(); @@ -121,7 +121,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface { // If mote is inactive, wake it up if (RAISES_EXTERNAL_INTERRUPT) - mote.setState(Mote.STATE_ACTIVE); + mote.setState(Mote.State.ACTIVE); setChanged(); notifyObservers(); diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiPIR.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiPIR.java index 13cc4d032..1e66209d2 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiPIR.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiPIR.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -109,7 +109,7 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface { // If mote is inactive, wake it up if (RAISES_EXTERNAL_INTERRUPT) - mote.setState(Mote.STATE_ACTIVE); + mote.setState(Mote.State.ACTIVE); this.setChanged(); this.notifyObservers(); diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java index 6ed40450d..e95a8e972 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRS232.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -181,7 +181,7 @@ public class ContikiRS232 extends MoteInterface implements ContikiMoteInterface moteMem.setByteArray("simSerialReceivingData", newData); if (RAISES_EXTERNAL_INTERRUPT) - mote.setState(Mote.STATE_ACTIVE); + mote.setState(Mote.State.ACTIVE); } diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRadio.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRadio.java index 89d4663d4..39317831f 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRadio.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiRadio.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -151,7 +151,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface { // If mote is inactive, wake it up if (RAISES_EXTERNAL_INTERRUPT) - myMote.setState(Mote.STATE_ACTIVE); + myMote.setState(Mote.State.ACTIVE); } public void advanceListenState() { diff --git a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiVib.java b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiVib.java index e826af0b6..b8fb2530f 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiVib.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/interfaces/ContikiVib.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -112,7 +112,7 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface { // If mote is inactive, wake it up if (RAISES_EXTERNAL_INTERRUPT) - mote.setState(Mote.STATE_ACTIVE); + mote.setState(Mote.State.ACTIVE); } } diff --git a/tools/cooja/java/se/sics/cooja/dialogs/UserPlatformsDialog.java b/tools/cooja/java/se/sics/cooja/dialogs/UserPlatformsDialog.java index 9b711c3f9..acdae43c2 100644 --- a/tools/cooja/java/se/sics/cooja/dialogs/UserPlatformsDialog.java +++ b/tools/cooja/java/se/sics/cooja/dialogs/UserPlatformsDialog.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -36,7 +36,6 @@ import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.util.Enumeration; import java.util.Vector; import javax.swing.*; diff --git a/tools/cooja/java/se/sics/cooja/interfaces/Battery.java b/tools/cooja/java/se/sics/cooja/interfaces/Battery.java index 50d92bae5..eb66adbc4 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/Battery.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/Battery.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -133,11 +133,11 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { return; // If mote is dead, do nothing - if (mote.getState() == Mote.STATE_DEAD) + if (mote.getState() == Mote.State.DEAD) return; // Check mote state - if (mote.getState() == Mote.STATE_LPM) { + if (mote.getState() == Mote.State.LPM) { // Mote is sleeping. Sum up energy usage. double totalEnergyConsumption = 0.0; totalEnergyConsumption += energyConsumptionLPMPerTick; @@ -171,7 +171,7 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { if (getCurrentEnergy() <= 0.0) { setChanged(); notifyObservers(); - mote.setState(Mote.STATE_DEAD); + mote.setState(Mote.State.DEAD); } } diff --git a/tools/cooja/java/se/sics/cooja/motes/DummyMote.java b/tools/cooja/java/se/sics/cooja/motes/DummyMote.java index da9cb72dc..751d8c226 100644 --- a/tools/cooja/java/se/sics/cooja/motes/DummyMote.java +++ b/tools/cooja/java/se/sics/cooja/motes/DummyMote.java @@ -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.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; @@ -100,12 +100,12 @@ public class DummyMote implements Mote { myInterfaceHandler.addPassiveInterface(myPosition); } - public void setState(int newState) { + public void setState(State newState) { logger.fatal("Dummy mote can not change state"); } - public int getState() { - return Mote.STATE_ACTIVE; + public State getState() { + return Mote.State.ACTIVE; } public void addStateObserver(Observer newObserver) { diff --git a/tools/cooja/java/se/sics/cooja/plugins/MoteInformation.java b/tools/cooja/java/se/sics/cooja/plugins/MoteInformation.java index 810f3a74f..865ca428c 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/MoteInformation.java +++ b/tools/cooja/java/se/sics/cooja/plugins/MoteInformation.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -39,6 +39,7 @@ import javax.swing.*; import org.apache.log4j.Logger; import se.sics.cooja.*; +import se.sics.cooja.Mote.State; /** * MoteInformation is a simple information window for motes. @@ -102,9 +103,9 @@ public class MoteInformation extends VisPlugin { label = new JLabel("-- STATE --"); label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT)); smallPane.add(BorderLayout.WEST, label); - if (mote.getState() == Mote.STATE_ACTIVE) + if (mote.getState() == Mote.State.ACTIVE) label = new JLabel("active"); - else if (mote.getState() == Mote.STATE_LPM) + else if (mote.getState() == State.LPM) label = new JLabel("low power mode"); else label = new JLabel("dead"); @@ -207,9 +208,9 @@ public class MoteInformation extends VisPlugin { // Register as state observer to detect if mote changes state mote.addStateObserver(stateObserver = new Observer() { public void update(Observable obs, Object obj) { - if (mote.getState() == Mote.STATE_ACTIVE) + if (mote.getState() == State.ACTIVE) stateLabel.setText("active"); - else if (mote.getState() == Mote.STATE_LPM) + else if (mote.getState() == Mote.State.LPM) stateLabel.setText("low power mode"); else stateLabel.setText("dead"); diff --git a/tools/cooja/java/se/sics/cooja/plugins/VisBattery.java b/tools/cooja/java/se/sics/cooja/plugins/VisBattery.java index 221c29a25..5a57691b4 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/VisBattery.java +++ b/tools/cooja/java/se/sics/cooja/plugins/VisBattery.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -96,7 +96,7 @@ public class VisBattery extends Visualizer2D { } public Color[] getColorOf(Mote mote) { - if (mote.getState() == Mote.STATE_DEAD) + if (mote.getState() == Mote.State.DEAD) return new Color[]{Color.RED}; Battery battery = mote.getInterfaces().getBattery(); diff --git a/tools/cooja/java/se/sics/cooja/plugins/VisState.java b/tools/cooja/java/se/sics/cooja/plugins/VisState.java index ec3bb33e7..6967a26e8 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/VisState.java +++ b/tools/cooja/java/se/sics/cooja/plugins/VisState.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * 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; @@ -36,6 +36,7 @@ import java.util.*; import org.apache.log4j.Logger; import se.sics.cooja.*; +import se.sics.cooja.Mote.State; /** * 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]; // If mote is sleeping, make outer circle blue - if (mote.getState() == Mote.STATE_LPM) + if (mote.getState() == Mote.State.LPM) returnColors[1] = Color.GRAY; // 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; else