|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -module.exports = {SocketListSend, SocketListReceive}; |
4 |
| - |
5 | 3 | const EventEmitter = require('events');
|
6 |
| -const util = require('util'); |
7 | 4 |
|
8 | 5 | // This object keep track of the socket there are sended
|
9 |
| -function SocketListSend(slave, key) { |
10 |
| - EventEmitter.call(this); |
| 6 | +class SocketListSend extends EventEmitter { |
| 7 | + constructor(child, key) { |
| 8 | + super(); |
| 9 | + this.key = key; |
| 10 | + this.child = child; |
| 11 | + } |
11 | 12 |
|
12 |
| - this.key = key; |
13 |
| - this.slave = slave; |
14 |
| -} |
15 |
| -util.inherits(SocketListSend, EventEmitter); |
| 13 | + _request(msg, cmd, callback) { |
| 14 | + var self = this; |
| 15 | + |
| 16 | + if (!this.child.connected) return onclose(); |
| 17 | + this.child.send(msg); |
16 | 18 |
|
17 |
| -SocketListSend.prototype._request = function(msg, cmd, callback) { |
18 |
| - var self = this; |
| 19 | + function onclose() { |
| 20 | + self.child.removeListener('internalMessage', onreply); |
| 21 | + callback(new Error('child closed before reply')); |
| 22 | + } |
19 | 23 |
|
20 |
| - if (!this.slave.connected) return onclose(); |
21 |
| - this.slave.send(msg); |
| 24 | + function onreply(msg) { |
| 25 | + if (!(msg.cmd === cmd && msg.key === self.key)) return; |
| 26 | + self.child.removeListener('disconnect', onclose); |
| 27 | + self.child.removeListener('internalMessage', onreply); |
22 | 28 |
|
23 |
| - function onclose() { |
24 |
| - self.slave.removeListener('internalMessage', onreply); |
25 |
| - callback(new Error('Slave closed before reply')); |
| 29 | + callback(null, msg); |
| 30 | + } |
| 31 | + |
| 32 | + this.child.once('disconnect', onclose); |
| 33 | + this.child.on('internalMessage', onreply); |
26 | 34 | }
|
27 | 35 |
|
28 |
| - function onreply(msg) { |
29 |
| - if (!(msg.cmd === cmd && msg.key === self.key)) return; |
30 |
| - self.slave.removeListener('disconnect', onclose); |
31 |
| - self.slave.removeListener('internalMessage', onreply); |
| 36 | + close(callback) { |
| 37 | + this._request({ |
| 38 | + cmd: 'NODE_SOCKET_NOTIFY_CLOSE', |
| 39 | + key: this.key |
| 40 | + }, 'NODE_SOCKET_ALL_CLOSED', callback); |
| 41 | + } |
32 | 42 |
|
33 |
| - callback(null, msg); |
| 43 | + getConnections(callback) { |
| 44 | + this._request({ |
| 45 | + cmd: 'NODE_SOCKET_GET_COUNT', |
| 46 | + key: this.key |
| 47 | + }, 'NODE_SOCKET_COUNT', function(err, msg) { |
| 48 | + if (err) return callback(err); |
| 49 | + callback(null, msg.count); |
| 50 | + }); |
34 | 51 | }
|
| 52 | +} |
35 | 53 |
|
36 |
| - this.slave.once('disconnect', onclose); |
37 |
| - this.slave.on('internalMessage', onreply); |
38 |
| -}; |
39 |
| - |
40 |
| -SocketListSend.prototype.close = function close(callback) { |
41 |
| - this._request({ |
42 |
| - cmd: 'NODE_SOCKET_NOTIFY_CLOSE', |
43 |
| - key: this.key |
44 |
| - }, 'NODE_SOCKET_ALL_CLOSED', callback); |
45 |
| -}; |
46 |
| - |
47 |
| -SocketListSend.prototype.getConnections = function getConnections(callback) { |
48 |
| - this._request({ |
49 |
| - cmd: 'NODE_SOCKET_GET_COUNT', |
50 |
| - key: this.key |
51 |
| - }, 'NODE_SOCKET_COUNT', function(err, msg) { |
52 |
| - if (err) return callback(err); |
53 |
| - callback(null, msg.count); |
54 |
| - }); |
55 |
| -}; |
56 | 54 |
|
57 | 55 | // This object keep track of the socket there are received
|
58 |
| -function SocketListReceive(slave, key) { |
59 |
| - EventEmitter.call(this); |
| 56 | +class SocketListReceive extends EventEmitter { |
| 57 | + constructor(child, key) { |
| 58 | + super(); |
60 | 59 |
|
61 |
| - this.connections = 0; |
62 |
| - this.key = key; |
63 |
| - this.slave = slave; |
| 60 | + this.connections = 0; |
| 61 | + this.key = key; |
| 62 | + this.child = child; |
64 | 63 |
|
65 |
| - function onempty(self) { |
66 |
| - if (!self.slave.connected) return; |
| 64 | + function onempty(self) { |
| 65 | + if (!self.child.connected) return; |
67 | 66 |
|
68 |
| - self.slave.send({ |
69 |
| - cmd: 'NODE_SOCKET_ALL_CLOSED', |
70 |
| - key: self.key |
| 67 | + self.child.send({ |
| 68 | + cmd: 'NODE_SOCKET_ALL_CLOSED', |
| 69 | + key: self.key |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + this.child.on('internalMessage', (msg) => { |
| 74 | + if (msg.key !== this.key) return; |
| 75 | + |
| 76 | + if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') { |
| 77 | + // Already empty |
| 78 | + if (this.connections === 0) return onempty(this); |
| 79 | + |
| 80 | + // Wait for sockets to get closed |
| 81 | + this.once('empty', onempty); |
| 82 | + } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') { |
| 83 | + if (!this.child.connected) return; |
| 84 | + this.child.send({ |
| 85 | + cmd: 'NODE_SOCKET_COUNT', |
| 86 | + key: this.key, |
| 87 | + count: this.connections |
| 88 | + }); |
| 89 | + } |
71 | 90 | });
|
72 | 91 | }
|
73 | 92 |
|
74 |
| - this.slave.on('internalMessage', (msg) => { |
75 |
| - if (msg.key !== this.key) return; |
76 |
| - |
77 |
| - if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') { |
78 |
| - // Already empty |
79 |
| - if (this.connections === 0) return onempty(this); |
80 |
| - |
81 |
| - // Wait for sockets to get closed |
82 |
| - this.once('empty', onempty); |
83 |
| - } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') { |
84 |
| - if (!this.slave.connected) return; |
85 |
| - this.slave.send({ |
86 |
| - cmd: 'NODE_SOCKET_COUNT', |
87 |
| - key: this.key, |
88 |
| - count: this.connections |
89 |
| - }); |
90 |
| - } |
91 |
| - }); |
92 |
| -} |
93 |
| -util.inherits(SocketListReceive, EventEmitter); |
| 93 | + add(obj) { |
| 94 | + this.connections++; |
94 | 95 |
|
95 |
| -SocketListReceive.prototype.add = function(obj) { |
96 |
| - this.connections++; |
| 96 | + // Notify previous owner of socket about its state change |
| 97 | + obj.socket.once('close', () => { |
| 98 | + this.connections--; |
97 | 99 |
|
98 |
| - // Notify previous owner of socket about its state change |
99 |
| - obj.socket.once('close', () => { |
100 |
| - this.connections--; |
| 100 | + if (this.connections === 0) this.emit('empty', this); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
101 | 104 |
|
102 |
| - if (this.connections === 0) this.emit('empty', this); |
103 |
| - }); |
104 |
| -}; |
| 105 | +module.exports = {SocketListSend, SocketListReceive}; |
0 commit comments