/
index.js
217 lines (184 loc) · 4.49 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
var inf = require('./inf')
var msg = require('./msg')
var type = require('../message/type')
var pubsub = require('./pubsub')
var reqrep = require('./reqrep')
var Buffer = type.Buffer
var isBuffer = Buffer.isBuffer
var PUB = type.PUB
var SUB = type.SUB
var REQ = type.REQ
var REP = type.REP
var INF = type.INF
var MID = type.MID
var Queue = module.exports = function(socket) {
this.n = 0
this.send = socket.send.bind(socket)
this.socket = socket
this._pendings = []
// Initialize inboxes
this[SUB] = {}
this[REQ] = {}
this[REP] = {}
// Bind event listeners
this.onconnect = this.onconnect.bind(this)
this.onmessage = this.onmessage.bind(this)
// Listen events
socket.on('connect', this.onconnect)
socket.on('message', this.onmessage)
var queue = this
if (socket.streams && socket.streams.length > 0) {
setTimeout(function() {
queue.ack(socket.streams)
}, 0)
}
}
/**
* Pubsub API
*/
Queue.prototype.pub = pubsub.pub
Queue.prototype.sub = pubsub.sub
Queue.prototype.onPub = pubsub.onPub
/**
* Reqrep API
*/
Queue.prototype.req = reqrep.req
Queue.prototype.rep = reqrep.rep
Queue.prototype.onReq = reqrep.onReq
Queue.prototype.onRep = reqrep.onRep
/**
* Info API
*/
Queue.prototype.ack = inf.ack
Queue.prototype.inf = inf.inf
Queue.prototype.getAckPack = inf.getAckPack
/**
* Message API
*/
Queue.prototype.encode = msg.encode
Queue.prototype.decode = msg.decode
/**
* Common sending patterns
*/
Queue.prototype.one = function(streams, pack) {
var len = streams.length
var stream = streams[this.n++ % len]
if (!stream)
return this.push(['one', streams, pack])
var buf = pack.buf || this.encode(pack)
// send the buf to selected stream
this.send(stream, buf)
}
Queue.prototype.all = function(streams, pack) {
var len = streams.length
if (0 === len)
return this.push(['all', streams, pack])
var send = this.send
var buf = pack.buf || this.encode(pack)
while (len-- > 0) {
send(streams[len], buf)
}
}
/**
* Event handlers
*/
Queue.prototype.onconnect = function(stream) {
// Acknowldge REP/SUB
if (stream.writable)
this.ack([stream])
// Flush pending messages
this._flush()
}
Queue.prototype.onmessage = function(data, stream) {
this.dispatch(data, stream)
}
/**
* Buffer message to internal queue.
*/
Queue.prototype.push = function(arr) {
this._pendings.push(arr)
}
/**
* Dispatching pack
*/
Queue.prototype.beforeDispatch = function(pack, stream, dispatch) {
dispatch(pack, stream)
}
/**
* Dispatch messages to corresponding handlers.
*
* @param {Buffer} data Raw data buffer
* @param {Stream} stream The stream from which the data was sent
*/
Queue.prototype.dispatch = function(data, stream) {
var pack = this.decode(data)
var fid = pack.fid
if (fid === type.F_BUFFER || fid === type.F_STRING)
pack.msg = [pack.msg]
var queue = this
this.beforeDispatch(pack, stream, function(pack, stream) {
var msg
if (true === pack.hasMsg)
msg = pack.msg || [pack.msg]
else
msg = []
var meta = pack.meta
var event = pack.event
switch (pack.type) {
case PUB:
queue.onPub(event, msg)
break
case REQ:
// Make reply function
if (meta && meta[MID]) {
var send = queue.send
var encode = queue.encode
var reply = function(repMsg) {
var len = arguments.length
if (len > 1) {
len--
var i = 0
repMsg = [repMsg]
while (i++ < len) {
repMsg[i] = arguments[i]
}
} else if ('string' !== typeof repMsg && !isBuffer(repMsg)) {
repMsg = [repMsg]
}
var buf = encode({
type: REP,
event: event,
msg: repMsg,
meta: meta
})
send(stream, buf)
}
msg.push(reply)
}
queue.onReq(event, msg)
break
case REP:
var msgId = meta[MID]
queue.onRep(event, msg, msgId)
break
case INF:
if ('LVE' === event && queue.onLeavePack)
queue.onLeavePack(pack, stream)
break
}
})
}
Queue.prototype._flush = function() {
var pendings = this._pendings
var len = pendings.length
if (0 < len) {
// Reset pending array
this._pendings = []
var i = 0
while (i < len) {
var _p = pendings[i++]
// this.req/pub(streams, ...)
this[_p[0]](_p[1], _p[2], _p[3], _p[4], _p[5], _p[6])
}
}
}