-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgossip.js
executable file
·65 lines (55 loc) · 1.47 KB
/
gossip.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
#! /usr/bin/env node
var blessed = require('blessed')
var contrib = require('blessed-contrib')
var path = require('path')
require('ssb-client')(function (err, sbot) {
if(err) throw err
var screen = blessed.screen()
var grid = new contrib.grid({rows: 12, cols: 12, screen: screen})
var table = contrib.table({
fg: 'default',
keys: true,
interactive: false,
columnSpacing: 2, //in chars
columnWidth: [10, process.stdout.columns - (10+15+4), 15] /*in chars*/
})
screen.append(table)
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
})
function poll () {
sbot.gossip.peers(function (err, peers) {
if (err) throw err
table.setData(peersToTableData(peers))
screen.render()
})
}
poll()
setInterval(poll, 1000)
})
function status (peer) {
if (peer.connected)
return 'Connected'
if (peer.time && peer.time.connect > peer.time.attempt)
return 'Connecting'
if (peer.failure)
return peer.failure + ' Failures'
return 'Disconnected'
}
function peersToTableData (peers) {
peers.sort(function (a, b) {
var an = (a.announcers) ? a.announcers.length : 0
var bn = (b.announcers) ? b.announcers.length : 0
return bn - an
})
return {
headers: ['Announcers', 'Address', 'Status'],
data: peers.map(function (p) {
return [
(p.announcers) ? p.announcers.length : 0,
p.host + ':' + p.port + ':' + p.key,
status(p)
]
})
}
}