git ssb

16+

Dominic / patchbay



Commit bf386541acf88522e4d21e6143eb5c9add5d0ead

show better network status, and support manual connects

Dominic Tarr committed on 9/17/2016, 10:03:42 AM
Parent: 18587bcb2aa7e5dd708e8a2dfef6e56ef56ca83e

Files changed

modules/network.jschanged
sbot-api.jschanged
modules/network.jsView
@@ -3,8 +3,9 @@
33 var plugs = require('../plugs')
44
55 var avatar = plugs.first(exports.avatar = [])
66 var sbot_gossip_peers = plugs.first(exports.sbot_gossip_peers = [])
7 +var sbot_gossip_connect = plugs.first(exports.sbot_gossip_connect = [])
78 //sbot_gossip_connect
89 //sbot_gossip_add
910
1011 var human = require('human-time')
@@ -16,20 +17,84 @@
1617 exports.menu_items = function () {
1718 return h('a', {href: '#/network'}, '/network')
1819 }
1920
21 +//types of peers
22 +
23 +
24 +//on the same wifi network
25 +function 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.
34 +function isLongterm (e) {
35 + return e.ping && e.ping.rtt.mean > 0
36 +}
37 +
38 +//pub is running scuttlebot < 8
39 +//have connected sucessfully
40 +function isLegacy (peer) {
41 + return /connect/.test(peer.state) || peer.duration.mean > 0 && !exports.isLongterm(peer)
42 +}
43 +
44 +//tried to connect, but failed.
45 +function isInactive (e) {
46 + return e.stateChange && e.duration.mean == 0
47 +}
48 +
49 +//havn't tried to connect peer yet.
50 +function isUnattempted (e) {
51 + return !e.stateChange
52 +}
53 +
54 +function 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 +
64 +var states = {
65 + connected: 3,
66 + connecting: 2
67 +}
68 +
69 +var types = {
70 + modern: 4,
71 + legacy: 3,
72 + inactive: 2,
73 + unattempted: 1,
74 + other: 0
75 +}
76 +
77 +function round(n) {
78 + return Math.round(n*100)/100
79 +}
80 +
81 +function 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 +
2091 exports.screen_view = function (path) {
2192
2293 if(path !== '/network') return
2394
24- var ol = h('ol.network')
95 + var ol = h('ul.network')
2596
26- var states = {
27- connected: 3,
28- connecting: 2,
29- disconnecting: 1
30- }
31-
3297 ;(function poll () {
3398
3499 //if this tab isn't open, don't update.
35100 //todo: make a better way to do this...
@@ -38,21 +103,37 @@
38103
39104 sbot_gossip_peers(function (err, list) {
40105 ol.innerHTML = ''
41106 list.sort(function (a, b) {
42- return (states[b.state] || 0) - (states[a.state] || 0) || b.stateChange - a.stateChange
107 + return (
108 + (states[b.state] || 0) - (states[a.state] || 0)
109 + || types[getType(b)] - types[getType(a)]
110 + || b.stateChange - a.stateChange
111 + )
43112 }).forEach(function (peer) {
44- ol.appendChild(h('li',
113 + ol.appendChild(h('div',
45114 avatar(peer.key, 'thumbnail'),
46115 h('div',
47116 peer.state || 'not connected',
48117 ' ',
118 + getType(peer),
119 + ' ',
120 + //TODO: show nicer details, with labels. etc.
121 + peer.ping ? duration(peer.ping.rtt.mean) : '',
122 + ' ',
123 + peer.ping ? duration(peer.ping.skew.mean) : '',
49124 h('label',
50125 {title: new Date(peer.stateChange).toString()},
51126 peer.stateChange && ('(' + human(new Date(peer.stateChange))) + ')')
52127 ),
53128 'source:'+peer.source,
54- h('pre', legacyToMultiServer(peer))
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 + }})
55136 )
56137 )
57138 })
58139
@@ -75,4 +156,10 @@
75156
76157
77158
78159
160 +
161 +
162 +
163 +
164 +
165 +
sbot-api.jsView
@@ -125,8 +125,12 @@
125125 }),
126126 sbot_gossip_peers: rec.async(function (cb) {
127127 sbot.gossip.peers(cb)
128128 }),
129 + //liteclient won't have permissions for this
130 + sbot_gossip_connect: rec.async(function (opts, cb) {
131 + sbot.gossip.connect(opts, cb)
132 + }),
129133 sbot_publish: rec.async(function (content, cb) {
130134 if(content.recps)
131135 content = ssbKeys.box(content, content.recps.map(function (e) {
132136 return ref.isFeed(e) ? e : e.link
@@ -158,4 +162,6 @@
158162
159163
160164
161165
166 +
167 +

Built with git-ssb-web