[LIB-9] Add original chesshog source files
[chesshog.git] / src / main / java / org / hedgecode / chess / uci / ExternalEngineRunner.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.uci;
18
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;
28
29 import org.hedgecode.chess.uci.annotation.CommandDirection;
30 import org.hedgecode.chess.uci.command.CommandParams;
31
32 /**
33  * External UCI Engine Runner.
34  *
35  * @author Dmitry Samoshin aka gotty
36  */
37 public final class ExternalEngineRunner implements EngineRunner {
38
39     private Process process;
40     private ProcessBuilder processBuilder;
41
42     private BufferedReader reader;
43     private BufferedWriter writer;
44
45     private boolean isInit = false;
46
47     ExternalEngineRunner(String extEngineCommand) {
48         processBuilder = new ProcessBuilder(extEngineCommand);
49     }
50
51     ExternalEngineRunner(List<String> extEngineCommands) {
52         processBuilder = new ProcessBuilder(extEngineCommands);
53     }
54
55     @Override
56     public Engine init() throws EngineException {
57         if (isInit)
58             throw new EngineException("uci.engine.already.init");
59
60         try {
61             process = processBuilder.start();
62         } catch (IOException e) {
63             throw new EngineException("uci.engine.external.start", e.getLocalizedMessage());
64         }
65         reader = new BufferedReader(
66                 new InputStreamReader(
67                         process.getInputStream()
68                 )
69         );
70         writer = new BufferedWriter(
71                 new OutputStreamWriter(
72                         process.getOutputStream()
73                 )
74         );
75         isInit = true;
76
77         return new ExternalEngine(
78                 writer
79         );
80     }
81
82     @Override
83     public void run(final CommandExecutor commandExecutor) throws EngineException {
84         if (!isInit)
85             throw new EngineException("uci.engine.not.init");
86
87         Thread engineThread = new Thread(
88                 new Runnable() {
89                     public void run() {
90                         try {
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);
96                                 commandExecutor.exec(
97                                         commandParams.getName(),
98                                         CommandDirection.FROM_ENGINE,
99                                         commandParams.getParams()
100                                 );
101                             }
102                         } finally {
103                             try {
104                                 writer.close();
105                                 reader.close();
106                             } catch (IOException ignored) {
107                             }
108                             commandExecutor.exec(
109                                     UCIConstants.TERMINATE,
110                                     CommandDirection.FROM_ENGINE,
111                                     null
112                             );
113                         }
114                     }
115                 }
116         );
117         engineThread.start();
118     }
119
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));
127
128         Thread engineThread = new Thread(
129                 new Runnable() {
130                     public void run() {
131                         Scanner scanner = new Scanner(br); //
132                         while (scanner.hasNextLine()) {
133                             System.out.println(scanner.nextLine());
134                         }
135 /*
136                         String line;
137                         try {
138                             while ((line = br.readLine()) != null) {
139                                 System.out.println(line);
140                                 bw.write("isready");
141                             }
142                         } catch (IOException e) {
143                             e.printStackTrace();
144                         }
145 */
146                         System.out.println("Engine terminated.");
147                     }
148                 }
149         );
150         engineThread.start();
151
152         bw.write("uci\n");
153         bw.flush();
154         bw.write("isready\n");
155         bw.flush();
156         bw.write("quit\n");
157         bw.flush();
158
159         //process.waitFor();
160
161     }
162
163
164     public static void main(String... args) throws Exception {
165 /*
166         String[] strings = " uci     test one more test".trim().split("\\s+", 2);
167         for (String string : strings)
168             System.out.println(string);
169 */
170
171         new ExternalEngineRunner("stockfish.exe").run();
172     }
173
174 }