Browse Source

Merge pull request #32 from pbca26/master

added shepherd cache-one route (wip)
all-modes
Satinder Grewal 8 years ago
committed by GitHub
parent
commit
35660dc79a
  1. 143
      routes/shepherd.js

143
routes/shepherd.js

@ -131,7 +131,7 @@ var allcoinsInProgress = false;
/* /*
* params: userpass, pubkey * params: userpass, pubkey
*/ */
shepherd.get('/allcoins', function(req, res, next) { shepherd.get('/cache-all', function(req, res, next) {
if (!allcoinsInProgress) { if (!allcoinsInProgress) {
allcoinsInProgress = true; allcoinsInProgress = true;
@ -171,7 +171,7 @@ shepherd.get('/allcoins', function(req, res, next) {
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
console.log('allcoins call started'); console.log('cache-all call started');
request({ request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey,
@ -207,7 +207,7 @@ shepherd.get('/allcoins', function(req, res, next) {
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
}; };
if (coin === 'BTC' && coin === 'SYS') { if (coin === 'BTC' || coin === 'SYS') {
delete dexUrls.refresh; delete dexUrls.refresh;
delete dexUrls.getbalance; delete dexUrls.getbalance;
} }
@ -257,11 +257,14 @@ shepherd.get('/allcoins', function(req, res, next) {
/* /*
* params: userpass, pubkey, coin, address * params: userpass, pubkey, coin, address
*/ */
shepherd.get('/refresh', function(req, res, next) { shepherd.get('/cache-one', function(req, res, next) {
if (!allcoinsInProgress) {
// TODO: add check to allow only one cache call/sequence in progress
var sessionKey = req.query.userpass, var sessionKey = req.query.userpass,
coin = req.query.coin, coin = req.query.coin,
address = req.query.address, address = req.query.address,
pubkey = req.query.pubkey, pubkey = req.query.pubkey,
callsArray = req.query.calls.split(':'),
errorObj = { errorObj = {
'msg': 'error', 'msg': 'error',
'result': 'error' 'result': 'error'
@ -278,24 +281,125 @@ shepherd.get('/refresh', function(req, res, next) {
}); });
}; };
console.log(callsArray);
res.end(JSON.stringify({
'msg': 'success',
'result': 'call is initiated'
}));
console.log('cache-one call started');
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) { if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) {
outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8')); outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8'));
if (outObj && !outObj.basilisk) { if (!outObj || !outObj.basilisk) {
outObj['basilisk'] = {}; outObj['basilisk'] = {};
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} else { } else {
if (!outObj[coin]) { if (!outObj[coin]) {
outObj['basilisk'][coin][address] = {}; outObj['basilisk'][coin] = {};
}
}
}
// update all available coin addresses
if (!address) {
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin].addresses = JSON.parse(body).result;
writeCache();
//callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3);
//console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
} }
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
outObj.basilisk[coin][address] = {};
writeCache();
async.forEachOf(_dexUrls, function(dexUrl, key) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
/*callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/
writeCache();
} }
});
});
});
} else { } else {
outObj = { // TODO: error
basilisk: {}
};
} }
});
} else {
var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
console.log(_dexUrls);
async.forEachOf(_dexUrls, function(dexUrl, key) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
/*callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/
writeCache();
}
});
});
/*var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
request({ request({
url: refreshUrl, url: refreshUrl,
@ -312,7 +416,14 @@ shepherd.get('/refresh', function(req, res, next) {
'result': iguanaDir + '/cache-' + pubkey + '.json updated' 'result': iguanaDir + '/cache-' + pubkey + '.json updated'
})); }));
} }
}); });*/
}
} else {
res.end(JSON.stringify({
'msg': 'error',
'result': 'another call is in progress already'
}));
}
}); });
shepherd.post('/debuglog', function(req, res) { shepherd.post('/debuglog', function(req, res) {
@ -482,15 +593,17 @@ shepherd.readDebugLog = function(fileLocation, lastNLines) {
return new Promise( return new Promise(
function(resolve, reject) { function(resolve, reject) {
if (lastNLines) { if (lastNLines) {
if (fs.existsSync(fileLocation)) { fs.access(fileLocation, fs.constants.R_OK, function(err) {
if (err) {
console.log('error reading ' + fileLocation);
reject('readDebugLog error: ' + err);
} else {
console.log('reading ' + fileLocation); console.log('reading ' + fileLocation);
readLastLines readLastLines
.read(fileLocation, lastNLines) .read(fileLocation, lastNLines)
.then((lines) => resolve(lines)); .then((lines) => resolve(lines));
} else {
reject('file ' + fileLocation + ' doesn\'t exist!');
} }
});
} else { } else {
reject('readDebugLog error: lastNLines param is not provided!'); reject('readDebugLog error: lastNLines param is not provided!');
} }

Loading…
Cancel
Save