DroidFish: Display an error message when loading an engine that doesn't support the UCI protocol.

This commit is contained in:
Peter Osterlund 2012-01-08 02:39:31 +00:00
parent f544cfdec3
commit 006e66397e
5 changed files with 37 additions and 7 deletions

View File

@ -409,6 +409,7 @@ you are not actively using the program.\
<string name="cuckoochess_engine">CuckooChess</string>
<string name="failed_to_start_engine">Failed to start engine</string>
<string name="engine_terminated">Engine terminated</string>
<string name="uci_protocol_error">UCI protocol error</string>
<string name="err_too_few_spaces">Too few spaces</string>
<string name="err_invalid_piece">Invalid piece</string>

View File

@ -41,22 +41,26 @@ public class ExternalEngine extends UCIEngineBase {
private static final String exePath = "/data/data/org.petero.droidfish/engine.exe";
private final Report report;
private Process engineProc;
private Thread startupThread;
private Thread exitThread;
private Thread stdInThread;
private Thread stdErrThread;
private List<String> inLines;
private boolean startedOk;
private boolean isRunning;
public ExternalEngine(Context context, String engine, Report report) {
this.context = context;
this.report = report;
engineFileName = new File(engine);
engineProc = null;
startupThread = null;
exitThread = null;
stdInThread = null;
stdErrThread = null;
inLines = new LinkedList<String>();
startedOk = false;
isRunning = false;
}
/** @inheritDoc */
@ -68,11 +72,26 @@ public class ExternalEngine extends UCIEngineBase {
ProcessBuilder pb = new ProcessBuilder(exePath);
engineProc = pb.start();
startupThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
return;
}
if (startedOk && isRunning && !isUCI)
report.reportError(context.getString(R.string.uci_protocol_error));
}
});
startupThread.start();
exitThread = new Thread(new Runnable() {
@Override
public void run() {
try {
engineProc.waitFor();
isRunning = false;
if (!startedOk)
report.reportError(context.getString(R.string.failed_to_start_engine));
else {
@ -96,13 +115,18 @@ public class ExternalEngine extends UCIEngineBase {
BufferedReader br = new BufferedReader(isr, 8192);
String line;
try {
boolean first = true;
while ((line = br.readLine()) != null) {
if ((ep == null) || Thread.currentThread().isInterrupted())
return;
synchronized (inLines) {
inLines.add(line);
inLines.notify();
startedOk = true;
if (first) {
startedOk = true;
isRunning = true;
first = false;
}
}
}
} catch (IOException e) {
@ -138,6 +162,7 @@ public class ExternalEngine extends UCIEngineBase {
/** @inheritDoc */
@Override
public void initOptions() {
super.initOptions();
setOption("Hash", 16);
}
@ -187,6 +212,8 @@ public class ExternalEngine extends UCIEngineBase {
/** @inheritDoc */
@Override
public void shutDown() {
if (startupThread != null)
startupThread.interrupt();
if (exitThread != null)
exitThread.interrupt();
super.shutDown();

View File

@ -38,12 +38,6 @@ public class InternalStockFish extends ExternalEngine {
super(context, "", report);
}
/** @inheritDoc */
@Override
public final void initOptions() {
setOption("Hash", 16);
}
/** @inheritDoc */
@Override
public final void setStrength(int strength) {

View File

@ -30,6 +30,7 @@ public abstract class UCIEngineBase implements UCIEngine {
private boolean processAlive;
private HashSet<String> allOptions;
private HashMap<String, String> currOptions;
protected boolean isUCI;
public static UCIEngine getEngine(Context context, String engine, Report report) {
if ("stockfish".equals(engine) && (EngineUtil.internalStockFishName() == null))
@ -46,6 +47,7 @@ public abstract class UCIEngineBase implements UCIEngine {
processAlive = false;
allOptions = new HashSet<String>();
currOptions = new HashMap<String, String>();
isUCI = false;
}
protected abstract void startProcess();
@ -58,6 +60,11 @@ public abstract class UCIEngineBase implements UCIEngine {
}
}
@Override
public void initOptions() {
isUCI = true;
}
@Override
public void shutDown() {
if (processAlive) {

View File

@ -84,6 +84,7 @@ public class CuckooChessEngine extends UCIEngineBase {
/** @inheritDoc */
@Override
public final void initOptions() {
super.initOptions();
}
/** @inheritDoc */