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 reaction on Exception/Error fom JNI level. #33

Open
wants to merge 1 commit 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
54 changes: 52 additions & 2 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_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,36 @@ public void execute(long simTime) {
myType.setCoreMemory(myMemory);

/* Handle a single Contiki events */
try {
myType.tick();
}
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 +191,13 @@ 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);
}
}

/**
Expand Down Expand Up @@ -202,8 +249,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
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();
}
}