-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.js
86 lines (84 loc) · 2.12 KB
/
send.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
// Generated by CoffeeScript 1.9.2
var zmq;
zmq = require('zmq');
module.exports = function(addresses, onreceipt) {
var _addresses, addr, i, len, socket, startifstopped;
if (!(addresses instanceof Array)) {
addresses = [addresses];
}
_addresses = {};
for (i = 0, len = addresses.length; i < len; i++) {
addr = addresses[i];
_addresses[addr] = true;
}
socket = null;
startifstopped = function() {
var j, len1, ref;
if (socket != null) {
return;
}
socket = zmq.socket('dealer');
ref = Object.keys(_addresses);
for (j = 0, len1 = ref.length; j < len1; j++) {
addr = ref[j];
socket.connect(addr);
}
return socket.on('message', function(_, msgid) {
msgid = msgid.toString();
return onreceipt(msgid);
});
};
return {
send: function(msgid, data) {
startifstopped();
return socket.send(['', msgid, data]);
},
connect: function(addresses) {
var j, k, len1, len2, results, toconnect;
if (!(addresses instanceof Array)) {
addresses = [addresses];
}
toconnect = [];
for (j = 0, len1 = addresses.length; j < len1; j++) {
addr = addresses[j];
if (_addresses[addr] != null) {
continue;
}
_addresses[addr] = true;
toconnect.push(addr);
}
if (socket == null) {
return;
}
results = [];
for (k = 0, len2 = toconnect.length; k < len2; k++) {
addr = toconnect[k];
results.push(socket.connect(addr));
}
return results;
},
disconnect: function(addresses) {
var j, len1, results;
if (!(addresses instanceof Array)) {
addresses = [addresses];
}
results = [];
for (j = 0, len1 = addresses.length; j < len1; j++) {
addr = addresses[j];
if (_addresses[addr] == null) {
continue;
}
delete _addresses[addr];
results.push(socket.disconnect(addr));
}
return results;
},
close: function() {
if (socket == null) {
return;
}
socket.close();
return socket = null;
}
};
};