forked from moose-team/friends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerbot.js
executable file
·58 lines (49 loc) · 1.37 KB
/
peerbot.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
var levelup = require('levelup')
var leveldown = require('leveldown')
var subleveldown = require('subleveldown')
var log = require('single-line-log').stdout
var eos = require('end-of-stream')
var minimist = require('minimist')
var Swarm = require('./lib/swarm.js')
module.exports = function (args) {
var db = levelup('./friendsdb', {db: leveldown})
db.channels = subleveldown(db, 'channels', {valueEncoding: 'json'})
var swarm = Swarm(subleveldown(db, 'swarm'))
// parse arg string into opts using minimist
var opts = minimist(args)
var chans = opts.channel || []
if (typeof chans === 'string') chans = [chans]
console.log('joining #friends')
swarm.addChannel('friends')
chans.forEach(function (chan) {
console.log('joining #' + chan)
swarm.addChannel(chan)
})
var counts = {
connects: 0,
disconnects: 0,
pushed: 0,
pulled: 0,
active: 0,
maxPeers: 0
}
swarm.on('push', function () {
counts.pushed++
log(JSON.stringify(counts))
})
swarm.on('pull', function () {
counts.pulled++
log(JSON.stringify(counts))
})
swarm.on('peer', function (p) {
counts.active++
counts.connects++
if (counts.active > counts.maxPeers) counts.maxPeers = counts.active
log(JSON.stringify(counts))
eos(p, function () {
counts.active--
counts.disconnects++
log(JSON.stringify(counts))
})
})
}