[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[computer-go] Connecting kgsGTP with a Delphi console. A workingexample
Ok,
Ok, following up on my problems as Delphi + Windows programmer (XP Pro
by the way - which might be the cause of some of my problems).
Below is a Delphi console program (no fancy windows) that seems to work,
some other files I used and the logs. This simple program does not do
anything useful (such as implementing GTP) but it does make kgsGTP happy
enough to connect to KGS.
ReadLn and WriteLn seemed not to do things correctly when my engine was
connected to kgsGTP. There were two main problems. A) ReadLn (or Read)
did not seem to wait for input but just caused the process going around
in the loop using the CPU 100%. (The program behaved ok if it was
running by itself and the input/output is mad in a text console window)
This was probably the cause of many of my earlier troubles. I was not
able to write responses that kgsGTP could read. I have no idea why. In
order to solve these problems I tried to write and read directly using
the lowest level Windows functions I could get. WriteFile, ReadFile and
GetStdHandle are the functions is used. All the other calls are standard
Delphi except for StrSearch that belongs to a free library called Jedi
Code Library.
The program is a crude hack as you will see. But it works. Perhaps some
real programmer could make a similar example program in C++ using better
style, error checking etc.
Note that "name" is not implemented and the program is guaranteed not to
produce more than a 100 lines in the logfile. (One of my first attempts
created a file of 200 Mbytes before I terminated the process...). So if
you use this code it should work even if the odds are against you, and
from then on it should be easy.
This code is free to use anyway you wish. But it will crash when KGSgtp
is terminated... but I consider that a minor problem.
--
Magnus Persson
Center for Adaptive Behavior and Cognition
*******
program Viking2KGS;
{$APPTYPE CONSOLE}
uses SysUtils, Windows, JCLStrings;
var
s: string;
log: TextFile;
n: integer;
rp, wp: THANdle;
buf: array[0..1000] of char;
procedure Send(c: string);
var
nwritten: DWord;
i: integer;
begin
c := c + #10;
for i := 1 to Length(c) do
buf[i-1] := c[i];
WriteFile(wp, buf, Length(c), nwritten, nil);
//Write(c + #10);
Append(log);
WriteLn(log, Format('OUT[%d %d]: %s', [Length(c), nwritten, c]));
CloseFile(log);
end;
procedure SendEnd(c: string);
begin
Send(c + #10);
end;
function ReadString: string;
var
i, numread: DWORD;
begin
result := '';
ReadFile(rp, buf, 1000, numread, nil);
for i := 0 to numread - 1 do
result := result + buf[i];
end;
begin
n := 100;
AssignFile(log, 'C:KGS.log');
Rewrite(log);
WriteLn(log, 'Start Log');
CloseFile(log);
rp := GetStdHandle(STD_INPUT_HANDLE);
wp := GetStdHandle(STD_OUTPUT_HANDLE);
WriteLn(rp);
WriteLn(wp);
while n >= 0 do
begin
s := ReadString;
if (s<> '') and (n > 0) then
begin
Append(log);
WriteLn(log, ' IN: ' + s);
CloseFile(log);
Dec(n);
if StrSearch('list_commands', s) > 0 then
begin
Send('= list_commands');
Send('= quit');
SendEnd('= name');
end
else
if StrSearch('quit', s) > 0 then
begin
SendEnd('=');
Exit;
end
else
SendEnd('?');
end
else
if n = 0 then
begin
Append(log);
WriteLn(log, 'END LOG');
CloseFile(log);
Dec(n);
end;
end;
end.
*******
I use a .bat file to start kgsGtp with one line like this
java -jar kgsGtp.jar myconfig.txt
myconfig.txt look like this
*******
name=viking4
password=pleasuseyourownpasswordhere
logFile=errors
verbose=t
engine=Viking2KGS.exe
open=t
********
here is the log of kgsGTP (The German is probably because my computer is
German):
******
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient main
FEIN: KGS GTP Client v2.5.6 starting up
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient d
AM FEINSTEN: Got successful response to command "list_commands": =
list_commands
= quit
= name
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient d
AM FEINSTEN: Queued command sent to engine: name
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient d
WARNUNG: Got error response to command "name": ?
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient d
AM FEINSTEN: Queued command sent to engine: version
12.03.2004 22:42:52 org.igoweb.igoweb.client.gtp.GtpClient d
WARNUNG: Got error response to command "version": ?
12.03.2004 22:42:53 org.igoweb.igoweb.client.gtp.GtpClient c
FEIN: Login successful.
12.03.2004 22:42:53 org.igoweb.igoweb.client.gtp.GtpClient c
FEIN: Auth level is not ROBOT_RANKED, can only play free games
12.03.2004 22:42:54 org.igoweb.igoweb.client.gtp.GtpClient b
FEIN: Joined room "Computer Go"
12.03.2004 22:42:54 org.igoweb.igoweb.client.gtp.GtpClient b
FEINER: No games to join. Creating an open game.
12.03.2004 22:42:54 org.igoweb.igoweb.client.gtp.GtpClient a
FEINER: Joined game
******
And here is the log from program with extra linefeeds removed
*****
Start Log
IN: list_commands
OUT[16 16]: = list_commands
OUT[7 7]: = quit
OUT[8 8]: = name
IN: name
OUT[3 3]: ?
IN: version
OUT[3 3]: ?
*****
_______________________________________________
computer-go mailing list
computer-go@xxxxxxxxxxxxxxxxx
http://computer-go.org/mailman/listinfo/computer-go