Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide simControl break mote thread action #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/corecomm_template.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class [CLASSNAME] extends CoreComm {

public native void tick();
public native void init();
public native void kill();
public native void setReferenceAddress(int addr);
public native void getMemory(int rel_addr, int length, byte[] mem);
public native void setMemory(int rel_addr, int length, byte[] mem);
Expand Down
6 changes: 6 additions & 0 deletions config/test_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int arr1[10];
int arr2[10];
int uvar1;
int uvar2;
int uvar3;

JNIEXPORT void JNICALL
Java_org_contikios_cooja_corecomm_[CLASS_NAME]_init(JNIEnv *env, jobject obj)
Expand Down Expand Up @@ -84,6 +85,11 @@ Java_org_contikios_cooja_corecomm_[CLASS_NAME]_tick(JNIEnv *env, jobject obj)
++var1;
++uvar1;
}
JNIEXPORT void JNICALL
Java_org_contikios_cooja_corecomm_[CLASS_NAME]_kill(JNIEnv *env, jobject obj)
{
++uvar3;
}
/*---------------------------------------------------------------------------*/
JNIEXPORT void JNICALL
Java_org_contikios_cooja_corecomm_[CLASS_NAME]_setReferenceAddress(JNIEnv *env, jobject obj, jint addr)
Expand Down
2 changes: 2 additions & 0 deletions java/org/contikios/cooja/CoreComm.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ public static CoreComm createCoreComm(String className, File libFile)
*/
public abstract void tick();

public abstract void kill();

/**
* Initializes a mote by running a startup script in the core. (Should only be
* run once, at the same time as the library is loaded)
Expand Down
11 changes: 10 additions & 1 deletion java/org/contikios/cooja/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public class Simulation extends Observable implements Runnable {
/* Used to restrict simulation speed */
private long speedLimitLastSimtime;
private long speedLimitLastRealtime;

private long lastStartTime;
private long currentSimulationTime = 0;
private TimeEvent currentSimulationEvent = null;

private String title = null;

