diff --git a/axe.js b/axe.js index 4531f73a..34f77925 100644 --- a/axe.js +++ b/axe.js @@ -18,32 +18,47 @@ var opt = root.opt, peers = opt.peers; if(false === opt.axe){ return } if(!Gun.window){ return } // handled by ^ lib/axe.js + var w = Gun.window, lS = w.localStorage || opt.localStorage || {}, loc = w.location || opt.location || {}, nav = w.navigator || opt.navigator || {}; var axe = root.axe = {}, tmp, id; var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); // DAM! - tmp = peers[id = location.origin + '/gun'] = peers[id] || {}; + tmp = peers[id = loc.origin + '/gun'] = peers[id] || {}; tmp.id = tmp.url = id; tmp.retry = tmp.retry || 0; tmp = peers[id = 'http://localhost:8765/gun'] = peers[id] || {}; tmp.id = tmp.url = id; tmp.retry = tmp.retry || 0; Gun.log.once("AXE", "AXE enabled: Trying to find network via (1) local peer (2) last used peers (3) a URL parameter, and last (4) hard coded peers."); - Gun.log.once("AXEWarn", "Warning: AXE alpha became super slow & laggy, now in testing only mode!"); - var last = (localStorage||'')['peers'] || ''; if(last){ last += ' ' } - last += (location.search.split('peers=')[1]||'').split('&')[0]; + Gun.log.once("AXEWarn", "Warning: AXE is in alpha, use only for testing!"); + var last = lS.peers || ''; if(last){ last += ' ' } + last += ((loc.search||'').split('peers=')[1]||'').split('&')[0]; root.on('bye', function(peer){ this.to.next(peer); + if(!peer.url){ return } // ignore WebRTC disconnects for now. + if(!nav.onLine){ peer.retry = 1 } if(peer.retry){ return } - if(axe.fall){ delete axe.fall[peer.id || peer.url] } - (function attempt(){ - clearTimeout(peer.attempt); + if(axe.fall){ delete axe.fall[peer.url || peer.id] } + (function next(){ + if(!axe.fall){ setTimeout(next, 9); return } // not found yet var fall = Object.keys(axe.fall||''), one = fall[(Math.random()*fall.length) >> 0]; - if(axe.fall && !fall.length){ return } - if(peers[one]){ attempt(); return } - if(!one){ peer.attempt = setTimeout(attempt, 9); return } + if(!fall.length){ lS.peers = ''; return } // out of peers + if(peers[one]){ next(); return } // already choose mesh.hi(one); }()); }); + root.on('hi', function(peer){ // TEMPORARY! Try to connect all peers. + this.to.next(peer); + if(!peer.url){ return } // ignore WebRTC disconnects for now. + (function next(){ + if(!peer.wire){ return } + if(!axe.fall){ setTimeout(next, 9); return } // not found yet + var one = (next.fall = next.fall || Object.keys(axe.fall||'')).pop(); + if(!one){ return } + setTimeout(next, 99); + mesh.say({dam: 'opt', opt: {peers: one}}, peer); + }()); + }); + function found(text){ axe.fall = {}; @@ -79,9 +94,11 @@ } if(last){ found(last); return } - try{ fetch((location.search.split('axe=')[1]||'').split('&')[0] || location.axe || 'https://raw.githubusercontent.com/wiki/amark/gun/volunteer.dht.md').then(function(res){ + try{ fetch(((loc.search||'').split('axe=')[1]||'').split('&')[0] || loc.axe || 'https://raw.githubusercontent.com/wiki/amark/gun/volunteer.dht.md').then(function(res){ return res.text() - }).then(found).catch(function(){ + }).then(function(text){ + found(lS.peers = text); + }).catch(function(){ found(); // nothing })}catch(e){found()} } diff --git a/gun.js b/gun.js index 88c1c455..92e506c5 100644 --- a/gun.js +++ b/gun.js @@ -1506,7 +1506,7 @@ //Type.obj.map(peer || opt.peers, each); // in case peer is a peer list. loop = 1; var wr = meta.raw; meta.raw = raw; // quick perf hack var i = 0, p; while(i < 9 && (p = (pl||'')[i++])){ - if(!(p = ps[p])){ continue } + if(!(p = ps[p] || (peer||'')[p])){ continue } mesh.say(msg, p); } meta.raw = wr; loop = 0; @@ -1619,7 +1619,7 @@ if(peer.id){ opt.peers[peer.url || peer.id] = peer; } else { - tmp = peer.id = peer.id || String.random(9); + tmp = peer.id = peer.id || peer.url || String.random(9); mesh.say({dam: '?', pid: root.opt.pid}, opt.peers[tmp] = peer); delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self } diff --git a/lib/axe.js b/lib/axe.js index 7b543198..f2242988 100644 --- a/lib/axe.js +++ b/lib/axe.js @@ -138,32 +138,37 @@ function start(root){ axe.up[p.pid] = p; // update same to be same. return; } - }; - root.on('hi', function(peer){ - this.to.next(peer); if(!peer.url){ return } - axe.up[peer.pid || peer.id] = peer; - }); - }()); + axe.up[peer.pid] = peer; + if(axe.stay){ axe.stay() } + }; - ;(function(){ // OPT MODULE mesh.hear['opt'] = function(msg, peer){ if(msg.ok){ return } var tmp = msg.opt; if(!tmp){ return } tmp = tmp.peers; if(!tmp || 'string' != typeof tmp){ return } - if(axe.up[tmp] || 99 <= Object.keys(axe.up).length){ return } // 99 TEMPORARILY UNTIL BENCHMARKED! + if(99 <= Object.keys(axe.up).length){ return } // 99 TEMPORARILY UNTIL BENCHMARKED! mesh.hi({id: tmp, url: tmp, retry: 9}); if(peer){ mesh.say({dam: 'opt', ok: 1, '@': msg['#']}, peer) } } - setInterval(function(tmp){ - if(!(tmp = root.stats && root.stats.stay)){ return } - (tmp.axe = tmp.axe || {}).up = Object.keys(axe.up||{}); - },1000 * 60); + + axe.stay = function(){ + clearTimeout(axe.stay.to); + axe.stay.to = setTimeout(function(tmp, urls){ + if(!(tmp = root.stats && root.stats.stay)){ return } + urls = {}; Object.keys(axe.up||'').forEach(function(p){ + p = (axe.up||'')[p]; if(p.url){ urls[p.url] = {} } + }); + (tmp.axe = tmp.axe || {}).up = urls; + }, 1000 * 9);//1000 * 60); + }; setTimeout(function(tmp){ if(!(tmp = root.stats && root.stats.stay && root.stats.stay.axe)){ return } - setTimeout.each(tmp.up||[], function(url){ mesh.hear.opt({opt: {peers: url}}) }); + if(!(tmp = tmp.up)){ return } + if(!(tmp instanceof Array)){ tmp = Object.keys(tmp) } + setTimeout.each(tmp||[], function(url){ mesh.hear.opt({opt: {peers: url}}) }); },1000); }());