From 3380cf03483887422948594f01d963b9770ec11f Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 7 Apr 2017 10:09:59 +0200 Subject: [PATCH] implement getTotalSize rpc --- rpc.js | 47 ++++++++++++++++++++++++++++++++++---------- www/common/pinpad.js | 34 ++++---------------------------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/rpc.js b/rpc.js index 705f07f8c..8db672868 100644 --- a/rpc.js +++ b/rpc.js @@ -178,6 +178,38 @@ var getChannelList = function (store, publicKey, cb) { }); }; +var getFileSize = function (store, channel, cb) { + if (!isValidChannel(channel)) { + console.log(channel); + Trace('INVALID_CHAN'); + return void cb('INVALID_CHAN'); + } + + return void store.getChannelSize(channel, function (e, size) { + if (e) { return void cb(e.code); } + cb(void 0, size); + }); +}; + +var getTotalSize = function (pinStore, messageStore, publicKey, cb) { + var bytes = 0; + + return void getChannelList(pinStore, publicKey, function (channels) { + if (!channels) { cb('NO_ARRAY'); } // unexpected + + var count = channels.length; + if (!count) { cb(void 0, 0); } + + channels.forEach(function (channel) { + return messageStore.getChannelSize(channel, function (e, size) { + count--; + if (!e) { bytes += size; } + if (count === 0) { return cb(void 0, bytes); } + }); + }); + }); +}; + var hashChannelList = function (A) { var uniques = []; @@ -305,22 +337,17 @@ RPC.create = function (config, cb) { return unpinChannel(store, safeKey, msg[1], function (e) { Respond(e); }); - case 'GET_HASH': return void getHash(store, safeKey, function (hash) { Respond(void 0, hash); }); case 'GET_TOTAL_SIZE': - return void Respond('NOT_IMPLEMENTED', msg); - case 'GET_FILE_SIZE': - if (!isValidChannel(msg[1])) { - return void Respond('INVALID_CHAN'); - } - - return void ctx.store.getChannelSize(msg[1], function (e, size) { - if (e) { return void Respond(e.code); } - Respond(void 0, size); + return getTotalSize(store, ctx.store, safeKey, function (e, size) { + if (e) { return void Respond(e); } + Respond(e, size); }); + case 'GET_FILE_SIZE': + return void getFileSize(ctx.store, msg[1], Respond); default: return void Respond('UNSUPPORTED_RPC_CALL', msg); } diff --git a/www/common/pinpad.js b/www/common/pinpad.js index f2f6bd04a..92721c11d 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -28,34 +28,8 @@ define([ rpc.send('GET_FILE_SIZE', file, cb); }; - var getFileListSize = function (rpc, list, cb) { - var bytes = 0; - - var left = list.length; - - list.forEach(function (chan) { - getFileSize(rpc, chan, function (e, msg) { - if (e) { - if (e === 'ENOENT') { - - // these channels no longer exists on the server - console.log(e, chan); - } else { - console.error(e); - } - } else if (msg && msg[0] && typeof(msg[0]) === 'number') { - bytes += msg[0]; - //console.log(bytes); - } else { - console.log("returned message was not a number: ", msg); - } - - --left; - if (left === 0) { - cb(void 0, bytes); - } - }); - }); + var getFileListSize = function (rpc, cb) { + return rpc.send('GET_TOTAL_SIZE', undefined, cb); }; var pinChannel = function (rpc, channel, cb) { @@ -108,8 +82,8 @@ define([ exp.getFileSize = function (file, cb) { getFileSize(rpc, file, cb); }; - exp.getFileListSize = function (list, cb) { - getFileListSize(rpc, list, cb); + exp.getFileListSize = function (cb) { + getFileListSize(rpc, cb); }; exp.getServerHash = function (cb) { getServerHash(rpc, edPublic, cb);