git ssb

4+

Dominic / scuttlebot



Commit d23b1c17f63476dccc32276fd2ec92b6475c8e46

make gossip's status a little more organized

Dominic Tarr committed on 6/24/2017, 6:10:17 AM
Parent: 3dc0b01f3766ffabfa08fc64a4a08aca20c64867

Files changed

plugins/gossip/index.jschanged
plugins/gossip/index.jsView
@@ -21,8 +21,31 @@
2121 function stringify(peer) {
2222 return [peer.host, peer.port, peer.key].join(':')
2323 }
2424
25 +function isObject (o) {
26 + return o && 'object' == typeof o
27 +}
28 +
29 +function toBase64 (s) {
30 + if(isString(s)) return s
31 + else s.toString('base64') //assume a buffer
32 +}
33 +
34 +function isString (s) {
35 + return 'string' == typeof s
36 +}
37 +
38 +function coearseAddress (address) {
39 + if(isObject(address)) {
40 + var protocol = 'net'
41 + if (address.host.endsWith(".onion"))
42 + protocol = 'onion'
43 + return [protocol, address.host, address.port].join(':') +'~'+['shs', toBase64(address.key)].join(':')
44 + }
45 + return address
46 +}
47 +
2548 /*
2649 Peers : [{
2750 key: id,
2851 host: ip,
@@ -59,16 +82,32 @@
5982 return e && e.key === id
6083 })
6184 }
6285
86 + function simplify (peer) {
87 + return {
88 + address: coearseAddress(peer),
89 + source: peer.source,
90 + state: peer.state, stateChange: peer.stateChange,
91 + failure: peer.failure,
92 + client: peer.client,
93 + stats: {
94 + duration: peer.duration || undefined,
95 + rtt: peer.ping ? peer.ping.rtt : undefined,
96 + skew: peer.ping ? peer.ping.skew : undefined,
97 + }
98 + }
99 + }
100 +
63101 server.status.hook(function (fn) {
64102 var _status = fn()
65103 _status.gossip = status
66104 peers.forEach(function (peer) {
67- if(peer.stateChange + 3e3 > Date.now())
68- status[peer.key] = peer
105 + if(peer.stateChange + 3e3 > Date.now() || peer.state === 'connected')
106 + status[peer.key] = simplify(peer)
69107 })
70108 return _status
109 +
71110 })
72111
73112 server.close.hook(function (fn, args) {
74113 closed = true
@@ -110,9 +149,9 @@
110149 if(!p) return cb()
111150
112151 p.stateChange = Date.now()
113152 p.state = 'connecting'
114- server.connect(p, function (err, rpc) {
153 + server.connect(coearseAddress(p), function (err, rpc) {
115154 if (err) {
116155 p.error = err.stack
117156 p.state = undefined
118157 p.failure = (p.failure || 0) + 1
@@ -205,9 +244,8 @@
205244 //get current state
206245
207246 server.on('rpc:connect', function (rpc, isClient) {
208247 var peer = getPeer(rpc.id)
209- status[rpc.id] = peer
210248 //don't track clients that connect, but arn't considered peers.
211249 //maybe we should though?
212250 if(!peer) {
213251 if(rpc.id !== server.id) {
@@ -218,8 +256,10 @@
218256 }
219257 return
220258 }
221259
260 + status[rpc.id] = simplify(peer)
261 +
222262 console.log('Connected', stringify(peer))
223263 //means that we have created this connection, not received it.
224264 peer.client = !!isClient
225265 peer.state = 'connected'
@@ -250,9 +290,8 @@
250290 var since = peer.stateChange
251291 peer.stateChange = Date.now()
252292 // if(peer.state === 'connected') //may be "disconnecting"
253293 peer.duration = stats(peer.duration, peer.stateChange - since)
254-// console.log(peer.duration)
255294 peer.state = undefined
256295 notify({ type: 'disconnect', peer: peer })
257296 server.emit('log:info', ['SBOT', rpc.id, 'disconnect'])
258297 })
@@ -293,7 +332,4 @@
293332 }
294333 }
295334
296335
297-
298-
299-

Built with git-ssb-web