new event scheduling api

This commit is contained in:
fros4943 2009-10-27 10:11:17 +00:00
parent a81f216acb
commit ed8867bcb5
12 changed files with 157 additions and 99 deletions

View File

@ -26,20 +26,22 @@
* 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: MspClock.java,v 1.9 2009/05/26 14:31:07 fros4943 Exp $ * $Id: MspClock.java,v 1.10 2009/10/27 10:14:35 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote.interfaces; package se.sics.cooja.mspmote.interfaces;
import java.util.Collection; import java.util.Collection;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.ClassDescription;
import se.sics.cooja.Mote;
import se.sics.cooja.Simulation;
import se.sics.cooja.interfaces.Clock; import se.sics.cooja.interfaces.Clock;
import se.sics.cooja.mspmote.MspMote;
import se.sics.mspsim.core.MSP430;
/** /**
* @author Fredrik Osterlind * @author Fredrik Osterlind
@ -48,12 +50,12 @@ import se.sics.mspsim.core.MSP430;
public class MspClock extends Clock { public class MspClock extends Clock {
private static Logger logger = Logger.getLogger(MspClock.class); private static Logger logger = Logger.getLogger(MspClock.class);
private MspMote myMote; private Simulation simulation;
private MSP430 cpu;
private long timeDrift; /* Microseconds */
public MspClock(Mote mote) { public MspClock(Mote mote) {
myMote = (MspMote) mote; simulation = mote.getSimulation();
cpu = myMote.getCPU();
} }
public void setTime(long newTime) { public void setTime(long newTime) {
@ -61,19 +63,19 @@ public class MspClock extends Clock {
} }
public long getTime() { public long getTime() {
long time = (long) ((double)cpu.cycles * Simulation.MILLISECOND / MspMote.NR_CYCLES_PER_MSEC); return simulation.getSimulationTime() + timeDrift;
return time > 0 ? time : 0;
} }
public void setDrift(long drift) { public void setDrift(long drift) {
myMote.usDrift = drift; timeDrift = drift;
} }
public long getDrift() { public long getDrift() {
return myMote.usDrift; return timeDrift;
} }
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
/* TODO Show current CPU speed */
return null; return null;
} }

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: SkyByteRadio.java,v 1.12 2009/05/26 14:33:30 fros4943 Exp $ * $Id: SkyByteRadio.java,v 1.13 2009/10/27 10:14:35 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote.interfaces; package se.sics.cooja.mspmote.interfaces;
@ -173,6 +173,7 @@ public class SkyByteRadio extends Radio implements CustomDataRadio {
for (byte b: crossBufferedData) { for (byte b: crossBufferedData) {
cc2420.receivedByte(b); cc2420.receivedByte(b);
} }
mote.requestImmediateWakeup();
crossBufferedData = null; crossBufferedData = null;
} }
}; };
@ -217,6 +218,7 @@ public class SkyByteRadio extends Radio implements CustomDataRadio {
for (byte b: packetData) { for (byte b: packetData) {
cc2420.receivedByte(b); cc2420.receivedByte(b);
} }
mote.requestImmediateWakeup();
} }
/* Custom data radio support */ /* Custom data radio support */
@ -232,6 +234,7 @@ public class SkyByteRadio extends Radio implements CustomDataRadio {
if (data instanceof CC2420RadioByte) { if (data instanceof CC2420RadioByte) {
lastIncomingByte = (CC2420RadioByte) data; lastIncomingByte = (CC2420RadioByte) data;
cc2420.receivedByte(lastIncomingByte.getPacketData()[0]); cc2420.receivedByte(lastIncomingByte.getPacketData()[0]);
mote.requestImmediateWakeup();
} }
} }

View File

