238 CHAPTER 8 PERSISTENT COMMUNICATIONS PATTERN (Web server address) 3.
Friday, October 26th, 2007238 CHAPTER 8 PERSISTENT COMMUNICATIONS PATTERN 3. In the background, the HTTP server has put the asynchronous request that is waiting for a message on the reading stream on hold. 4. When the asynchronous request returns with a response, the response is processed and then a timer is started to call the function that executes another asynchronous request. What is important about the sequence of events is to call the timer only when the asynchronous request has returned. Calling the timer earlier would result in an infinite number of requests being made immediately, causing the web browser to lock and the server to suffer a denial of service attack. Using the timer, we are confronted with another problem: the window.setTimeout method requires a reference to a text-based script. The reference cannot be an object reference, because JavaScript when converting an object reference will reference either an undefined reference or a value that does not exist. The problem is clearly illustrated in the following source code: function runIt(value) { window.setTimeout(”Loop(value)”, 1000); } The function runIt has a parameter, value, which is an object reference and is used in the script expression of the method setTimeout. The problem is that the script expression is a piece of text, and the timer can execute only a piece of text, not a reference to the variable value. A solution is not to reference the variable, but to copy the value of the variable to the text script, as the following source code illustrates: function runIt(value) { window.setTimeout(”Loop(” + value + “)”, 1000); } In the modified source code, the function Loop will be called properly with the value of the variable value. Knowing that a variable has to be converted to a value is a step closer to the solution, but is not the entire solution. The main problem is that the variable value will reference an object instance, and serializing an object instance is a bad practice because it results in multiple object instances with similar states. Having multiple versions of the same object results in object state consistency problems. The solution is to pass a value that represents an index of an array. The resulting implementation uses the array property this.instances and associated properties (callDelay, index, instances, and instanceCount) to store and manage references of the reading streams shown in abbreviated detail as follows: this.index = this.instanceCount.counter; this.instances[ this.index] = this; this.instanceCount.counter ++; ClientCommunicator.prototype.instances = new Array(); ClientCommunicator.prototype.instanceCount = new CounterHack();
Check Tomcat Web Hosting services for best quality webspace to host your web application.