git ssb

16+

Dominic / patchbay



Tree: d20fb6972ed016e0dfeadef9da886fa2caa08a41

Files: d20fb6972ed016e0dfeadef9da886fa2caa08a41 / modules_extra / network.js

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

Built with git-ssb-web