/* * 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 org.hedgecode.chess.uci.annotation.CommandDirection; /** * Synchronized UCI Command Executor. * * @author Dmitry Samoshin aka gotty */ public class SyncCommandExecutor implements CommandExecutor { private Engine engine; private Acceptor acceptor; private CommandDescriptor commandDescriptor; private SyncCommandExecutor(Engine engine, Acceptor acceptor) throws Exception { this.engine = engine; this.acceptor = acceptor; initDescriptor(); } private void initDescriptor() throws Exception { commandDescriptor = new CommandDescriptor(); } @Override public synchronized void exec(String commandName, CommandDirection direction, String params) { UCICommand uciCommand = commandDescriptor.getCommand( commandName, direction ); if (uciCommand != null) { uciCommand.exec( engine, acceptor, params ); } else { System.out.println( String.format("%s %s", commandName, params != null ? params : "" ).trim() ); // todo } } static final class Factory { static CommandExecutor create(Engine engine, Acceptor acceptor) throws Exception { return new SyncCommandExecutor(engine, acceptor); } } }