Skip to content

Commit

Permalink
AXE - peer/soul subscribe. In this version the peers receive only dat…
Browse files Browse the repository at this point in the history
…a 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.
  • Loading branch information
rogowski committed Nov 21, 2018
1 parent 688c9a8 commit 680b2d1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
31 changes: 25 additions & 6 deletions axe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -77,4 +96,4 @@
module.exports = AXE;
})(USE, './axe');

}());
}());
40 changes: 40 additions & 0 deletions examples/axe.html
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 5 additions & 1 deletion examples/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 1 deletion gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -2210,4 +2211,4 @@
var noop = function(){};
})(USE, './adapters/websocket');

}());
}());
10 changes: 9 additions & 1 deletion lib/super.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){}
}());
}());
3 changes: 1 addition & 2 deletions src/adapters/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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){}


0 comments on commit 680b2d1

Please sign in to comment.