forked from interpretor/zyre.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zre_msg.js
409 lines (331 loc) · 9.74 KB
/
zre_msg.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2017 Sebastian Rager
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
const zeromq = require('zeromq');
const ZRE_VERSION = 2;
const ZRE_HEADER = Buffer.from([0xAA, 0xA1]);
const HELLO = 1;
const WHISPER = 2;
const SHOUT = 3;
const JOIN = 4;
const LEAVE = 5;
const PING = 6;
const PING_OK = 7;
function putNumber1(num) {
const buf = Buffer.alloc(1);
buf.writeUInt8(num);
return buf;
}
function getNumber1(buf) {
return buf.readUInt8();
}
function putNumber2(num) {
const buf = Buffer.alloc(2);
buf.writeUInt16BE(num);
return buf;
}
function getNumber2(buf) {
return buf.readUInt16BE();
}
function putNumber4(num) {
const buf = Buffer.alloc(4);
buf.writeUInt32BE(num);
return buf;
}
function getNumber4(buf) {
return buf.readUInt32BE();
}
function putString(str) {
return Buffer.concat([
putNumber1(Buffer.byteLength(str, 'utf8')),
Buffer.from(str, 'utf8'),
]);
}
function getString(buf) {
const pointer = getNumber1(buf) + 1;
const value = buf.toString('utf8', 1, pointer);
return { value, pointer };
}
function putLongString(str) {
return Buffer.concat([
putNumber4(Buffer.byteLength(str, 'utf8')),
Buffer.from(str, 'utf8'),
]);
}
function getLongString(buf) {
const pointer = getNumber4(buf) + 4;
const value = buf.toString('utf8', 4, pointer);
return { value, pointer };
}
function putStrings(strArr) {
let stringsBuffer = putNumber4(strArr.length);
strArr.forEach((e) => {
stringsBuffer = Buffer.concat([
stringsBuffer,
putLongString(String(e)),
]);
});
return stringsBuffer;
}
function getStrings(buf) {
const count = getNumber4(buf);
const value = [];
let pointer = 4;
for (let i = 0; i < count; i += 1) {
const string = getLongString(buf.slice(pointer));
value.push(string.value);
pointer += string.pointer;
}
return { value, pointer };
}
function putDictionary(obj) {
let dictBuffer = putNumber4(Object.keys(obj).length);
Object.keys(obj).forEach((i) => {
dictBuffer = Buffer.concat([
dictBuffer,
putString(String(i)),
putLongString(String(obj[i])),
]);
});
return dictBuffer;
}
function getDictionary(buf) {
const count = getNumber4(buf);
const value = {};
let pointer = 4;
for (let i = 0; i < count; i += 1) {
const keyString = getString(buf.slice(pointer));
const valuePointer = pointer + keyString.pointer;
const valueString = getLongString(buf.slice(valuePointer));
value[keyString.value] = valueString.value;
pointer = valuePointer + valueString.pointer;
}
return { value, pointer };
}
/**
* ZreMsg represents a message in ZRE format.
*/
class ZreMsg {
/**
* @param {number} cmd - ZreMsg command as number
* @param {object} [options] - Options object
* @param {number} [options.sequence=1] - Sequence of the message
* @param {string} [options.group] - Group which the node/peer joins or leaves
* @param {Buffer} [options.content] - Content of the message
* @param {string} [options.endpoint] - TCP address of the node/peer
* @param {string[]} [options.groups] - Groups in which the node/peer participates
* @param {number} [options.status] - Groups status of the node/peer
* @param {string} [options.name] - Name of the node/peer
* @param {object} [options.headers] - Headers of the node/peer
*/
constructor(cmd, { sequence = 1, group, content, endpoint, groups, status, name, headers } = {}) {
this._cmd = cmd;
this._sequence = sequence;
this._group = group;
this._content = content;
this._endpoint = endpoint;
this._groups = groups;
this._status = status;
this._name = name;
this._headers = headers;
}
/**
* @return {number} ZreMsg command as number
*/
getCmd() {
return this._cmd;
}
/**
* @return {number} Sequence of the message
*/
getSequence() {
return this._sequence;
}
/**
* @return {string} Group which the node/peer joins or leaves
*/
getGroup() {
return this._group;
}
/**
* @return {Buffer} Content of the message
*/
getContent() {
return this._content;
}
/**
* @return {string} TCP address of the node/peer
*/
getEndpoint() {
return this._endpoint;
}
/**
* @return {string[]} Groups in which the node/peer participates
*/
getGroups() {
return this._groups;
}
/**
* @return {number} Groups status of the node/peer
*/
getStatus() {
return this._status;
}
/**
* @return {string} Name of the node/peer
*/
getName() {
return this._name;
}
/**
* @return {object} Headers of the node/peer
*/
getHeaders() {
return this._headers;
}
/**
* @param {number} sequence - Sequence of the message
*/
setSequence(sequence) {
this._sequence = sequence;
}
/**
* @param {string} group - Group which the node/peer joins or leaves
*/
setGroup(group) {
this._group = group;
}
/**
* Sends this ZreMsg with the given zeromq dealer socket.
*
* @param {zeromq.Socket} socket - Zeromq dealer socket
* @return {Promise}
*/
send(socket) {
return new Promise((resolve) => {
if (this._cmd === WHISPER || this._cmd === SHOUT) {
socket.send(this.toBuffer(), zeromq.ZMQ_SNDMORE);
socket.send(this._content, 0, () => {
resolve(this._cmd);
});
} else {
socket.send(this.toBuffer(), 0, () => {
resolve(this._cmd);
});
}
});
}
/**
* Creates a binary Buffer from this ZreMsg.
*
* @return {Buffer} Binary Buffer in ZreMsg format
*/
toBuffer() {
const bufArr = [];
bufArr.push(ZRE_HEADER);
bufArr.push(putNumber1(this._cmd));
bufArr.push(putNumber1(ZRE_VERSION));
bufArr.push(putNumber2(this._sequence));
if (typeof this._group !== 'undefined') bufArr.push(putString(this._group));
if (typeof this._endpoint !== 'undefined') bufArr.push(putString(this._endpoint));
if (typeof this._groups !== 'undefined') bufArr.push(putStrings(this._groups));
if (typeof this._status !== 'undefined') bufArr.push(putNumber1(this._status));
if (typeof this._name !== 'undefined') bufArr.push(putString(this._name));
if (typeof this._headers !== 'undefined') bufArr.push(putDictionary(this._headers));
return Buffer.concat(bufArr);
}
/**
* Reads, validates and creates a new ZreMsg from the given Buffer and frame.
*
* @param {Buffer} buffer - Binary Buffer in ZreMsg format
* @param {Buffer} frame - Message content as binary Buffer
* @return {ZreMsg}
*/
static read(buffer, frame) {
try {
if (buffer.compare(ZRE_HEADER, 0, 2, 0, 2) !== 0) throw Error;
if (getNumber1(buffer.slice(3)) !== ZRE_VERSION) throw Error;
switch (getNumber1(buffer.slice(2))) {
case HELLO: {
let pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
pointer += 2;
// A HELLO message always has to be the first message
if (sequence !== 1) throw Error;
const endpointString = getString(buffer.slice(pointer));
const endpoint = endpointString.value;
pointer += endpointString.pointer;
const groupsStrings = getStrings(buffer.slice(pointer));
const groups = groupsStrings.value;
pointer += groupsStrings.pointer;
const status = getNumber1(buffer.slice(pointer));
pointer += 1;
const nameString = getString(buffer.slice(pointer));
const name = nameString.value;
pointer += nameString.pointer;
const headersDict = getDictionary(buffer.slice(pointer));
const headers = headersDict.value;
return new ZreMsg(HELLO, { sequence, endpoint, groups, status, name, headers });
}
case WHISPER: {
const pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
return new ZreMsg(WHISPER, { sequence, content: frame });
}
case SHOUT: {
let pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
pointer += 2;
const group = getString(buffer.slice(pointer)).value;
return new ZreMsg(SHOUT, { sequence, group, content: frame });
}
case JOIN: {
let pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
pointer += 2;
const groupString = getString(buffer.slice(pointer));
const group = groupString.value;
pointer += groupString.pointer;
const status = getNumber1(buffer.slice(pointer));
return new ZreMsg(JOIN, { sequence, group, status });
}
case LEAVE: {
let pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
pointer += 2;
const groupString = getString(buffer.slice(pointer));
const group = groupString.value;
pointer += groupString.pointer;
const status = getNumber1(buffer.slice(pointer));
return new ZreMsg(LEAVE, { sequence, group, status });
}
case PING: {
const pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
return new ZreMsg(PING, { sequence });
}
case PING_OK: {
const pointer = 4;
const sequence = getNumber2(buffer.slice(pointer));
return new ZreMsg(PING_OK, { sequence });
}
default:
throw Error;
}
} catch (err) {
return undefined;
}
}
}
ZreMsg.HELLO = HELLO;
ZreMsg.WHISPER = WHISPER;
ZreMsg.SHOUT = SHOUT;
ZreMsg.JOIN = JOIN;
ZreMsg.LEAVE = LEAVE;
ZreMsg.PING = PING;
ZreMsg.PING_OK = PING_OK;
module.exports = ZreMsg;