2014-01-07 02:19:07 +00:00
|
|
|
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2013-07-07 21:14:29 +00:00
|
|
|
* Copyright (C) 2010-2013 Robert Ancell
|
2014-02-16 20:06:54 +00:00
|
|
|
* Copyright (C) 2013-2014 Michael Catanzaro
|
2016-03-15 17:12:58 +00:00
|
|
|
* Copyright (C) 2015-2016 Sahil Sareen
|
2013-07-07 21:14:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
2016-02-15 20:53:54 +00:00
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
2013-07-07 21:14:29 +00:00
|
|
|
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
|
|
|
|
* license.
|
|
|
|
*/
|
|
|
|
|
2013-05-12 00:35:52 +00:00
|
|
|
public abstract class ChessEngine : Object
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-05-12 00:49:23 +00:00
|
|
|
private string binary;
|
2013-07-21 17:12:43 +00:00
|
|
|
private string[] args;
|
2013-01-02 19:17:18 +00:00
|
|
|
|
2014-02-16 20:06:54 +00:00
|
|
|
private uint delay_seconds;
|
2014-06-25 15:20:03 +00:00
|
|
|
private uint pending_move_source_id;
|
2014-02-16 20:06:54 +00:00
|
|
|
|
2015-07-19 15:40:34 +00:00
|
|
|
private Pid pid = 0;
|
2015-07-19 18:32:19 +00:00
|
|
|
private int stdin_fd = -1;
|
|
|
|
private int stderr_fd = -1;
|
2015-07-19 15:40:34 +00:00
|
|
|
private IOChannel? stdout_channel;
|
2015-07-20 18:38:46 +00:00
|
|
|
private uint child_watch_id = 0;
|
2015-07-19 15:40:34 +00:00
|
|
|
private uint stdout_watch_id = 0;
|
2014-01-11 21:43:12 +00:00
|
|
|
private bool started = false;
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
protected virtual void process_input (char[] data) {}
|
|
|
|
|
|
|
|
public signal void starting ();
|
|
|
|
public signal void ready_changed ();
|
|
|
|
public signal void moved (string move);
|
|
|
|
public signal void resigned ();
|
2015-07-19 19:01:01 +00:00
|
|
|
public signal void stopped_unexpectedly ();
|
2013-08-19 03:54:05 +00:00
|
|
|
public signal void error ();
|
2013-08-25 17:49:23 +00:00
|
|
|
public signal void claim_draw ();
|
|
|
|
public signal void offer_draw ();
|
2015-02-23 13:07:42 +00:00
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
private bool _ready = false;
|
|
|
|
public bool ready
|
|
|
|
{
|
|
|
|
protected set
|
|
|
|
{
|
|
|
|
_ready = value;
|
|
|
|
ready_changed ();
|
|
|
|
}
|
|
|
|
public get
|
|
|
|
{
|
|
|
|
return _ready;
|
|
|
|
}
|
|
|
|
}
|
2013-05-12 00:49:23 +00:00
|
|
|
|
2014-02-16 20:06:54 +00:00
|
|
|
public ChessEngine (string binary, string[] args, uint delay_seconds)
|
2013-05-12 00:49:23 +00:00
|
|
|
{
|
|
|
|
this.binary = binary;
|
|
|
|
this.args = args;
|
2014-02-16 20:06:54 +00:00
|
|
|
this.delay_seconds = delay_seconds;
|
2013-05-12 00:49:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:44:00 +00:00
|
|
|
~ChessEngine ()
|
|
|
|
{
|
2015-07-23 18:01:29 +00:00
|
|
|
assert (!started);
|
2015-07-20 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
public bool start ()
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (pid == 0)
|
2015-07-20 18:38:46 +00:00
|
|
|
requires (child_watch_id == 0)
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (stdout_watch_id == 0)
|
2015-07-19 18:32:19 +00:00
|
|
|
requires (stdin_fd == -1)
|
|
|
|
requires (stderr_fd == -1)
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (!started)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-07-21 17:12:43 +00:00
|
|
|
string[] argv = {binary};
|
|
|
|
foreach (var arg in args)
|
|
|
|
argv += arg;
|
|
|
|
argv += null;
|
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
int stdout_fd;
|
|
|
|
try
|
|
|
|
{
|
2011-01-01 01:42:50 +00:00
|
|
|
Process.spawn_async_with_pipes (null, argv, null,
|
2012-07-14 05:13:12 +00:00
|
|
|
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
|
2014-03-21 03:41:17 +00:00
|
|
|
() => Portability.maybe_kill_orphan_engine (),
|
2012-07-14 05:13:12 +00:00
|
|
|
out pid, out stdin_fd, out stdout_fd, out stderr_fd);
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
catch (SpawnError e)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-11-02 17:54:51 +00:00
|
|
|
warning ("Failed to execute chess engine: %s\n", e.message);
|
2010-12-08 07:18:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:38:46 +00:00
|
|
|
child_watch_id = ChildWatch.add (pid, engine_stopped_cb);
|
2012-07-14 05:13:12 +00:00
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
stdout_channel = new IOChannel.unix_new (stdout_fd);
|
2010-12-08 07:18:30 +00:00
|
|
|
try
|
|
|
|
{
|
2011-01-01 01:42:50 +00:00
|
|
|
stdout_channel.set_flags (IOFlags.NONBLOCK);
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
catch (IOChannelError e)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-11-02 17:54:51 +00:00
|
|
|
warning ("Failed to set input from chess engine to non-blocking: %s", e.message);
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
2014-01-11 21:43:12 +00:00
|
|
|
stdout_channel.set_close_on_unref (true);
|
2013-05-25 18:41:11 +00:00
|
|
|
stdout_watch_id = stdout_channel.add_watch (IOCondition.IN, read_cb);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
2014-01-11 21:43:12 +00:00
|
|
|
started = true;
|
2010-12-08 07:18:30 +00:00
|
|
|
starting ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-14 05:13:12 +00:00
|
|
|
|
2015-07-23 18:38:47 +00:00
|
|
|
private void engine_stopped_cb (Pid pid)
|
2015-07-20 18:38:46 +00:00
|
|
|
requires (started)
|
2015-07-23 18:38:47 +00:00
|
|
|
requires (pid == this.pid)
|
2012-07-14 05:13:12 +00:00
|
|
|
{
|
2015-07-20 18:38:46 +00:00
|
|
|
stop (false);
|
|
|
|
stopped_unexpectedly ();
|
2012-07-14 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 00:35:52 +00:00
|
|
|
public abstract void start_game ();
|
2010-12-08 07:18:30 +00:00
|
|
|
|
2013-05-12 00:35:52 +00:00
|
|
|
public abstract void report_move (ChessMove move);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
2014-06-25 15:20:03 +00:00
|
|
|
protected abstract void do_undo ();
|
2011-01-25 11:44:15 +00:00
|
|
|
|
2014-02-16 20:06:54 +00:00
|
|
|
protected abstract void request_move ();
|
|
|
|
|
|
|
|
public void move ()
|
|
|
|
{
|
2014-06-25 15:20:03 +00:00
|
|
|
pending_move_source_id = Timeout.add_seconds (delay_seconds, () => {
|
|
|
|
pending_move_source_id = 0;
|
2014-02-16 20:06:54 +00:00
|
|
|
request_move ();
|
2014-12-31 03:37:03 +00:00
|
|
|
return Source.REMOVE;
|
2014-02-16 20:06:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-25 15:20:03 +00:00
|
|
|
public void undo ()
|
|
|
|
{
|
|
|
|
if (pending_move_source_id != 0)
|
|
|
|
{
|
|
|
|
Source.remove (pending_move_source_id);
|
|
|
|
pending_move_source_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_undo ();
|
|
|
|
}
|
|
|
|
|
2015-07-19 18:32:19 +00:00
|
|
|
public void stop (bool kill_engine = true)
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (!started || stdout_channel != null)
|
2015-07-19 18:32:19 +00:00
|
|
|
requires (!started || stdin_fd != -1)
|
|
|
|
requires (!started || stderr_fd != -1)
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (!started || pid != 0)
|
2015-07-20 18:38:46 +00:00
|
|
|
requires (!started || child_watch_id != 0)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2014-01-11 21:43:12 +00:00
|
|
|
if (!started)
|
|
|
|
return;
|
2015-07-19 18:32:19 +00:00
|
|
|
started = false;
|
2014-01-11 21:43:12 +00:00
|
|
|
|
2015-07-19 15:40:34 +00:00
|
|
|
// This can be unset on errors in read_cb.
|
2015-07-23 18:40:22 +00:00
|
|
|
if (stdout_watch_id != 0) {
|
2015-07-19 15:40:34 +00:00
|
|
|
Source.remove (stdout_watch_id);
|
2015-07-23 18:40:22 +00:00
|
|
|
stdout_watch_id = 0;
|
|
|
|
}
|
2014-01-11 21:43:12 +00:00
|
|
|
|
|
|
|
try
|
2014-01-08 02:24:37 +00:00
|
|
|
{
|
2014-01-11 21:43:12 +00:00
|
|
|
stdout_channel.shutdown (false);
|
2014-01-08 02:24:37 +00:00
|
|
|
}
|
2014-01-11 21:43:12 +00:00
|
|
|
catch (IOChannelError e)
|
2014-01-08 02:29:11 +00:00
|
|
|
{
|
2014-01-11 21:43:12 +00:00
|
|
|
warning ("Failed to close channel to engine's stdout: %s", e.message);
|
2014-01-08 02:29:11 +00:00
|
|
|
}
|
2015-07-19 18:32:19 +00:00
|
|
|
stdout_channel = null;
|
2014-01-11 21:43:12 +00:00
|
|
|
|
|
|
|
if (FileUtils.close (stdin_fd) == -1)
|
2014-06-23 01:37:58 +00:00
|
|
|
warning ("Failed to close pipe to engine's stdin: %s", strerror (errno));
|
2015-07-19 18:32:19 +00:00
|
|
|
stdin_fd = -1;
|
2014-01-11 21:43:12 +00:00
|
|
|
|
|
|
|
if (FileUtils.close (stderr_fd) == -1)
|
2014-06-23 01:37:58 +00:00
|
|
|
warning ("Failed to close pipe to engine's stderr: %s", strerror (errno));
|
2015-07-19 18:32:19 +00:00
|
|
|
stderr_fd = -1;
|
2014-01-11 21:43:12 +00:00
|
|
|
|
2015-07-20 18:38:46 +00:00
|
|
|
Source.remove (child_watch_id);
|
|
|
|
child_watch_id = 0;
|
|
|
|
|
2018-07-27 23:01:46 +00:00
|
|
|
if (kill_engine && Posix.kill (pid, Posix.Signal.TERM) == -1)
|
2014-01-11 21:43:12 +00:00
|
|
|
warning ("Failed to kill engine: %s", strerror (errno));
|
2015-07-19 18:32:19 +00:00
|
|
|
Process.close_pid (pid);
|
|
|
|
pid = 0;
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
private bool read_cb (IOChannel source, IOCondition condition)
|
2015-07-19 15:40:34 +00:00
|
|
|
requires (stdout_watch_id != 0)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
|
|
|
char[] buf;
|
|
|
|
size_t n_read;
|
2011-01-01 01:42:50 +00:00
|
|
|
IOStatus status;
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
buf = new char[1024];
|
|
|
|
try
|
|
|
|
{
|
|
|
|
status = source.read_chars (buf, out n_read);
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
catch (ConvertError e)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-11-02 17:54:51 +00:00
|
|
|
warning ("Failed to read from engine: %s", e.message);
|
2014-01-08 02:24:37 +00:00
|
|
|
stdout_watch_id = 0;
|
2010-12-08 07:18:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
catch (IOChannelError e)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-11-02 17:54:51 +00:00
|
|
|
warning ("Failed to read from engine: %s", e.message);
|
2014-01-08 02:24:37 +00:00
|
|
|
stdout_watch_id = 0;
|
2010-12-08 07:18:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
if (status == IOStatus.EOF)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2014-01-08 00:49:10 +00:00
|
|
|
debug ("EOF");
|
2014-01-08 02:24:37 +00:00
|
|
|
stdout_watch_id = 0;
|
2010-12-08 07:18:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
if (status == IOStatus.NORMAL)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
|
|
|
buf.resize ((int) n_read);
|
|
|
|
process_input (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void write (char[] data)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
2014-01-08 00:10:36 +00:00
|
|
|
size_t n_written = 0;
|
2010-12-08 07:18:30 +00:00
|
|
|
|
2014-01-08 00:10:36 +00:00
|
|
|
do
|
|
|
|
{
|
2014-10-21 10:43:02 +00:00
|
|
|
n_written = Posix.write (stdin_fd, &data[offset], data.length - offset);
|
2010-12-08 07:18:30 +00:00
|
|
|
offset += n_written;
|
2014-01-08 00:10:36 +00:00
|
|
|
} while (n_written > 0 && offset < data.length);
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void write_line (string line)
|
|
|
|
{
|
|
|
|
string l = line + "\n";
|
2011-01-01 01:42:50 +00:00
|
|
|
debug ("Writing line to engine: '%s'", line);
|
2010-12-08 07:18:30 +00:00
|
|
|
char[] d = l.to_utf8 ();
|
|
|
|
if (d != null)
|
|
|
|
write (d);
|
|
|
|
}
|
|
|
|
}
|