2023-01-10 16:37:24 +01:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
/// Manage UCI messages and link into chess component
|
2023-01-11 12:07:28 +01:00
|
|
|
mod uci_command;
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 17:18:15 +01:00
|
|
|
use log::{info, warn};
|
2023-01-11 12:07:28 +01:00
|
|
|
use std::io::*;
|
|
|
|
use crate::uci_command::*;
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 17:18:15 +01:00
|
|
|
pub struct UciEngine<Fi: BufRead, Fo: Write> {
|
2023-01-11 12:07:28 +01:00
|
|
|
source: Fi,
|
|
|
|
destination: Fo,
|
|
|
|
uciok: bool,
|
|
|
|
id: Id,
|
|
|
|
}
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 17:18:15 +01:00
|
|
|
impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn new(source: Fi, destination: Fo) -> UciEngine<Fi, Fo> {
|
|
|
|
UciEngine::<Fi, Fo>{
|
|
|
|
source, destination,
|
2023-01-11 17:18:15 +01:00
|
|
|
id: Id::new(),
|
2023-01-11 12:07:28 +01:00
|
|
|
uciok: false}
|
|
|
|
}
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 17:18:15 +01:00
|
|
|
pub fn init(&mut self){
|
|
|
|
self.push(GuiCommand::Uci);
|
|
|
|
|
|
|
|
// Consume commands until uciok messages
|
|
|
|
while !self.uciok {
|
|
|
|
self.pull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_game(&mut self) {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn terminate(&mut self, reason: &str) {
|
|
|
|
self.uciok = false;
|
|
|
|
info!("UCI termination: {}", reason);
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn name(&self) -> Option<String> {
|
|
|
|
self.id.name()
|
|
|
|
}
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn author(&self) -> Option<String> {
|
|
|
|
self.id.author()
|
2023-01-10 16:37:24 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn update(&mut self, id: &Id){
|
|
|
|
if self.is_uciok() {warn!("Engine info should not be updated now (uciok)");}
|
|
|
|
self.id.update(id);
|
|
|
|
}
|
2023-01-11 17:18:15 +01:00
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn update_from_str(&mut self, name: Option<&str>, author: Option<&str>){
|
|
|
|
if self.is_uciok() {warn!("Engine info should not be updated now (uciok)");}
|
|
|
|
self.id.update_from_str(name, author);
|
|
|
|
}
|
2023-01-10 16:37:24 +01:00
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn uciok(&mut self){
|
|
|
|
self.uciok = true;
|
2023-01-10 16:37:24 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:07:28 +01:00
|
|
|
pub fn is_uciok(&self) -> bool {
|
|
|
|
self.uciok
|
2023-01-10 16:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse UCI commands
|
|
|
|
///
|
|
|
|
/// * id
|
|
|
|
/// * name *x*
|
|
|
|
///
|
|
|
|
/// this must be sent after receiving the "uci" command to identify the engine,
|
|
|
|
/// e.g. "id name Shredder X.Y\n"
|
|
|
|
/// * author *x*
|
|
|
|
///
|
|
|
|
/// this must be sent after receiving the "uci" command to identify the engine,
|
|
|
|
/// e.g. "id author Stefan MK\n"
|
|
|
|
///
|
|
|
|
/// ```rust
|
2023-01-11 12:07:28 +01:00
|
|
|
/// use chess_uci::*;
|
|
|
|
/// use std::io;
|
|
|
|
/// let mut engine = UciEngine::new(io::empty(), io::sink());
|
2023-01-10 16:37:24 +01:00
|
|
|
///
|
2023-01-11 12:07:28 +01:00
|
|
|
/// engine.exec("id name Baptiste");
|
|
|
|
/// assert_eq!(engine.name().expect(""), "Baptiste");
|
2023-01-10 16:37:24 +01:00
|
|
|
///
|
2023-01-11 12:07:28 +01:00
|
|
|
/// engine.exec("id author Monique Jouve");
|
|
|
|
/// assert_eq!(engine.author().expect(""), "Monique Jouve");
|
2023-01-10 16:37:24 +01:00
|
|
|
///
|
|
|
|
/// ```
|
2023-01-11 12:07:28 +01:00
|
|
|
///
|
|
|
|
/// * uciok
|
|
|
|
///
|
|
|
|
/// Must be sent after the id and optional options to tell the GUI that the engine
|
|
|
|
/// has sent all infos and is ready in uci mode.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use chess_uci::*;
|
|
|
|
/// use std::io;
|
|
|
|
/// let mut engine = UciEngine::new(io::empty(), io::sink());
|
|
|
|
/// assert_eq!(engine.is_uciok(), false);
|
|
|
|
///
|
|
|
|
/// assert_eq!(engine.is_uciok(), false);
|
|
|
|
///
|
|
|
|
/// engine.exec(" uciok");
|
|
|
|
/// assert_eq!(engine.is_uciok(), true);
|
|
|
|
/// ```
|
|
|
|
pub fn exec(&mut self, command: &str){
|
|
|
|
match parse (&mut command.to_string()) {
|
|
|
|
Ok(EngineCommand::Id{id}) => self.update(&id),
|
|
|
|
Ok(EngineCommand::UciOk) => self.uciok(),
|
2023-01-11 17:18:15 +01:00
|
|
|
Ok(EngineCommand::Opt { options: _ }) => {},
|
2023-01-11 12:07:28 +01:00
|
|
|
Ok(_) => {unimplemented!("command not implemented")},
|
|
|
|
Err(_) => warn!("Not a command"),
|
2023-01-10 16:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 17:18:15 +01:00
|
|
|
pub fn pull(&mut self) {
|
|
|
|
let mut command = String::new();
|
|
|
|
match self.source.read_line(&mut command) {
|
|
|
|
Ok(0) => self.terminate("Chess engine closed connection."),
|
|
|
|
Ok(_) => {
|
|
|
|
info!("← {}", command);
|
|
|
|
self.exec(&command)
|
|
|
|
},
|
|
|
|
Err(reason) => warn!("Unable to read from engine: {reason}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, command: GuiCommand) {
|
|
|
|
let command_str = command.to_string();
|
|
|
|
match self.destination.write(&command_str.as_bytes()){
|
|
|
|
Ok(n) if n == command_str.len() => info!("☒ gui: {command_str} "),
|
|
|
|
Ok(n) => warn!("⚠ gui: {command_str} truncated at {n}"),
|
|
|
|
Err(reason) => warn!("Unable to send command {command_str}: {reason}"),
|
|
|
|
}
|
2023-01-10 16:37:24 +01:00
|
|
|
}
|
|
|
|
}
|