Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plumbing for per-simulation options #754

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions java/org/contikios/cooja/Cooja.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -2230,8 +2229,11 @@ public void run() {
}
}

/** Structure to hold the simulation parameters. */
public record SimConfig(Map<String, String> 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<SimConfig> configs) {}
}
31 changes: 26 additions & 5 deletions java/org/contikios/cooja/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Cooja.SimConfig> 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, String>();
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'");
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
Expand Down