improvements the angular rpc system

This commit is contained in:
hamza zia 2013-03-02 12:01:07 +01:00
parent 3e89b06f17
commit 32b25f29b4
8 changed files with 72 additions and 46 deletions

View File

@ -71,7 +71,7 @@
</head>
<!-- }}} -->
<body>
<body ng-app="webui">
<!-- {{{ header -->
<div class="navbar navbar-inverse navbar-fixed-top" ng-controller="NavCtrl">
@ -151,7 +151,7 @@
class="dropdown-toggle">Settings <b class="caret"></b></a>
<ul class="dropdown-menu">
<li ng-show="false">
<li>
<a
ng-click="changeCSettings()"
href="#"><i class="icon-wrench"></i> Connection Settings</a>
@ -446,7 +446,7 @@
</tr>
<tr ng-click="download.collapsed = !download.collapsed" class="download-detail">
<td colspan="2">
<div class="more_info" collapse="download.collapsed">
<div collapse="download.collapsed">
<canvas bitfield="download.bitfield" draw="!download.collapsed" pieces="download.numPieces" class="progress chunk-canvas" width="1400" style="width: 100%; margin: 5px;" chunkbar></canvas>
<ul class="stats">
<li class="label">Status: <span class="download-status">{{download.status}}</span></li>

View File

@ -13,10 +13,12 @@ angular.module('webui.ctrls.alert', [
var obj = { msg: msg, type: type };
scope.pendingAlerts.push(obj);
/*
setTimeout(function() {
var ind = scope.pendingAlerts.indexOf(obj);
if (ind != -1) scope.removeAlert(ind);
}, 5000);
*/
scope.$digest();
});

View File

@ -66,7 +66,6 @@ angular
// if any parents is collapsable, then confirm if it isnt
if (canDraw)
draw();
};
scope.$watch(attrs.dspeed, function(val) {

View File

@ -1,18 +1,18 @@
angular.module('webui', [
var webui = angular.module('webui', [
'webui.services.utils', 'webui.services.deps', 'webui.services.base64',
'webui.services.constants', 'webui.services.rpc',
'webui.services.modals', 'webui.services.alerts',
'webui.services.settings', 'webui.services.settings.filters',
'webui.filters.bytes', 'webui.filters.path',
'webui.directives.chunkbar', 'webui.directives.dgraph', 'webui.directives.fselect',
'webui.ctrls.download', 'webui.ctrls.nav', 'webui.ctrls.modal', 'webui.ctrls.alert'
'webui.ctrls.download', 'webui.ctrls.nav', 'webui.ctrls.modal', 'webui.ctrls.alert',
// external deps
'ui.bootstrap.collapse', 'ui.bootstrap.dropdownToggle',
'ui.bootstrap.modal', 'ui.bootstrap.alert'
]);
/*
$(function() {
angular.bootstrap(document, [
// external deps
'ui.bootstrap.collapse', 'ui.bootstrap.dropdownToggle',
'ui.bootstrap.modal', 'ui.bootstrap.alert',
'webui'
])
angular.bootstrap(document, ['webui'])
});
*/

View File

@ -12,6 +12,10 @@ angular.module('webui.services.alerts', ['webui.services.deps'])
},
addAlerter: function(cb) {
alerters.push(cb);
},
// a simple function for debugging
log: function(msg) {
this.addAlert(msg, 'info');
}
};
}]);

View File

@ -1,10 +1,13 @@
angular
.module('webui.services.rpc', [
'webui.services.rpc.syscall', 'webui.services.constants', 'webui.services.alerts'
])
.factory('$rpc', ['$syscall', '$globalTimeout', '$alerts', function(syscall, time, alerts) {
.module('webui.services.rpc', [
'webui.services.rpc.syscall', 'webui.services.constants', 'webui.services.alerts'
])
.factory('$rpc', ['$syscall', '$globalTimeout', '$alerts',
function(syscall, time, alerts) {
var subscriptions = []
, configurations = [{ host: 'localhost', port: 6800 }]
, currentConf = {}
, timeout = null
, forceNextUpdate = false;
@ -14,11 +17,19 @@ angular
// (i.e. serially) so should be private
// to maintain that invariant
var update = function() {
if (!subscriptions.length)
return;
if (configurations.length)
syscall.init(configurations[0]);
clearTimeout(timeout);
timeout = null;
if (!subscriptions.length) {
timeout = setTimeout(update, time);
return;
}
if (configurations.length) {
currentConf = configurations.shift();
syscall.init(currentConf);
}
subscriptions = _.filter(subscriptions, function(e) { return !!e });
var params = _.map(subscriptions, function(s) {
@ -29,16 +40,19 @@ angular
});
clearTimeout(timeout);
timeout = null;
syscall.invoke({
name: 'system.multicall',
params: [params],
success: function(data) {
// configuration worked, save it in cookie for next time!!
//
if (configurations.length) {
// configuration worked, save it in cookie for next time and
// delete the pipelined configurations!!
alerts.log('success alas!! saving current configuration');
configurations = [];
}
_.each(data.result, function(d, i) {
var handle = subscriptions[i];
if (handle) {
@ -54,15 +68,17 @@ angular
if (forceNextUpdate) {
forceNextUpdate = false;
return update();
timeout = setTimeout(update, 0);
}
else {
timeout = setTimeout(update, time);
}
timeout = setTimeout(update, time);
},
error: function() {
// If some proposed configurations are still in the pipeline then retry
if (configurations.length) {
configurations.shift();
update();
alerts.log('trying another configuration, last one didnt connect');
timeout = setTimeout(update, 0);
}
else {
alerts.addAlert('<strong>Oh Snap!</strong> Could not connect to the aria2 server, retrying after ' + time / 1000 + ' secs', 'error');
@ -70,7 +86,11 @@ angular
}
}
});
}
};
// initiate the update loop
timeout = setTimeout(update, time);
return {
// conf can be configuration or array of configurations,
// each one will be tried one after the other till success,
@ -130,14 +150,9 @@ angular
// force the global syscall update
forceUpdate: function() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
update();
timeout = setTimeout(update, 0);
}
else if (configurations.length) update();
else {
// a batch call is already in progress,
// wait till it returns and force the next one

View File

@ -1,9 +1,10 @@
angular
.module('webui.services.rpc.sockcall', [
'webui.services.deps', 'webui.services.utils', 'webui.services.base64'
])
.factory('$sockcall', ['$_', '$json', '$name', '$utils', function(_, JSON, name, utils) {
.module('webui.services.rpc.sockcall', [
'webui.services.deps', 'webui.services.utils', 'webui.services.base64',
'webui.services.alerts'
])
.factory('$sockcall', ['$_', '$json', '$name', '$utils', '$alerts',
function(_, JSON, name, utils, alerts) {
var sockRPC = {
// true when sockrpc is ready to be used,
// false when either initializing
@ -70,11 +71,13 @@ angular
// should be called initially to start using the sock rpc
init: function(conf) {
sockRPC.initialized = false;
if (typeof WebSocket == "undefined") {
alerts.addAlert('Web sockets not supported, falling back to jsonp', 'info');
return;
}
sockRPC.conf = conf || sockRPC.conf;
sockRPC.initialized = false;
sockRPC.scheme = sockRPC.conf.encryption ? 'wss' : 'ws';
@ -92,6 +95,7 @@ angular
catch (ex) {
// ignoring IE securty exception on local ip addresses
console.log('not using websocket for aria2 rpc due to: ', ex);
alerts.addAlert('Web sockets not working due to ' + ex.message, 'info');
}
},
};

View File

@ -1,9 +1,10 @@
angular
.module('webui.services.rpc.syscall', [
'webui.services.rpc.jsoncall', 'webui.services.rpc.sockcall',
'webui.services.utils'
])
.factory('$syscall', ['$log', '$jsoncall', '$sockcall', function(log, jsonRPC, sockRPC) {
.module('webui.services.rpc.syscall', [
'webui.services.rpc.jsoncall', 'webui.services.rpc.sockcall',
'webui.services.utils', 'webui.services.alerts'
])
.factory('$syscall', ['$log', '$jsoncall', '$sockcall', '$alerts',
function(log, jsonRPC, sockRPC, alerts) {
return {
// called to initialize the rpc interface, call everytime configuration changes
// conf has the following structure:
@ -16,6 +17,7 @@ angular
// pass (string): password for the http authentication if enabled
// }
init: function(conf) {
conf = conf || {
host: 'localhost',
port: 6800