forked from zhoutony/html5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround-robin.js
More file actions
55 lines (41 loc) · 873 Bytes
/
round-robin.js
File metadata and controls
55 lines (41 loc) · 873 Bytes
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
/**
* Deps.
*/
var slice = require('../utils').slice;
/**
* Round-robin plugin.
*
* Provides a `send` method which will
* write the `msg` to all connected peers.
*
* @param {Object} options
* @api private
*/
module.exports = function(options){
options = options || {};
var fallback = options.fallback || function(){};
return function(sock){
/**
* Bind callback to `sock`.
*/
fallback = fallback.bind(sock);
/**
* Initialize counter.
*/
var n = 0;
/**
* Sends `msg` to all connected peers round-robin.
*/
sock.send = function(){
var socks = this.socks;
var len = socks.length;
var sock = socks[n++ % len];
var msg = slice(arguments);
if (sock && sock.writable) {
sock.write(this.pack(msg));
} else {
fallback(msg);
}
};
};
};