--- /dev/null
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.uci;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.hedgecode.chess.uci.annotation.CommandDirection;
+import org.hedgecode.chess.uci.command.CommandParams;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class ConsoleAcceptorRunner implements AcceptorRunner {
+
+ private ConsoleAcceptor acceptor;
+
+ private boolean isRunning;
+
+ ConsoleAcceptorRunner() {
+ acceptor = new ConsoleAcceptor(
+ System.in, System.out
+ );
+ isRunning = false;
+ }
+
+ ConsoleAcceptorRunner(InputStream input, OutputStream output) {
+ acceptor = new ConsoleAcceptor(
+ input, output
+ );
+ isRunning = false;
+ }
+
+ @Override
+ public Acceptor init() throws AcceptorException {
+ if (isRunning)
+ throw new AcceptorException("uci.acceptor.already.run");
+ return acceptor;
+ }
+
+ public void run(final CommandExecutor commandExecutor) throws AcceptorException {
+ if (isRunning)
+ throw new AcceptorException("uci.acceptor.already.run");
+
+ Thread engineThread = new Thread(
+ new Runnable() {
+ public void run() {
+ Transmitter<String> acceptorTransmitter = acceptor.transmitter();
+ try {
+ while (acceptorTransmitter.isActive()) {
+ if (acceptorTransmitter.hasCommand()) {
+ String command = acceptorTransmitter.transmitCommand();
+ if (command.isEmpty()) continue;
+ CommandParams commandParams = new CommandParams(command);
+ commandExecutor.exec(
+ commandParams.getName(),
+ CommandDirection.TO_ENGINE,
+ commandParams.getParams()
+ );
+ if (UCIConstants.QUIT.equals(commandParams.getName()))
+ acceptorTransmitter.stop();
+ }
+ }
+ } finally {
+ acceptorTransmitter.close();
+ isRunning = false;
+ }
+ }
+ }
+ );
+ engineThread.start();
+ isRunning = true;
+ }
+
+}