New PluginType SIM_CONTROL_PLUGIN for sim control

SIM_CONTROL_PLGUIN are handled like SIM_PLUGIN, with one exception.
If the simulation is started with -nogui=<simfile> than it is checked
if a controlling plugin is loaded. If not, the simulation terminates
with an error message.
This commit is contained in:
Andreas Löscher 2014-11-06 17:16:24 +01:00
parent a50bc08adc
commit 6cbe94b7e9
3 changed files with 37 additions and 18 deletions

View File

@ -952,7 +952,8 @@ public class Cooja extends Observable {
String tooltip = "<html><pre>"; String tooltip = "<html><pre>";
if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) { if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
tooltip += "Cooja plugin: "; tooltip += "Cooja plugin: ";
} else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) { } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN
|| pluginType == PluginType.SIM_CONTROL_PLUGIN) {
tooltip += "Simulation plugin: "; tooltip += "Simulation plugin: ";
if (getSimulation() == null) { if (getSimulation() == null) {
menuItem.setEnabled(false); menuItem.setEnabled(false);
@ -963,7 +964,8 @@ public class Cooja extends Observable {
tooltip += description + " (" + newPluginClass.getName() + ")"; tooltip += description + " (" + newPluginClass.getName() + ")";
/* Check if simulation plugin depends on any particular radio medium */ /* Check if simulation plugin depends on any particular radio medium */
if ((pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) && (getSimulation() != null)) { if ((pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN
|| pluginType == PluginType.SIM_CONTROL_PLUGIN) && (getSimulation() != null)) {
if (newPluginClass.getAnnotation(SupportedArguments.class) != null) { if (newPluginClass.getAnnotation(SupportedArguments.class) != null) {
boolean active = false; boolean active = false;
Class<? extends RadioMedium>[] radioMediums = newPluginClass.getAnnotation(SupportedArguments.class).radioMediums(); Class<? extends RadioMedium>[] radioMediums = newPluginClass.getAnnotation(SupportedArguments.class).radioMediums();
@ -1020,7 +1022,8 @@ public class Cooja extends Observable {
} }
int pluginType = pluginClass.getAnnotation(PluginType.class).value(); int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType != PluginType.SIM_PLUGIN && pluginType != PluginType.SIM_STANDARD_PLUGIN) { if (pluginType != PluginType.SIM_PLUGIN && pluginType != PluginType.SIM_STANDARD_PLUGIN
&& pluginType == PluginType.SIM_CONTROL_PLUGIN) {
continue; continue;
} }
@ -1848,8 +1851,8 @@ public class Cooja extends Observable {
pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, Cooja.class }) pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, Cooja.class })
.newInstance(argMote, argSimulation, argGUI); .newInstance(argMote, argSimulation, argGUI);
} else if (pluginType == PluginType.SIM_PLUGIN } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) { || pluginType == PluginType.SIM_CONTROL_PLUGIN) {
if (argGUI == null) { if (argGUI == null) {
throw new PluginConstructionException("No GUI argument for simulation plugin"); throw new PluginConstructionException("No GUI argument for simulation plugin");
} }
@ -1931,7 +1934,8 @@ public class Cooja extends Observable {
try { try {
if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) { if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
pluginClass.getConstructor(new Class[] { Cooja.class }); pluginClass.getConstructor(new Class[] { Cooja.class });
} else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) { } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN
|| pluginType == PluginType.SIM_CONTROL_PLUGIN) {
pluginClass.getConstructor(new Class[] { Simulation.class, Cooja.class }); pluginClass.getConstructor(new Class[] { Simulation.class, Cooja.class });
} else if (pluginType == PluginType.MOTE_PLUGIN) { } else if (pluginType == PluginType.MOTE_PLUGIN) {
pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, Cooja.class }); pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, Cooja.class });
@ -3247,19 +3251,19 @@ public class Cooja extends Observable {
} }
Cooja gui = sim.getCooja(); Cooja gui = sim.getCooja();
/* Make sure at least one test editor is controlling the simulation */ /* Make sure at least one plugin controlling the simulation */
boolean hasEditor = false; boolean hasController = false;
for (Plugin startedPlugin : gui.startedPlugins) { for (Plugin startedPlugin : gui.startedPlugins) {
if (startedPlugin instanceof ScriptRunner) { int pluginType = startedPlugin.getClass().getAnnotation(PluginType.class).value();
hasEditor = true; if (pluginType == PluginType.SIM_CONTROL_PLUGIN) {
break; hasController = true;
} }
} }
/* Backwards compatibility: /* Backwards compatibility:
* simulation has no test editor, but has external (old style) test script. * simulation has no control plugin, but has external (old style) test script.
* We will manually start a test editor from here. */ * We will manually start a test editor from here. */
if (!hasEditor) { if (!hasController) {
File scriptFile = new File(config.substring(0, config.length()-4) + ".js"); File scriptFile = new File(config.substring(0, config.length()-4) + ".js");
if (scriptFile.exists()) { if (scriptFile.exists()) {
logger.info("Detected old simulation test, starting test editor manually from: " + scriptFile); logger.info("Detected old simulation test, starting test editor manually from: " + scriptFile);
@ -3280,8 +3284,7 @@ public class Cooja extends Observable {
} }
} }
sim.setSpeedLimit(null);
sim.startSimulation();
} else if (args.length > 0 && args[0].startsWith("-applet")) { } else if (args.length > 0 && args[0].startsWith("-applet")) {

View File

@ -110,5 +110,17 @@ public @interface PluginType {
*/ */
public static final int COOJA_STANDARD_PLUGIN = 5; public static final int COOJA_STANDARD_PLUGIN = 5;
/**
* Simulation Control Plugin
*
* A Simulation Control Plugin indicates control over the simulation. If COOJA
* is loaded in nogui mode, it will terminate if no controll plugin is present.
*
* COOJA plugins are available via the plugins menubar.
*
* When constructed, a COOJA plugin is given the current GUI.
*/
public static final int SIM_CONTROL_PLUGIN = 6;
int value(); int value();
} }

View File

@ -88,7 +88,7 @@ import org.contikios.cooja.dialogs.MessageList;
import org.contikios.cooja.util.StringUtils; import org.contikios.cooja.util.StringUtils;
@ClassDescription("Simulation script editor") @ClassDescription("Simulation script editor")
@PluginType(PluginType.SIM_PLUGIN) @PluginType(PluginType.SIM_CONTROL_PLUGIN)
public class ScriptRunner extends VisPlugin { public class ScriptRunner extends VisPlugin {
private static final long serialVersionUID = 7614358340336799109L; private static final long serialVersionUID = 7614358340336799109L;
private static Logger logger = Logger.getLogger(ScriptRunner.class); private static Logger logger = Logger.getLogger(ScriptRunner.class);
@ -274,6 +274,10 @@ public class ScriptRunner extends VisPlugin {
if (script != null) { if (script != null) {
updateScript(script); updateScript(script);
} }
/* start simulation */
simulation.setSpeedLimit(null);
simulation.startSimulation();
} }
public void setLinkFile(File source) { public void setLinkFile(File source) {