From 374e25c2e48f198795d7c8b6f099ad74b1db97b0 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Fri, 4 Nov 2022 15:50:37 +0100 Subject: [PATCH] Add plumbing for per-simulation options This adds the required infrastructure for passing in options to simulations. There is currently nothing that uses the options, but this makes it easy to add consumers. --- java/org/contikios/cooja/Cooja.java | 58 +++++++++++++++-------------- java/org/contikios/cooja/Main.java | 31 ++++++++++++--- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/java/org/contikios/cooja/Cooja.java b/java/org/contikios/cooja/Cooja.java index 0c35bb8705..00031a977d 100644 --- a/java/org/contikios/cooja/Cooja.java +++ b/java/org/contikios/cooja/Cooja.java @@ -49,6 +49,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.MissingResourceException; import java.util.Observable; import java.util.Observer; @@ -1390,36 +1391,34 @@ public static void go(Config config) { System.exit(1); } // Check if simulator should be quick-started. - if (config.configs != null) { - int rv = 0; - for (var cfg : config.configs) { - var file = new File(cfg); - Simulation sim = null; - try { - sim = config.vis - ? Cooja.gui.doLoadConfig(file, config.updateSim, config.randomSeed) - : gui.loadSimulationConfig(file, true, false, config.randomSeed); - } catch (Exception e) { - logger.fatal("Exception when loading simulation: ", e); - } - if (sim == null) { - System.exit(1); - } - if (!config.vis) { - sim.setSpeedLimit(null); - var ret = sim.startSimulation(true); - if (ret == null) { - logger.info("TEST OK\n"); - } else { - logger.warn("TEST FAILED\n"); - rv = Math.max(rv, ret); - } - } + int rv = 0; + for (var simConfig : config.configs) { + var file = new File(simConfig.file); + Simulation sim = null; + try { + sim = config.vis + ? Cooja.gui.doLoadConfig(file, config.updateSim, config.randomSeed) + : gui.loadSimulationConfig(file, true, false, config.randomSeed); + } catch (Exception e) { + logger.fatal("Exception when loading simulation: ", e); + } + if (sim == null) { + System.exit(1); } - if (!config.vis || config.updateSim) { - gui.doQuit(rv); + if (!config.vis) { + sim.setSpeedLimit(null); + var ret = sim.startSimulation(true); + if (ret == null) { + logger.info("TEST OK\n"); + } else { + logger.warn("TEST FAILED\n"); + rv = Math.max(rv, ret); + } } } + if (!config.configs.isEmpty() && (!config.vis || config.updateSim)) { + gui.doQuit(rv); + } } /** @@ -2230,8 +2229,11 @@ public void run() { } } + /** Structure to hold the simulation parameters. */ + public record SimConfig(Map opts, String file) {} + /** Structure to hold the Cooja startup configuration. */ public record Config(boolean vis, Long randomSeed, String externalToolsConfig, boolean updateSim, String logDir, String contikiPath, String coojaPath, String javac, - String[] configs) {} + List configs) {} } diff --git a/java/org/contikios/cooja/Main.java b/java/org/contikios/cooja/Main.java index aef13910c2..b62753e7b4 100644 --- a/java/org/contikios/cooja/Main.java +++ b/java/org/contikios/cooja/Main.java @@ -29,6 +29,8 @@ package org.contikios.cooja; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.appender.ConsoleAppender; @@ -186,9 +188,29 @@ public static void main(String[] args) { } } - // Verify soundness of -nogui/-quickstart argument. + // Parse and verify soundness of -nogui/-quickstart argument. + ArrayList simConfigs = new ArrayList<>(); if (options.action != null) { - for (var file : options.action.nogui == null ? options.action.quickstart : options.action.nogui) { + for (String arg : options.action.nogui == null ? options.action.quickstart : options.action.nogui) { + // Argument on the form "file.csc[,key1=value1,key2=value2, ..]" + var map = new HashMap(); + String file = null; + for (var item : arg.split(",", -1)) { + if (file == null) { + file = item; + continue; + } + var pair = item.split("=", -1); + if (pair.length != 2) { + System.err.println("Faulty key=value specification: " + item); + System.exit(1); + } + map.put(pair[0], pair[1]); + } + if (file == null) { + System.err.println("Failed argument parsing of -nogui/-quickstart: " + arg); + System.exit(1); + } if (!file.endsWith(".csc") && !file.endsWith(".csc.gz")) { String option = options.action.nogui == null ? "-quickstart" : "-nogui"; System.err.println("Cooja " + option + " expects a filename extension of '.csc'"); @@ -198,6 +220,7 @@ public static void main(String[] args) { System.err.println("File '" + file + "' does not exist"); System.exit(1); } + simConfigs.add(new Cooja.SimConfig(map, file)); } } @@ -264,9 +287,7 @@ public static void main(String[] args) { } var cfg = new Config(vis, options.randomSeed, options.externalToolsConfig, options.updateSimulation, - options.logDir, options.contikiPath, options.coojaPath, options.javac, - options.action == null - ? null : options.action.quickstart == null ? options.action.nogui : options.action.quickstart); + options.logDir, options.contikiPath, options.coojaPath, options.javac, simConfigs); // Configure logger if (options.logConfigFile == null) { ConfigurationBuilder builder = ConfigurationBuilderFactory.newConfigurationBuilder();