forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtp_socket.ts
More file actions
183 lines (161 loc) · 5.75 KB
/
Copy pathgtp_socket.ts
File metadata and controls
183 lines (161 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Let's not worry about Socket.io type safety for now.
declare var io: any;
import {Nullable} from './base';
type TextHandler = (msg: string) => void;
type CmdHandler = (result: string, ok: boolean) => void;
type DataHandler = (obj: any) => void;
type ConnectCallback = () => void;
// The GtpSocket serializes all calls to send so that only one call is ever
// outstanding at a time. This isn't strictly necessary, but it makes reading
// the debug logs easier because we don't end up with request and result logs
// all jumbled up.
class Socket {
private sock: any = null;
private cmdQueue: {cmd: string, resolve: any, reject: any}[] = [];
private gameToken: string;
private handshakeComplete = false;
private connectCallback: Nullable<ConnectCallback> = null;
private lines: string[] = [];
private dataHandlers: {prefix: string, handler: DataHandler}[] = [];
private textHandlers: TextHandler[] = [];
// Connects to the Minigui server at the given URI.
// Returns a promise that gets resolved of the board size when the connection
// is established.
connect(uri: string) {
this.sock = io.connect(uri)
this.sock.on('json', (msg: string) => {
let obj = JSON.parse(msg);
if (obj.token != this.gameToken) {
console.log('ignoring', obj, `${obj.token} != ${this.gameToken}`);
return;
}
if (obj.stdout !== undefined) {
if (obj.stdout != '') {
this.lines.push(obj.stdout.trim());
} else {
this.cmdHandler(this.lines.join('\n'));
this.lines = [];
}
} else if (obj.stderr !== undefined) {
this.stderrHandler(obj.stderr);
}
});
return new Promise((resolve) => {
// Connect to the server.
this.sock.on('connect', () => {
this.newSession();
// Probe for the supported board size.
// Minigo only supports a single board size and will reject GTP
// "boardsize" commands for any other size.
this.send('boardsize 9')
.then(() => { resolve(9); })
.catch(() => {
this.send('boardsize 19').then(() => { resolve(19); });
});
});
});
}
// Add a handler that will be invoked whenever the Minigo engine writes
// anything to stdout, or writes something to stderr that isn't handled by
// one of the data handlers registered via onData.
onText(handler: TextHandler) {
this.textHandlers.push(handler);
}
// Add a handler that accepts lines that begin with the given prefix plus ':'.
// The matching handlers are invoked in the order they were registered.
// Before the handlers are invoked, the contents of the line that follows the
// matching prefix are parsed as a JSON object if possible and passed as the
// handler argument. The raw line suffix is passed if it can't be parsed.
onData(prefix: string, handler: DataHandler) {
this.dataHandlers.push({prefix: prefix + ':', handler: handler});
}
// Sends a GTP command, invoking the optional handler when the command is
// complete.
send(cmd: string) {
return new Promise((resolve, reject) => {
this.cmdQueue.push({cmd: cmd, resolve: resolve, reject: reject});
if (this.cmdQueue.length == 1) {
this.sendNext();
}
});
}
newSession() {
// Generates a new, unique session token and sends it to the server via
// a special echo __NEW_TOKEN__ command. The server forwards this on to its
// child Minigo process, which echos it back to the server after finishing
// processing any other outstanding work. The server's stdout/stderr
// handling logic in std_bg_thread looks for the __NEW_TOKEN__ string,
// extracts the session token and then attaches that token to all subsequent
// messages sent to the frontend.
this.cmdQueue = [];
let token = `session-id-${Date.now()}`;
this.gameToken = token;
this.send(`echo __NEW_TOKEN__ ${token}`);
}
private cmdHandler(line: string) {
let {cmd, resolve, reject} = this.cmdQueue[0];
this.textHandler(`${cmd} ${line}`);
if (line[0] == '=' || line[0] == '?') {
// This line contains the response from a GTP command; pop the command off
// the queue.
this.cmdQueue = this.cmdQueue.slice(1);
if (this.cmdQueue.length > 0) {
this.sendNext();
}
}
let ok = line[0] == '=';
let result = line.substr(1).trim();
if (ok) {
resolve(result);
} else {
reject(result);
}
}
private stderrHandler(line: string) {
let handled = false;
for (let {prefix, handler} of this.dataHandlers) {
if (line.substr(0, prefix.length) == prefix) {
let stripped = line.substr(prefix.length);
let obj;
try {
obj = JSON.parse(stripped);
} catch (e) {
obj = stripped;
}
handler(obj);
handled = true;
}
}
if (!handled) {
this.textHandler(line);
}
}
private textHandler(str: string) {
for (let handler of this.textHandlers) {
handler(str);
}
}
private sendNext() {
let {cmd} = this.cmdQueue[0];
if (this.textHandler) {
this.textHandler(`${cmd}`);
}
this.sock.emit('gtpcmd', {data: cmd});
}
}
export {
Socket,
};