OK, lets first see what the Text clock from Ross Goodwin looks like after being ported to GatewayScript on DataPower. More details can be found in posting Brilliant: Text Clock displays current time using text from public domain books. As you can see in the 50% resolution screenshot below the page states the time in big bold red characters, "the time is", "eight", "fourty-two", "in the morning". Additionally the DataPower port reports the maximal time taken by asynchronous url.open() calls.
There were several problems to be resolved in porting, described one by one now, all changes can be looked up in this side-by-side diff. MPGW service export rgTextClock.zip allows you to easily play with (access it at "http://yourBox:3600" in your browser)..
The script needs access to "urlopen" module, header variables (in order to set "text/html" Content-Type for the generated response) and service variable (in order to set 'var://service/mpgw/skip-backside' making rgTextClock MPGW service a loopback service). In addition it needs to simulate write access to HTML elements identified by ID. This is done by object "o" acting as associative array. Varaible "t" and constant "d" of 50 millisecond are used in function "check()" discussed further below:
Now we have to solve the problem on how long to wait until all asynchronous "urlopen.open()" calls have finished, the same functionality as provided by DataPower's event-sink action on rule level. This is done by function "check()" that gets called with a 50ms delay. If "all is done", it just calls function "result()" to generate and return the HTML response page. Otherwise it just calls itself again, after another delay of 50ms. When is "all is done" achieved? In the case we just have to count the number of completed HTML elements writte, or the number of keys of object "o". If it is 7, we are done. Here you can see function "check()" together with its initial invocation:
For completeness this is function "result()" that
- generates response HTML page (includes a 15sec page refresh for clock updates in your browser)
- sets "text/html" Content-Type header
- does skip-backside for a loopback service
If you watch the "completed in" times reported below the main response you will notice quite some variance. Btw, since the data is accessed from Ross' web server your DataPower box needs internet access in order to work.
The refresh every 15 seconds will show 4 different pages per minute, but the time displayed will be the same for a given minute.