Skip to content

Commit 7a9474d

Browse files
author
gerhard.kalab
committedDec 7, 2014
skip multiple analyze commands within a short time span
1 parent 7b2e54a commit 7a9474d

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed
 

‎src/org/scid/android/engine/PipedProcess.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class PipedProcess {
1717
private BufferedReader reader = null;
1818
private BufferedWriter writer = null;
1919
private Process process;
20+
private String nextAnalyzeCommand;
2021

2122
/** Start process. */
2223
public final void initialize(EngineConfig engineConfig) {
@@ -31,6 +32,27 @@ public EngineConfig getEngineConfig() {
3132
return engineConfig;
3233
}
3334

35+
private void writeAnalyzeCommands(String cmd) {
36+
writeLineToProcess("stop");
37+
writeLineToProcess("isready");
38+
writeLineToProcess(cmd);
39+
writeLineToProcess("go infinite");
40+
}
41+
42+
public synchronized void analyzeNext() {
43+
if (this.nextAnalyzeCommand != null) {
44+
writeAnalyzeCommands(nextAnalyzeCommand);
45+
this.nextAnalyzeCommand = null;
46+
}
47+
}
48+
49+
/**
50+
* TODO: use a delayed queue instead (e.g. DelayQueue) or FutureTask?
51+
*/
52+
public synchronized void setNextAnalyzeCommand(String nextAnalyzeCommand) {
53+
this.nextAnalyzeCommand = nextAnalyzeCommand;
54+
}
55+
3456
public boolean isAlive() {
3557
return processAlive && !isTerminated();
3658
}
@@ -83,9 +105,9 @@ public final synchronized String readLineFromProcess() {
83105
} catch (IOException e) {
84106
Log.e(TAG, "Error reading from process");
85107
}
86-
// if (ret != null && ret.length() > 0) {
87-
// Log.d("SCID", "Engine -> GUI: " + ret);
88-
// }
108+
// if (ret != null && ret.length() > 0) {
109+
// Log.d("SCID", "Engine -> GUI: " + ret);
110+
// }
89111
return ret;
90112
}
91113

@@ -95,7 +117,7 @@ public final synchronized String readLineFromProcess() {
95117
* @throws IOException
96118
*/
97119
public final synchronized void writeLineToProcess(String data) {
98-
// Log.d("SCID", "GUI -> Engine: " + data);
120+
// Log.d("SCID", "GUI -> Engine: " + data);
99121
try {
100122
writeToProcess(data + "\n");
101123
} catch (IOException e) {
@@ -106,7 +128,8 @@ public final synchronized void writeLineToProcess(String data) {
106128

107129
/** Start the child process. */
108130
private final void startProcess(EngineConfig engineConfig) {
109-
ProcessBuilder builder = new ProcessBuilder(engineConfig.getExecutablePath());
131+
ProcessBuilder builder = new ProcessBuilder(
132+
engineConfig.getExecutablePath());
110133
builder.redirectErrorStream(true);
111134
try {
112135
Log.d(TAG, "starting process");

‎src/org/scid/android/engine/UciReadTask.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ protected Void doInBackground(Void... params) {
5656
} catch (InterruptedException e) {
5757
// ignore
5858
}
59-
if (line == null || line.length() == 0 || listener == null) {
60-
continue;
61-
}
62-
String[] tokens = tokenize(line);
63-
if (tokens[0].equals("info")) {
64-
parseInfoCmd(tokens);
59+
if (line != null && line.length() > 0 && listener != null) {
60+
String[] tokens = tokenize(line);
61+
if (tokens[0].equals("info")) {
62+
parseInfoCmd(tokens);
63+
}
6564
}
6665
if (!this.isCancelled()
67-
&& System.currentTimeMillis() > (lastPublishedTime + 300)) {
66+
&& System.currentTimeMillis() > (lastPublishedTime + 500)) {
6867
publishProgress();
6968
lastPublishedTime = System.currentTimeMillis();
69+
engineProcess.analyzeNext();
7070
}
7171
}
7272
engineProcess.writeLineToProcess("quit");
@@ -188,11 +188,8 @@ private final String[] tokenize(String cmdLine) {
188188
return cmdLine.trim().split("\\s+");
189189
}
190190

191-
public void setCurrentPosition(Position currentPosition) {
192-
this.currentPosition = currentPosition;
193-
}
194-
195-
public void setCurrentGame(Game game) {
191+
public void setCurrentPosition(Game game, Position currentPosition) {
196192
this.game = game;
193+
this.currentPosition = currentPosition;
197194
}
198195
}

‎src/org/scid/android/gamelogic/ChessController.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,8 @@ private final synchronized void startAnalysis() {
324324
}
325325
}
326326
PipedProcess process = computerPlayer.getEngine();
327-
process.writeLineToProcess("stop");
328-
process.writeLineToProcess("isready");
329-
process.writeLineToProcess(posStr.toString());
330-
process.writeLineToProcess("go infinite");
331-
readTask.setCurrentPosition(currentPosition);
332-
readTask.setCurrentGame(game);
327+
process.setNextAnalyzeCommand(posStr.toString());
328+
readTask.setCurrentPosition(game, currentPosition);
333329
}
334330
updateGUI();
335331
}

0 commit comments

Comments
 (0)
Failed to load comments.