@ -26,38 +26,44 @@
* 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: SkySerial.java,v 1.16 2009/06/02 09:34:59 fros4943 Exp $ * $Id: SkySerial.java,v 1.17 2009/10/27 10:14:35 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote.interfaces; package se.sics.cooja.mspmote.interfaces;
import java.util.*; import java.util.Vector;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import se.sics.cooja.*; import se.sics.cooja.ClassDescription;
import se.sics.cooja.Mote;
import se.sics.cooja.Simulation;
import se.sics.cooja.TimeEvent; import se.sics.cooja.TimeEvent;
import se.sics.mspsim.core.*;
import se.sics.cooja.dialogs.SerialUI; import se.sics.cooja.dialogs.SerialUI;
import se.sics.cooja.interfaces.SerialPort; import se.sics.cooja.interfaces.SerialPort;
import se.sics.cooja.mspmote.SkyMote; import se.sics.cooja.mspmote.SkyMote;
import se.sics.mspsim.core.IOUnit;
import se.sics.mspsim.core.USART;
import se.sics.mspsim.core.USARTListener;
/** /**
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
@ClassDescription("Serial port") @ClassDescription("Serial port")
public class SkySerial extends SerialUI implements SerialPort { public class SkySerial extends SerialUI implements SerialPort {
private static final long DELAY_INCOMING_DATA = 69; /* Corresponds to 115200 bit/s */ private static final long DELAY_INCOMING_DATA = 69; /* 115200 bit/s */
private static Logger logger = Logger.getLogger(SkySerial.class); private static Logger logger = Logger.getLogger(SkySerial.class);
private Simulation simulation;
private SkyMote mote; private SkyMote mote;
private USART usart; private USART usart;
private Vector<Byte> incomingData = new Vector<Byte>(); private Vector<Byte> incomingData = new Vector<Byte>();
public SkySerial(Mote mote) { public SkySerial(Mote mote) {
this.mote = (SkyMote) mote; this.mote = (SkyMote) mote;
this.simulation = mote.getSimulation();
/* Listen to port writes */ /* Listen to port writes */
IOUnit ioUnit = this.mote.getCPU().getIOUnit("USART 1"); IOUnit ioUnit = this.mote.getCPU().getIOUnit("USART 1");
@ -78,7 +84,25 @@ public class SkySerial extends SerialUI implements SerialPort {
public void writeByte(byte b) { public void writeByte(byte b) {
incomingData.add(b); incomingData.add(b);
mote.getSimulation().scheduleEvent(writeDataEvent, mote.getSimulation().getSimulationTime()); if (writeDataEvent.isScheduled()) {
return;
}
/* Simulation thread: schedule immediately */
if (simulation.isSimulationThread()) {
simulation.scheduleEvent(writeDataEvent, simulation.getSimulationTime());
return;
}
/* Non-simulation thread: poll */
simulation.invokeSimulationThread(new Runnable() {
public void run() {
if (writeDataEvent.isScheduled()) {
return;
}
simulation.scheduleEvent(writeDataEvent, simulation.getSimulationTime());
}
});
} }
public void writeString(String s) { public void writeString(String s) {
@ -109,13 +133,14 @@ public class SkySerial extends SerialUI implements SerialPort {
b = incomingData.remove(0); b = incomingData.remove(0);
} }
usart.byteReceived(b); usart.byteReceived(b);
mote.requestImmediateWakeup();
} }
private TimeEvent writeDataEvent = new TimeEvent(0) { private TimeEvent writeDataEvent = new TimeEvent(0) {
public void execute(long t) { public void execute(long t) {
tryWriteNextByte(); tryWriteNextByte();
if (!incomingData.isEmpty()) { if (!incomingData.isEmpty()) {
mote.getSimulation().scheduleEvent(this, t+DELAY_INCOMING_DATA); simulation.scheduleEvent(this, t+DELAY_INCOMING_DATA);
} }
} }
}; };
@ -123,5 +148,4 @@ public class SkySerial extends SerialUI implements SerialPort {
public Mote getMote() { public Mote getMote() {
return mote; return mote;
} }
} }

View File

@ -26,24 +26,39 @@
* 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: TR1001Radio.java,v 1.13 2009/05/26 14:33:30 fros4943 Exp $ * $Id: TR1001Radio.java,v 1.14 2009/10/27 10:14:35 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote.interfaces; package se.sics.cooja.mspmote.interfaces;
import java.util.*;
import javax.swing.*;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.*; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.mspsim.core.*; import se.sics.cooja.ClassDescription;
import se.sics.cooja.*; import se.sics.cooja.Mote;
import se.sics.cooja.RadioPacket;
import se.sics.cooja.Simulation;
import se.sics.cooja.TimeEvent; import se.sics.cooja.TimeEvent;
import se.sics.cooja.interfaces.*; import se.sics.cooja.interfaces.CustomDataRadio;
import se.sics.cooja.interfaces.Position;
import se.sics.cooja.interfaces.Radio;
import se.sics.cooja.mspmote.ESBMote; import se.sics.cooja.mspmote.ESBMote;
import se.sics.mspsim.core.IOUnit;
import se.sics.mspsim.core.USART;
import se.sics.mspsim.core.USARTListener;
/** /**
* TR1001 radio interface on ESB platform. Assumes driver specifics such as * TR1001 radio interface on ESB platform. Assumes driver specifics such as
@ -133,9 +148,9 @@ public class TR1001Radio extends Radio implements USARTListener,
/* Convert to TR1001 packet data */ /* Convert to TR1001 packet data */
TR1001RadioByte[] byteArr = TR1001RadioPacketConverter.fromCoojaToTR1001(packet); TR1001RadioByte[] byteArr = TR1001RadioPacketConverter.fromCoojaToTR1001(packet);
final ArrayList<TR1001RadioByte> byteList = new ArrayList<TR1001RadioByte>(); final ArrayDeque<TR1001RadioByte> byteList = new ArrayDeque<TR1001RadioByte>();
for (TR1001RadioByte b : byteArr) { for (TR1001RadioByte b : byteArr) {
byteList.add(b); byteList.addLast(b);
} }
/* Feed incoming bytes to radio "slowly" via time events */ /* Feed incoming bytes to radio "slowly" via time events */
@ -143,10 +158,11 @@ public class TR1001Radio extends Radio implements USARTListener,
public void execute(long t) { public void execute(long t) {
/* Stop receiving data when buffer is empty */ /* Stop receiving data when buffer is empty */
if (byteList.isEmpty() || isInterfered) { if (byteList.isEmpty() || isInterfered) {
byteList.clear();
return; return;
} }
TR1001RadioByte b = byteList.remove(0); TR1001RadioByte b = byteList.pop();
radioUSART.byteReceived(b.getByte()); radioUSART.byteReceived(b.getByte());
mote.getSimulation().scheduleEvent(this, t + DELAY_BETWEEN_BYTES); mote.getSimulation().scheduleEvent(this, t + DELAY_BETWEEN_BYTES);
@ -196,9 +212,11 @@ public class TR1001Radio extends Radio implements USARTListener,
// Remember recent radio activity // Remember recent radio activity
millisSinceLastSend = 0; millisSinceLastSend = 0;
mote.getSimulation().scheduleEvent( if (!followupTransmissionEvent.isScheduled()) {
followupTransmissionEvent, mote.getSimulation().scheduleEvent(
mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND); followupTransmissionEvent,
mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
}
if (outgoingDataLength >= outgoingData.length) { if (outgoingDataLength >= outgoingData.length) {
logger.warn("----- TR1001 DROPPING OUTGOING BYTE (buffer overflow) -----"); logger.warn("----- TR1001 DROPPING OUTGOING BYTE (buffer overflow) -----");

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: ContikiButton.java,v 1.13 2009/05/26 14:24:20 fros4943 Exp $ * $Id: ContikiButton.java,v 1.14 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -127,7 +127,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
/* If mote is inactive, wake it up */ /* If mote is inactive, wake it up */
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
setChanged(); setChanged();
notifyObservers(); notifyObservers();
@ -141,7 +141,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
/* If mote is inactive, wake it up */ /* If mote is inactive, wake it up */
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
setChanged(); setChanged();
notifyObservers(); notifyObservers();

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: ContikiPIR.java,v 1.7 2009/05/26 14:24:20 fros4943 Exp $ * $Id: ContikiPIR.java,v 1.8 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -119,7 +119,7 @@ public class ContikiPIR extends PIR implements ContikiMoteInterface {
if (moteMem.getByteValueOf("simPirIsActive") == 1) { if (moteMem.getByteValueOf("simPirIsActive") == 1) {
moteMem.setByteValueOf("simPirChanged", (byte) 1); moteMem.setByteValueOf("simPirChanged", (byte) 1);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
} }

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: ContikiRS232.java,v 1.10 2009/06/15 14:40:28 fros4943 Exp $ * $Id: ContikiRS232.java,v 1.11 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -119,8 +119,8 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
public void writeString(String message) { public void writeString(String message) {
final byte[] dataToAppend = message.getBytes(); final byte[] dataToAppend = message.getBytes();
TimeEvent writeStringEvent = new MoteTimeEvent(mote, 0) { mote.getSimulation().invokeSimulationThread(new Runnable() {
public void execute(long t) { public void run() {
/* Append to existing buffer */ /* Append to existing buffer */
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength"); int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
int newSize = oldSize + dataToAppend.length; int newSize = oldSize + dataToAppend.length;
@ -135,13 +135,9 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
moteMem.setByteArray("simSerialReceivingData", newData); moteMem.setByteArray("simSerialReceivingData", newData);
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1); moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
}; });
mote.getSimulation().scheduleEvent(
writeStringEvent,
mote.getSimulation().getSimulationTime()
);
} }
public double energyConsumption() { public double energyConsumption() {
@ -196,13 +192,17 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
/* Reschedule us if more bytes are available */ /* Reschedule us if more bytes are available */
mote.getSimulation().scheduleEvent(this, t); mote.getSimulation().scheduleEvent(this, t);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
}; };
mote.getSimulation().scheduleEvent( mote.getSimulation().invokeSimulationThread(new Runnable() {
pendingBytesEvent, public void run() {
mote.getSimulation().getSimulationTime() mote.getSimulation().scheduleEvent(
); pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
}
});
} }
public void writeByte(final byte b) { public void writeByte(final byte b) {
@ -245,13 +245,17 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
/* Reschedule us if more bytes are available */ /* Reschedule us if more bytes are available */
mote.getSimulation().scheduleEvent(this, t); mote.getSimulation().scheduleEvent(this, t);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
}; };
mote.getSimulation().scheduleEvent( mote.getSimulation().invokeSimulationThread(new Runnable() {
pendingBytesEvent, public void run() {
mote.getSimulation().getSimulationTime() mote.getSimulation().scheduleEvent(
); pendingBytesEvent,
mote.getSimulation().getSimulationTime()
);
}
});
} }
} }

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: ContikiRadio.java,v 1.29 2009/09/17 11:06:35 fros4943 Exp $ * $Id: ContikiRadio.java,v 1.30 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -223,7 +223,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
// Unlock (if locked) // Unlock (if locked)
myMoteMemory.setByteValueOf("simReceiving", (byte) 0); myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
lastEventTime = mote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED; lastEvent = RadioEvent.RECEPTION_FINISHED;
@ -241,7 +241,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
lastEventTime = mote.getSimulation().getSimulationTime(); lastEventTime = mote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED; lastEvent = RadioEvent.RECEPTION_FINISHED;
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
this.setChanged(); this.setChanged();
this.notifyObservers(); this.notifyObservers();
} }
@ -308,7 +308,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
* data to the mote. * data to the mote.
*/ */
private void lockInReceivingMode() { private void lockInReceivingMode() {
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
// Lock core radio in receiving loop // Lock core radio in receiving loop
myMoteMemory.setByteValueOf("simReceiving", (byte) 1); myMoteMemory.setByteValueOf("simReceiving", (byte) 1);

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: ContikiVib.java,v 1.7 2009/05/26 14:24:20 fros4943 Exp $ * $Id: ContikiVib.java,v 1.8 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -119,7 +119,7 @@ public class ContikiVib extends MoteInterface implements ContikiMoteInterface {
if (moteMem.getByteValueOf("simVibIsActive") == 1) { if (moteMem.getByteValueOf("simVibIsActive") == 1) {
moteMem.setByteValueOf("simVibChanged", (byte) 1); moteMem.setByteValueOf("simVibChanged", (byte) 1);
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
} }

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: Battery.java,v 1.9 2009/09/17 11:08:07 fros4943 Exp $ * $Id: Battery.java,v 1.10 2009/10/27 10:11:17 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -127,7 +127,7 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
/* Check if we are out of energy */ /* Check if we are out of energy */
if (getEnergyConsumption() > INITIAL_ENERGY) { if (getEnergyConsumption() > INITIAL_ENERGY) {
mote.scheduleImmediateWakeup(); mote.requestImmediateWakeup();
} }
setChanged(); setChanged();

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.19 2009/10/23 11:55:53 fros4943 Exp $ * $Id: LogScriptEngine.java,v 1.20 2009/10/27 10:12:00 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -103,11 +103,11 @@ public class LogScriptEngine {
/* Check if test script requested us to stop */ /* Check if test script requested us to stop */
if (stopSimulation) { if (stopSimulation) {
stopSimulationEvent.execute(0); stopSimulationRunnable.run();
stopSimulation = false; stopSimulation = false;
} }
if (quitCooja) { if (quitCooja) {
quitEvent.execute(0); quitRunnable.run();
quitCooja = false; quitCooja = false;
} }
} }
@ -173,8 +173,8 @@ public class LogScriptEngine {
* @param mote Mote * @param mote Mote
*/ */
public void fakeMoteLogOutput(final String msg, final Mote mote) { public void fakeMoteLogOutput(final String msg, final Mote mote) {
simulation.scheduleEvent(new TimeEvent(0) { simulation.invokeSimulationThread(new Runnable() {
public void execute(long time) { public void run() {
handleNewMoteOutput( handleNewMoteOutput(
mote, mote,
mote.getID(), mote.getID(),
@ -182,7 +182,7 @@ public class LogScriptEngine {
msg msg
); );
} }
}, simulation.getSimulationTime()); });
} }
public void setScriptLogObserver(Observer observer) { public void setScriptLogObserver(Observer observer) {
@ -281,17 +281,16 @@ public class LogScriptEngine {
String jsCode = parser.getJSCode(); String jsCode = parser.getJSCode();
long timeoutTime = parser.getTimeoutTime(); long timeoutTime = parser.getTimeoutTime();
if (timeoutTime > 0) { if (timeoutTime < 0) {
simulation.scheduleEvent( logger.info("No timeout defined, using default (us): " + DEFAULT_TIMEOUT);
timeoutEvent, timeoutTime = DEFAULT_TIMEOUT;
simulation.getSimulationTime() + timeoutTime);
} else {
logger.info("No timeout defined, using default (us): " +
(simulation.getSimulationTime() + DEFAULT_TIMEOUT));
simulation.scheduleEvent(
timeoutEvent,
(simulation.getSimulationTime() + DEFAULT_TIMEOUT));
} }
final long absoluteTimeout = simulation.getSimulationTime() + timeoutTime;
simulation.invokeSimulationThread(new Runnable() {
public void run() {
simulation.scheduleEvent(timeoutEvent, absoluteTimeout);
}
});
engine.eval(jsCode); engine.eval(jsCode);
@ -375,10 +374,10 @@ public class LogScriptEngine {
if (GUI.isVisualized()) { if (GUI.isVisualized()) {
log("[if test was run without visualization, COOJA would now have been terminated]\n"); log("[if test was run without visualization, COOJA would now have been terminated]\n");
stopSimulation = true; stopSimulation = true;
simulation.scheduleEvent(stopSimulationEvent, simulation.getSimulationTime()); simulation.invokeSimulationThread(stopSimulationRunnable);
} else { } else {
quitCooja = true; quitCooja = true;
simulation.scheduleEvent(quitEvent, simulation.getSimulationTime()); simulation.invokeSimulationThread(quitRunnable);
} }
timeoutEvent.remove(); timeoutEvent.remove();
@ -392,19 +391,19 @@ public class LogScriptEngine {
if (GUI.isVisualized()) { if (GUI.isVisualized()) {
log("[if test was run without visualization, COOJA would now have been terminated]\n"); log("[if test was run without visualization, COOJA would now have been terminated]\n");
stopSimulation = true; stopSimulation = true;
simulation.scheduleEvent(stopSimulationEvent, simulation.getSimulationTime()); simulation.invokeSimulationThread(stopSimulationRunnable);
} else { } else {
quitCooja = true; quitCooja = true;
simulation.scheduleEvent(quitEvent, simulation.getSimulationTime()); simulation.invokeSimulationThread(quitRunnable);
} }
semaphoreSim.release(100); semaphoreSim.release(100);
throw new RuntimeException("test script killed"); throw new RuntimeException("test script killed");
} }
public void generateMessage(long delay, final String msg) { public void generateMessage(final long delay, final String msg) {
final Mote currentMote = (Mote) engine.get("mote"); final Mote currentMote = (Mote) engine.get("mote");
TimeEvent generateEvent = new TimeEvent(0) { final TimeEvent generateEvent = new TimeEvent(0) {
public void execute(long t) { public void execute(long t) {
if (scriptThread == null || if (scriptThread == null ||
!scriptThread.isAlive()) { !scriptThread.isAlive()) {
@ -422,9 +421,13 @@ public class LogScriptEngine {
stepScript(); stepScript();
} }
}; };
simulation.scheduleEvent( simulation.invokeSimulationThread(new Runnable() {
generateEvent, public void run() {
simulation.getSimulationTime() + delay*Simulation.MILLISECOND); simulation.scheduleEvent(
generateEvent,
simulation.getSimulationTime() + delay*Simulation.MILLISECOND);
}
});
} }
}); });
@ -444,14 +447,14 @@ public class LogScriptEngine {
stepScript(); stepScript();
} }
}; };
private TimeEvent stopSimulationEvent = new TimeEvent(0) { private Runnable stopSimulationRunnable = new Runnable() {
public void execute(long time) { public void run() {
simulation.stopSimulation(); simulation.stopSimulation();
timeoutEvent.remove(); timeoutEvent.remove();
} }
}; };
private TimeEvent quitEvent = new TimeEvent(0) { private Runnable quitRunnable = new Runnable() {
public void execute(long time) { public void run() {
simulation.stopSimulation(); simulation.stopSimulation();
new Thread() { new Thread() {
public void run() { public void run() {

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: SimControl.java,v 1.15 2009/07/06 12:29:57 fros4943 Exp $ * $Id: SimControl.java,v 1.16 2009/10/27 10:12:00 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -122,7 +122,7 @@ public class SimControl extends VisPlugin {
stopEvent.remove(); stopEvent.remove();
} }
long t = ((Number)e.getNewValue()).intValue()*Simulation.MILLISECOND; final long t = ((Number)e.getNewValue()).intValue()*Simulation.MILLISECOND;
if (t <= SimControl.this.simulation.getSimulationTime()) { if (t <= SimControl.this.simulation.getSimulationTime()) {
/* No simulation stop scheduled */ /* No simulation stop scheduled */
stopTimeTextField.setBackground(Color.LIGHT_GRAY); stopTimeTextField.setBackground(Color.LIGHT_GRAY);
@ -131,7 +131,11 @@ public class SimControl extends VisPlugin {
/* Schedule simulation stop */ /* Schedule simulation stop */
stopTimeTextField.setBackground(Color.WHITE); stopTimeTextField.setBackground(Color.WHITE);
stopTimeTextField.setToolTipText("Simulation will stop at time (us): " + t); stopTimeTextField.setToolTipText("Simulation will stop at time (us): " + t);
SimControl.this.simulation.scheduleEvent(stopEvent, t); SimControl.this.simulation.invokeSimulationThread(new Runnable() {
public void run() {
SimControl.this.simulation.scheduleEvent(stopEvent, t);
}
});
} }
} }
}); });