axe put routes

This commit is contained in:
Mark 2019-06-12 01:18:54 -07:00
parent 86043aa89e
commit dc42f63204
4 changed files with 35 additions and 25 deletions

45
axe.js
View File

@ -59,13 +59,11 @@
var puts = Object.keys(msg.put); var puts = Object.keys(msg.put);
var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls? var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls?
var subs = dht(soul); var subs = dht(soul);
// console.log('[AXE] VERIFY soul: %s, subs: %s, Peers: %s, msg: ', soul, subs, Object.keys(peers), msg);
if (!subs) { return; } if (!subs) { return; }
var tmp = []; var tmp = [];
Gun.obj.map(subs.split(','), function(pid) { Gun.obj.map(subs.split(','), function(pid) {
if (pid in peers) { if (pid in peers) {
tmp.push(pid); tmp.push(pid);
// console.log('[AXE] SEND TO >>>>> ', pid, msg.put.bob || msg.put);
mesh.say(msg, peers[pid]); mesh.say(msg, peers[pid]);
} }
}); });
@ -74,27 +72,25 @@
dht(soul, tmp.join(',')); dht(soul, tmp.join(','));
} }
} }
function route(get){ var tmp;
if(!get){ return }
if('string' != typeof (tmp = get['#'])){ return }
return tmp;
}
var Rad = (Gun.window||{}).Radix || USE('./lib/radix', 1); var Rad = (Gun.window||{}).Radix || USE('./lib/radix', 1);
at.opt.dht = Rad(); at.opt.dht = Rad();
at.on('in', input/*USE('./lib/super', 1)*/, at); at.on('in', function input(msg){
// at.on('out', function(msg, a) {
// this.to.next(msg);
// console.log('[AXE] out:', msg, a);
// }, at);
function input(msg){
var to = this.to, peer = (msg._||{}).via; var to = this.to, peer = (msg._||{}).via;
var dht = opt.dht; var dht = opt.dht;
var routes = axe.routes || (axe.routes = {}); // USE RAD INSTEAD! TMP TESTING! var routes = axe.routes || (axe.routes = {}); // USE RAD INSTEAD! TMP TESTING!
var get = msg.get, hash, tmp; var get = msg.get, hash, tmp;
if(get && opt.super && peer){ //if(get && opt.super && peer){
hash = Gun.obj.hash(get); // USE RAD INSTEAD! if(get && opt.super && peer && (tmp = route(get))){
hash = tmp; //Gun.obj.hash(get); // USE RAD INSTEAD!
(routes[hash] || (routes[hash] = {}))[peer.id] = peer; (routes[hash] || (routes[hash] = {}))[peer.id] = peer;
(peer.routes || (peer.routes = {}))[hash] = routes[hash]; (peer.routes || (peer.routes = {}))[hash] = routes[hash];
/*if(soul = get['#']){ // SWITCH BACK TO USING DHT! /*if(soul = get['#']){ // SWITCH BACK TO USING DHT!
if(key = get['.']){ if(key = get['.']){
@ -111,7 +107,7 @@
}*/ }*/
} }
if((tmp = msg['@']) && (tmp = at.dup.s[tmp]) && (tmp = tmp.it)){ if((tmp = msg['@']) && (tmp = at.dup.s[tmp]) && (tmp = tmp.it)){
(tmp = (tmp._||ok)).ack = (tmp.ack || 0) + 1; (tmp = (tmp._||ok)).ack = (tmp.ack || 0) + 1; // count remote ACKs to GET.
} }
to.next(msg); to.next(msg);
@ -126,13 +122,12 @@
}); });
}); });
} }
} });
if(at.opt.super){ if(at.opt.super){
var rotate = 0; var rotate = 0;
mesh.way = function(msg) { mesh.way = function(msg) {
if (msg.rtc) { if (msg.rtc) {
// console.log('[AXE] MSG WEBRTC: ', msg.rtc);
if (msg.rtc.to) { if (msg.rtc.to) {
/// Send announce to one peer only if the msg have 'to' attr /// Send announce to one peer only if the msg have 'to' attr
var peer = (peers) ? peers[msg.rtc.to] : null; var peer = (peers) ? peers[msg.rtc.to] : null;
@ -140,8 +135,8 @@
return; return;
} }
} }
if(msg.get){ if(msg.get && (tmp = route(msg.get))){
var hash = Gun.obj.hash(msg.get); var hash = tmp; //Gun.obj.hash(msg.get);
var routes = axe.routes || (axe.routes = {}); // USE RAD INSTEAD! TMP TESTING! var routes = axe.routes || (axe.routes = {}); // USE RAD INSTEAD! TMP TESTING!
var peers = routes[hash]; var peers = routes[hash];
function chat(peers, old){ // what about optimizing for directed peers? function chat(peers, old){ // what about optimizing for directed peers?
@ -171,6 +166,18 @@
return chat(peers); return chat(peers);
} }
// TODO: PUTs need to only go to subs! // TODO: PUTs need to only go to subs!
if(msg.put){
var routes = axe.routes || (axe.routes = {}); // USE RAD INSTEAD! TMP TESTING!
var peers = {};
Gun.obj.map(msg.put, function(node, soul){
var hash = soul; //Gun.obj.hash({'#': soul});
var to = routes[hash];
if(!to){ return }
Gun.obj.to(to, peers);
});
mesh.say(msg, peers);
return;
}
mesh.say(msg, opt.peers); return; // TODO: DISABLE THIS!!! USE DHT! mesh.say(msg, opt.peers); return; // TODO: DISABLE THIS!!! USE DHT!
@ -181,14 +188,12 @@
} else { } else {
mesh.route = function(msg) { mesh.route = function(msg) {
if (msg.rtc) { if (msg.rtc) {
// console.log('[AXE] MSG WEBRTC: ', msg.rtc);
} }
if (!msg.put) { mesh.say(msg); return; } if (!msg.put) { mesh.say(msg); return; }
verify(opt.dht, msg); verify(opt.dht, msg);
/// Always send to superpeers? /// Always send to superpeers?
Gun.obj.map(peers, function(peer) { Gun.obj.map(peers, function(peer) {
if (peer.url) { if (peer.url) {
// console.log('SEND TO SUPERPEER', msg);
mesh.say(msg, peer); mesh.say(msg, peer);
} }
}); });

8
gun.js
View File

@ -2230,12 +2230,16 @@
return wire; return wire;
}catch(e){}} }catch(e){}}
var wait = 2 * 1000;
function reconnect(peer){ function reconnect(peer){
clearTimeout(peer.defer); clearTimeout(peer.defer);
peer.defer = setTimeout(function(){ if(doc && peer.retry <= 0){ return } peer.retry = (peer.retry || opt.retry || 60) - 1;
peer.defer = setTimeout(function to(){
if(doc && doc.hidden){ return setTimeout(to,wait) }
open(peer); open(peer);
}, 2 * 1000); }, wait);
} }
var doc = 'undefined' !== typeof document && document;
}); });
var noop = function(){}; var noop = function(){};
})(USE, './adapters/websocket'); })(USE, './adapters/websocket');

View File

@ -231,8 +231,9 @@ $(function(){
}); });
tmp += '}\n'; tmp += '}\n';
}); });
(node = document.createElement('style')).innerHTML = tmp; var tag = document.createElement('style');
document.body.appendChild(node); tag.innerHTML = tmp;
document.body.appendChild(tag);
} }
}catch(e){}}()); }catch(e){}}());
;(function(){ ;(function(){

View File

@ -1,6 +1,6 @@
{ {
"name": "gun", "name": "gun",
"version": "0.2019.515", "version": "0.2019.612",
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
"main": "index.js", "main": "index.js",
"browser": "gun.js", "browser": "gun.js",