From 23783bc8ae2db1c1594300c3b82c9b776818b99a Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 15 Jan 2016 11:23:12 -0500 Subject: [PATCH] kad.js : work on kademlia integration a bit more --- storage/kad.js | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/storage/kad.js b/storage/kad.js index 375aec1d7..4d4457193 100644 --- a/storage/kad.js +++ b/storage/kad.js @@ -10,43 +10,23 @@ module.exports.create=function(conf,cb){ transport: kad.transports.UDP, }); - var indices={}, - Channel={}; - - var makeChannel=function(cName){ - Channel[cName]={ - lastModified:0, - messages:[], - }; - }, - makeIndex=function(cName){ - indices[cName]=-1; - }, - loadIndex=function(cName,out){ - indices[cName]=parseInt(out); - typeof indices[cName] !== 'number' && - console.error('FOUND A NON-NUMERIC INDEX for channel: %s',cName); - }, - getIndex=function(cName,f){ - if(typeof indices[cName] !== 'undefined'){ - f(indices[cName]); - }else{ - dht.get(cName+'=>index',function(e,out){ - e? makeIndex(cName): loadIndex(cName,out); - f(indices[cName]); - }); - } + var getIndex=function(cName,f){ + dht.get(cName+'=>index',function(e,out){ + e && console.error(e) || f(Number(out)); + }); }; cb({ message:function(cName, content, cb){ getIndex(cName, function(index){ - var index = ++indices[cName]; + index+=1; dht.put(cName+'=>index', ''+index,function(e){ - e && console.error(e); + e && console.error("ERROR updating index (%s): %s",index,e) || + console.log("PUT SUCCESS: %s", cName+'=>index') }); dht.put(cName+'=>'+index, content, function(e){ - e && console.error(e); + e && console.error("ERROR updating value at %s: %s",cName+'=>'+index,e)|| + console.log("PUT SUCCESS: %s", cName+'=>index') cb(); }); }); @@ -55,7 +35,8 @@ module.exports.create=function(conf,cb){ getIndex(cName, function(index){ for(var i=index;i>=0;i--){ dht.get(cName+'=>'+i,function(e,out){ - if(e) return console.error(e); + if(e) return console.error("DHT GET ERROR: %s",e); + console.log("GET SUCCESS: %s", cName+'=>index') cb(out); }); }