-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
194 lines (173 loc) · 6.15 KB
/
client.js
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
/*
*
*/
var zmq = require('zeromq');
var PAYLOAD_STR = ":::";
var SOCKET_TYPE = "pair";
/**
* Class for communicating with a `PyServer` using ZeroMQ via TCP.
*
* On each 'beat', a mesage is always sent, and a timer is initialized which
* will cause a timeout if a response is not recieved before it goes off. If an
* addition message is being sent (called a 'payload'), then subsequent messages
* are sent to the `PyServer`. The payload takes the form of two messages:
* first a function-name and then an argument (both strings).
*
* On the `PyServer` side, the given function is called with the given argument,
* and the return value is passed on the subsequent heartbeat response.
*
* When this `JSClient` doesn't get a response within a given time (`timeout`), the method,
* `JSClient.timeoutFunc` will be called. This can be reset to point to a function which does
* some sort of recovery action, for example: restarting/recreating the `PyServer` and creating
* a new JSClient.
*
*/
class JSClient {
/**
* Create a JSClient instance.
*
* @param {str} uri - Location to communicate with via TCP.
* @param {str} port - Port to access on URI.
* @param {int} timeout - Timeout interval in milliseconds.
*/
constructor(uri="127.0.0.1", port="3000", timeout=5000, debug=false, timeoutFunc=null) {
let addr = "tcp://" + uri + ":" + port;
this._log("Initializing %s socket", SOCKET_TYPE, debug=true);
let socket = zmq.socket(SOCKET_TYPE);
console.log("Starting connection to", addr);
socket.connect(addr);
this._log('Connected to ', addr);
this.addr = addr;
this._socket = socket;
this._debug = debug;
this._killer = null;
this._timeout = timeout;
this._payload_name = null;
this._payload_arg = null;
this._payload_func = null;
if (timeoutFunc == null) {
timeoutFunc = this._timeoutError;
}
this.timeoutFunc = timeoutFunc;
}
_timeoutError() {
throw new Error("Connection timeout!");
}
/**
* Perform a single communication heartbeat (send and recv combination).
*/
_beat() {
let self = this;
// Send payload after heartbeat
if (this._payload_name != null) {
this._log("\tSending beat with payload");
this._socket.send("beat", zmq.ZMQ_SNDMORE);
this._socket.send(this._payload_name, zmq.ZMQ_SNDMORE);
this._socket.send(this._payload_arg);
// Reset (`func` reset later)
this._payload_name = null;
this._payload_arg = null;
}
// No payload to send
else {
this._log("\tSending beat");
this._socket.send("beat");
}
this._killer = setTimeout(function() {
self._log("Nothing received. Exiting...");
self._socket.close();
self.timeoutFunc();
}, this._timeout);
}
/**
* Begin heartbeat communicated with the `PyServer`.
*/
run() {
let self = this;
this._socket.on('message', function(msg) {
self._log("recieved: " + msg);
clearTimeout(self._killer);
let ind = msg.indexOf(PAYLOAD_STR);
if (ind >= 0) {
let start = ind + PAYLOAD_STR.length;
let response = msg.toString().substr(start, msg.length-start);
let error = (response.substr(0, 5) == "error");
if (self._payload_func == null || error) {
self._log("Response: '%s'", response);
if (self._payload_func == null) {
let err = "Received payload response, no function set!";
console.error(err);
// throw new Error(err);
} else {
self._log("Error returned, not calling response function!");
}
} else {
self._payload_func(response);
}
// Reset payload response function
self._payload_func = null;
} else if (self._payload_func != null ) {
self._log("WARNING: payload function set, but no return value!")
}
self._beat();
});
self._beat();
}
/**
* Request a function call from the `PyServer`.
*
* @param {string} name - The `PyServer` function name to be called.
* @param {string} arg - The argument to be included in the `PyServer`
* function call.
* @param {function} func - The to call with the result returned from the
* `PyServer`. Must accept a single argument.
*/
call(name, arg, func) {
this._payload_name = name;
this._payload_arg = arg;
this._payload_func = func;
}
stop() {
var stopFailed;
var self = this;
this._socket.on('message', function(msg) {
self._log("recieved: " + msg);
if (msg == "STOPPED") {
clearTimeout(self._killer);
self._log("Server successfully exited.")
self._socket.send("STOP");
}
});
stopFailed = setTimeout(function() {
self._log("No stopped signal recieved!");
self._socket.close();
throw new Error("Connection timeout during stop signal!")
}, this._timeout);
}
_log(msg, debug=null) {
// Set default debug value
if (debug == null) {
debug = this._debug;
}
if (debug) {
let _msg = getDateTime() + " :: " + msg;
console.log(_msg);
}
}
}
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + ":" + month + ":" + day + " " + hour + ":" + min + ":" + sec;
}
module.exports = JSClient;