UNPKG

ajsfw

Version:
207 lines (206 loc) 8.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Protocol_1 = require("../message-transport/Protocol"); var exceptions = require("./exceptions"); var constants_1 = require("./constants"); var enums_1 = require("./enums"); var RMIRouter = (function () { function RMIRouter() { this.__nextEndpointId = 1; this.__nextCallId = 0; this.__routingTable = {}; this.__callPromises = {}; this.__endpointReadyListeners = []; this.__initialized = true; } RMIRouter.prototype.addInterface = function (iface) { var _this = this; iface.registerReceiver(Protocol_1.Protocol.AjsRMI, function (iface, data) { _this.__routeMessage(iface, data); }); }; RMIRouter.prototype.addEndpointRegisteredListener = function (listener) { if (this.__endpointReadyListeners.indexOf(listener) === -1) { this.__endpointReadyListeners.push(listener); } }; RMIRouter.prototype.removeEndpointReegisteredListener = function (listener) { if (this.__endpointReadyListeners.indexOf(listener) !== -1) { this.__endpointReadyListeners.splice(this.__endpointReadyListeners.indexOf(listener), 1); } }; RMIRouter.prototype.registerRmiEndpoint = function (iface, endpointId) { return __awaiter(this, void 0, void 0, function () { var newEndpointId; return __generator(this, function (_a) { newEndpointId = this.__getNextEndpointId(); this.__routingTable[newEndpointId] = iface; this.__routingTable[endpointId] = iface; return [2, newEndpointId]; }); }); }; RMIRouter.prototype.endpointRegistrationConfirmed = function (iface, endpointId) { return __awaiter(this, void 0, void 0, function () { var _i, _a, l; return __generator(this, function (_b) { for (_i = 0, _a = this.__endpointReadyListeners; _i < _a.length; _i++) { l = _a[_i]; l(iface, endpointId); } return [2]; }); }); }; RMIRouter.prototype.makeCall = function (target, method) { var _this = this; var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } if (!this.__initialized && target !== constants_1.RMI_ROUTER) { throw new exceptions.RMIEndpointNotInitializedException(); } return new Promise(function (resolve, reject) { var callId = _this.__getNextCallId(); _this.__callPromises[callId] = { resolver: resolve, rejector: reject }; var rmiCall = { callId: callId, method: method, args: args }; _this.__sendRMIMessage(target, enums_1.RmiMessageType.Call, rmiCall); }); }; RMIRouter.prototype.notify = function (target, method) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } var rmiNotify = { method: method, args: args }; this.__sendRMIMessage(target, enums_1.RmiMessageType.Notify, rmiNotify); }; RMIRouter.prototype.__getNextCallId = function () { return this.__nextCallId++; }; RMIRouter.prototype.__returnOk = function (target, callId, data) { var ret = { errorCode: 0, callId: callId, data: data }; this.__sendRMIMessage(target, enums_1.RmiMessageType.Return, ret); }; RMIRouter.prototype.__returnError = function (target, callId, errorCode, error) { var ret = { errorCode: errorCode, callId: callId, data: error.toString() }; console.error(error); this.__sendRMIMessage(target, enums_1.RmiMessageType.Return, ret); }; RMIRouter.prototype.__sendRMIMessage = function (target, rmiType, data) { var rmiMessage = { rmiDestinationId: target, rmiSourceId: 0, rmiType: rmiType, data: data }; this.__routeMessage(null, rmiMessage); }; RMIRouter.prototype.__processMessage = function (iface, message) { if (message.rmiDestinationId !== 0) { return; } switch (message.rmiType) { case enums_1.RmiMessageType.Call: this.__processCall(iface, message.rmiSourceId, message.data); break; case enums_1.RmiMessageType.Notify: this.__processNotify(iface, message.rmiSourceId, message.data); break; case enums_1.RmiMessageType.Return: this.__processReturn(message.data); break; default: console.error("Invalid RMI message type!"); break; } }; RMIRouter.prototype.__processCall = function (iface, sourceId, call) { return __awaiter(this, void 0, void 0, function () { var callArgs, result, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); if (!(call.method in this)) { throw new exceptions.InvalidServiceMethodException(); } callArgs = [iface, sourceId].concat(call.args); return [4, this[call.method].apply(this, callArgs)]; case 1: result = _a.sent(); this.__returnOk(sourceId, call.callId, result); return [3, 3]; case 2: e_1 = _a.sent(); this.__returnError(sourceId, call.callId, -1, e_1); return [3, 3]; case 3: return [2]; } }); }); }; RMIRouter.prototype.__processNotify = function (iface, sourceId, notify) { try { if (!(notify.method in this)) { throw new exceptions.InvalidServiceMethodException(); } var callArgs = [iface, sourceId].concat(notify.args); this[notify.method].apply(this, callArgs); } catch (e) { console.error(e); } }; RMIRouter.prototype.__processReturn = function (ret) { if (!(ret.callId in this.__callPromises)) { console.error(new exceptions.InvalidReturnDataException()); return; } var callPromise = this.__callPromises[ret.callId]; delete this.__callPromises[ret.callId]; if (ret.errorCode === 0) { callPromise.resolver(ret.data); } else { callPromise.rejector(ret.data); } }; RMIRouter.prototype.__routeMessage = function (iface, message) { if (message.rmiDestinationId === 0) { this.__processMessage(iface, message); return; } if (!(message.rmiDestinationId in this.__routingTable)) { throw new Error("Failed to locate target RMI endpoint."); } this.__routingTable[message.rmiDestinationId].send(Protocol_1.Protocol.AjsRMI, message); if (message.rmiDestinationId >= 1000000) { delete this.__routingTable[message.rmiDestinationId]; } }; RMIRouter.prototype.__getNextEndpointId = function () { return this.__nextEndpointId++; }; return RMIRouter; }()); exports.RMIRouter = RMIRouter;