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
* 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;
@ -52,19 +52,13 @@ import org.jdom.Element;
public interface Mote {
/**
* Active state.
* Possible mote states
*/
public static int STATE_ACTIVE = 1;
/**
* Sleeping state.
*/
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.

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.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<Element> configXML) {
mySim = simulation;
myState = STATE_ACTIVE;
myState = State.ACTIVE;
for (Element element: configXML) {
String name = element.getName();

View File

@ -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();

View File

@ -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();

View File

@ -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);
}

View File

@ -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() {

View File

@ -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);
}
}

View File

@ -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.*;

View File

@ -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);
}
}

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.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) {

View File

@ -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");

View File

@ -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();

View File

@ -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