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
226 lines (198 loc) · 7.1 KB
/
Copy pathgtp_socket.ts
File metadata and controls
226 lines (198 loc) · 7.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// 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;
function trimText(str: string, len: number) {
if (str.length > len) {
return `${str.substr(0, len - 3)}...`;
}
return str;
}
// 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 token: string;
private handshakeComplete = false;
private connectCallback: Nullable<ConnectCallback> = null;
private lines: string[] = [];
private dataHandlers: {prefix: string, handler: DataHandler}[] = [];
private textHandlers: TextHandler[] = [];
private playerName = "";
private debug = false;
// 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, playerName: string, debug=false) {
this.playerName = playerName;
this.debug = debug;
this.sock = io.connect(uri)
this.sock.on('json', (msg: string) => {
let obj = JSON.parse(msg);
if (obj.token != this.token) {
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();
resolve();
});
});
}
// 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, returning a promise that is resolved when the
// command succeeds or is rejected if the command fails.
send(cmd: string): Promise<any> {
return new Promise((resolve, reject) => {
this.cmdQueue.push({cmd: cmd, resolve: resolve, reject: reject});
if (this.cmdQueue.length == 1) {
this.sendNext();
}
});
}
// Like send(cmd), but if the last call to sendOne is for the same GTP
// command (different arguments are allowed) and that command has not yet
// been sent, the pending command is rejected and replaced with this one.
sendOne(cmd: string) {
// Either the queue is empty (in which case we must just send) or there's
// a single command currently being executed (which we must let finish).
if (this.cmdQueue.length <= 1) {
return this.send(cmd);
}
// If the last command in the queue doesn't match this one: just send.
let lastCmd = this.cmdQueue[this.cmdQueue.length - 1].cmd;
if (cmd.split(' ', 1)[0] != lastCmd.split(' ', 1)[0]) {
return this.send(cmd);
}
// The last command in the queue matches this one: reject it and replace
// it with ours.
this.cmdQueue[this.cmdQueue.length - 1].reject('send one');
this.cmdQueue.length -= 1;
return this.send(cmd);
}
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 = `${this.playerName}-${Date.now()}`;
this.token = token;
this.send(`echo __NEW_TOKEN__ ${token}`);
}
private cmdHandler(line: string) {
let {cmd, resolve, reject} = this.cmdQueue[0];
if (this.debug) {
console.log(`### ${this.playerName} OUT ${cmd} ${line}`);
}
this.textHandler(`${trimText(line, 1024)}`);
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;
if (this.debug) {
console.log(`### ${this.playerName} ERR ${line}`);
}
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;
}
try {
handler(obj);
} catch (e) {
// Catch and log all exceptions to prevent them breaking the socket.
console.log(`Error handling ${trimText(line, 1024)}`);
console.log(e);
}
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(trimText(cmd, 1024));
}
if (this.debug) {
console.log(`### ${this.playerName} SND ${cmd}`);
}
this.sock.emit('gtpcmd', {player: this.playerName, data: cmd});
}
}
export {
Socket,
};