1 |
pcg |
1.1 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
|
|
# lots of things have been hardcoded. search for #d# to find the places |
4 |
|
|
|
5 |
|
|
use KGS::Protocol; |
6 |
|
|
use KGS::Listener::Debug; |
7 |
|
|
|
8 |
|
|
use IO::Socket::INET; |
9 |
|
|
use Event; |
10 |
|
|
|
11 |
|
|
use GetOpt::Long; |
12 |
|
|
use Carp; |
13 |
|
|
|
14 |
|
|
$VERSION = 0.0; # be more confident.... |
15 |
|
|
|
16 |
|
|
$SIG{QUIT} = sub { Carp::confess "SIGQUIT" }; |
17 |
|
|
|
18 |
|
|
my $kgs; |
19 |
|
|
my $verbose = 1; |
20 |
|
|
my $user = "guest"; |
21 |
|
|
my $pass = undef; |
22 |
|
|
|
23 |
|
|
sub usage { |
24 |
|
|
print STDERR <<EOF |
25 |
|
|
Usage: $0 [options] -- engine engine-args... |
26 |
|
|
-u username usernmae to connect |
27 |
|
|
-p password optional password to connect (none => guest) |
28 |
|
|
-v increase verbosity |
29 |
|
|
-q decrease verbosity |
30 |
|
|
|
31 |
|
|
$0 connects to the kiseido go server, starts the named engine |
32 |
|
|
and communicates with it using GTP protocol using it's stdin and stdout. |
33 |
|
|
|
34 |
|
|
If no engine is given, uses stdin/stdout itself for communications. |
35 |
|
|
|
36 |
|
|
The engine can optionally act as controller, too, as long as it isn't |
37 |
|
|
confused by responses on it's command input stream. |
38 |
|
|
|
39 |
|
|
Command extension used by the controller: |
40 |
|
|
|
41 |
|
|
kgs-login message # login successful |
42 |
|
|
... |
43 |
|
|
|
44 |
|
|
Commands usable by the client: |
45 |
|
|
|
46 |
|
|
... |
47 |
|
|
|
48 |
|
|
EOF |
49 |
|
|
exit shift; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
GetOptions ( |
53 |
|
|
"u=s" => \$user, |
54 |
|
|
"v" => sub { $verbose++ }, |
55 |
|
|
"q" => sub { $verbose-- }, |
56 |
|
|
"h" => sub { usage(0) }; |
57 |
|
|
) or die usage(1); |
58 |
|
|
|
59 |
|
|
package kgs; |
60 |
|
|
|
61 |
|
|
use base KGS::Listener; |
62 |
|
|
|
63 |
|
|
my $conn = new KGS::Protocol; |
64 |
|
|
|
65 |
|
|
sub new { |
66 |
|
|
my $class = shift; |
67 |
|
|
my $self = bless { @_ }, $class; |
68 |
|
|
|
69 |
|
|
print STDERR "$0 version $VERSION connecting...\n" if $verbose; |
70 |
|
|
|
71 |
|
|
my $sock = new IO::Socket::INET PeerHost => $ENV{KGSHOST} || "kgs.kiseido.com", PeerPort => "2379" |
72 |
|
|
or die "connect: $!"; |
73 |
|
|
|
74 |
|
|
$sock->blocking (1); |
75 |
|
|
$conn->handshake ($sock); |
76 |
|
|
|
77 |
|
|
$self->listen ($conn, "any"); |
78 |
|
|
|
79 |
|
|
# Listener for kgs data |
80 |
|
|
$self->{w} = Event->io (fd => $sock, poll => 'r', cb => sub { |
81 |
|
|
my $len = sysread $sock, my $buf, 16384; |
82 |
|
|
if ($len) { |
83 |
|
|
$conn->feed_data($buf); |
84 |
|
|
} elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) { |
85 |
|
|
print STDERR "disconnected\n" if $verbose; |
86 |
|
|
Event::unloop; |
87 |
|
|
} |
88 |
|
|
}); |
89 |
|
|
|
90 |
|
|
# Listener for keyboard input. Needs a tty |
91 |
|
|
$self->{kbd} = Event->io (fd => \*STDIN, poll => 'r', cb => sub { |
92 |
|
|
$self->inject_cmd (<STDIN>); |
93 |
|
|
}); |
94 |
|
|
|
95 |
|
|
$conn->login ("kgsclient 0.0", $self->{user}, delete $self->{password}); |
96 |
|
|
|
97 |
|
|
$self; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
sub inject_login { |
101 |
|
|
my ($self, $msg) = @_; |
102 |
|
|
|
103 |
|
|
print STDERR "login: $msg->{message}\n" if $verbose; |
104 |
|
|
# $self->send (join_room => |
105 |
|
|
# channel => 13, |
106 |
|
|
# user => { name => $self->{user} }); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
sub inject_msg_room { |
110 |
|
|
my ($self, $msg) = @_; |
111 |
|
|
print "$msg->{name}#$msg->{channel}: $msg->{message}\n"; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
sub inject_any { |
115 |
|
|
my ($self, $msg) = @_; |
116 |
|
|
print "DEBUG: $msg->{type}#$msg->{channel}" if $verbose; |
117 |
|
|
for (sort keys %$msg) { |
118 |
|
|
print " $_<$msg->{$_}>"; |
119 |
|
|
} |
120 |
|
|
print "\n"; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
# user input |
124 |
|
|
sub inject_cmd { |
125 |
|
|
my ($self, $cmd) = @_; |
126 |
|
|
chomp $cmd; |
127 |
|
|
print ">>>>> $cmd\n" if $verbose; |
128 |
|
|
$self->send (msg_room => |
129 |
|
|
channel => 13, |
130 |
|
|
name => $self->{user}, |
131 |
|
|
message => $cmd); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
package gtp; |
135 |
|
|
|
136 |
|
|
sub new { |
137 |
|
|
my $class = shift; |
138 |
|
|
bless { @_ }, $class; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
sub set_fh { |
142 |
|
|
my ($self, $in, $out) = @_; |
143 |
|
|
|
144 |
|
|
$self->{i} = $in; |
145 |
|
|
$self->{o} = $out; |
146 |
|
|
|
147 |
|
|
Event->io (fd => |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
sub run_engine { |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
package main; |
154 |
|
|
|
155 |
|
|
$kgs = new kgs user => "kgsclient", undef; #d# |
156 |
|
|
|
157 |
|
|
Event::loop; |
158 |
|
|
|
159 |
|
|
1; |
160 |
|
|
|
161 |
|
|
|