Expand Down Expand Up @@ -276,9 +277,11 @@ public void run() {
if (nextEvent.time < currentSimulationTime) {
throw new RuntimeException("Next event is in the past: " + nextEvent.time + " < " + currentSimulationTime + ": " + nextEvent);
}
currentSimulationEvent= nextEvent;
currentSimulationTime = nextEvent.time;
/*logger.info("Executing event #" + EVENT_COUNTER++ + " @ " + currentSimulationTime + ": " + nextEvent);*/
nextEvent.execute(currentSimulationTime);
currentSimulationEvent = null;

if (stopSimulation) {
isRunning = false;
Expand Down Expand Up @@ -377,6 +380,12 @@ public void stopSimulation() {
stopSimulation(true);
}

public void breakSimulation() {
stopSimulation(true);
if (currentSimulationEvent != null)
currentSimulationEvent.kill();
}

/**
* Starts simulation if stopped, executes one millisecond, and finally stops
* simulation again.
Expand Down
3 changes: 3 additions & 0 deletions java/org/contikios/cooja/TimeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public boolean remove() {

public abstract void execute(long t);

//kills event execution - signal to execution thread
public void kill() {};

public String getShort() {
return "" + time + (name != null ? ": " + name : "");
}
Expand Down
69 changes: 66 additions & 3 deletions java/org/contikios/cooja/contikimote/ContikiMote.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
import org.contikios.cooja.Simulation;
import org.contikios.cooja.mote.memory.MemoryInterface;
import org.contikios.cooja.motes.AbstractWakeupMote;
import org.contikios.cooja.util.StringUtils;
import java.lang.Throwable;
import java.lang.Exception;
import java.lang.Error;
import java.lang.RuntimeException;


/**
* A Contiki mote executes an actual Contiki system via
Expand All @@ -65,6 +71,8 @@ public class ContikiMote extends AbstractWakeupMote implements Mote {
private ContikiMoteType myType = null;
private SectionMoteMemory myMemory = null;
private MoteInterfaceHandler myInterfaceHandler = null;
public enum MoteState{ STATE_OK, STATE_EXEC, STATE_HANG};
public MoteState execute_state = MoteState.STATE_OK;

/**
* Creates a new mote of given type.
Expand Down Expand Up @@ -113,6 +121,7 @@ public MoteType getType() {

public void setType(MoteType type) {
myType = (ContikiMoteType) type;
execute_state = MoteState.STATE_OK;
}

/**
Expand All @@ -127,6 +136,8 @@ public void setType(MoteType type) {
*/
@Override
public void execute(long simTime) {
if (execute_state != MoteState.STATE_OK)
return;

/* Poll mote interfaces */
myInterfaceHandler.doActiveActionsBeforeTick();
Expand All @@ -142,7 +153,38 @@ public void execute(long simTime) {
myType.setCoreMemory(myMemory);

/* Handle a single Contiki events */
myType.tick();
try {
execute_state = MoteState.STATE_EXEC;
myType.tick();
execute_state = MoteState.STATE_OK;
}
catch (RuntimeException e) {
execute_state = MoteState.STATE_HANG;
//coffeecatch_throw_exception rises Error
String dump = StringUtils.dumpStackTrace(e);
logger.fatal( "mote" + getID()
+ "crashed with:" + e.toString()
+ dump
);
}
catch (Exception e) {
execute_state = MoteState.STATE_HANG;
//coffeecatch_throw_exception rises Error
String dump = StringUtils.dumpStackTrace(e);
logger.fatal( "mote" + getID()
+ "crashed with:" + e.toString()
+ dump
);
}
catch (Error e) {
execute_state = MoteState.STATE_HANG;
//coffeecatch_throw_exception rises Error
String dump = StringUtils.dumpStackTrace(e);
logger.fatal( "mote" + getID()
+ "crashed with:" + e.toString()
+ dump
);
}

/* Copy mote memory from Contiki */
myType.getCoreMemory(myMemory);
Expand All @@ -151,6 +193,24 @@ public void execute(long simTime) {
myMemory.pollForMemoryChanges();
myInterfaceHandler.doActiveActionsAfterTick();
myInterfaceHandler.doPassiveActionsAfterTick();

if (execute_state != MoteState.STATE_OK) {
simulation.stopSimulation();
logger.warn( "stop simulation by hang of mote"+getID() );
// do not remove mote, just make it hung
//simulation.removeMote(this);
}
}

/**
* try abort mote executing thread
* */
@Override
public void kill() {
if (execute_state == MoteState.STATE_EXEC) {
logger.warn( "killing mote"+getID() );
myType.kill();
}
}

/**
Expand Down Expand Up @@ -202,8 +262,11 @@ public boolean setConfigXML(Simulation simulation, Collection<Element> configXML
simulation.getCooja().tryLoadClass(this, MoteInterface.class, intfClass);

if (moteInterfaceClass == null) {
logger.fatal("Could not load mote interface class: " + intfClass);
return false;
logger.fatal("Could not load mote"+ getID() +" interface class: " + intfClass);
continue;
//TODO new CCOJA revisions may have not investigated interfaces
// ignore this miss, to allow load later projects
//return false;
}

MoteInterface moteInterface = myInterfaceHandler.getInterfaceOfType(moteInterfaceClass);
Expand Down
7 changes: 7 additions & 0 deletions java/org/contikios/cooja/contikimote/ContikiMoteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,13 @@ public void tick() {
myCoreComm.tick();
}

/**
* try abort mote executing thread
* */
public void kill() {
myCoreComm.kill();
}

/**
* Creates and returns a copy of this mote type's initial memory (just after
* the init function has been run). When a new mote is created it should get
Expand Down
9 changes: 9 additions & 0 deletions java/org/contikios/cooja/motes/AbstractWakeupMote.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public abstract class AbstractWakeupMote implements Mote {
public void execute(long t) {
AbstractWakeupMote.this.execute(t);
}
@Override
public void kill() {
AbstractWakeupMote.this.kill();
}
public String toString() {
return "EXECUTE " + this.getClass().getName();
}
Expand All @@ -68,6 +72,11 @@ public void setSimulation(Simulation simulation) {
*/
public abstract void execute(long time);

/**
* try abort mote executing thread
* */
public void kill() {}

/**
* Execute mote software as soon as possible.
*
Expand Down
8 changes: 8 additions & 0 deletions java/org/contikios/cooja/plugins/SimControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public SimControl(Simulation simulation, Cooja gui) {
smallPanel.add(startButton = new JButton(startAction));
smallPanel.add(stopButton = new JButton(stopAction));
smallPanel.add(new JButton(stepAction));
smallPanel.add(new JButton(breakAction));
smallPanel.add(new JButton(reloadAction));

smallPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
Expand Down Expand Up @@ -321,6 +322,12 @@ public void actionPerformed(ActionEvent e) {
simulation.stepMillisecondSimulation();
}
};
private Action breakAction = new AbstractAction("Break") {
public void actionPerformed(ActionEvent e) {
simulation.breakSimulation();
stopButton.requestFocus();
}
};
private Action reloadAction = new AbstractAction("Reload") {
public void actionPerformed(ActionEvent e) {
simulation.getCooja().reloadCurrentSimulation(simulation.isRunning());
Expand All @@ -334,6 +341,7 @@ public String getQuickHelp() {
"<p><i>Pause</i> stops the simulation. " +
"<p>The keyboard shortcut for starting and pausing the simulation is <i>Ctrl+S</i>. " +
"<p><i>Step</i> runs the simulation for one millisecond. " +
"<p><i>Break</i> signal kill to simulaton thread of current mote. " +
"<p><i>Reload</i> reloads and restarts the simulation. " +
"<p>Simulation speed is controlled via the Speed limit menu.";
}
Expand Down
11 changes: 11 additions & 0 deletions java/org/contikios/cooja/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Throwable;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.zip.GZIPInputStream;

Expand Down Expand Up @@ -215,4 +217,13 @@ public static boolean saveToFile(File file, String text) {
return false;
}
}

public static
String dumpStackTrace(Throwable ex) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
ex.printStackTrace( printWriter );
printWriter.flush();
return writer.toString();
}
}