A Few Examples
So that's the theory. There wasn't much of it, but don't let that discourage youit's possible to build some fairly powerful distributed applications using the simple functions described in the previous sections.
Information Delivery with WDDX and HTTP
This section discusses one of the most popular applications of this technology, using it to build a primitive push/pull engine for information delivery over the web. I'll be using a MySQL database as the data source, WDDX to represent the data, and PHP to perform the actual mechanics of the transaction.
Requirements
Let's assume the existence of a fictional corporationXTI Inc.that plans to start up an online subscription service offering access to share market data. XTI already has access to this information via an independent source, and its database of stocks and their prices is automatically updated every few minutes with the latest market data. XTI's plan is to offer customers access to this data, allowing them to use it on their own web sites in exchange for a monthly subscription fee.
Listing 5.16 has a slice of the MySQL table that holds the data we're interested in.
Listing 5.16 A Sample Recordset from the MySQL Table Holding Stock Market Information
+--------+--------+---------------------+ | symbol | price | timestamp | +--------+--------+---------------------+ | DTSJ | 78.46 | 2001-11-22 12:20:57 | | DNDS | 5.89 | 2001-11-22 12:32:12 | | MDNC | 12.94 | 2001-11-22 12:21:34 | | CAJD | 543.89 | 2001-11-22 12:29:01 | | WXYZ | 123.67 | 2001-11-22 12:28:32 | +--------+--------+---------------------+
All that is required is an interface to this database so that subscribers to the service can connect to the system and obtain prices for all or some stocks (keyed against each stock's unique four-character symbol).
Implementing these requirements via WDDX is fairly simple and can be accomplished via two simple scriptsone for each end of the connection. A WDDX server can be used at the XTI end of the connection to accept incoming client requests and deliver formatted WDDX packets to them. At the other end of the connection, WDDX-friendly clients can read these packets, decode them, and use them in whatever manner they desire.
Server
Let's implement the server first. Listing 5.17 has the complete code.
Listing 5.17 A Simple WDDX Server
<?php // server.php - creates WDDX packet containing symbol, price and timestamp of
all/selected stock(s) // this script will run at the URL http://caesar.xtidomain.com/customers/server.php // open connection to database $connection = mysql_connect("mysql.xtidomain.com", "wddx", "secret") or die
("Unable to connect!"); mysql_select_db("db7643") or die ("Unable to select database!"); // get data $query = "SELECT symbol, price, timestamp FROM stocks"; // if a symbol is specified, modify query to get only that record if ($_GET['symbol']; { $symbol = $_GET['symbol']; $query .= " WHERE symbol = '$symbol'"; } $query .= " ORDER BY timestamp"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if a result is returned if (mysql_num_rows($result) > 0) { // iterate through resultset while($row = mysql_fetch_row($result)) { // add data to $sPackage[] associative array // $sPackage is an array of the form ($symbol => array($price,
$timestamp), ... ) $sPackage[$row[0]] = array($row[1], $row[2]); } } // close database connection mysql_close($connection); // create WDDX packet echo wddx_serialize_value($sPackage); ?>
This may appear complex, but it's actually pretty simple. Because the data is stored in a database, the first task must be to extract it using standard MySQL query functions. The returned resultset may contain either a complete list of all stocks currently in the database with their prices or a single record corresponding to a client-specified stock symbol.
Next, this data must be packaged into a form that can be used by the client. For this example, I packaged the data into an associative array named $sPackage, whose every key corresponds to a stock symbol in the table. Every key is linked to a value, which is itself a two-element array containing the price and timestamp.
After all the records in the resultset are processed, the $sPackage array is serialized into a WDDX packet with wddx_serialize_value() and then printed as output.
Client
So, you now have a server that is capable of creating a WDDX packet from the results of a database query. All you need now is a client to connect to this server, retrieve the packet, and decode it into a native PHP array for use on an HTML page. Listing 5.18 contains the code for this client.
Listing 5.18 A Simple WDDX Client
<?php // client.php - read and decode WDDX packet // this script runs at http://brutus.clientdomain.com/client.php // url of server page $url = "http://caesar.xtidomain.com/customers/server.php"; // probably implement some sort of authentication mechanism here // proceed further only if client is successfully authenticated // read WDDX packet into string $output = join ('', file($url)); // deserialize $cPackage = wddx_deserialize($output); ?> <html> <head> <basefont face="Arial"> <!-- reload page every two minutes for latest data --> <meta http-equiv="refresh" content="120; URL= http://brutus.clientdomain.com/client.php"> </head> <body> <? // if array contains data if (sizeof($cPackage) > 0) { // format and display ?> <table border="1" cellspacing="5" cellpadding="5"> <tr> <td><b>Symbol</b></td> <td><b>Price (USD)</b></td> <td><b>Timestamp</b></td> </tr> <?php // iterate through array // key => symbol // value = array(price, timestamp) while (list($key, $value) = each($cPackage)) { echo "<tr>\n"; echo "<td>$key</td>\n"; echo "<td>$value[0]</td>\n"; echo "<td>$value[1]</td>\n"; echo "</tr>\n"; } ?> </table> <? } else { echo "No data available"; } ?> </body> </html>
The client is even simpler than the server. It connects to the specified server URL and authenticates itself. (I didn't go into the details of the authentication mechanism to be used, but it would probably be a host-username-password combination to be validated against XTI's customer database.) It then reads the WDDX packet printed by the server into an array with the file() function. This array is then converted into a string and deserialized into a native PHP associative array with wddx_deserialize().
After the data is decoded into a PHP associative array, a while loop can be used to iterate through it, identifying and displaying the important elements as a table.
Figure 5.1 shows what the resulting output looks like.
Figure 5.1 Retrieving stock prices from a database via a WDDX-based client-server system.The beauty of this system is that the server and connecting clients are relatively independent of each other. As long as a client has the relevant permissions, and understands how to connect to the server and read the WDDX packet returned by it, it can massage and format the data per its own special requirements. To illustrate this, consider Listing 5.19, which demonstrates an alternative clientthis one performing a "search" on the server for a user-specified stock symbol.
Listing 5.19 An Alternative WDDX Client
<?php // client.php - read and decode WDDX packet // this script runs at http://brutus.clientdomain.com/client.php if(!$_POST['submit']) { ?> <!-- search page --> <!-- lots of HTML layout code - snipped out --> <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post"> Enter stock symbol: <input type="text" name="symbol" size="4" maxlength="4"> <input type="submit" name="submit" value="Search"> </form> <? } else { // perform a few error checks // sanitize search term // query server with symbol as parameter $symbol = $_POST['symbol']; $url = "http://caesar.xtidomain.com/customers/server.php?symbol=$symbol"; // probably implement some sort of authentication mechanism here // proceed further only if client is successfully authenticated // read WDDX packet into string $output = join ('', file($url)); // deserialize $cPackage = wddx_deserialize($output); // if any data in array if (sizeof($cPackage) > 0) { // format and display list($key, $value) = each($cPackage); echo "Current price for symbol $key is $value[0]"; } else { echo "No data available"; } } ?>
This script consists of two parts:
The search form itself, which contains a text box for user input
The form processor, which connects to the content server with the user-specified stock symbol as parameter and massages the resulting WDDX output into easily readable HTML
Again, even though the two clients operate in two different ways (one displays a complete list of items, whereas the other uses a search term to filter down to one specific item), no change was required to the server or to the formatting of the WDDX packet.
Perl of Wisdom
It's not necessary that the WDDX clients described in Listings 5.18 and 5.19 be written in PHP. As discussed previously in this chapter, WDDX creates platform-independent data structures that can then be deserialized into native structures on the target platform. Consequently, it's possible for a WDDX client written in Perl or Python to connect to a WDDX server written in PHP, receive WDDX-compliant data packets, and use them within a script or program.
As an illustration, consider the following Perl port (see Listing 5.20) of the client described in Listing 5.18.
Listing 5.20 A Perl WDDX Client
#!/usr/bin/perl # need this to read HTTP response use LWP::UserAgent; # need this to deserialize WDDX packets use WDDX; # instantiate client, connect and read response $client = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => 'http://caesar.xtidomain.com/ _customers/server.php'); my $res = $client->request($req); # response is good... if ($res->is_success) { # deserialize resulting packet as hash and get reference my $wddx = new WDDX; $packet = $wddx->deserialize($res->content); $hashref = $packet->as_hashref; # get a list of keys (stock symbols) within the hash @keys = $packet->keys(); # iterate through hash foreach $key (@keys) { # get a reference to the array [price, timestamp] for each key $arrayref = $$hashref{$key}; # print data in colon-separated format print "$key:$$arrayref[0]:$$arrayref[1]\n"; } } # response is bad... else { print "Error: bad connection!\n"; }
In this case, I used Perl's libwww module to connect to the WDDX server and read the resulting packet, and the WDDX module to deserialize it into a Perl hash reference. After the data is converted into a Perl-compliant structure, accessing and displaying the various elements is a snap.
Perl's WDDX.pm module is far more powerful than the WDDX module that ships with PHP 4.0, offering a wide array of different methods to simplify access to arrays, hashes, and recordsets. (Recordsets are not supported by PHP's WDDX module as of this writing, although support might become available in future versions.)
Remote Software Updates with WDDX and Socket Communication
The preceding section, "Information Delivery with WDDX and HTTP," demonstrated a WDDX client and server running over HTTP. As you might imagine, though, that's not the only way to use WDDX; this next example demonstrates WDDX-based data exchange using socket communication between a PHP server and client.
Requirements
In order to set the tone, let's again consider a fictional organization, Generic Corp (GCorp), which provides its customers with Linux-based software widgets. GCorp updates these widgets on a regular basis, and makes them available to paying customers via an online repository.
Now, GCorp has no fixed update schedule for these widgetsthey're handled by different development teams, and are released to the online repository as installable files in RedHat Package Manager (RPM) format at irregular intervals. What GCorp wants is a way for every customer to automatically receive notification of software updates as and when they're released.
Most companies would send out email notification every time an update happened. But this is GCorp, and they like to make things complicated.
What GCorp has planned, therefore, is to have a WDDX server running on its web site, which automatically scans the repository and creates a WDDX packet containing information on the latest software versions available. This information can then be provided to any requesting client.
The client at the other end should have the necessary intelligence built into it to compare the version numbers received from the server with the version numbers of software currently installed on the local system. It may then automatically download and install the latest versions, or simply send notification to the system administrator about the update.
This is easily accomplished with WDDX; for variety, I'll perform the data exchange over TCP/IP sockets rather than HTTP.
Server
Let's begin with the server (see Listing 5.21), which opens up a socket and waits for connections from requesting clients.
Listing 5.21 A WDDX Server to Read and Communicate Version Information Over a TCP/IP Socket
<?php // IMPORTANT! This script should not be run via your Web server! // You will need to run it from the command line, // or as a service from inetd.conf // set up some socket parameters $ip = "127.0.0.1"; $port = 7890; // area to look for updated files $repository = "/tmp/updates/"; // start with socket creation // get a handler if (($socket = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) { // this is fairly primitive error handling echo "Could not create socket\n"; } // bind to the port if (($ret = socket_bind ($socket, $ip, $port)) < 0) { echo "Could not bind to socket\n"; } // start listening for connections if (($ret = socket_listen ($socket, 7)) < 0) { echo "Could not create socket listener\n"; } // if incoming connection, accept and spawn another socket // for data transfer if (($child = socket_accept($socket)) < 0) { echo "Could not accept incoming connection\n"; } if (!$input = socket_read ($child, 2)) { echo "Could not read input\n"; } else { // at this stage, GCorp might want to perform authentication // using the input received by the client // assuming authentication succeeds... // look in the updates directory $dir = opendir($repository); while($file = readdir($dir)) { // omit the "." and ".." directories if($file != "." && $file != "..") { $info = explode("-", $file); // create an array of associative arrays, one for each
file found // each associative array has the keys "name", "version"
and "size" $filelist[] = array("name" => $info[0], "version" => $info[1],
"size" => filesize($repository . $file)); } } closedir($dir); // serialize the array $output = wddx_serialize_value($filelist); // and send it to the client if(socket_write ($child, $output, strlen ($output)) < 0) { echo "Could not write WDDX packet"; } // clean up socket_close ($child); } socket_close ($socket); ?>
I will not get into the details of how the socket server is actually createdif you're interested, the PHP manual has extensive information on thisbut instead focus on how the server obtains information on the updates available and serializes it into a WDDX packet.
The variable $repository sets up the location of the online software repository maintained by GCorp's QA team. When the socket server receives an incoming connection, it obtains a file list from the repository and creates an array whose every element corresponds to a file in the repository.
Every element of the array is itself an associative array that contains the keys name, version, and size, corresponding to the package name, version, and size, respectively. (Some of this information is obtained by parsing the filename with PHP's string functions.) This entire array is serialized with wddx_serialize_value() and written to the requesting client via the open socket.
I used PHP to implement the server here for convenience; however, it's just as easy to use Perl, Python, or Java (as discussed in the "Perl Of Wisdom" sidebar). Note also that socket programming support was added to PHP fairly recently and is, therefore, not yet completely stable.
Client
At the other end of the connection, a WDDX-compliant client has to deserialize the packet received from the server and then compare the information within it against the information it has on locally installed versions of the software. Listing 5.22 demonstrates one implementation of such a client.
Listing 5.22 A WDDX Client to Retrieve Version Information Over a TCP/ IP Socket
<?php // IMPORTANT! This script should not be run via your Web server! // You will need to run it manually from the command line, or via crontab // set up some socket parameters // this is the IP address of the socket server $ip = "234.56.789.1"; $port = 7890; // open a socket connection $socket = fsockopen($ip, $port); if (!$socket) { echo "Could not open connection\n"; } else { // send a carriage return fwrite($socket, "\n"); $packet = fgets($socket, 4096); // get and deserialize list of server packages $remote_packages = wddx_deserialize($packet); // close the socket fclose($socket); // make sure that the deserialized packet is an array if(!is_array($remote_packages)) { $message= "Bad/unsupported data format received\n"; } else { // now, start processing the received data for ($x=0; $x<sizeof($remote_packages); $x++) { // for each item in the array // check to see if a corresponding package is installed on
the local system $local_package = exec("rpm -qa | grep " .
$remote_packages[$x]['name']); // not there? that means it's a new package // dump it into the $new_packages[] array if ($local_package == "") { $new_packages[] = $remote_packages[$x]; } else { // present? check version // if a new version is available, // dump it into the $updated_packages[] array $arr = explode("-", $local_package); if ($arr[1] < $remote_packages[$x]['version']) { $updated_packages[] = $remote_packages[$x]; } } } // finally, put together a notification for the sysop // list of updates, with file size and version // an option here might be to initiate an automatic // download of the updates // RPM can be used to auto-install the downloaded updates if (sizeof($updated_packages) > 0) { $message .= "The following updates are available on our
server:\n"; for ($x=0; $x<sizeof($updated_packages); $x++) { $message .= "Package: ". $updated_packages[$x]['name']
. "\n"; $message .= "Version: " . $updated_packages[$x]['version']
. "\n"; $message .= "Size: " . $updated_packages[$x]['size']
. " bytes\n\n"; } } // ...and list of new packages, with file size and version if (sizeof($new_packages) > 0) { $message .= "The following new packages have been added to our
server (may require purchase):\n"; for ($x=0; $x<sizeof($new_packages); $x++) { $message .= "Package: ". $new_packages[$x]['name'] . "\n"; $message .= "Version: " . $new_packages[$x]['version']
. "\n"; $message .= "Size: " . $new_packages[$x]['size']
. " bytes\n\n"; } } // mail it out if(mail("root@localhost", "GCorp package updates for this week",
$message)) { echo "Operation successfully completed"; } else { echo "Error processing mail message"; } } } ?>
In this case, the client uses PHP's fsockopen() function to connect to the server and retrieve the WDDX packet. It then deserializes this packet into an array, and proceeds to iterate through it.
Because all GCorp packages are distributed in RPM format, it's fairly simple to obtain information on the currently installed versions of the files listed in the array with the rpm utility (standard on most Linux systems). These version numbers are compared with the version numbers of files on GCorp's server (remember the version key of each associative array?), and two new arrays are created:
$updated_packages, which contains a list of updated packages
$new_packages, which contains a list of packages available on the server but not installed locally
This information is then emailed to the system administrator via PHP's mail() function.
This isn't the only option, obviously; a variant of this might be for the client to automatically download and install the new software automatically. A more sophisticated client might even identify the new packages and send advertisements for, or information on, new software available, on a per-customer basis.
Command and Control
It's not advisable to run Listings 5.21 and 5.22 via your web server; rather, they should be run from the command line using the PHP binary.
For example, if you have a PHP binary located in /usr/local/bin/php, you would run the server script as the following:
$ /usr/local/bin/php -q /usr/local/work/bin/socket_server.php
The additional -q (quiet) parameter forces PHP to omit the Content-Type: text/html header that it usually sends prior to executing a script.
In case you don't have a PHP binary available, it's pretty easy to compile one. Just follow the instructions in Appendix A.
Although the socket server should always be activewhich is why I suggest running it as a service managed by the inet daemonthe client should be set up to instantiate connections on a more irregular basis, either via manual intervention or UNIX cron.
Other Applications for WDDX
As the preceding examples demonstrated, WDDX makes it possible to exchange data between different sites and systems in a simple and elegant manner. Consequently, one of its more common applications involves acting as the vehicle for the syndication of frequently updated content over the web. Examples of areas in which WDDX can be used include the following:
News syndication services, which "push" the latest headlines, sports scores, stock and currency market information, and weather forecasts to connecting clients from a news database (for an example, check out http://www.moreover.com/, which offers news headlines in WDDX)
Ad-rotation services, which accept demographic data from requesting clients and return links to appropriate banner ads for display on the resulting web page
E-commerce agents, which browse different sites and return comparative pricing information for selected items
Software-update services, which automatically update remote systems with new software versions