AXE - peer/soul subscribe. In this version the peers receive only data subscribed with gun.get(...soul...).once(...)

TODO:
 1) if the peer change the id, will lost the subscribes. Workaround for this is set the pid (unique) in: Gun(pid: 'mypeerid', peers:[...])
 2) If the superpeer die, the subscribes will be lost. Each peer must resubscribe all souls.
This commit is contained in:
Adriano Rogowski 2018-11-21 19:28:28 -02:00
parent 688c9a84f9
commit 680b2d115a
6 changed files with 82 additions and 11 deletions

31
axe.js
View File

@ -47,11 +47,11 @@
// 1. If any remembered peers or from last cache or extension
// 2. Fallback to use hard coded peers from dApp
// 3. Or any offered peers.
if(Gun.obj.empty(p)){
Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){
p[url] = {url: url, axe: {}};
});
}
//if(Gun.obj.empty(p)){
// Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){
// p[url] = {url: url, axe: {}};
// });
//}
// Our current hypothesis is that it is most optimal
// to take peers in a common network, and align
// them in a line, where you only have left and right
@ -61,6 +61,25 @@
// in case the p2p linear latency is high.
// Or there could be plenty of other better options.
console.log("axe", at.opt);
if(at.opt.super){
function verify(msg, send, at) {
var peers = Object.keys(p), puts = Object.keys(msg.put), i, j, peer;
var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls?
for (i=0; i < peers.length; ++i) {
peer = p[peers[i]];
//if (peer.url) {console.log('AXE do not reject superpeers'); send(msg, peer); continue;} /// always send to superpeers?
if (!peer.id) {console.log('AXE peer without id: ', peer); continue;}
if (!Gun.subscribe[soul] || !Gun.subscribe[soul][peer.id]) { console.log('AXE SAY reject msg to peer: %s, soul: %s', peer.id, soul); continue; }
send(msg, peer);
}
}
AXE.say = function(msg, send, at) {
if (!msg.put) { send(msg); return; }
console.log('AXE HOOK!! ', msg);
verify(msg, send, at);
};
/// TODO: remove peer from all Gun.subscribe. On `mesh.bye` event?
}
if(at.opt.super){
at.on('in', USE('./lib/super', 1), at);
} else {
@ -77,4 +96,4 @@
module.exports = AXE;
})(USE, './axe');
}());
}());

40
examples/axe.html Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>Testing AXE</title>
</head>
<body>
<script src="../gun.js"></script>
<!-- <script src="../axe.js"></script> -->
<!-- <script src="../sea.js"></script> -->
<script>
var pid = location.hash.slice(1);
var opt = ({
peers: [`${location.origin}/gun`]
});
if (pid) { opt.pid = pid; }
Gun.on('opt', function(ctx) {
this.to.next(ctx);
ctx.on('hi', function(opt) {
console.log('HI!! PEER', new Date(), opt.pid);
});
if (pid) {
ctx.on('out', function(msg) {
msg.pid = pid;
this.to.next(msg);
});
}
});
var gun = Gun(opt);
//var user = gun.user();
</script>
</body>
</html>

View File

@ -2,12 +2,16 @@ console.log("If module not found, install express globally `npm i express -g`!")
var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
var express = require('express');
var Gun = require('..');
require('../axe');
var app = express();
app.use(Gun.serve);
app.use(express.static(__dirname));
var server = app.listen(port);
Gun({ file: 'data.json', web: server });
var gun = Gun({ file: 'data', web: server });
global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun; /// make global to `node --inspect` - debug only
console.log('Server started on port ' + port + ' with /gun');

3
gun.js
View File

@ -1941,6 +1941,7 @@
return;
}
// add hook for AXE?
if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; }
mesh.say(msg);
}
@ -2210,4 +2211,4 @@
var noop = function(){};
})(USE, './adapters/websocket');
}());
}());

View File

@ -12,10 +12,18 @@
} else {
}
subscribe(soul, peer, msg);
}
to.next(msg);
}
/// Store the subscribes
Gun.subscribe = {}; /// TODO: use Rad instead of plain object?
function subscribe(soul, peer, msg) {
if (!peer.id) { console.log('super jump peer without id: ', peer, msg); return; } /// TODO: this occurs in first subscription. Use peer reference or peer.wire.id?
Gun.subscribe[soul] = Gun.subscribe[soul] || {};
Gun.subscribe[soul][peer.id] = 1;
}
var empty = {}, u;
if(Gun.window){ return }
try{module.exports = input}catch(e){}
}());
}());

View File

@ -20,6 +20,7 @@ function Mesh(ctx){
return;
}
// add hook for AXE?
if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; }
mesh.say(msg);
}
@ -228,5 +229,3 @@ Mesh.hash = function(s){ // via SO
Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
try{ module.exports = Mesh }catch(e){}