added generate_message macro to contiki scripts

This commit is contained in:
fros4943 2009-01-12 10:45:40 +00:00
parent 30e6d4943a
commit 424985c5f8
2 changed files with 63 additions and 4 deletions

View file

@ -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);
}
});

View file

@ -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 */+