interface handler support for new interface polling format

This commit is contained in:
fros4943 2008-10-28 12:40:35 +00:00
parent 7e864bd3c7
commit 1de0d38ccb

View file

@ -1,155 +1,119 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science. All rights
* reserved.
*
* Copyright (c) 2008, 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: MoteInterfaceHandler.java,v 1.3 2008/09/22 16:18:22 joxe Exp $
* 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: MoteInterfaceHandler.java,v 1.4 2008/10/28 12:40:35 fros4943 Exp $
*/
package se.sics.cooja;
import java.util.*;
import org.apache.log4j.Logger;
import se.sics.cooja.interfaces.*;
/**
* A mote interface handler holds all interfaces for a specific mote. Even
* though an interface handler strictly does not need any interfaces at all, a
* position interface is highly recommended. (A lot of plugins depend on a mote
* position, for example a typical visualizer.)
*
* Interfaces are divided into active and passive interfaces. Active interfaces
* are only polled if the mote is active, while passive interfaces are polled in
* all states except dead state.
*
* The mote interface handler holds all interfaces for a specific mote.
* Interfaces should be polled via this class when the mote is ticked.
*
* @author Fredrik Osterlind
*
* @see #doActiveActionsAfterTick()
* @see #doActiveActionsBeforeTick()
* @see #doPassiveActionsAfterTick()
* @see #doPassiveActionsBeforeTick()
*
* @author Fredrik Österlind
*/
public class MoteInterfaceHandler {
private static Logger logger = Logger.getLogger(MoteInterfaceHandler.class);
private Vector<MoteInterface> allInterfaces = new Vector<MoteInterface>();
/* Cached interfaces */
private Battery myBattery;
private Beeper myBeeper;
private Button myButton;
private Clock myClock;
private IPAddress myIPAddress;
private LED myLED;
private Log myLog;
private MoteID myMoteID;
private PIR myPIR;
private Position myPosition;
private Radio myRadio;
private Vector<MoteInterface> myActiveInterfaces = new Vector<MoteInterface>();
private MoteInterface[] activeCache = null;
private Vector<MoteInterface> myPassiveInterfaces = new Vector<MoteInterface>();
private PolledBeforeActiveTicks[] polledBeforeActive = null;
private PolledAfterActiveTicks[] polledAfterActive = null;
private PolledBeforeAllTicks[] polledBeforeAll = null;
private PolledAfterAllTicks[] polledAfterAll = null;
/**
* Creates a new empty mote interface handler.
* Creates new empty mote interface handler.
*/
public MoteInterfaceHandler() {
}
/**
* Creates a new mote interface handler. All given interfaces are loaded.
*
* @param mote
* The mote holding this interface handler
* @param allInterfaces
* Simulation interfaces to load
* Creates new mote interface handler. All given interfaces are created.
*
* @param mote Mote
* @param interfaceClasses Mote interface classes
*/
public MoteInterfaceHandler(Mote mote,
Vector<Class<? extends MoteInterface>> allInterfaces) {
public MoteInterfaceHandler(Mote mote, Vector<Class<? extends MoteInterface>> interfaceClasses) {
for (Class<? extends MoteInterface> interfaceClass : interfaceClasses) {
MoteInterface intf = MoteInterface.generateInterface(interfaceClass, mote);
// Load all interfaces
for (Class<? extends MoteInterface> interfaceClass : allInterfaces) {
boolean isPassive = false;
// Check if interface is active or passive
if (PassiveMoteInterface.class.isAssignableFrom(interfaceClass))
isPassive = true;
// Load interface
MoteInterface loadedInterface = MoteInterface.generateInterface(
interfaceClass, mote);
if (loadedInterface != null)
if (isPassive)
addPassiveInterface(loadedInterface);
else
addActiveInterface(loadedInterface);
else
logger.warn("Interface could not be loaded: " + interfaceClass);
if (intf != null) {
addInterface(intf);
} else {
logger.fatal("Could not load interface: " + interfaceClass);
}
}
}
/**
* Get an interface (active or passive) of the given type. Returns the first
* loaded interface found, that is either of the given class or of a subclass.
*
* For example, if the current radio interface is wanted, this method would be
* called like the following: getInterfaceOfType(Radio.class)
*
* @param interfaceType
* Type of interface to return
* @return Interface or null if no interface loaded of given type
* Returns interface of given type. Returns the first interface found that
* is either of the given class or of a subclass.
*
* Usage: getInterfaceOfType(Radio.class)
*
* @param interfaceType Class of interface to return
* @return Mote interface, or null if no interface exists of given type
*/
public <N extends MoteInterface> N getInterfaceOfType(Class<N> interfaceType) {
for (MoteInterface intf : allInterfaces) {
if (interfaceType.isAssignableFrom(intf.getClass())) {
return (N) intf;
}
}
Enumeration<? extends MoteInterface> allActive = myActiveInterfaces
.elements();
while (allActive.hasMoreElements()) {
N nextInterface = (N) allActive.nextElement();
if (interfaceType.isAssignableFrom(nextInterface.getClass()))
return nextInterface;
}
Enumeration<? extends MoteInterface> allPassive = myPassiveInterfaces
.elements();
while (allPassive.hasMoreElements()) {
N nextInterface = (N) allPassive.nextElement();
if (interfaceType.isAssignableFrom(nextInterface.getClass()))
return nextInterface;
}
return null;
}
/**
* Returns the battery interface (if any).
*
*
* @return Battery interface
*/
public Battery getBattery() {
@ -161,7 +125,7 @@ public class MoteInterfaceHandler {
/**
* Returns the beeper interface (if any).
*
*
* @return Beeper interface
*/
public Beeper getBeeper() {
@ -173,7 +137,7 @@ public class MoteInterfaceHandler {
/**
* Returns the button interface (if any).
*
*
* @return Button interface
*/
public Button getButton() {
@ -185,7 +149,7 @@ public class MoteInterfaceHandler {
/**
* Returns the clock interface (if any).
*
*
* @return Clock interface
*/
public Clock getClock() {
@ -197,8 +161,8 @@ public class MoteInterfaceHandler {
/**
* Returns the IP address interface (if any).
*
* @return IPAddress interface
*
* @return IP Address interface
*/
public IPAddress getIPAddress() {
if (myIPAddress == null) {
@ -209,7 +173,7 @@ public class MoteInterfaceHandler {
/**
* Returns the LED interface (if any).
*
*
* @return LED interface
*/
public LED getLED() {
@ -221,7 +185,7 @@ public class MoteInterfaceHandler {
/**
* Returns the log interface (if any).
*
*
* @return Log interface
*/
public Log getLog() {
@ -233,7 +197,7 @@ public class MoteInterfaceHandler {
/**
* Returns the mote ID interface (if any).
*
*
* @return Mote ID interface
*/
public MoteID getMoteID() {
@ -245,7 +209,7 @@ public class MoteInterfaceHandler {
/**
* Returns the PIR interface (if any).
*
*
* @return PIR interface
*/
public PIR getPIR() {
@ -257,7 +221,7 @@ public class MoteInterfaceHandler {
/**
* Returns the position interface (if any).
*
*
* @return Position interface
*/
public Position getPosition() {
@ -269,7 +233,7 @@ public class MoteInterfaceHandler {
/**
* Returns the radio interface (if any).
*
*
* @return Radio interface
*/
public Radio getRadio() {
@ -280,99 +244,104 @@ public class MoteInterfaceHandler {
}
/**
* Polls all active interfaces. This method should be called during a mote
* tick before the mote software is executed.
* Polls active interfaces before mote tick.
*/
public void doActiveActionsBeforeTick() {
// Assuming only one caller!!!
if (activeCache == null) {
activeCache = (MoteInterface[]) myActiveInterfaces.toArray(new MoteInterface[myActiveInterfaces.size()]);
}
// for (int i = 0; i < myActiveInterfaces.size(); i++)
// myActiveInterfaces.get(i).doActionsBeforeTick();
for (int i = 0, n = activeCache.length; i < n; i++) {
activeCache[i].doActionsBeforeTick();
if (polledBeforeActive == null) {
ArrayList<PolledBeforeActiveTicks> intfs = new ArrayList<PolledBeforeActiveTicks>();
for (MoteInterface intf: allInterfaces) {
if (intf instanceof PolledBeforeActiveTicks) {
intfs.add((PolledBeforeActiveTicks)intf);
}
}
polledBeforeActive = intfs.toArray(new PolledBeforeActiveTicks[intfs.size()]);
}
for (PolledBeforeActiveTicks element : polledBeforeActive) {
element.doActionsBeforeTick();
}
}
/**
* Polls all active interfaces. This method should be called during a mote
* tick after the mote software has executed.
* Polls active interfaces after mote tick.
*/
public void doActiveActionsAfterTick() {
for (int i = 0; i < myActiveInterfaces.size(); i++)
myActiveInterfaces.get(i).doActionsAfterTick();
if (polledAfterActive == null) {
ArrayList<PolledAfterActiveTicks> intfs = new ArrayList<PolledAfterActiveTicks>();
for (MoteInterface intf: allInterfaces) {
if (intf instanceof PolledAfterActiveTicks) {
intfs.add((PolledAfterActiveTicks)intf);
}
}
polledAfterActive = intfs.toArray(new PolledAfterActiveTicks[intfs.size()]);
}
for (PolledAfterActiveTicks element : polledAfterActive) {
element.doActionsAfterTick();
}
}
/**
* Polls all passive interfaces. This method should be called during a mote
* tick before the mote software is executed.
* Polls passive interfaces before mote tick.
*/
public void doPassiveActionsBeforeTick() {
for (int i = 0; i < myPassiveInterfaces.size(); i++)
myPassiveInterfaces.get(i).doActionsBeforeTick();
if (polledBeforeAll == null) {
ArrayList<PolledBeforeAllTicks> intfs = new ArrayList<PolledBeforeAllTicks>();
for (MoteInterface intf: allInterfaces) {
if (intf instanceof PolledBeforeAllTicks) {
intfs.add((PolledBeforeAllTicks)intf);
}
}
polledBeforeAll = intfs.toArray(new PolledBeforeAllTicks[intfs.size()]);
}
for (PolledBeforeAllTicks element : polledBeforeAll) {
element.doActionsBeforeTick();
}
}
/**
* Polls all passive interfaces. This method should be called during a mote
* tick after the mote software has executed.
* Polls passive interfaces after mote tick.
*/
public void doPassiveActionsAfterTick() {
for (int i = 0; i < myPassiveInterfaces.size(); i++)
myPassiveInterfaces.get(i).doActionsAfterTick();
if (polledAfterAll == null) {
ArrayList<PolledAfterAllTicks> intfs = new ArrayList<PolledAfterAllTicks>();
for (MoteInterface intf: allInterfaces) {
if (intf instanceof PolledAfterAllTicks) {
intfs.add((PolledAfterAllTicks)intf);
}
}
polledAfterAll = intfs.toArray(new PolledAfterAllTicks[intfs.size()]);
}
for (PolledAfterAllTicks element : polledAfterAll) {
element.doActionsAfterTick();
}
}
/**
* Returns all passive mote interfaces.
*
* @return All passive mote interface
* @return Mote interfaces
*/
public Vector<MoteInterface> getAllPassiveInterfaces() {
return myPassiveInterfaces;
public Vector<MoteInterface> getInterfaces() {
return allInterfaces;
}
/**
* Returns all active mote interfaces.
*
* @return All active mote interface
* Add mote interface.
*
* @param intf Mote interface
* @see PolledBeforeActiveTicks
* @see PolledBeforeAllTicks
* @see PolledAfterActiveTicks
* @see PolledAfterAllTicks
*/
public Vector<MoteInterface> getAllActiveInterfaces() {
return myActiveInterfaces;
}
public void addInterface(MoteInterface intf) {
allInterfaces.add(intf);
/**
* Add an active interface to corresponding mote. An active interface is only
* allowed to act if the mote is in active state. However, since interfaces
* may awaken a sleeping mote up via external interrupts, most of the
* interfaces should be active.
*
* For example a button interface should be active. When the button is
* pressed, the interface will wake the mote up (simulated external
* interrupt), and then that button will be allowed to act before next tick.
*
* A passive interface is an interface which will always act if the mote is
* not dead. For example a battery should always be allowed to act since a
* mote needs energy even if it is in sleep mode.
*
* @see #addPassiveInterface(MoteInterface)
* @param newInterface
* New interface
*/
public void addActiveInterface(MoteInterface newInterface) {
myActiveInterfaces.add(newInterface);
activeCache = null;
}
/**
* Add a passive interface to corresponding mote. For explanation of passive
* vs active interfaces, see addActiveInterface(MoteInterface).
*
* @see #addActiveInterface(MoteInterface)
* @param newInterface
* New interface
*/
public void addPassiveInterface(MoteInterface newInterface) {
myPassiveInterfaces.add(newInterface);
polledBeforeActive = null;
polledAfterActive = null;
polledBeforeAll = null;
polledAfterAll = null;
}
}