-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
348 lines (293 loc) · 9.02 KB
/
index.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
/*
* free and unencumbered software released into the public domain.
*/
var nn = require('bindings')('nanomsg.node') //should be .iojs
var dup = require('stream').Duplex
var sock = {
pub : [nn.NN_PUB, nn.NN_SUB],
sub : [nn.NN_SUB, nn.NN_PUB],
bus : [nn.NN_BUS, nn.NN_BUS],
pair : [nn.NN_PAIR, nn.NN_PAIR],
surv : [nn.NN_SURVEYOR, nn.NN_RESPONDENT],
surveyor : [nn.NN_SURVEYOR, nn.NN_RESPONDENT],
resp : [nn.NN_RESPONDENT, nn.NN_SURVEYOR],
respondent : [nn.NN_RESPONDENT, nn.NN_SURVEYOR],
req : [nn.NN_REQ, nn.NN_REP],
rep : [nn.NN_REP, nn.NN_REQ],
pull : [nn.NN_PULL, nn.NN_PUSH],
push : [nn.NN_PUSH, nn.NN_PULL],
}
var af = {
af_sp : nn.AF_SP,
AF_SP : nn.AF_SP,
af_sp_raw : nn.AF_SP_RAW,
raw : nn.AF_SP_RAW,
af : nn.AF_SP
}
var sol = {
linger : nn.NN_LINGER,
sndbuf : nn.NN_SNDBUF,
rcvbuf : nn.NN_RCVBUF,
sndtimeo : nn.NN_SNDTIMEO,
rcvtimeo : nn.NN_RCVTIMEO,
reconn : nn.NN_RECONNECT_IVL,
maxreconn : nn.NN_RECONNECT_IVL_MAX,
sndprio : nn.NN_SNDPRIO,
tcpnodelay : nn.NN_TCP_NODELAY
}
if (nn.NN_VERSION > 3) sol.rcvprio = nn.NN_RCVPRIO
module.exports = {
version: nn.NN_VERSION,
versionstr: '0.'+nn.NN_VERSION+'-beta',
socket: function ( type, opts ) {
//preflight check
if(typeof opts == 'string') opts = { fam: opts }
opts = opts || { fam: 'af' }
if(!opts.hasOwnProperty('fam')) opts.fam = 'af'
//ensure first nanomsg socket is not zero, else disregard it
var nns = nn.Socket(af[opts.fam], sock[type][0])
if(nns == 0) return new self(nn.Socket(af[opts.fam],sock[type][0]), type, opts)
return new self( nns, type, opts )
}
}
require('util').inherits( self, dup)
function self (s, t, o) {
//error handle
if(s < 0) throw new Error(nn.Err() + ': ' + t + ' creating socket'+'\n')
var ctx = this
this.socket = s
this.fam = o.fam
this.type = t
this.how = {}
this.bind = bind
this.connect = connect
this.close = close
this.shutdown = shutdown
this.unhook = unhook
this.setsockopt = setsockopt
this.getsockopt = getsockopt
this.linger = linger
this.sndbuf = sndbuf
this.rcvbuf = rcvbuf
this.sndtimeo = sndtimeo
this.rcvtimeo = rcvtimeo
this.reconn = reconn
this.maxreconn = maxreconn
this.sndprio = sndprio
this.rcvprio = rcvprio
this.tcpnodelay = tcpnodelay
this.sleep = uvsleeper( function (t, d) { setTimeout(d, t) } )
this.send = function(msg) { nn.Send( s, msg ) }
this.recv = recv
this.destroyed = false
this.destroy = destroy
for(var sokopt in sol) if(o.hasOwnProperty(sokopt)) this[sokopt](o[sokopt])
switch(t){
case 'pub':
case 'push':
break
case 'surv':
case 'surveyor':
case 'rep':
case 'pull':
case 'sub':
case 'pair':
case 'bus':
case 'req':
case 'resp':
case 'respondent': nn.Recv( s, recv )
break
}
function recv (msg){
//msgs that are -1 is a req/surveyor issue so break the loop
if (msg == -1) return
ctx.push( msg )
if (!ctx.destroyed) nn.Recv( s, recv )
}
this._read = function(n){ }
this._write = function(buf, _, next){ nn.Send(s,buf); next() }
dup.call(this)
}
function close(fn) {
if (!fn) fn = function(){}
this.destroy()
switch(this.type){
case 'pub':
case 'push':
nn.Close( this.socket )
return fn('closing a pub or a push')
case 'pair':
return fn(this.unhook(Math.floor(Math.random()*100000000)))
default:
return fn(this.unhook())
}
}
function shutdown(addr) {
var confirm = this.how[addr][1] + ' endpoint '+ addr + ' shutdown'
if(nn.Shutdown(this.socket, this.how[addr][0]) < 0)
throw new Error(nn.Err() +': '+this.type+' shutdown@' + addr+'\n')
delete this.how[addr]; return confirm
}
function bind (addr) {
var eid = nn.Bind( this.socket, addr )
if(eid < 0) throw new Error(nn.Err() +': '+this.type+' bind@' + addr+'\n')
this.how[addr] = [eid,'bind']; return this
}
function connect (addr) {
var eid = nn.Connect( this.socket, addr )
if(eid < 0) throw new Error(nn.Err() +': '+this.type+' connect@' + addr+'\n')
this.how[addr] = [eid,'connect']; return this
}
function setsockopt(level, option, value){
return nn.Setsockopt(this.socket, nn[level], nn[option], value)
}
function getsockopt(level, option){
return nn.Getsockopt(this.socket, nn[level], nn[option])
}
function linger(number){
if(number){
if(setsol(this.socket, 'linger', number) > -1)
return 'linger set to ' + number + 'ms'
throw new Error(nn.Err() + ': '+this.type+' linger@'+number+'\n')
} else {
return getsol(this.socket, 'linger')
}
}
function sndbuf(number){
if(number){
if(setsol(this.socket, 'sndbuf', number) > -1)
return 'sndbuf set to ' + number + ' bytes'
throw new Error(nn.Err() + ': '+this.type+' sndbuf@'+number+'\n')
} else {
return getsol(this.socket, 'sndbuf')
}
}
function rcvbuf(number){
if(number){
if(setsol(this.socket, 'rcvbuf', number) > -1)
return 'rcvbuf set to ' + number + ' bytes'
throw new Error(nn.Err() + ': '+this.type+' rcvbuf@'+number+'\n')
} else {
return getsol(this.socket, 'rcvbuf')
}
}
function sndtimeo(number){
if(number){
if(setsol(this.socket, 'sndtimeo', number) > -1)
return 'sndtimeo set to ' + number + 'ms'
throw new Error(nn.Err() + ': '+this.type+' reconn@'+number+'\n')
} else {
return getsol(this.socket, 'sndtimeo')
}
}
function rcvtimeo(number){
if(number){
if(setsol(this.socket, 'rcvtimeo', number) > -1)
return 'rcvtimeo set to ' + number + 'ms'
throw new Error(nn.Err() + ': '+this.type+' reconn@'+number+'\n')
} else {
return getsol(this.socket, 'rcvtimeo')
}
}
function reconn(number){
if(number){
if(setsol(this.socket, 'reconn', number) > -1)
return 'reconn set to ' + number + 'ms'
throw new Error(nn.Err() + ': '+this.type+' reconn@'+number+'\n')
} else {
return getsol(this.socket, 'reconn')
}
}
function maxreconn(number){
if(number){
if(setsol(this.socket, 'maxreconn', number) > -1)
return 'maxreconn set to ' + number + 'ms'
throw new Error(nn.Err() + ': '+this.type+' maxreconn@'+number+'\n')
} else {
return getsol(this.socket, 'maxreconn')
}
}
function sndprio(number){
if(number){
if(setsol(this.socket, 'sndprio', number) > -1)
return 'sndprio set to ' + number
throw new Error(nn.Err() + ': '+this.type+' sndprio@'+number+'\n')
} else {
return getsol(this.socket, 'sndprio')
}
}
function rcvprio(number){
if(nn.NN_VERSION > 3){
if(number){
if(setsol(this.socket, 'rcvprio', number) > -1)
return 'rcvprio set to ' + number
throw new Error(nn.Err() + ': '+this.type+' rcvprio@'+number+'\n')
} else {
return getsol(this.socket, 'rcvprio')
}
} else {
var version = 'current lib version: '+ nn.versionstr + '\n'
var err = 'rcvprio: available in nanomsg beta-0.4 and higher.'
throw new Error(version + err + ':' +this.type+' rcvprio@'+number+'\n')
}
}
function tcpnodelay(bool){
if(arguments.length){
if(bool){
if(nn.Setsockopt(this.socket, nn.NN_TCP, nn.NN_TCP_NODELAY, 1) > -1)
return 'tcp nodelay: on'
throw new Error(nn.Err() + ': '+this.type+' nodelay@'+'activing'+'\n')
} else {
if(nn.Setsockopt(this.socket, nn.NN_TCP, nn.NN_TCP_NODELAY, 0) > -1)
return 'tcp nodelay: off'
throw new Error(nn.Err() + ': '+this.type+' nodelay@'+'deactiving'+'\n')
}
} else {
switch(nn.Getsockopt(this.socket, nn.NN_TCP, nn.NN_TCP_NODELAY)){
case 1: return 'tcp nodelay: on'
case 0: return 'tcp nodelay: off'
default:
throw new Error(nn.Err() + ': '+this.type+' nodelay@'+'getsockopt'+'\n')
}
}
}
function setsol(socket, option, value){
return nn.Setsockopt(socket, nn.NN_SOL_SOCKET, sol[option], value)
}
function getsol(socket, option){
return nn.Getsockopt(socket, nn.NN_SOL_SOCKET, sol[option])
}
function destroy(){
if (this.destroyed) return;
this.destroyed = true
}
function unhook(random){
var unsocket = nn.Socket(af[this.fam], sock[this.type][1])
var addr = 'inproc://unhook' + unsocket
if (random) addr += random
nn.Bind( unsocket, addr )
nn.Connect( this.socket, addr )
this.sleep(10)
switch (this.type){
case 'surv':
case 'surveyor':
case 'req':
nn.Send( this.socket, 'swimming like a fish' )
nn.Rcv(unsocket)
}
return nn.Send( unsocket, 'unhook and close' )
}
function uvsleeper(fn) {
return function () {
var done = false, err, res
fn.apply(this, Array.prototype.slice.apply(arguments).concat(cb))
while (!done) nn.Sleeper()
if (err) throw err
return res
function cb (e, r) {
err = e
res = r
done = true
}
}
}