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,36 +138,64 @@ 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) {
try { Mote mote = (Mote) obj;
if (scriptThread == null || handleNewMoteOutput(
!scriptThread.isAlive()) { mote,
logger.info("script thread not alive. try deactivating script."); mote.getInterfaces().getMoteID().getMoteID(),
/*scriptThread.isInterrupted()*/ mote.getSimulation().getSimulationTime(),
return; mote.getInterfaces().getLog().getLastLogMessage()
} );
/* Update script variables */
Mote mote = (Mote) obj;
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();
} catch (UndeclaredThrowableException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"See console for more information.",
"Script error", JOptionPane.ERROR_MESSAGE);
unregisterLogObserver();
if (LogScriptEngine.this.gui.getSimulation() != null) {
LogScriptEngine.this.gui.getSimulation().stopSimulation();
}
}
} }
}; };
}
private void handleNewMoteOutput(Mote mote, int id, long time, String msg) {
try {
if (scriptThread == null ||
!scriptThread.isAlive()) {
logger.info("script thread not alive. try deactivating script.");
/*scriptThread.isInterrupted()*/
return;
}
/* Update script variables */
engine.put("mote", mote);
engine.put("id", id);
engine.put("time", time);
engine.put("msg", msg);
stepScript();
} catch (UndeclaredThrowableException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"See console for more information.",
"Script error", JOptionPane.ERROR_MESSAGE);
unregisterLogObserver();
if (LogScriptEngine.this.gui.getSimulation() != null) {
LogScriptEngine.this.gui.getSimulation().stopSimulation();
}
}
}
/**
* 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");*/