2016-05-13 18:09:12 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-06 15:41:28 +01:00
|
|
|
angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$q', '$websocket', 'ariaNgSettingService', 'ariaNgLogService', function ($q, $websocket, ariaNgSettingService, ariaNgLogService) {
|
2017-03-19 16:36:14 +01:00
|
|
|
var rpcUrl = ariaNgSettingService.getCurrentRpcUrl();
|
2016-05-16 17:41:39 +02:00
|
|
|
var socketClient = null;
|
2016-06-28 18:18:17 +02:00
|
|
|
|
2016-05-26 18:29:05 +02:00
|
|
|
var sendIdStates = {};
|
2016-06-28 18:18:17 +02:00
|
|
|
var eventCallbacks = {};
|
|
|
|
|
|
|
|
var processMethodCallback = function (content) {
|
|
|
|
var uniqueId = content.id;
|
|
|
|
|
|
|
|
if (!uniqueId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = sendIdStates[uniqueId];
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var context = state.context;
|
|
|
|
|
|
|
|
state.deferred.resolve({
|
|
|
|
success: true,
|
|
|
|
context: context
|
|
|
|
});
|
|
|
|
|
|
|
|
if (content.result && context.successCallback) {
|
2018-04-05 18:58:14 +02:00
|
|
|
ariaNgLogService.debug('[aria2WebSocketRpcService.request] ' + (context && context.requestBody && context.requestBody.method ? context.requestBody.method + ' ' : '') + 'response success', content);
|
2016-11-06 15:41:28 +01:00
|
|
|
|
2016-06-28 18:18:17 +02:00
|
|
|
context.successCallback(context.id, content.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.error && context.errorCallback) {
|
2018-04-05 18:58:14 +02:00
|
|
|
ariaNgLogService.debug('[aria2WebSocketRpcService.request] ' + (context && context.requestBody && context.requestBody.method ? context.requestBody.method + ' ' : '') + 'response error', content);
|
2016-11-06 15:41:28 +01:00
|
|
|
|
2016-06-28 18:18:17 +02:00
|
|
|
context.errorCallback(context.id, content.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete sendIdStates[uniqueId];
|
|
|
|
};
|
|
|
|
|
|
|
|
var processEventCallback = function (content) {
|
|
|
|
var method = content.method;
|
|
|
|
|
|
|
|
if (!method) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var callbacks = eventCallbacks[method];
|
|
|
|
|
|
|
|
if (!angular.isArray(callbacks) || callbacks.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
var callback = callbacks[i];
|
|
|
|
var context = (angular.isArray(content.params) && content.params.length > 0 ? content.params[0] : null);
|
|
|
|
callback(context);
|
|
|
|
}
|
|
|
|
};
|
2016-05-13 18:09:12 +02:00
|
|
|
|
2016-05-16 17:41:39 +02:00
|
|
|
var getSocketClient = function () {
|
2016-08-01 19:26:10 +02:00
|
|
|
if (socketClient === null) {
|
2017-10-08 09:37:54 +02:00
|
|
|
try {
|
|
|
|
socketClient = $websocket(rpcUrl);
|
|
|
|
|
|
|
|
socketClient.onMessage(function (message) {
|
|
|
|
if (!message || !message.data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var content = angular.fromJson(message.data);
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.id) {
|
|
|
|
processMethodCallback(content);
|
|
|
|
} else if (content.method) {
|
|
|
|
processEventCallback(content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
error: 'Cannot initialize WebSocket!',
|
|
|
|
exception: ex
|
2016-05-16 17:41:39 +02:00
|
|
|
}
|
2017-10-08 09:37:54 +02:00
|
|
|
}
|
2016-05-13 18:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 09:37:54 +02:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
instance: socketClient
|
|
|
|
};
|
2016-05-16 17:41:39 +02:00
|
|
|
};
|
2016-05-13 18:09:12 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
request: function (context) {
|
|
|
|
if (!context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:41:39 +02:00
|
|
|
var client = getSocketClient();
|
2016-05-13 18:09:12 +02:00
|
|
|
var uniqueId = context.uniqueId;
|
|
|
|
var requestBody = angular.toJson(context.requestBody);
|
|
|
|
|
2018-04-05 18:58:14 +02:00
|
|
|
ariaNgLogService.debug('[aria2WebSocketRpcService.request] ' + (context && context.requestBody && context.requestBody.method ? context.requestBody.method + ' ' : '') + 'request start', context);
|
2016-11-06 15:41:28 +01:00
|
|
|
|
2016-05-26 18:29:05 +02:00
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
sendIdStates[uniqueId] = {
|
|
|
|
context: context,
|
|
|
|
deferred: deferred
|
|
|
|
};
|
|
|
|
|
2017-10-08 09:37:54 +02:00
|
|
|
if (client.instance) {
|
|
|
|
client.instance.send(requestBody);
|
|
|
|
} else {
|
|
|
|
deferred.reject({
|
|
|
|
success: false,
|
|
|
|
context: context
|
|
|
|
});
|
|
|
|
|
|
|
|
ariaNgLogService.debug('[aria2WebSocketRpcService.request] client error', client);
|
|
|
|
context.errorCallback(context.id, { message: client.error });
|
|
|
|
}
|
2016-05-13 18:09:12 +02:00
|
|
|
|
2016-05-26 18:29:05 +02:00
|
|
|
return deferred.promise;
|
2016-06-28 18:18:17 +02:00
|
|
|
},
|
|
|
|
on: function (eventName, callback) {
|
|
|
|
var callbacks = eventCallbacks[eventName];
|
|
|
|
|
|
|
|
if (!angular.isArray(callbacks)) {
|
|
|
|
callbacks = eventCallbacks[eventName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
callbacks.push(callback);
|
2016-05-13 18:09:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
2016-08-01 16:49:16 +02:00
|
|
|
}());
|