rewriting mote interfaces for faster simulation execution.

log interface
This commit is contained in:
fros4943 2008-10-28 10:28:38 +00:00
parent 960f32f3d4
commit 86c3ea9bc8

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -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: ContikiLog.java,v 1.5 2008/10/03 15:18:48 fros4943 Exp $ * $Id: ContikiLog.java,v 1.6 2008/10/28 10:28:38 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -40,11 +40,12 @@ import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.Log; import se.sics.cooja.interfaces.Log;
import se.sics.cooja.interfaces.PolledAfterActiveTicks;
/** /**
* The class Log is an abstract interface to a mote's logging output. It needs * Log mote interface. Captures both log_message(,) and printf(..).
* read access to the following core variables: *
* Contiki variables:
* <ul> * <ul>
* <li>char simLoggedFlag * <li>char simLoggedFlag
* (1=mote has new outgoing log messages, else no new) * (1=mote has new outgoing log messages, else no new)
@ -53,18 +54,18 @@ import se.sics.cooja.interfaces.Log;
* <li>byte[] simLoggedData (data of new log messages) * <li>byte[] simLoggedData (data of new log messages)
* </ul> * </ul>
* <p> * <p>
* Dependency core interfaces are: *
* Core interface:
* <ul> * <ul>
* <li>simlog_interface * <li>simlog_interface
* </ul> * </ul>
* <p> * <p>
* This observable is changed and notifies observers whenever a log message has
* been received from the core (checked after each tick). The public method
* getLastLogMessage gives access to the last log message.
* *
* @author Fredrik Osterlind * This observable notifies at new mote log output.
*
* @author Fredrik Österlind
*/ */
public class ContikiLog extends Log implements ContikiMoteInterface { public class ContikiLog extends Log implements ContikiMoteInterface, PolledAfterActiveTicks {
private static Logger logger = Logger.getLogger(ContikiLog.class); private static Logger logger = Logger.getLogger(ContikiLog.class);
private Mote mote = null; private Mote mote = null;
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
@ -87,13 +88,9 @@ public class ContikiLog extends Log implements ContikiMoteInterface {
return new String[] { "simlog_interface" }; return new String[] { "simlog_interface" };
} }
public void doActionsBeforeTick() {
// Nothing to do
}
public void doActionsAfterTick() { public void doActionsAfterTick() {
if (moteMem.getByteValueOf("simLoggedFlag") == 1) { if (moteMem.getByteValueOf("simLoggedFlag") == 1) {
int totalLength = moteMem.getIntValueOf("simLoggedLength"); int totalLength = moteMem.getIntValueOf("simLoggedLength");
byte[] bytes = moteMem.getByteArray("simLoggedData", totalLength); byte[] bytes = moteMem.getByteArray("simLoggedData", totalLength);
char[] chars = new char[bytes.length]; char[] chars = new char[bytes.length];
for (int i=0; i < chars.length; i++) { for (int i=0; i < chars.length; i++) {
@ -103,8 +100,8 @@ public class ContikiLog extends Log implements ContikiMoteInterface {
moteMem.setByteValueOf("simLoggedFlag", (byte) 0); moteMem.setByteValueOf("simLoggedFlag", (byte) 0);
moteMem.setIntValueOf("simLoggedLength", 0); moteMem.setIntValueOf("simLoggedLength", 0);
String fullMessage[] = String.valueOf(chars).split("\n"); String messages[] = String.valueOf(chars).split("\n");
for (String message: fullMessage) { for (String message: messages) {
lastLogMessage = message; lastLogMessage = message;
this.setChanged(); this.setChanged();
@ -127,13 +124,13 @@ public class ContikiLog extends Log implements ContikiMoteInterface {
if (lastLogMessage == null) { if (lastLogMessage == null) {
logTextPane.setText(""); logTextPane.setText("");
} else { } else {
logTextPane.append(lastLogMessage); logTextPane.append(lastLogMessage + "\n");
} }
Observer observer; Observer observer;
this.addObserver(observer = new Observer() { this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
logTextPane.append(lastLogMessage); logTextPane.append(lastLogMessage + "\n");
logTextPane.setCaretPosition(logTextPane.getDocument().getLength()); logTextPane.setCaretPosition(logTextPane.getDocument().getLength());
} }
}); });