/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| BI and PM: prmt *| (C) Copyright IBM Corp. 2002, 2016 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ //DISPATCHER CONTROL function CDispatcher(maxNumSCs) { if (typeof maxNumSCs == "undefined") { this.m_oQueueManager = new CQueueManager(); } else { this.m_oQueueManager = new CQueueManager(maxNumSCs); } } // @param extraHeaders [[key1,value1],[key2,value2],../[keyN,valueN], CDispatcher.prototype.createRequest = function(gateway, requestParams, callbackFunction, extraHeaders) { return new CRequest(gateway, requestParams, callbackFunction, extraHeaders); }; CDispatcher.prototype.dispatchRequest = function(CRequest) { return this.m_oQueueManager.addRequestToQueue(CRequest); }; CDispatcher.prototype.getRequestStatus = function(CRequest) { return CRequest.getStatus(); }; CDispatcher.prototype.cancelRequest = function(CRequest) { var isCancelled = this.m_oQueueManager.removeRequestFromQueue(CRequest.getId(), false); if (isCancelled >= 1) { return 1; } return isCancelled; }; CDispatcher.prototype.setErrorHandlerFunction = function(errorFunction) { this.m_oQueueManager.setErrorHandlerFunction(errorFunction); }; //REQUEST CONTROL function CRequest(gateway, requestParams, callbackFunction, extraHeaders) { this.m_sGateway = gateway; this.m_sRequestParams = requestParams; this.m_fCallbackFunction = callbackFunction; this.m_iRequestId = generateUniqueId(); this.m_sStatus = 0; this.m_oSC = null; this.m_sResponseType = "HTML"; this.m_extraHeaders = (typeof extraHeaders != "undefined"? extraHeaders : null); } CRequest.prototype.getId = function() { return this.m_iRequestId; }; CRequest.prototype.setResponseType = function(responseType) { this.m_sResponseType = responseType; }; CRequest.prototype.getResponseType = function() { return this.m_sResponseType; }; CRequest.prototype.setStatus = function(statusVal) { try { this.m_sStatus = statusVal; } catch (e) { return false; } return true; }; CRequest.prototype.getStatus = function() { return this.m_sStatus; }; CRequest.prototype.getSC = function() { return this.m_oSC; }; CRequest.prototype.setSC = function(sSCval) { try { this.m_oSC = sSCval; } catch (e) { return false; } return true; }; CRequest.prototype.getGateway = function() { return this.m_sGateway; }; CRequest.prototype.getRequestParams = function() { return this.m_sRequestParams; }; CRequest.prototype.getCallbackFunction = function() { return this.m_fCallbackFunction; }; CRequest.prototype.getExtraHeaders = function() { return this.m_extraHeaders; }; //QUEUE MANAGER CONTROL var XMLHttpRequests; /* Do not reset this variable if it is already set. It could happen if this file is included twice in a page. */ if (typeof XMLHttpRequests == "undefined") { XMLHttpRequests = new Array(); } function CQueueManager(maxNumSCs) { this.m_arQueue = new Array(); this.m_arSCs = new Array(); if (typeof maxNumSCs != "undefined") { this.iMaxNumSCs = maxNumSCs; } else { this.iMaxNumSCs = 2; } for (var i = 0; i < this.iMaxNumSCs; i++) { var arLen = XMLHttpRequests.length; var newSC = new CServerCommunicator(this, arLen); XMLHttpRequests[arLen] = new Array(null, newSC); this.m_arSCs[this.m_arSCs.length] = newSC; } this.errorHandlerFunction = null; } CQueueManager.prototype.setErrorHandlerFunction = function(errorFunction) { this.errorHandlerFunction = errorFunction; }; CQueueManager.prototype.addRequestToQueue = function(CRequest) { var returnStatus = -1; try { CRequest.setStatus(1); this.m_arQueue[this.m_arQueue.length] = CRequest; returnStatus = 0; var requestSuccess = this.sendNextRequest(); if (requestSuccess == true) { return 1; } else { CRequest.setStatus(-1); return returnStatus; } } catch (e) { CRequest.setStatus(-1); if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("addRequestToQueue", e); } return returnStatus; } }; CQueueManager.prototype.removeRequestFromQueue = function(requestId, callCallback, response) { try { var thisArrayEntry = -1; for (var i = 0; i < this.m_arQueue.length; i++) { if (this.m_arQueue[i] != null && this.m_arQueue[i].getId() == requestId) { thisArrayEntry = i; break; } } if (callCallback == true) { var callbackFunction = this.m_arQueue[thisArrayEntry].getCallbackFunction(); if (callbackFunction && typeof callbackFunction.processResponse == "function") { callbackFunction.processResponse(response); } else if (typeof callbackFunction == "function") { try { callbackFunction(response); } catch (e) { if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("removeRequestFromQueue_1", e); } return -1; } } else if (typeof callbackFunction == "string") { try { eval(callbackFunction); } catch (e) { if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("removeRequestFromQueue_1", e); } return -1; } } this.m_arQueue[thisArrayEntry].setStatus(3); delete (this.m_arQueue[thisArrayEntry]); return 1; } else { if (this.m_arQueue[thisArrayEntry].getStatus() == 2) { try { this.m_arSCs[this.m_arQueue[thisArrayEntry].getSC()].cancelRequest(); } catch (e) { if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("removeRequestFromQueue_2", e); } return 0; } } this.m_arQueue[thisArrayEntry].setStatus(-2); delete (this.m_arQueue[i]); } var requestSuccess = this.sendNextRequest(); if (requestSuccess == false) { return 3; } return 2; } catch (e) { if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("removeRequestFromQueue_3", e); } return -1; } }; CQueueManager.prototype.sendNextRequest = function() { try { var theNextSC = -1; var i = 0; for (i = 0; i < this.m_arSCs.length; i++) { if (this.m_arSCs[i].getBusy() <= 0) { theNextSC = i; break; } } if (theNextSC > -1) { var theNextRequest = -1; for (i = 0; i < this.m_arQueue.length; i++) { if (this.m_arQueue[i] != null && this.m_arQueue[i].getStatus() == 1) { this.m_arQueue[i].setStatus(2); theNextRequest = i; break; } } if (theNextRequest > -1) { this.m_arQueue[theNextRequest].setSC(theNextSC); this.m_arSCs[theNextSC].sendRequest(this.m_arQueue[theNextRequest]); } else { //This will empty the queue if there's nothing left in it var activeRequests = false; for (i = 0; i < this.m_arQueue.length; i++) { if (this.m_arQueue[i] != null && (this.m_arQueue[i].getStatus() == 1 || this.m_arQueue[i].getStatus() == 2)) { activeRequests = true; break; } } if (activeRequests == false) { this.m_arQueue = new Array(); } } } } catch (e) { if (typeof this.errorHandlerFunction == "function") { this.errorHandlerFunction("sendNextRequest", e); } return false; } return true; }; //SERVER COMMUNICATOR CONTROL function CServerCommunicator(CQueueManager, XMLHttpRequestArrayIndex) { this.m_bIsBusy = -1; this.m_oQueueManager = CQueueManager; this.m_oRequest = null; this.m_oXMLHttpRequestIndex = XMLHttpRequestArrayIndex; } CServerCommunicator.prototype.sendRequest = function( v_oRequest ) { this.setBusy(1); this.m_oRequest = v_oRequest; var v_oHTTPRequest = this.getHttpRequestObj(); v_oHTTPRequest.onreadystatechange = XMLHttpRequestReadyStateChanged; v_oHTTPRequest.open("POST", v_oRequest.getGateway(), true); v_oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // add extra headers when available var v_aExtraHeadersArray = v_oRequest.getExtraHeaders(); if (v_aExtraHeadersArray) { var kCount = v_aExtraHeadersArray.length; var v_aHeaders = null; for (var i = 0; i < kCount; i++) { v_aHeaders = v_aExtraHeadersArray[i]; if (v_aHeaders.length == 2 && v_aHeaders[0] && v_aHeaders[0].length && v_aHeaders[1] && v_aHeaders[1].length) { v_oHTTPRequest.setRequestHeader(v_aHeaders[0], v_aHeaders[1]); } } } v_oHTTPRequest.send( v_oRequest.getRequestParams() ); XMLHttpRequests[this.m_oXMLHttpRequestIndex][0] = v_oHTTPRequest; }; CServerCommunicator.prototype.getHttpRequestObj = function() { var v_oHTTPRequest = null; if ("ActiveXObject" in window) { v_oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); } else { v_oHTTPRequest = new XMLHttpRequest(); } return v_oHTTPRequest; }; CServerCommunicator.prototype.getStatus = function() { return XMLHttpRequests[this.m_oXMLHttpRequestIndex][0].status; }; CServerCommunicator.prototype.getBusy = function() { return this.m_bIsBusy; }; CServerCommunicator.prototype.setBusy = function(busyVal) { try { this.m_bIsBusy = busyVal; } catch (e) { return false; } return true; }; CServerCommunicator.prototype.cancelRequest = function() { return XMLHttpRequests[this.m_oXMLHttpRequestIndex][0].abort(); }; // Ready State Function function XMLHttpRequestReadyStateChanged() { for (var i = 0; i < XMLHttpRequests.length; i++) { if (XMLHttpRequests[i] && XMLHttpRequests[i][0] && XMLHttpRequests[i][0].readyState == 4) { try { XMLHttpRequests[i][1].setBusy(0); var responseType = XMLHttpRequests[i][1].m_oRequest.getResponseType(); var removeSuccess = -1; if (responseType == "HTML") { removeSuccess = XMLHttpRequests[i][1].m_oQueueManager.removeRequestFromQueue(XMLHttpRequests[i][1].m_oRequest.getId(), true, XMLHttpRequests[i][0].responseText); } else if (responseType == "XML") { removeSuccess = XMLHttpRequests[i][1].m_oQueueManager.removeRequestFromQueue(XMLHttpRequests[i][1].m_oRequest.getId(), true, XMLHttpRequests[i][0].responseXML); } else if (responseType == "XMLHTML") { removeSuccess = XMLHttpRequests[i][1].m_oQueueManager.removeRequestFromQueue(XMLHttpRequests[i][1].m_oRequest.getId(), true, new Array(XMLHttpRequests[i][0].responseXML, XMLHttpRequests[i][0].responseText)); } else if (responseType == "HTTP") { removeSuccess = XMLHttpRequests[i][1].m_oQueueManager.removeRequestFromQueue(XMLHttpRequests[i][1].m_oRequest.getId(), true, XMLHttpRequests[i][0]); } XMLHttpRequests[i][0] = null; if (removeSuccess >= 1) { XMLHttpRequests[i][1].m_oQueueManager.sendNextRequest(); } else { return false; } } catch (e) { return false; } return true; } } return false; } // Utility Functions function generateUniqueId() { return (Date.parse((new Date()).toString()) / 1000000) + Math.random(); }