forked from MeltwaterArchive/zeromq.node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zmq-3.0.js
251 lines (221 loc) · 6.99 KB
/
zmq-3.0.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
/*globals require exports process Buffer */
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var IOWatcher = process.binding('io_watcher').IOWatcher;
var zmq = require('./binding');
// A map of convenient names to the ZMQ constants for socket types.
var namemap = (function() {
var m = {};
m.pub = m.publish = m.publisher = zmq.ZMQ_PUB;
m.xpub = m.xpublish = m.xpublisher = zmq.ZMQ_XPUB;
m.sub = m.subscribe = m.subscriber = zmq.ZMQ_SUB;
m.xsub = m.xsubscribe = m.xsubscriber = zmq.ZMQ_XSUB;
m.req = m.request = m.requester = zmq.ZMQ_REQ;
m.xreq = m.xrequest = m.xrequester = zmq.ZMQ_XREQ;
m.rep = m.reply = m.replier = zmq.ZMQ_REP;
m.xrep = m.xreply = m.xreplier = zmq.ZMQ_XREP;
m.push = m.pusher = zmq.ZMQ_PUSH;
m.pull = m.puller = zmq.ZMQ_PULL;
m.dealer = zmq.ZMQ_DEALER;
m.router = zmq.ZMQ_ROUTER;
m.pair = zmq.ZMQ_PAIR;
return m;
})();
// Context management happens here. We lazily initialize a default context,
// and use that everywhere. Also cleans up on exit.
var context_ = null;
var defaultContext = function() {
if (context_ !== null) {
return context_;
}
var io_threads = 1;
if (process.env.ZMQ_IO_THREADS) {
io_threads = parseInt(process.env.ZMQ_IO_THREADS, 10);
if (!io_threads || io_threads < 1) {
util.error('Invalid number in ZMQ_IO_THREADS, using 1 IO thread.');
io_threads = 1;
}
}
context_ = new zmq.Context(io_threads);
process.on('exit', function() {
context_.close();
context_ = null;
});
return context_;
};
// The socket type returned by `createSocket`. Wraps the low-level ZMQ binding
// with all the conveniences.
var Socket = function(typename) {
var self = this,
typecode = typename;
if (typeof(typecode) !== 'number') {
typecode = namemap[typename];
if (!namemap.hasOwnProperty(typename) || typecode === undefined) {
throw new TypeError("Unknown socket type: " + typename);
}
}
self.type = typename;
self._zmq = new zmq.Socket(defaultContext(), typecode);
self._outgoing = [];
self._watcher = new IOWatcher();
self._watcher.callback = function() { self._flush(); };
self._watcher.set(self._fd, true, false);
self._watcher.start();
self._inFlush = false;
};
util.inherits(Socket, EventEmitter);
// Define property accessors for all socket options.
var sockProp = function(name, option) {
Socket.prototype.__defineGetter__(name, function() {
return this._zmq.getsockopt(option);
});
Socket.prototype.__defineSetter__(name, function(value) {
if (typeof value == 'string') {
value = new Buffer(value, 'utf8');
}
return this._zmq.setsockopt(option, value);
});
};
sockProp('_fd', zmq.ZMQ_FD);
sockProp('_ioevents', zmq.ZMQ_EVENTS);
sockProp('_receiveMore', zmq.ZMQ_RCVMORE);
sockProp('_subscribe', zmq.ZMQ_SUBSCRIBE);
sockProp('_unsubscribe', zmq.ZMQ_UNSUBSCRIBE);
sockProp('ioThreadAffinity', zmq.ZMQ_AFFINITY);
sockProp('backlog', zmq.ZMQ_BACKLOG);
sockProp('highWaterMark', zmq.ZMQ_HWM);
sockProp('identity', zmq.ZMQ_IDENTITY);
sockProp('lingerPeriod', zmq.ZMQ_LINGER);
sockProp('multicastLoop', zmq.ZMQ_MCAST_LOOP);
sockProp('multicastDataRate', zmq.ZMQ_RATE);
sockProp('receiveBufferSize', zmq.ZMQ_RCVBUF);
sockProp('reconnectInterval', zmq.ZMQ_RECONNECT_IVL);
sockProp('multicastRecovery', zmq.ZMQ_RECOVERY_IVL);
sockProp('sendBufferSize', zmq.ZMQ_SNDBUF);
sockProp('diskOffloadSize', zmq.ZMQ_SWAP);
// `bind` and `connect` map directly to our binding.
Socket.prototype.bind = function(addr, cb) {
var self = this;
self._watcher.stop();
self._zmq.bind(addr, function(err) {
self._watcher.start();
cb(err);
});
};
Socket.prototype.bindSync = function(addr) {
var self = this;
self._watcher.stop();
try {
self._zmq.bindSync(addr);
} catch (e) {
self._watcher.start();
throw e;
}
self._watcher.start();
};
Socket.prototype.connect = function(addr) {
this._zmq.connect(addr);
};
// `subscribe` and `unsubcribe` are exposed as methods.
// The binding expects a setsockopt call for these, though.
Socket.prototype.subscribe = function(filter) {
this._subscribe = filter;
};
Socket.prototype.unsubscribe = function(filter) {
this._unsubscribe = filter;
};
// Queue a message. Each arguments is a multipart message part.
// It is assumed that strings should be send in UTF-8 encoding.
Socket.prototype.send = function() {
var i, part, flags,
length = arguments.length,
parts = [];
for (i = 0; i < length; i++) {
part = arguments[i];
// We only send Buffers, but if you give us a type that can
// easily be converted, we'll do that for you.
if (!(part instanceof Buffer)) {
part = new Buffer(part, 'utf-8');
}
flags = 0;
if (i !== length-1) {
flags |= zmq.ZMQ_SNDMORE;
}
parts.push([part, flags]);
}
this._outgoing = this._outgoing.concat(parts);
this._flush();
};
Socket.prototype.currentSendBacklog = function() {
return this._outgoing.length;
};
// The workhorse that does actual send and receive operations.
// This helper is called from `send` above, and in response to
// the watcher noticing the signaller fd is readable.
Socket.prototype._flush = function() {
// Don't allow recursive flush invocation as it can lead to stack
// exhaustion and write starvation
if (this._inFlush === true) { return; }
this._inFlush = true;
try {
while (true) {
var emitArgs, sendArgs,
flags = this._ioevents;
if (this._outgoing.length === 0) {
flags &= ~zmq.ZMQ_POLLOUT;
}
if (!flags) {
break;
}
if (flags & zmq.ZMQ_POLLIN) {
emitArgs = ['message'];
do {
emitArgs.push(new Buffer(this._zmq.recv()));
} while (this._receiveMore);
this.emit.apply(this, emitArgs);
if (this._zmq.state != zmq.STATE_READY) {
this._inFlush = false;
return;
}
}
// We send as much as possible in one burst so that we don't
// starve sends if we receive more than one message for each
// one sent.
while ((flags & zmq.ZMQ_POLLOUT) && (this._outgoing.length !== 0)) {
sendArgs = this._outgoing.shift();
this._zmq.send.apply(this._zmq, sendArgs);
flags = this._ioevents;
}
}
}
catch (e) {
e.flags = flags;
e.outgoing = util.inspect(this._outgoing);
try {
this.emit('error', e);
} catch (e2) {
this._inFlush = false;
throw e2;
}
}
this._inFlush = false;
};
// Clean up the socket.
Socket.prototype.close = function() {
this._watcher.stop();
this._watcher = undefined;
this._zmq.close();
};
// The main function of the library.
exports.createSocket = function(typename, options) {
var key,
sock = new Socket(typename);
if (typeof(options) === 'object') {
for (key in options) {
if (options.hasOwnProperty(key)) {
sock[key] = options[key];
}
}
}
return sock;
};