123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- /*
- *+------------------------------------------------------------------------+
- *| 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();
- }
|