Home

Who are we

Articles

SOAP - A Quick Intro to Webservices

XML-RPC - A Five-Minute Tutorial

Installing Apache2 and mod_perl (Windows XP)

Links

Untitled Document

XML-RPC Implementations

Enough theory - time to get our hands dirty. In the following I will show a simple XML-RPC server in Perl, Python and finally Java. This is similar to Eric Kidd's great XML-RPC HowTo but has a more narrow range in the language selection. On the other hand the there a server examples for Python and the packages used are a bit different.

A Perl XML-RPC Server

We start with the server because in order to test our client we need a working server. The service I want to implement returns the server time or date (or both) as array elements depending on the parameter used in the procedure call. The package used is XMLRPC::Lite, which is part of the default distribution of perl:

dateInfoServer.pl
 
#!/usr/bin/perl
package Date;
use strict;
use Time::localtime;
use XMLRPC::Transport::HTTP;

sub dateInfo {
    shift;
    my $option = shift;

    my $time = localtime;
    my $year = $time->year()+1900; 
    my $month = $time->mon()+1; 
    my $day = $time->mday();
    my $hour = $time->hour();
    my $min = $time->min();
    my $sec = $time->sec();
    
    if ($option eq "date") {
	return [sprintf("%02d/%02d/%04d", $month, $day, $year)];
    } elsif ($option eq "time") {
	return [sprintf("%02d:%02d:%02d", $hour, $min, $sec)];
    } elsif ($option eq "all") {
	return [sprintf("%02d/%02d/%04d", 
	  $month, $day, $year), 
	    sprintf("%02d:%02d:%02d", $hour, $min, $sec)]
    } else {
	return ["Error - unrecognized option"]; 
    }
}


XMLRPC::Transport::HTTP::Daemon
  ->new(LocalPort => 9000, ReuseAddr => 1)
  ->dispatch_to('Date::dateInfo')
  ->handle;

exit;

The server itself is defined and configured in XMLRPC::Transport::HTTP::Daemon->new() creates an instance of an HTTP server and the method dispatch_to registers Date::dateInfo with the server. Note that client requests to Date.dateInfo get automatically translated to the perl convention Date::dateInfo. The first part of the code listing implements the function dateInfo itself. You see that dateInfo is just a normal function in the Date package (namespace). It is important however to know that XMLRPC::Lite calls the functions as class methods. For perl that means that the first parameter passed on to the function is the class instance itself. We don't need that in this case, so we just drop it with shift.

A Perl XML-RPC Client

The client takes one parameter as input option and passes that on to the server. Possible values are 'date', 'time', or 'all', to return either the current server data, the server time, or both.

dateInfoClient.pl
 
#! /usr/bin/perl
use strict; 
use XMLRPC::Lite;

my $option = $ARGV[0]; 

# setup the connection protocol
my $server = 'http://localhost:9000'; 
my $client = XMLRPC::Lite->proxy($server);
$client ->on_fault(sub { die "Transport error: " .
			     $_[1]->faultstring } );

my $response = $client->call('Date.dateInfo', $option)
  ->result(); 

# read out the response
unless (defined $response) {
    print "Call returned error.\n";
    exit; 
}
if ($option eq "date") {
    print "Current server date is ". $response->[0] ."\n";
} elsif ($option eq "time") {
    print "Current server time is ". $response->[0] ."\n";
} elsif ($option eq "all") {
    print "Current server date is ". $response->[0] ."\n";
    print "Current server time is ". $response->[1] ."\n";
}
exit;

The main parts here are the calls XMLRPC::Lite->proxy() which defines the client and the submission of the request and capturing of the response in $client->call()->result(). The remaining part of the code is just defining options for the request and processing the response (an array reference) for display.

If you startup the server and run dateInfoClient you should get something like :

 
>dateInfoClient.pl all
Current server date is 08/25/2005
Current server time is 22:29:38

(To stop the server hit Ctrl-C.)

 

Previous - XML-RPC Basics