git ssb

16+

Dominic / patchbay



Tree: a692047f69bbb665d6be280233fdf5a8131c4594

Files: a692047f69bbb665d6be280233fdf5a8131c4594 / modules_extra / network.js

3709 bytesRaw
1var isVisible = require('is-visible').isVisible
2var h = require('hyperscript')
3var plugs = require('../plugs')
4
5var avatar = plugs.first(exports.avatar = [])
6var sbot_gossip_peers = plugs.first(exports.sbot_gossip_peers = [])
7var sbot_gossip_connect = plugs.first(exports.sbot_gossip_connect = [])
8//sbot_gossip_connect
9//sbot_gossip_add
10
11var human = require('human-time')
12
13function legacyToMultiServer(addr) {
14 return 'net:'+addr.host + ':'+addr.port + '~shs:'+addr.key.substring(1).replace('.ed25519','')
15}
16
17exports.menu_items = function () {
18 return h('a', {href: '#/network'}, '/network')
19}
20
21exports.builtin_tabs = function () {
22 return ['/network']
23}
24
25//types of peers
26
27
28//on the same wifi network
29function isLocal (e) {
30 // don't rely on private ip address, because
31 // cjdns creates fake private ip addresses.
32 return ip.isPrivate(e.host) && e.type === 'local'
33}
34
35
36//pub is running scuttlebot >=8
37//have connected successfully.
38function isLongterm (e) {
39 return e.ping && e.ping.rtt && e.ping.rtt.mean > 0
40}
41
42//pub is running scuttlebot < 8
43//have connected sucessfully
44function isLegacy (peer) {
45 return /connect/.test(peer.state) || (peer.duration && peer.duration.mean) > 0 && !isLongterm(peer)
46}
47
48//tried to connect, but failed.
49function isInactive (e) {
50 return e.stateChange && (e.duration && e.duration.mean == 0)
51}
52
53//havn't tried to connect peer yet.
54function isUnattempted (e) {
55 return !e.stateChange
56}
57
58function getType (e) {
59 return (
60 isLongterm(e) ? 'modern'
61 : isLegacy(e) ? 'legacy'
62 : isInactive(e) ? 'inactive'
63 : isUnattempted(e) ? 'unattempted'
64 : 'other' //should never happen
65 )
66}
67
68var states = {
69 connected: 3,
70 connecting: 2
71}
72
73var types = {
74 modern: 4,
75 legacy: 3,
76 inactive: 2,
77 unattempted: 1,
78 other: 0
79}
80
81function round(n) {
82 return Math.round(n*100)/100
83}
84
85function duration (s) {
86 if(!s) return s
87 if (Math.abs(s) > 30000)
88 return round(s/60000)+'m'
89 else if (Math.abs(s) > 500)
90 return round(s/1000)+'s'
91 else
92 return round(s)+'ms'
93}
94
95exports.screen_view = function (path) {
96
97 if(path !== '/network') return
98
99 var ol = h('ul.network')
100
101 ;(function poll () {
102
103 //if this tab isn't open, don't update.
104 //todo: make a better way to do this...
105 if(!isVisible(ol))
106 return setTimeout(poll, 1000)
107
108 sbot_gossip_peers(function (err, list) {
109 ol.innerHTML = ''
110 list.sort(function (a, b) {
111 return (
112 (states[b.state] || 0) - (states[a.state] || 0)
113 || types[getType(b)] - types[getType(a)]
114 || b.stateChange - a.stateChange
115 )
116 }).forEach(function (peer) {
117 ol.appendChild(h('div',
118 avatar(peer.key, 'thumbnail'),
119 h('div',
120 peer.state || 'not connected',
121 ' ',
122 getType(peer),
123 ' ',
124 //TODO: show nicer details, with labels. etc.
125 (peer.ping && peer.ping.rtt) ? duration(peer.ping.rtt.mean) : '',
126 ' ',
127 (peer.ping && peer.ping.skew) ? duration(peer.ping.skew.mean) : '',
128 h('label',
129 {title: new Date(peer.stateChange).toString()},
130 peer.stateChange && ('(' + human(new Date(peer.stateChange))) + ')')
131 ),
132 'source:'+peer.source,
133 h('pre', legacyToMultiServer(peer)),
134 h('button', 'connect', {onclick: function () {
135 sbot_gossip_connect(peer, function (err) {
136 if(err) console.error(err)
137 else console.log('connected to', peer)
138 })
139 }})
140 )
141 )
142 })
143
144 setTimeout(poll, 5000)
145 })
146
147 })()
148
149 return h('div.column.scroll-y', ol)
150}
151
152
153

Built with git-ssb-web