
org.xmlBlaster.util.RequestResponseDispatcher=function(){this.requestMap=new Object();this.requestIdCounter=1;this.getRequestIdNextVal=function(){return this.requestIdCounter++;},this.onResponseOrException=function(bounceId,dataReceived,resultCsvFlag){var requestInfo=this.requestMap[bounceId];var resultCsvFlag=new String(resultCsvFlag)||null;if(typeof requestInfo==undefined||requestInfo==null){return false;}
var deleteEntry=true;if(dataReceived!=null){if(requestInfo.timeoutId!=null){clearTimeout(requestInfo.timeoutId);}
if(requestInfo.timeoutSec==org.xmlBlaster.util.RequestResponseDispatcher.FOREVER_MULTIPLE_SHOTS){deleteEntry=false;}
if(resultCsvFlag==null&&dataReceived.getPropValue!=undefined){resultCsvFlag=dataReceived.getPropValue(org.xmlBlaster.util.PropTO.KEY_RESULT_CSVFLAG);}
var incrementalResponse=(resultCsvFlag!=null&&resultCsvFlag.indexOf("incrementalResponse=true")!=-1);if(incrementalResponse){deleteEntry=false;}}
if(deleteEntry){delete this.requestMap[bounceId];}
else{if(requestInfo.extendTimeoutOnIncrementalResponse){this.extendTimeout(requestInfo.bounceId,requestInfo.timeoutSec);}}
if(requestInfo.responseFp!=null){requestInfo.responseFp.call(requestInfo.responseThisArg,dataReceived,requestInfo.returnObj,requestInfo);return true;}
return false;}
this.requestTimedout=function(bounceId){var dataReceived=null;var resultCsvFlag=null;this.onResponseOrException(bounceId,dataReceived,resultCsvFlag);}
this.registerRequest=function(bounceId_,timeoutSec_,responseFp_,responseThisArg,returnObj,silentlyDiscardOldIdentical,extendTimeoutOnIncrementalResponse){var bounceId=bounceId_||"";var silentlyDiscardOldIdentical=(silentlyDiscardOldIdentical===undefined)?false:silentlyDiscardOldIdentical;var extendTimeoutOnIncrementalResponse=(extendTimeoutOnIncrementalResponse===undefined)?false:extendTimeoutOnIncrementalResponse;var timeoutSec=20;if(typeof timeoutSec_!=undefined)
timeoutSec=timeoutSec_;var responseFp=responseFp_||null;var currDispatcher=this;if(typeof returnObj==undefined)
returnObj=null;var timeoutId=null;if(timeoutSec>0){var timeoutMillis=timeoutSec*1000;timeoutId=setTimeout(function(){currDispatcher.requestTimedout(bounceId);},timeoutMillis);}
var requestInfo={requestResponseDispatcher:this,bounceId:bounceId,timeoutSec:timeoutSec,timeoutId:timeoutId,responseFp:responseFp,responseThisArg:responseThisArg,returnObj:returnObj,extendTimeoutOnIncrementalResponse:extendTimeoutOnIncrementalResponse}
var isNew=this.requestMap[bounceId]===undefined;if(!isNew){if(silentlyDiscardOldIdentical){this.unregisterRequest(bounceId);}
else{throw"IllegalArgumentException: Multiple registration! org.xmlBlaster.util.RequestResponseDispatcher.registerRequest("
+bounceId+", "+timeoutSec+", "+responseFp+", "
+responseThisArg+", "+returnObj+")";}}
this.requestMap[bounceId]=requestInfo;}
this.unregisterRequest=function(bounceId){if(bounceId===undefined)
return null;requestInfo=this.requestMap[bounceId];if(requestInfo===undefined)
return null;if(requestInfo!=null&&requestInfo.timeoutId!=null)
clearTimeout(requestInfo.timeoutId);delete this.requestMap[bounceId];return requestInfo;}
this.extendTimeout=function(bounceId,timeoutSec){var requestInfo=this.requestMap[bounceId];if(typeof requestInfo==undefined||requestInfo==null){return null;}
var timeoutSec=timeoutSec||20;if(timeoutSec<=0)
timeoutSec=20;if(requestInfo.timeoutId!=null){clearTimeout(requestInfo.timeoutId);}
var currDispatcher=this;var timeoutMillis=timeoutSec*1000;requestInfo.timeoutId=setTimeout(function(){currDispatcher.requestTimedout(bounceId);},timeoutMillis);return requestInfo;}
this.size=function(){var count=0;for(var key in this.requestMap){count++;}
return count;}
this.clear=function(){for(var key in this.requestMap){var requestInfo=this.requestMap[key];var timeoutId=requestInfo.timeoutId;if(timeoutId!=null)
clearTimeout(timeoutId);delete this.requestMap[key];}}}
org.xmlBlaster.util.RequestResponseDispatcher.createDummyRequestInfo=function(){var requestInfo={requestResponseDispatcher:null,bounceId:null,timeoutSec:0,timeoutId:0,responseFp:null,responseThisArg:null,returnObj:null,exception:null}
return requestInfo;}
org.xmlBlaster.util.RequestResponseDispatcher.FOREVER_ONE_SHOT=0;org.xmlBlaster.util.RequestResponseDispatcher.FOREVER_MULTIPLE_SHOTS=-1;
