Repository navigation
Expand file tree
/
Copy pathgtp_socket.ts
More file actions
140 lines (121 loc) · 4.22 KB
/
Copy pathgtp_socket.ts
File metadata and controls
140 lines (121 loc) · 4.22 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
// 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;
type DebugHandler = (msg: string) => void;
type CmdHandler = (cmd: string, result: string, ok: boolean) => void;
type StderrHandler = (line: string) => 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;
private cmdQueue = new Array<{cmd: string, callback: CmdHandler | null}>();
private gameToken: string;
private handshakeComplete = false;
private connectCallback: ConnectCallback | null = null;
private stderrHandlers = new Array<{prefix: string, handler: StderrHandler}>();
constructor(uri: string, private debugHandler: DebugHandler) {
this.sock = io.connect(uri)
this.debugHandler = debugHandler;
this.sock.on('connect', () => {
this.newSession();
if (this.connectCallback) {
this.connectCallback();
}
});
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.trim() != '') {
this.cmdHandler(obj.stdout);
}
} else if (obj.stderr !== undefined) {
this.stderrHandler(obj.stderr);
}
});
}
onConnect(callback: ConnectCallback) {
this.connectCallback = callback;
}
addStderrHandler(prefix: string, handler: StderrHandler) {
this.stderrHandlers.push({prefix: prefix, handler: handler});
}
send(cmd: string, callback?: CmdHandler) {
this.cmdQueue.push({cmd: cmd, callback: callback || null});
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;
let cmd = `echo __NEW_TOKEN__ ${token}`;
this.sock.emit('gtpcmd', {data: cmd});
}
private cmdHandler(line: string) {
let {cmd, callback} = this.cmdQueue[0];
if (this.debugHandler) {
this.debugHandler(`${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();
}
}
if (callback) {
let ok = line[0] == '=';
let result = line.substr(1).trim();
callback(cmd, result, ok);
}
}
private stderrHandler(line: string) {
for (let {prefix, handler} of this.stderrHandlers) {
if (line.substr(0, prefix.length) == prefix) {
handler(line.substr(prefix.length));
break;
}
}
}
private sendNext() {
let {cmd, callback} = this.cmdQueue[0];
if (this.debugHandler) {
this.debugHandler(`${cmd}`);
}
this.sock.emit('gtpcmd', {data: cmd});
}
}
export {
CmdHandler,
DebugHandler,
Socket,
StderrHandler,
};