Commit d23b1c17f63476dccc32276fd2ec92b6475c8e46
make gossip's status a little more organized
Dominic Tarr committed on 6/24/2017, 6:10:17 AMParent: 3dc0b01f3766ffabfa08fc64a4a08aca20c64867
Files changed
plugins/gossip/index.js | changed |
plugins/gossip/index.js | ||
---|---|---|
@@ -21,8 +21,31 @@ | ||
21 | 21 … | function stringify(peer) { |
22 | 22 … | return [peer.host, peer.port, peer.key].join(':') |
23 | 23 … | } |
24 | 24 … | |
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 … | + | |
25 | 48 … | /* |
26 | 49 … | Peers : [{ |
27 | 50 … | key: id, |
28 | 51 … | host: ip, |
@@ -59,16 +82,32 @@ | ||
59 | 82 … | return e && e.key === id |
60 | 83 … | }) |
61 | 84 … | } |
62 | 85 … | |
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 … | + | |
63 | 101 … | server.status.hook(function (fn) { |
64 | 102 … | var _status = fn() |
65 | 103 … | _status.gossip = status |
66 | 104 … | 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) | |
69 | 107 … | }) |
70 | 108 … | return _status |
109 … | + | |
71 | 110 … | }) |
72 | 111 … | |
73 | 112 … | server.close.hook(function (fn, args) { |
74 | 113 … | closed = true |
@@ -110,9 +149,9 @@ | ||
110 | 149 … | if(!p) return cb() |
111 | 150 … | |
112 | 151 … | p.stateChange = Date.now() |
113 | 152 … | p.state = 'connecting' |
114 | - server.connect(p, function (err, rpc) { | |
153 … | + server.connect(coearseAddress(p), function (err, rpc) { | |
115 | 154 … | if (err) { |
116 | 155 … | p.error = err.stack |
117 | 156 … | p.state = undefined |
118 | 157 … | p.failure = (p.failure || 0) + 1 |
@@ -205,9 +244,8 @@ | ||
205 | 244 … | //get current state |
206 | 245 … | |
207 | 246 … | server.on('rpc:connect', function (rpc, isClient) { |
208 | 247 … | var peer = getPeer(rpc.id) |
209 | - status[rpc.id] = peer | |
210 | 248 … | //don't track clients that connect, but arn't considered peers. |
211 | 249 … | //maybe we should though? |
212 | 250 … | if(!peer) { |
213 | 251 … | if(rpc.id !== server.id) { |
@@ -218,8 +256,10 @@ | ||
218 | 256 … | } |
219 | 257 … | return |
220 | 258 … | } |
221 | 259 … | |
260 … | + status[rpc.id] = simplify(peer) | |
261 … | + | |
222 | 262 … | console.log('Connected', stringify(peer)) |
223 | 263 … | //means that we have created this connection, not received it. |
224 | 264 … | peer.client = !!isClient |
225 | 265 … | peer.state = 'connected' |
@@ -250,9 +290,8 @@ | ||
250 | 290 … | var since = peer.stateChange |
251 | 291 … | peer.stateChange = Date.now() |
252 | 292 … | // if(peer.state === 'connected') //may be "disconnecting" |
253 | 293 … | peer.duration = stats(peer.duration, peer.stateChange - since) |
254 | -// console.log(peer.duration) | |
255 | 294 … | peer.state = undefined |
256 | 295 … | notify({ type: 'disconnect', peer: peer }) |
257 | 296 … | server.emit('log:info', ['SBOT', rpc.id, 'disconnect']) |
258 | 297 … | }) |
@@ -293,7 +332,4 @@ | ||
293 | 332 … | } |
294 | 333 … | } |
295 | 334 … | |
296 | 335 … | |
297 | - | |
298 | - | |
299 | - |
Built with git-ssb-web