2013-01-29 13:47:40 +01:00
|
|
|
angular
|
2013-03-02 12:01:07 +01:00
|
|
|
.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) {
|
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-03-11 17:01:58 +01:00
|
|
|
onerror: function(ev) {
|
2013-01-17 07:51:03 +01:00
|
|
|
_.each(sockRPC.handles, function(h) { h.error() });
|
|
|
|
sockRPC.handles = [];
|
2013-03-11 17:01:58 +01:00
|
|
|
sockRPC.initialized = false;
|
2016-04-13 11:23:19 +02:00
|
|
|
if (sockRPC.onready) {
|
|
|
|
sockRPC.onready();
|
|
|
|
sockRPC.onready = null;
|
|
|
|
}
|
2013-03-11 17:01:58 +01:00
|
|
|
},
|
|
|
|
onclose: function(ev) {
|
|
|
|
if (sockRPC.handles && sockRPC.handles.length)
|
2014-02-22 03:39:52 +01:00
|
|
|
sockRPC.onerror('Connection reset while calling aria2');
|
2013-03-11 17:01:58 +01:00
|
|
|
sockRPC.initialized = false;
|
2016-04-13 11:23:19 +02:00
|
|
|
if (sockRPC.onready) {
|
|
|
|
sockRPC.onready();
|
|
|
|
sockRPC.onready = null;
|
|
|
|
}
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// when connection opens
|
|
|
|
onopen: function() {
|
2015-04-04 21:36:04 +02:00
|
|
|
console.log('websocket initialized!!!');
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.initialized = true;
|
2016-04-13 11:23:19 +02:00
|
|
|
if (sockRPC.onready) {
|
|
|
|
sockRPC.onready();
|
|
|
|
sockRPC.onready = null;
|
|
|
|
}
|
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,
|
2013-07-31 22:17:06 +02:00
|
|
|
id: utils.uuid(),
|
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
|
2016-04-13 11:23:19 +02:00
|
|
|
// onready is called when initial connection is resolved
|
|
|
|
init: function(conf, onready) {
|
2013-03-02 12:01:07 +01:00
|
|
|
sockRPC.initialized = false;
|
2016-04-13 11:23:19 +02:00
|
|
|
if (sockRPC.onready) {
|
|
|
|
// make previous call is resolved
|
|
|
|
sockRPC.onready();
|
|
|
|
sockRPC.onready = null;
|
|
|
|
}
|
2013-03-02 12:01:07 +01:00
|
|
|
|
2013-01-15 09:24:09 +01:00
|
|
|
if (typeof WebSocket == "undefined") {
|
2014-02-22 03:39:52 +01:00
|
|
|
alerts.addAlert('Web sockets are not supported! Falling back to JSONP.', 'info');
|
2016-04-13 11:23:19 +02:00
|
|
|
onready();
|
2013-01-15 09:24:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.conf = conf || sockRPC.conf;
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-03-09 06:35:53 +01:00
|
|
|
sockRPC.scheme = sockRPC.conf.encrypt ? 'wss' : 'ws';
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
if (sockRPC.sock) {
|
2013-03-11 17:01:58 +01:00
|
|
|
sockRPC.sock.onopen = sockRPC.sock.onmessage = sockRPC.sock.onerror = sockRPC.sock.onclose = null;
|
|
|
|
sockRPC.onerror({message: 'Changing the websocket aria2 server details'});
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:19:26 +01:00
|
|
|
try {
|
2015-09-12 10:55:02 +02:00
|
|
|
var authUrl = sockRPC.scheme + '://' + conf.host + ':' + conf.port + (conf.path || '/jsonrpc');
|
2015-04-04 21:36:04 +02:00
|
|
|
if (sockRPC.conf.auth && sockRPC.conf.auth.user && sockRPC.conf.auth.pass) {
|
|
|
|
authUrl = sockRPC.scheme + '://' +
|
|
|
|
sockRPC.conf.auth.user + ":" +
|
|
|
|
sockRPC.conf.auth.pass + "@" +
|
|
|
|
sockRPC.conf.host + ':' +
|
2015-09-12 10:55:02 +02:00
|
|
|
sockRPC.conf.port + (conf.path || '/jsonrpc');
|
2015-04-04 21:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sockRPC.sock = new WebSocket(authUrl);
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.sock.onopen = sockRPC.onopen;
|
2013-03-11 17:01:58 +01:00
|
|
|
sockRPC.sock.onclose = sockRPC.onclose;
|
|
|
|
sockRPC.sock.onerror = sockRPC.onerror;
|
2013-01-17 07:51:03 +01:00
|
|
|
sockRPC.sock.onmessage = sockRPC.onmessage;
|
2016-04-13 11:23:19 +02:00
|
|
|
sockRPC.onready = onready;
|
2013-01-15 12:19:26 +01:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2016-04-13 11:23:19 +02:00
|
|
|
// ignoring IE security 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-03-02 12:01:07 +01:00
|
|
|
alerts.addAlert('Web sockets not working due to ' + ex.message, 'info');
|
2016-04-13 11:23:19 +02:00
|
|
|
onready();
|
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
|
|
|
}]);
|
|
|
|
|