2 * Copyright (c) 2018. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.uci;
19 import java.io.BufferedReader;
20 import java.io.BufferedWriter;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.util.List;
27 import java.util.Scanner;
29 import org.hedgecode.chess.uci.annotation.CommandDirection;
30 import org.hedgecode.chess.uci.command.CommandParams;
33 * External UCI Engine Runner.
35 * @author Dmitry Samoshin aka gotty
37 public final class ExternalEngineRunner implements EngineRunner {
39 private Process process;
40 private ProcessBuilder processBuilder;
42 private BufferedReader reader;
43 private BufferedWriter writer;
45 private boolean isInit = false;
47 ExternalEngineRunner(String extEngineCommand) {
48 processBuilder = new ProcessBuilder(extEngineCommand);
51 ExternalEngineRunner(List<String> extEngineCommands) {
52 processBuilder = new ProcessBuilder(extEngineCommands);
56 public Engine init() throws EngineException {
58 throw new EngineException("uci.engine.already.init");
61 process = processBuilder.start();
62 } catch (IOException e) {
63 throw new EngineException("uci.engine.external.start", e.getLocalizedMessage());
65 reader = new BufferedReader(
66 new InputStreamReader(
67 process.getInputStream()
70 writer = new BufferedWriter(
71 new OutputStreamWriter(
72 process.getOutputStream()
77 return new ExternalEngine(
83 public void run(final CommandExecutor commandExecutor) throws EngineException {
85 throw new EngineException("uci.engine.not.init");
87 Thread engineThread = new Thread(
91 Scanner scanner = new Scanner(reader);
92 while (scanner.hasNextLine()) {
93 String command = scanner.nextLine();
94 if (command.isEmpty()) continue;
95 CommandParams commandParams = new CommandParams(command);
97 commandParams.getName(),
98 CommandDirection.FROM_ENGINE,
99 commandParams.getParams()
106 } catch (IOException ignored) {
108 commandExecutor.exec(
109 UCIConstants.TERMINATE,
110 CommandDirection.FROM_ENGINE,
117 engineThread.start();
120 public void run() throws IOException, InterruptedException {
121 //Map<String, String> environment = processBuilder.environment();
122 Process process = processBuilder.start();
123 InputStream is = process.getInputStream();
124 BufferedReader br = new BufferedReader(new InputStreamReader(is));
125 OutputStream os = process.getOutputStream();
126 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
128 Thread engineThread = new Thread(
131 Scanner scanner = new Scanner(br); //
132 while (scanner.hasNextLine()) {
133 System.out.println(scanner.nextLine());
138 while ((line = br.readLine()) != null) {
139 System.out.println(line);
142 } catch (IOException e) {
146 System.out.println("Engine terminated.");
150 engineThread.start();
154 bw.write("isready\n");
164 public static void main(String... args) throws Exception {
166 String[] strings = " uci test one more test".trim().split("\\s+", 2);
167 for (String string : strings)
168 System.out.println(string);
171 new ExternalEngineRunner("stockfish.exe").run();