DroidFish: Display an error message if an external engine fails to start.

This commit is contained in:
Peter Osterlund 2012-01-04 19:07:26 +00:00
parent 869f45334e
commit 915b0d6897

View File

@ -16,17 +16,21 @@ public class ExternalEngine extends UCIEngineBase {
private static final String exePath = "/data/data/org.petero.droidfish/engine.exe"; private static final String exePath = "/data/data/org.petero.droidfish/engine.exe";
private final Report report; private final Report report;
private Process engineProc; private Process engineProc;
private Thread exitThread;
private Thread stdInThread; private Thread stdInThread;
private Thread stdErrThread; private Thread stdErrThread;
private List<String> inLines; private List<String> inLines;
private boolean startedOk;
public ExternalEngine(String engine, Report report) { public ExternalEngine(String engine, Report report) {
this.report = report; this.report = report;
engineFileName = new File(engine); engineFileName = new File(engine);
engineProc = null; engineProc = null;
exitThread = null;
stdInThread = null; stdInThread = null;
stdErrThread = null; stdErrThread = null;
inLines = new LinkedList<String>(); inLines = new LinkedList<String>();
startedOk = false;
} }
/** @inheritDoc */ /** @inheritDoc */
@ -38,6 +42,19 @@ public class ExternalEngine extends UCIEngineBase {
ProcessBuilder pb = new ProcessBuilder(exePath); ProcessBuilder pb = new ProcessBuilder(exePath);
engineProc = pb.start(); engineProc = pb.start();
exitThread = new Thread(new Runnable() {
@Override
public void run() {
try {
engineProc.waitFor();
if (!startedOk)
report.reportError("Failed to start engine");
} catch (InterruptedException e) {
}
}
});
exitThread.start();
// Start a thread to read stdin // Start a thread to read stdin
stdInThread = new Thread(new Runnable() { stdInThread = new Thread(new Runnable() {
@Override @Override
@ -56,6 +73,7 @@ public class ExternalEngine extends UCIEngineBase {
synchronized (inLines) { synchronized (inLines) {
inLines.add(line); inLines.add(line);
inLines.notify(); inLines.notify();
startedOk = true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -144,6 +162,8 @@ public class ExternalEngine extends UCIEngineBase {
if (engineProc != null) if (engineProc != null)
engineProc.destroy(); engineProc.destroy();
engineProc = null; engineProc = null;
if (exitThread != null)
exitThread.interrupt();
if (stdInThread != null) if (stdInThread != null)
stdInThread.interrupt(); stdInThread.interrupt();
if (stdErrThread != null) if (stdErrThread != null)