added method for faking mote output in tests: mostly used for debugging

This commit is contained in:
fros4943 2009-04-03 17:05:14 +00:00
parent b2a98f49e6
commit 38b38edf95
1 changed files with 62 additions and 33 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: LogScriptEngine.java,v 1.9 2009/03/03 15:55:39 fros4943 Exp $ * $Id: LogScriptEngine.java,v 1.10 2009/04/03 17:05:14 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -138,6 +138,18 @@ public class LogScriptEngine {
/* Create log observer: watches all log interfaces */ /* Create log observer: watches all log interfaces */
logObserver = new Observer() { logObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
Mote mote = (Mote) obj;
handleNewMoteOutput(
mote,
mote.getInterfaces().getMoteID().getMoteID(),
mote.getSimulation().getSimulationTime(),
mote.getInterfaces().getLog().getLastLogMessage()
);
}
};
}
private void handleNewMoteOutput(Mote mote, int id, long time, String msg) {
try { try {
if (scriptThread == null || if (scriptThread == null ||
!scriptThread.isAlive()) { !scriptThread.isAlive()) {
@ -147,11 +159,10 @@ public class LogScriptEngine {
} }
/* Update script variables */ /* Update script variables */
Mote mote = (Mote) obj;
engine.put("mote", mote); engine.put("mote", mote);
engine.put("id", mote.getInterfaces().getMoteID().getMoteID()); engine.put("id", id);
engine.put("time", mote.getSimulation().getSimulationTime()); engine.put("time", time);
engine.put("msg", mote.getInterfaces().getLog().getLastLogMessage()); engine.put("msg", msg);
stepScript(); stepScript();
@ -166,8 +177,25 @@ public class LogScriptEngine {
} }
} }
} }
};
/**
* Inject faked mote log output.
* Should only be used for debugging!
*
* @param msg Log message
* @param mote Mote
*/
public void fakeMoteLogOutput(final String msg, final Mote mote) {
gui.getSimulation().scheduleEvent(new TimeEvent(0) {
public void execute(long time) {
handleNewMoteOutput(
mote,
mote.getInterfaces().getMoteID().getMoteID(),
mote.getSimulation().getSimulationTime(),
msg
);
}
}, gui.getSimulation().getSimulationTime());
} }
public void setScriptLogObserver(Observer observer) { public void setScriptLogObserver(Observer observer) {
@ -245,15 +273,15 @@ public class LogScriptEngine {
semaphoreSim = null; semaphoreSim = null;
} }
if (scriptThread != null) { if (scriptThread != null && scriptThread != Thread.currentThread()) {
try { try {
scriptThread.join(); scriptThread.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
}
}
scriptThread = null; scriptThread = null;
}
}
} }
@ -323,12 +351,13 @@ public class LogScriptEngine {
} else { } else {
if (!GUI.isVisualized()) { if (!GUI.isVisualized()) {
logger.fatal("Test script error, terminating Cooja."); logger.fatal("Test script error, terminating Cooja.");
e.printStackTrace(); logger.fatal("Script error:", e);
System.exit(1); System.exit(1);
} }
/* Forward exception */ logger.fatal("Script error:", e);
throw e; gui.getSimulation().stopSimulation();
deactivateScript();
} }
} }
/*logger.info("test script thread exits");*/ /*logger.info("test script thread exits");*/