added method for getting mote interfaces based on class names + getInterfaces() returns a collection instead of a vector

This commit is contained in:
fros4943 2010-03-14 19:50:34 +00:00
parent 4710441bb6
commit 4fa88d2bfd
2 changed files with 30 additions and 13 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: MoteInterfaceHandler.java,v 1.8 2009/04/20 16:13:11 fros4943 Exp $ * $Id: MoteInterfaceHandler.java,v 1.9 2010/03/14 19:50:34 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -50,7 +50,7 @@ import se.sics.cooja.interfaces.*;
public class MoteInterfaceHandler { public class MoteInterfaceHandler {
private static Logger logger = Logger.getLogger(MoteInterfaceHandler.class); private static Logger logger = Logger.getLogger(MoteInterfaceHandler.class);
private Vector<MoteInterface> allInterfaces = new Vector<MoteInterface>(); private ArrayList<MoteInterface> moteInterfaces = new ArrayList<MoteInterface>();
/* Cached interfaces */ /* Cached interfaces */
private Battery myBattery; private Battery myBattery;
@ -104,7 +104,7 @@ public class MoteInterfaceHandler {
* @return Mote interface, or null if no interface exists of given type * @return Mote interface, or null if no interface exists of given type
*/ */
public <N extends MoteInterface> N getInterfaceOfType(Class<N> interfaceType) { public <N extends MoteInterface> N getInterfaceOfType(Class<N> interfaceType) {
for (MoteInterface intf : allInterfaces) { for (MoteInterface intf : moteInterfaces) {
if (interfaceType.isAssignableFrom(intf.getClass())) { if (interfaceType.isAssignableFrom(intf.getClass())) {
return (N) intf; return (N) intf;
} }
@ -113,6 +113,23 @@ public class MoteInterfaceHandler {
return null; return null;
} }
/**
* Returns the first interface with a class name that ends with the given arguments.
* Example: mote.getInterfaces().get("Temperature");
*
* @param <N>
* @param classname
* @return
*/
public MoteInterface get(String classname) {
for (MoteInterface intf : moteInterfaces) {
if (intf.getClass().getName().endsWith(classname)) {
return intf;
}
}
return null;
}
/** /**
* Returns the battery interface (if any). * Returns the battery interface (if any).
* *
@ -261,7 +278,7 @@ public class MoteInterfaceHandler {
public void doActiveActionsBeforeTick() { public void doActiveActionsBeforeTick() {
if (polledBeforeActive == null) { if (polledBeforeActive == null) {
ArrayList<PolledBeforeActiveTicks> intfs = new ArrayList<PolledBeforeActiveTicks>(); ArrayList<PolledBeforeActiveTicks> intfs = new ArrayList<PolledBeforeActiveTicks>();
for (MoteInterface intf: allInterfaces) { for (MoteInterface intf: moteInterfaces) {
if (intf instanceof PolledBeforeActiveTicks) { if (intf instanceof PolledBeforeActiveTicks) {
intfs.add((PolledBeforeActiveTicks)intf); intfs.add((PolledBeforeActiveTicks)intf);
} }
@ -280,7 +297,7 @@ public class MoteInterfaceHandler {
public void doActiveActionsAfterTick() { public void doActiveActionsAfterTick() {
if (polledAfterActive == null) { if (polledAfterActive == null) {
ArrayList<PolledAfterActiveTicks> intfs = new ArrayList<PolledAfterActiveTicks>(); ArrayList<PolledAfterActiveTicks> intfs = new ArrayList<PolledAfterActiveTicks>();
for (MoteInterface intf: allInterfaces) { for (MoteInterface intf: moteInterfaces) {
if (intf instanceof PolledAfterActiveTicks) { if (intf instanceof PolledAfterActiveTicks) {
intfs.add((PolledAfterActiveTicks)intf); intfs.add((PolledAfterActiveTicks)intf);
} }
@ -299,7 +316,7 @@ public class MoteInterfaceHandler {
public void doPassiveActionsBeforeTick() { public void doPassiveActionsBeforeTick() {
if (polledBeforeAll == null) { if (polledBeforeAll == null) {
ArrayList<PolledBeforeAllTicks> intfs = new ArrayList<PolledBeforeAllTicks>(); ArrayList<PolledBeforeAllTicks> intfs = new ArrayList<PolledBeforeAllTicks>();
for (MoteInterface intf: allInterfaces) { for (MoteInterface intf: moteInterfaces) {
if (intf instanceof PolledBeforeAllTicks) { if (intf instanceof PolledBeforeAllTicks) {
intfs.add((PolledBeforeAllTicks)intf); intfs.add((PolledBeforeAllTicks)intf);
} }
@ -318,7 +335,7 @@ public class MoteInterfaceHandler {
public void doPassiveActionsAfterTick() { public void doPassiveActionsAfterTick() {
if (polledAfterAll == null) { if (polledAfterAll == null) {
ArrayList<PolledAfterAllTicks> intfs = new ArrayList<PolledAfterAllTicks>(); ArrayList<PolledAfterAllTicks> intfs = new ArrayList<PolledAfterAllTicks>();
for (MoteInterface intf: allInterfaces) { for (MoteInterface intf: moteInterfaces) {
if (intf instanceof PolledAfterAllTicks) { if (intf instanceof PolledAfterAllTicks) {
intfs.add((PolledAfterAllTicks)intf); intfs.add((PolledAfterAllTicks)intf);
} }
@ -334,8 +351,8 @@ public class MoteInterfaceHandler {
/** /**
* @return Mote interfaces * @return Mote interfaces
*/ */
public Vector<MoteInterface> getInterfaces() { public Collection<MoteInterface> getInterfaces() {
return allInterfaces; return moteInterfaces;
} }
/** /**
@ -348,7 +365,7 @@ public class MoteInterfaceHandler {
* @see PolledAfterAllTicks * @see PolledAfterAllTicks
*/ */
public void addInterface(MoteInterface intf) { public void addInterface(MoteInterface intf) {
allInterfaces.add(intf); moteInterfaces.add(intf);
polledBeforeActive = null; polledBeforeActive = null;
polledAfterActive = null; polledAfterActive = null;
@ -357,6 +374,6 @@ public class MoteInterfaceHandler {
} }
public String toString() { public String toString() {
return "Mote interfaces handler (" + allInterfaces.size() + " mote interfaces)"; return "Mote interfaces handler (" + moteInterfaces.size() + " mote interfaces)";
} }
} }

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: MoteInterfaceViewer.java,v 1.8 2010/01/15 10:54:42 fros4943 Exp $ * $Id: MoteInterfaceViewer.java,v 1.9 2010/03/14 19:50:34 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -115,7 +115,7 @@ public class MoteInterfaceViewer extends VisPlugin implements HasQuickHelp, Mote
interfacePanel.removeAll(); interfacePanel.removeAll();
String interfaceDescription = (String) selectInterfaceComboBox.getSelectedItem(); String interfaceDescription = (String) selectInterfaceComboBox.getSelectedItem();
selectedMoteInterface = null; selectedMoteInterface = null;
Vector<MoteInterface> intfs = mote.getInterfaces().getInterfaces(); Collection<MoteInterface> intfs = mote.getInterfaces().getInterfaces();
for (MoteInterface intf : intfs) { for (MoteInterface intf : intfs) {
if (GUI.getDescriptionOf(intf).equals(interfaceDescription)) { if (GUI.getDescriptionOf(intf).equals(interfaceDescription)) {
selectedMoteInterface = intf; selectedMoteInterface = intf;