[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / chesshog-hedgefish / src / main / java / org / hedgecode / chess / hedgefish / HedgefishEngineRunner.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.hedgefish;
18
19 import org.hedgecode.chess.uci.CommandExecutor;
20 import org.hedgecode.chess.uci.Engine;
21 import org.hedgecode.chess.uci.EngineException;
22 import org.hedgecode.chess.uci.EngineRunner;
23 import org.hedgecode.chess.uci.Transmitter;
24 import org.hedgecode.chess.uci.UCIConstants;
25 import org.hedgecode.chess.uci.annotation.CommandDirection;
26
27 /**
28  * Hedgefish UCI Engine Runner.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public final class HedgefishEngineRunner implements EngineRunner {
33
34     private HedgefishEngine engine;
35     private boolean isRunning;
36
37     HedgefishEngineRunner() {
38         engine = new HedgefishEngine();
39         isRunning = false;
40     }
41
42     @Override
43     public Engine init() throws EngineException {
44         if (isRunning)
45             throw new EngineException("uci.engine.already.run");
46         return engine;
47     }
48
49     @Override
50     public void run(final CommandExecutor commandExecutor) throws EngineException {
51         if (isRunning)
52             throw new EngineException("uci.engine.already.run");
53
54         Thread engineThread = new Thread(
55                 new Runnable() {
56                     public void run() {
57                         Transmitter<HedgefishCommand> engineTransmitter = engine.transmitter();
58                         try {
59                             while (engineTransmitter.isActive()) {
60                                 if (engineTransmitter.hasCommand()) {
61                                     HedgefishCommand command = engineTransmitter.transmitCommand();
62                                     commandExecutor.exec(
63                                             command.getName(),
64                                             CommandDirection.FROM_ENGINE,
65                                             command.getParams()
66                                     );
67                                 }
68                             }
69                         } finally {
70                             commandExecutor.exec(
71                                     UCIConstants.TERMINATE,
72                                     CommandDirection.FROM_ENGINE,
73                                     null
74                             );
75                             engineTransmitter.close();
76                             isRunning = false;
77                         }
78                     }
79                 }
80         );
81         engineThread.start();
82         isRunning = true;
83     }
84
85 }