Here's a fun example of the power of SXTM's TrafficScript language. While it is clearly not very useful as a tool for your busy e-commerce sites, hopefully it will get you thinking about the possibilities of TrafficScript!
To set up, create a new virtual server. It can use the 'discard' pool for traffic, since no back-end nodes will be needed - SXTM will be generating all of the content. Next, create a new TrafficScript rule with the following code. Finally, don't forget to associate this TrafficScript rule with the virtual server. That's it!
# Mandelbrot set viewer in TrafficScript. # # This rule is split into two distinct parts. The first half generates the # HTML for the page, and the second half calculates the colour of each # individual image on the page. $path = http.getPath(); if( $path == "/" ) { # Generating the HTML for the page $rows = 60.0; $columns = 60.0; # Get the area of the mandelbrot set to view $minx = http.getFormParam( "minx" ); $miny = http.getFormParam( "miny" ); $maxx = http.getFormParam( "maxx" ); $maxy = http.getFormParam( "maxy" ); if( $minx == "" ) $minx = -2.5; if( $miny == "" ) $miny = -2.0; if( $maxx == "" ) $maxx = 1.5; if( $maxy == "" ) $maxy = 2.0; $minx = lang.toDouble( $minx ); $miny = lang.toDouble( $miny ); $maxx = lang.toDouble( $maxx ); $maxy = lang.toDouble( $maxy ); $width = $maxx - $minx; $height = $maxy - $miny; $magf = 4.0; $stepx = $width / $columns; $stepy = $height / $rows; # Build up the initial web page $page = "" . "" . "\n" . "Zoom back out "; # Scan each row $yval = $maxy; while( $yval > $miny ) { # Build each row separately, and append them one at a time to the # main $page string, as this is more efficient $line = ""; # Zoom parameters $nminy = $yval - $height / $magf; $nmaxy = $yval + $height / $magf; $aref = " / $xval = $minx; $w2 = $width / $magf; while( $xval < $maxx ) { $line = $line . $aref . ( $xval - $w2 ) . "&maxx=" . ( $xval + $w2 ) . "\">" . ". $xval . "&y=" . $yval . "\">"; $xval = $xval + $stepx; } $page = $page . $line . " "; $yval = $yval - $stepy; } $page = $page . "\n"; http.sendResponse( "200 OK", "text/html", $page, "" ); } else if( $path == "/s.gif" ) { # Calculating the colour of one particular area of the mandelbrot. # Maximum number of iterations before we drop out $maxiter = 255; # real and imaginary components of c, based on the co-ordinates $cr = lang.toDouble( http.getFormParam( "x" )); $ci = lang.toDouble( http.getFormParam( "y" )); $zr = 0.0; $zc = 0.0; # The main loop. It calculates z=zz+c, and loops until zz >= 2 $n = $maxiter; while( $n > 0 ) { $zrs = $zr * $zr; $zis = $zi * $zi; if(( $zrs + $zis ) >= 4.0 ) break; $zi = 2 * $zr * $zi + $ci; $zr = $zrs - $zis + $cr; $n = $n - 1; } # Scale the colours. Use a logarithmic scaling, so that the low iterations # can be seen $c = 255 * ( $maxiter - $n ) / lang.toDouble( $maxiter ); $c = 255 - ( math.log( $c ) * 255 / math.log( 255 )); # An 8x8 GIF, completely black $image = string.hexDecode( "47494638376108000800800100000000ffffff2c". "0000000008000800000207848fa9cbed5d00003b" ); # Red component of the colour is byte 13 $image = string.replaceBytes( $image, lang.chr( $c ), 13 ); http.sendResponse( "200 OK", "image/gif", $image, "" ); }
Now, when you view the front page of the website, you will see the strange pattern of the Mandelbrot fractal. Clicking on part of the image will zoom in, allowing you to see more detail. Generating the fractal is in fact fairly easy; your web browser will probably be the slowest link in the chain, as it struggles to render the 3600 images on each of the generated web pages...
So, none of us at Stingray can think of a real-world use for this code, but we think it's neat anyway! It also shows how SXTM is adept at generating content as well as directing traffic.