forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtp_socket.js
More file actions
151 lines (151 loc) · 5.06 KB
/
Copy pathgtp_socket.js
File metadata and controls
151 lines (151 loc) · 5.06 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
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function trimText(str, len) {
if (str.length > len) {
return `${str.substr(0, len - 3)}...`;
}
return str;
}
class Socket {
constructor() {
this.sock = null;
this.cmdQueue = [];
this.handshakeComplete = false;
this.connectCallback = null;
this.lines = [];
this.dataHandlers = [];
this.textHandlers = [];
this.playerName = "";
this.debug = false;
}
connect(uri, playerName, debug = false) {
this.playerName = playerName;
this.debug = debug;
this.sock = io.connect(uri);
this.sock.on('json', (msg) => {
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) => {
this.sock.on('connect', () => {
this.newSession();
resolve();
});
});
}
onText(handler) {
this.textHandlers.push(handler);
}
onData(prefix, handler) {
this.dataHandlers.push({ prefix: prefix + ':', handler: handler });
}
send(cmd) {
return new Promise((resolve, reject) => {
this.cmdQueue.push({ cmd: cmd, resolve: resolve, reject: reject });
if (this.cmdQueue.length == 1) {
this.sendNext();
}
});
}
sendOne(cmd) {
if (this.cmdQueue.length <= 1) {
return this.send(cmd);
}
let lastCmd = this.cmdQueue[this.cmdQueue.length - 1].cmd;
if (cmd.split(' ', 1)[0] != lastCmd.split(' ', 1)[0]) {
return this.send(cmd);
}
this.cmdQueue[this.cmdQueue.length - 1].reject('send one');
this.cmdQueue.length -= 1;
return this.send(cmd);
}
newSession() {
this.cmdQueue = [];
let token = `${this.playerName}-${Date.now()}`;
this.token = token;
this.send(`echo __NEW_TOKEN__ ${token}`);
}
cmdHandler(line) {
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.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);
}
}
stderrHandler(line) {
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) {
console.log(`Error handling ${trimText(line, 1024)}`);
console.log(e);
}
handled = true;
}
}
if (!handled) {
this.textHandler(line);
}
}
textHandler(str) {
for (let handler of this.textHandlers) {
handler(str);
}
}
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 });
}
}
exports.Socket = Socket;
});
//# sourceMappingURL=gtp_socket.js.map