From b322eda91a37b7754cde83f561d815c49cf7cdb5 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Tue, 5 Jun 2012 14:45:16 +0200 Subject: [PATCH] added support for mote interface requirements in SupportedArguments annotation NativeIPGateway plugin now requires an IP address mote interface --- .../sics/cooja/plugins/NativeIPGateway.java | 17 +++--- tools/cooja/java/se/sics/cooja/GUI.java | 60 +++++++++++++------ .../se/sics/cooja/SupportedArguments.java | 5 ++ 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/tools/cooja/apps/native_gateway/java/se/sics/cooja/plugins/NativeIPGateway.java b/tools/cooja/apps/native_gateway/java/se/sics/cooja/plugins/NativeIPGateway.java index 9a28a9022..83f9e34a1 100644 --- a/tools/cooja/apps/native_gateway/java/se/sics/cooja/plugins/NativeIPGateway.java +++ b/tools/cooja/apps/native_gateway/java/se/sics/cooja/plugins/NativeIPGateway.java @@ -69,18 +69,21 @@ import org.jdom.Element; import se.sics.cooja.ClassDescription; import se.sics.cooja.GUI; +import se.sics.cooja.GUI.RunnableInEDT; import se.sics.cooja.Mote; import se.sics.cooja.MotePlugin; import se.sics.cooja.PluginType; import se.sics.cooja.Simulation; +import se.sics.cooja.SupportedArguments; import se.sics.cooja.VisPlugin; -import se.sics.cooja.GUI.RunnableInEDT; import se.sics.cooja.dialogs.CompileContiki; import se.sics.cooja.dialogs.MessageList; +import se.sics.cooja.interfaces.IPAddress; import se.sics.cooja.interfaces.SerialPort; @ClassDescription("Open Native IP Gateway") @PluginType(PluginType.MOTE_PLUGIN) +@SupportedArguments(moteInterfaces = {IPAddress.class}) public class NativeIPGateway extends VisPlugin implements MotePlugin { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(NativeIPGateway.class); @@ -238,7 +241,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { } /* Detect selected network interface */ - NetworkInterfaceW intf = + NetworkInterfaceW intf = (NetworkInterfaceW) ((JComboBox)e.getSource()).getSelectedItem(); if (networkInterface == intf) { /* Already selected */ @@ -546,7 +549,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { final JDialog progressDialog; if (GUI.isVisualized()) { progressDialog = new JDialog( - (Window)GUI.getTopParentContainer(), + (Window)GUI.getTopParentContainer(), "Starting Native IP Gateway plugin" ); } else { @@ -564,7 +567,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { progressDialog.getContentPane().add(BorderLayout.NORTH, progressBar); progressDialog.getContentPane().add(BorderLayout.CENTER, new JScrollPane(output)); progressDialog.setSize(350, 150); - progressDialog.setLocationRelativeTo((Window)GUI.getTopParentContainer()); + progressDialog.setLocationRelativeTo(GUI.getTopParentContainer()); progressDialog.setVisible(true); GUI.setProgressMessage("Compiling hello-world.minimal-net (Native IP Gateway)"); return true; @@ -613,7 +616,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { } }); Runtime.getRuntime().addShutdownHook(shutdownHook); - + /* Waiting some time - otherwise pcap may not discover the new interface */ Thread.sleep(250); @@ -622,7 +625,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { logger.fatal("Error when creating tap0: " + e.getMessage()); logger.fatal("Try using an already existing network interface"); } - + /* Hide progress bar */ if (GUI.isVisualized()) { new RunnableInEDT() { @@ -1007,7 +1010,7 @@ public class NativeIPGateway extends VisPlugin implements MotePlugin { } deleteTunInterface(); - + if (shutdownHook != null) { Runtime.getRuntime().removeShutdownHook(shutdownHook); shutdownHook = null; diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index 29ef99eb2..3ef3b2217 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -2031,30 +2031,54 @@ public class GUI extends Observable { menuItem.putClientProperty("class", motePluginClass); menuItem.putClientProperty("mote", mote); - /* Check if mote plugin depends on any particular type of mote */ + /* Check mote plugin requirements */ boolean enableMenuItem = true; if (motePluginClass.getAnnotation(SupportedArguments.class) != null) { - enableMenuItem = false; - Class[] motes = motePluginClass.getAnnotation(SupportedArguments.class).motes(); - StringBuilder sb = new StringBuilder(); - sb.append( - "
This plugin:\n" +
-            motePluginClass.getName() +
-            "\ndoes not support motes of type:\n" +
-            mote.getClass().getName() +
-            "\n\nIt only supports motes of types:\n"
+        /* Check mote interfaces */
+        boolean moteInterfacesOK = true;
+        Class[] moteInterfaces = motePluginClass.getAnnotation(SupportedArguments.class).moteInterfaces();
+        StringBuilder moteTypeInterfacesError = new StringBuilder();
+        moteTypeInterfacesError.append(
+            "The plugin:\n" +
+            getDescriptionOf(motePluginClass) +
+            "\nrequires the following mote interfaces:\n"
         );
-        for (Class o: motes) {
-          sb.append(o.getName() + "\n");
-          if (o.isAssignableFrom(mote.getClass())) {
-            enableMenuItem = true;
-            break;
+        for (Class requiredMoteInterface: moteInterfaces) {
+          moteTypeInterfacesError.append(getDescriptionOf(requiredMoteInterface) + "\n");
+          if (mote.getInterfaces().getInterfaceOfType(requiredMoteInterface) == null) {
+            moteInterfacesOK = false;
           }
         }
-        sb.append("");
-        if (!enableMenuItem) {
-          menuItem.setToolTipText(sb.toString());
+
+        /* Check mote type */
+        boolean moteTypeOK = false;
+        Class[] motes = motePluginClass.getAnnotation(SupportedArguments.class).motes();
+        StringBuilder moteTypeError = new StringBuilder();
+        moteTypeError.append(
+            "The plugin:\n" +
+            getDescriptionOf(motePluginClass) +
+            "\ndoes not support motes of type:\n" +
+            getDescriptionOf(mote) +
+            "\n\nIt only supports motes of types:\n"
+        );
+        for (Class supportedMote: motes) {
+          moteTypeError.append(getDescriptionOf(supportedMote) + "\n");
+          if (supportedMote.isAssignableFrom(mote.getClass())) {
+            moteTypeOK = true;
+          }
         }
+
+        if (!moteInterfacesOK) {
+          menuItem.setToolTipText(
+              "
" + moteTypeInterfacesError + ""
+          );
+        }
+        if (!moteTypeOK) {
+          menuItem.setToolTipText(
+              "
" + moteTypeError + ""
+          );
+        }
+        enableMenuItem = moteInterfacesOK && moteTypeOK;
       }
 
       menuItem.setEnabled(enableMenuItem);
diff --git a/tools/cooja/java/se/sics/cooja/SupportedArguments.java b/tools/cooja/java/se/sics/cooja/SupportedArguments.java
index fa09710cb..0b1d5ceed 100644
--- a/tools/cooja/java/se/sics/cooja/SupportedArguments.java
+++ b/tools/cooja/java/se/sics/cooja/SupportedArguments.java
@@ -61,4 +61,9 @@ public @interface SupportedArguments {
    * @return List of accepted radio medium classes.
    */
   Class[] radioMediums() default { RadioMedium.class };
+
+  /**
+   * @return List of required mote interfaces.
+   */
+  Class[] moteInterfaces() default { MoteInterface.class };
 }