2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.services.rpc.sockcall', [
|
|
|
|
'webui.services.deps', 'webui.services.utils', 'webui.services.base64'
|
|
|
|
])
|
|
|
|
.factory('$sockcall', ['$_', '$json', '$name', '$utils', function(_, JSON, name, utils) {
|
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
var sockRPC = {
|
2013-01-15 09:24:09 +01:00
|
|
|
// true when sockrpc is ready to be used,
|
|
|
|
// false when either initializing
|
|
|
|
// or no support for web sockets
|
|
|
|
initialized: false,
|
|
|
|
|
|
|
|
// ongoing connection handles containing connection id and callbacks
|
|
|
|
handles: [],
|
|
|
|
|
|
|
|
// websocket connection socket used for all connections
|
|
|
|
sock: null,
|
|
|
|
|
|
|
|
// connection configuration
|
|
|
|
conf: null,
|
|
|
|
|
|
|
|
// socket connection scheme, default to unencrypted connection
|
|
|
|
scheme: 'ws',
|
|
|
|
|
|
|
|
// called when a connection error occurs
|
2013-01-17 07:51:03 +01:00
|
|
|
onerror: function(err) {
|
|
|
|
_.each(sockRPC.handles, function(h) { h.error() });
|
|
|
|
sockRPC.handles = [];
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// when connection opens
|
|
|
|
onopen: function() {
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.initialized = true;
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// when message is recieved
|
|
|
|
onmessage: function(message) {
|
|
|
|
var data = JSON.parse(message.data);
|
|
|
|
|
|
|
|
// reverse loop because we are deleting elements
|
|
|
|
// while looping over the old items
|
2013-01-17 07:51:03 +01:00
|
|
|
for (var i = sockRPC.handles.length - 1; i >= 0; i--) {
|
|
|
|
if (sockRPC.handles[i].id === data.id) {
|
|
|
|
sockRPC.handles[i].success(data);
|
|
|
|
sockRPC.handles.splice(i, 1);
|
2013-01-15 09:24:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// call to use the rpc
|
|
|
|
invoke: function(opts) {
|
|
|
|
var data = {
|
|
|
|
jsonrpc: 2.0,
|
|
|
|
id: name + '_' + utils.randStr(),
|
2013-01-17 07:51:03 +01:00
|
|
|
method: opts.name,
|
2013-01-15 09:24:09 +01:00
|
|
|
params: opts.params && opts.params.length ? opts.params : undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data.params && !data.params.length) data.params = undefined;
|
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.handles.push({
|
2013-01-15 09:24:09 +01:00
|
|
|
success: opts.success || angular.noop,
|
|
|
|
error: opts.error || angular.noop,
|
|
|
|
id: data.id
|
|
|
|
});
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.sock.send( JSON.stringify(data) );
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// should be called initially to start using the sock rpc
|
|
|
|
init: function(conf) {
|
|
|
|
if (typeof WebSocket == "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.conf = conf || sockRPC.conf;
|
|
|
|
sockRPC.initialized = false;
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.scheme = sockRPC.conf.encryption ? 'wss' : 'ws';
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
if (sockRPC.sock) {
|
|
|
|
sockRPC.onopen = sockRPC.sock.onmessage = sockRPC.sock.onerror = sockRPC.sock.onclose = null;
|
|
|
|
sockRPC.onerror();
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:19:26 +01:00
|
|
|
try {
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.sock = new WebSocket(sockRPC.scheme + '://' + conf.host + ':' + conf.port + '/jsonrpc');
|
|
|
|
sockRPC.sock.onopen = sockRPC.onopen;
|
|
|
|
sockRPC.sock.onclose = sockRPC.sock.onerror = sockRPC.onerror;
|
|
|
|
sockRPC.sock.onmessage = sockRPC.onmessage;
|
2013-01-15 12:19:26 +01:00
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// ignoring IE securty exception on local ip addresses
|
2013-01-17 07:51:03 +01:00
|
|
|
console.log('not using websocket for aria2 rpc due to: ', ex);
|
2013-01-15 12:19:26 +01:00
|
|
|
}
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
|
|
|
};
|
2013-01-17 07:51:03 +01:00
|
|
|
|
|
|
|
return sockRPC;
|
2013-01-15 09:24:09 +01:00
|
|
|
}]);
|
|
|
|
|