[LIB-9] Add chesshog-qrcode module
[chesshog.git] / chesshog-uci / 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.IOException;
20 import java.util.List;
21
22 import org.hedgecode.chess.uci.annotation.CommandDirection;
23 import org.hedgecode.chess.uci.command.CommandParams;
24
25 /**
26  * External UCI Engine Runner.
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public final class ExternalEngineRunner implements EngineRunner {
31
32     private Process process;
33     private ProcessBuilder processBuilder;
34
35     private ExternalEngine engine;
36
37     private boolean isInit = false;
38
39     ExternalEngineRunner(String extEngineCommand) {
40         processBuilder = new ProcessBuilder(extEngineCommand);
41     }
42
43     ExternalEngineRunner(List<String> extEngineCommands) {
44         processBuilder = new ProcessBuilder(extEngineCommands);
45     }
46
47     @Override
48     public Engine init() throws EngineException {
49         if (isInit)
50             throw new EngineException("uci.engine.already.init");
51
52         try {
53             process = processBuilder.start();
54         } catch (IOException e) {
55             throw new EngineException("uci.engine.external.start", e.getLocalizedMessage());
56         }
57
58         engine = new ExternalEngine(
59                 process.getInputStream(),
60                 process.getOutputStream()
61         );
62         isInit = true;
63
64         return engine;
65     }
66
67     @Override
68     public void run(final CommandExecutor commandExecutor) throws EngineException {
69         if (!isInit)
70             throw new EngineException("uci.engine.not.init");
71
72         Thread engineThread = new Thread(
73                 new Runnable() {
74                     public void run() {
75                         Transmitter<String> engineTransmitter = engine.transmitter();
76                         try {
77                             while (engineTransmitter.hasCommand()) {
78                                 String command = engineTransmitter.transmitCommand();
79                                 if (command.isEmpty()) continue;
80                                 CommandParams commandParams = new CommandParams(command);
81                                 commandExecutor.exec(
82                                         commandParams.getName(),
83                                         CommandDirection.FROM_ENGINE,
84                                         commandParams.getParams()
85                                 );
86                             }
87                         } finally {
88                             commandExecutor.exec(
89                                     UCIConstants.TERMINATE,
90                                     CommandDirection.FROM_ENGINE,
91                                     null
92                             );
93                             engineTransmitter.stop();
94                             engineTransmitter.close();
95                             if (process.isAlive()) process.destroy();
96                             isInit = false;
97                         }
98                     }
99                 }
100         );
101         engineThread.start();
102     }
103
104 }