Permalink
Browse files

[api] Added basic support for Hnet.set

  • Loading branch information...
1 parent 9e6efc0 commit 86036583a9d77676c407f085ed895b04547ac400 @Marak Marak committed Jan 1, 2012
Showing with 124 additions and 43 deletions.
  1. +19 −25 README.md
  2. +25 −0 example/save-pulled-hnet-data.js
  3. +75 −11 lib/hnet.js
  4. +5 −7 lib/hnet/engines/couchdb.js
View
@@ -1,32 +1,24 @@
-# hnet - a distributed, decentralized, and somewhat anonymous database
+# hnet - an experimental decentralized and anonymous database
-## hnet spreads small amounts of data across several engines
+## hnet spreads small amounts of data across several non-traditional storage engines such as images, gists, pastebin, twitter streams, irc chat rooms, etc...
-It's good for distributing things like ip routing tables
+### hnet is ideal for distributing small amounts of state anonymously. By design, it is not reliable, fast, or consistent.
-## Usage
+### An ideal use-case for hnet would be storing sets of IP addresses and ports for servers.
-var hnet = require('hnet');
+# How does it work?
-//
-// Will start lazy loading sources
-//
-hnet.load(['http://hnet.iriscouch.com/public/0', 'https://gist.github.com/01889d7e9e8cc35375c8', 'http://hnet.iriscouch.com/public/1']);
+*hnet uses couchiris at top*
-hnet.on('load', function(set){
- console.log('just load data set from ', set);
-});
+*img of db layout*
-hnet.on('ready', function(){
- console.log('all data sets have been load');
- console.log(hnet.get());
-});
+*another img of client getting data*
-//
-// Get will always work, regardless of how many sets are load.
-// If no data sets have been load, get will just not return any data
-//
-console.log(hnet.get());
+*description of data format*
+
+*basic linking*
+
+*basic circular dep linking*
## hnet protocol
@@ -77,11 +69,13 @@ console.log(hnet.get());
# TODO:
-Add additional engines for:
-
- - PasteBin
+* Finish pluggable crypto system
+* Create basic implementation for `Hnet.set`
+* Add additional engines for:
- Image Stenography
+ - PasteBin
- Reddit
- Imgur
- Hacker News deadlink jail
-
+ - Twitter
+ - IRC
@@ -0,0 +1,25 @@
+var Hnet = require('../lib/hnet').Hnet;
+
+var hnet = new Hnet();
+
+
+hnet.on('node', function (node, data, options) {
+ //console.log('level::' + options.currentDepth + '::' + node.uri);
+});
+
+hnet.on('level::*', function(nodes, options){
+ //console.log('level::' + this.event.split('::')[1] + '::loaded');
+ //console.log(JSON.stringify(hnet.get(), true, 2));
+});
+
+hnet.on('level::0', function(nodes, options){
+ //console.log(hnet.nodes);
+ hnet.save(function(err, response, body){
+
+ //console.log(err, body);
+
+ });
+});
+
+hnet.load(['http://hnet.iriscouch.com/public/0']);
+
View
@@ -36,6 +36,13 @@ var Hnet = exports.Hnet = function (options) {
//
+ // TODO: upgrade EE2 to version that has "**"
+ //
+ self.on('*::*', function(data){
+ console.log('debug:', this.event, data);
+ });
+
+ //
// Select a default crypto engine
//
// self.encrypt = self.cyphers['CaesarZalgo'].encrypt;
@@ -136,9 +143,9 @@ Hnet.prototype.parseDocument = function (node, doc, options) {
}
});
}
-
-
- self.store.push(doc);
+ doc.forEach(function(record){
+ self.store.push(record);
+ });
}
//
@@ -148,6 +155,8 @@ Hnet.prototype.load = function(nodes, options, cb){
var self = this;
+ console.log('loading data now...', nodes)
+
//
// If the incoming node is just a string,
// coherse it into a one element array
@@ -249,20 +258,75 @@ Hnet.prototype.get = function(){
};
-Hnet.prototype.set = function(node, cb){
+Hnet.prototype.save = function(cb){
+
+ var self = this,
+ dataSet,
+ _keys = Object.keys(self.nodes),
+ backLinks = [],
+ middle,
+ last;
- var self = this;
/*
if(self.encrypt) {
data = self.encrypt(data);
}
*/
+
+ //
+ // Remark: If we have no nodes at all, we'll just create a new one using hnet.iriscouch.com
+ //
+ if (_keys.length === 0) {
+ console.log('no known nodes, using default');
+ backLinks.push({uri: 'http://hnet.iriscouch.com/public/0', type: 'couchdb'});
+ } else if (_keys.length === 1) {
+ //
+ // Remark: Pick 1 top-level node, and 2 other "random" nodes to link back to.
+ //
+ backLinks.push(self.nodes[_keys[0]]);
+ } else if (_keys.length >= 3) {
+ middle = self.nodes[_keys[_keys.length / 2]];
+ last = self.nodes[_keys[_keys.length - 1]];
+ //
+ // Remark: Instead of random, let's try the middle and last element instead
+ //
+ backLinks.push(_link(middle));
+ backLinks.push(_link(last));
+
+ }
- //
- // Pick 1 top-level node, and 2 other random nodes to link back to.
- //
- if(self.engines[node[0].type].set) {
- self.engines[node[0].type].set(node[0], cb);
- }
+ dataSet = self.store;
+ backLinks.forEach(function(savePoint){
+ dataSet.push(savePoint);
+ });
+ //
+ // Pick two "random" engines
+ //
+ var _engines = ['couchdb', 'gist'];
+ _engines.forEach(function(engine){
+ if (self.engines[engine].save) {
+ self.engines[engine].save(dataSet, function(err, response, body) {
+ if (err) {
+ return cb(err);
+ }
+ //
+ // If we have be given an id back from the Couch
+ //
+ if (body.id) {
+ self.nodes['http://hnet.iriscouch.com/public/' + body.id];
+ }
+ self.emit('node::added', 'http://hnet.iriscouch.com/public/' + body.id)
+ cb(null, body);
+ });
+ }
+ });
};
+
+
+//
+// Converts node uris into JSON-RPC link commands
+//
+function _link(node) {
+ return { "method": "link", "params": [node] };
+}
@@ -22,21 +22,19 @@ couch.load = function (options, cb) {
request({ uri: options.uri, method: "GET" }, cb);
};
-couch.set = function (node, cb) {
+couch.save = function (node, cb) {
var self = this;
- var parts = node.uri.split('/');
- node.data._id = node.data._id || parts[parts.length -1] || 'no-id';
- parts.pop();
- node.uri = parts.join('/');
- request({ uri: node.uri, json: node.data, method: "POST" }, function(err, response, body){
+ request({ uri: "http://hnet.iriscouch.com/public", json: true, body: JSON.stringify({"h":node}), method: "POST" }, function(err, response, body){
+ cb(err, response, body);
+ /*
if(self.nodes[node.uri]) {
self.nodes[node.uri].rev = body.rev;
} else {
self.nodes[node.uri] = {
body:rev
}
-
}
+ */
});
};

0 comments on commit 8603658

Please sign in to comment.