From 424985c5f85ac482e0152e3904f2b267d360981a Mon Sep 17 00:00:00 2001 From: fros4943 Date: Mon, 12 Jan 2009 10:45:40 +0000 Subject: [PATCH] added generate_message macro to contiki scripts --- .../sics/cooja/plugins/LogScriptEngine.java | 37 ++++++++++++++++++- .../se/sics/cooja/plugins/ScriptParser.java | 30 +++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java b/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java index 0689b38bb..5d63e98ba 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java +++ b/tools/cooja/java/se/sics/cooja/plugins/LogScriptEngine.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: LogScriptEngine.java,v 1.7 2009/01/08 16:33:14 fros4943 Exp $ + * $Id: LogScriptEngine.java,v 1.8 2009/01/12 10:45:40 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -86,6 +86,7 @@ public class LogScriptEngine { public void log(String log); public void testOK(); public void testFailed(); + public void generateMessage(long delay, String msg); } private void stepScript() { @@ -150,6 +151,7 @@ public class LogScriptEngine { engine.put("mote", mote); engine.put("id", mote.getInterfaces().getMoteID().getMoteID()); engine.put("time", mote.getSimulation().getSimulationTime()); + engine.put("msg", mote.getInterfaces().getLog().getLastLogMessage()); stepScript(); @@ -374,6 +376,10 @@ public class LogScriptEngine { } } }, gui.getSimulation().getSimulationTime()); + + if (timeoutEvent != null) { + timeoutEvent.remove(); + } } public void testFailed() { log("TEST FAILED\n"); @@ -399,6 +405,35 @@ public class LogScriptEngine { } }, gui.getSimulation().getSimulationTime()); + if (timeoutEvent != null) { + timeoutEvent.remove(); + } + } + + public void generateMessage(long delay, final String msg) { + final Mote currentMote = (Mote) engine.get("mote"); + + TimeEvent generateEvent = new TimeEvent(0) { + public void execute(long t) { + if (scriptThread == null || + !scriptThread.isAlive()) { + logger.info("script thread not alive. try deactivating script."); + /*scriptThread.isInterrupted()*/ + return; + } + + /* Update script variables */ + engine.put("mote", currentMote); + engine.put("id", currentMote.getInterfaces().getMoteID().getMoteID()); + engine.put("time", currentMote.getSimulation().getSimulationTime()); + engine.put("msg", msg); + + stepScript(); + } + }; + gui.getSimulation().scheduleEvent( + generateEvent, + gui.getSimulation().getSimulationTime() + delay); } }); diff --git a/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java b/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java index a32fe6950..98750eb61 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java +++ b/tools/cooja/java/se/sics/cooja/plugins/ScriptParser.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ScriptParser.java,v 1.2 2009/01/08 17:47:37 fros4943 Exp $ + * $Id: ScriptParser.java,v 1.3 2009/01/12 10:45:40 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -65,6 +65,8 @@ public class ScriptParser { code = replaceWaitUntils(code); + code = replaceGenerateMessages(code); + this.code = code; } @@ -215,6 +217,28 @@ public class ScriptParser { return code; } + private String replaceGenerateMessages(String code) throws ScriptSyntaxErrorException { + Pattern pattern = Pattern.compile( + "GENERATE_MSG\\(" + + "([0-9]+)" /* timeout */ + + "[\\s]*,[\\s]*" + + "(.*)" /* code */ + + "\\)" + ); + Matcher matcher = pattern.matcher(code); + + while (matcher.find()) { + long time = Long.parseLong(matcher.group(1)); + String msg = matcher.group(2); + + code = matcher.replaceFirst( + "log.generateMessage(" + time + "," + msg + ")"); + matcher.reset(code); + } + + return code; + } + public String getJSCode() { return "function SCRIPT_KILL() { " + @@ -236,7 +260,7 @@ public class ScriptParser { " SEMAPHORE_SCRIPT.acquire(); " /* SWITCH BLOCKS HERE! */ + " if (SHUTDOWN) { SCRIPT_KILL(); } " + " if (TIMEOUT) { SCRIPT_TIMEOUT(); } " + - " msg = mote.getInterfaces().getLog().getLastLogMessage(); " + + " msg = new java.lang.String(msg); " + " node.setMoteMsg(mote, msg); " + "};\n" + "function run() { " + @@ -244,7 +268,7 @@ public class ScriptParser { "SEMAPHORE_SCRIPT.acquire(); " + /* STARTUP BLOCKS HERE! */ "if (SHUTDOWN) { SCRIPT_KILL(); } " + "if (TIMEOUT) { SCRIPT_TIMEOUT(); } " + - "msg = mote.getInterfaces().getLog().getLastLogMessage(); " + + "msg = new java.lang.String(msg); " + "node.setMoteMsg(mote, msg); " + code + "\n" + "while (true) { SCRIPT_SWITCH(); } " /* SCRIPT ENDED */+