2011-01-01 01:42:50 +00:00
|
|
|
public class ChessEngine : Object
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
|
|
|
public string binary;
|
2013-01-02 19:17:18 +00:00
|
|
|
public string args;
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
private Pid pid;
|
2010-12-08 07:18:30 +00:00
|
|
|
private int stdin_fd;
|
|
|
|
private int stderr_fd;
|
2011-01-01 01:42:50 +00:00
|
|
|
private IOChannel stdout_channel;
|
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 ();
|
|
|
|
public signal void stopped ();
|
|
|
|
|
|
|
|
private bool _ready = false;
|
|
|
|
public bool ready
|
|
|
|
{
|
|
|
|
protected set
|
|
|
|
{
|
|
|
|
_ready = value;
|
|
|
|
ready_changed ();
|
|
|
|
}
|
|
|
|
public get
|
|
|
|
{
|
|
|
|
return _ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool start ()
|
|
|
|
{
|
2013-01-02 19:17:18 +00:00
|
|
|
string[] argv = { binary, args, 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,
|
|
|
|
null,
|
|
|
|
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
|
|
|
{
|
|
|
|
stderr.printf ("Failed to execute chess engine: %s\n", e.message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-14 05:13:12 +00:00
|
|
|
ChildWatch.add (pid, engine_stopped_cb);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
stderr.printf ("Failed to set input from chess engine to non-blocking: %s", e.message);
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
stdout_channel.add_watch (IOCondition.IN, read_cb);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
starting ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-14 05:13:12 +00:00
|
|
|
|
|
|
|
private void engine_stopped_cb (Pid pid, int status)
|
|
|
|
{
|
|
|
|
stopped ();
|
|
|
|
}
|
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
public virtual void start_game ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void request_move ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void report_move (ChessMove move)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-25 11:44:15 +00:00
|
|
|
public virtual void undo ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
public void stop ()
|
|
|
|
{
|
2012-07-14 05:13:12 +00:00
|
|
|
if (pid != 0)
|
|
|
|
Posix.kill (pid, Posix.SIGTERM);
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
private bool read_cb (IOChannel source, IOCondition condition)
|
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
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
catch (IOChannelError e)
|
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
|
|
|
{
|
|
|
|
stdout.printf ("EOF\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-01 01:42:50 +00:00
|
|
|
if (status == IOStatus.NORMAL)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2011-01-01 01:42:50 +00:00
|
|
|
//debug ("Read %zu octets from engine", n_read);
|
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;
|
|
|
|
size_t n_written;
|
|
|
|
|
|
|
|
while (offset < data.length)
|
|
|
|
{
|
|
|
|
/* Unnecessary copying but there seems to be a vala bug here */
|
|
|
|
char[] d = new char[data.length - offset];
|
|
|
|
for (int i = 0; i < data.length - offset; i++)
|
|
|
|
d[i] = data[offset + i];
|
|
|
|
|
|
|
|
n_written = Posix.write(stdin_fd, d, d.length);
|
|
|
|
if (n_written < 0)
|
|
|
|
return;
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
//debug ("Wrote %zu octets to engine", n_written);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
offset += n_written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|