Browse Source

reuse the same mechanism that limits parallel dhtgets also for decodeshorturl

readme-update
Miguel Freitas 9 years ago
parent
commit
f0d80e25c0
  1. 19
      js/interface_common.js
  2. 53
      js/twister_io.js

19
js/interface_common.js

@ -832,16 +832,17 @@ function fetchShortenedURI(req) {
console.warn('can\'t fetch URI "' + req + '": daemon is obsolete, version 0.9.35 or higher is required'); console.warn('can\'t fetch URI "' + req + '": daemon is obsolete, version 0.9.35 or higher is required');
return; return;
} }
twisterRpc('decodeshorturl', [req],
decodeshorturl( req,
function (req, ret) { function (req, ret) {
twister.URIs[req] = ret; if( ret ) {
$.localStorage.set('twistaURIs', twister.URIs); twister.URIs[req] = ret;
applyShortenedURI(req, ret); $.localStorage.set('twistaURIs', twister.URIs);
}, req, applyShortenedURI(req, ret);
function (req, ret) { } else {
console.warn('can\'t fetch URI "' + req + '": ' + ret.message); console.warn('can\'t fetch URI "' + req + '": ' + ret.message);
}, req }
); }, req);
} }
function applyShortenedURI(short, long) { function applyShortenedURI(short, long) {

53
js/twister_io.js

@ -55,7 +55,10 @@ function _dhtgetProcessPending(locator, multi, ret)
var cbFunc = _dhtgetPendingMap[locator][i].cbFunc; var cbFunc = _dhtgetPendingMap[locator][i].cbFunc;
var cbArg = _dhtgetPendingMap[locator][i].cbArg; var cbArg = _dhtgetPendingMap[locator][i].cbArg;
if( multi == 's' ) { if( multi == 'url' ) {
// here is decodeshorturl case
cbFunc(cbArg, ret);
} else if( multi == 's' ) {
if( ret[0] != undefined ) { if( ret[0] != undefined ) {
cbFunc(cbArg, ret[0]["p"]["v"], ret); cbFunc(cbArg, ret[0]["p"]["v"], ret);
} else { } else {
@ -110,6 +113,25 @@ function dhtget( username, resource, multi, cbFunc, cbArg, timeoutArgs ) {
} }
} }
// decode shortened url
// the expanded url is returned to callback
// null is passed to callback in case of an error
function decodeshorturl( locator, cbFunc, cbArg, timeoutArgs ) {
if( locator in _dhtgetPendingMap) {
_dhtgetAddPending(locator, cbFunc, cbArg);
} else {
_dhtgetAddPending(locator, cbFunc, cbArg);
// limit the number of simultaneous decodeshorturl's and dhtgets.
// this should leave some sockets for other non-blocking daemon requests.
if( _dhtgetsInProgress < _maxDhtgets ) {
_decodeshorturlInternal( locator, timeoutArgs );
} else {
// just queue the locator. it will be unqueue when some dhtget completes.
_queuedDhtgets.push(locator);
}
}
}
function _dhtgetInternal( username, resource, multi, timeoutArgs ) { function _dhtgetInternal( username, resource, multi, timeoutArgs ) {
var locator = _dhtgetLocator(username, resource, multi); var locator = _dhtgetLocator(username, resource, multi);
_dhtgetsInProgress++; _dhtgetsInProgress++;
@ -131,10 +153,35 @@ function _dhtgetInternal( username, resource, multi, timeoutArgs ) {
}, locator); }, locator);
} }
function _decodeshorturlInternal( locator, timeoutArgs ) {
_dhtgetsInProgress++;
argsList = [locator];
if( typeof timeoutArgs !== 'undefined' ) {
argsList = argsList.concat(timeoutArgs);
}
twisterRpc("decodeshorturl", argsList,
function(args, ret) {
_dhtgetsInProgress--;
_dhtgetProcessPending(args.locator, "url", ret);
_dhtgetDequeue();
}, {locator:locator},
function(cbArg, ret) {
console.log("ajax error:" + ret);
_dhtgetsInProgress--;
_dhtgetAbortPending(locator);
_dhtgetDequeue();
}, locator);
}
function _dhtgetDequeue() { function _dhtgetDequeue() {
if( _queuedDhtgets.length ) { if( _queuedDhtgets.length ) {
var locatorSplit = _queuedDhtgets.pop().split(";"); var locator = _queuedDhtgets.pop();
_dhtgetInternal(locatorSplit[0], locatorSplit[1], locatorSplit[2]); var locatorSplit = locator.split(";");
if( locatorSplit.length == 3) {
_dhtgetInternal(locatorSplit[0], locatorSplit[1], locatorSplit[2]);
} else {
_decodeshorturlInternal( locator )
}
} }
} }

Loading…
Cancel
Save