Herong's Tutorial Notes on Perl - Part B
Dr. Herong Yang, Version 4.11

RPC::XML Module

Part:   1  2  3   4 

(Continued from previous part...)

Of course, this is a very simple XML-RPC server:

  • It offers 3 methods: com.herong.hello, com.herong.getCelsius, and com.herong.getInfo.
  • com.herong.hello has one signature: return type of string, and no input argument. Its handler function is defined inside the add_method call.
  • com.herong.getCelsius has two signatures. The first one requires no input argument, and the second one requires one "double" argument.
  • com.herong.getInfo has one signature of returning an 'array'. The corresponding handler function returns a reference to a list. The server knows how to convert the content of the reference to an 'array'.

If you run the RpcXmlServer.pl in a command window, you will see:

Adding com.herong.hello...
Adding com.herong.getCelsius...
Adding com.herong.getInfo...
Listening at localhost:8001...

To test my server program, I wrote the following client program, RpcXmlClient.pl:

#- RpcXmlClient.pl
#- Copyright (c) 2005 by Dr. Herong Yang
#
   require RPC::XML;
   require RPC::XML::Client;
   my $client = RPC::XML::Client->new('http://localhost:8001');
#
   print "\nCalling system.listMethods...\n";
   my $res = $client->send_request('system.listMethods');
   print "   Response class = ".(ref $res)."\n";
   print "   Response type = ".$res->type."\n";
   print "   Response string = ".$res->as_string."\n";
   print "   Response value = ".$res->value."\n";
   foreach (@{$res->value}) {
      print "      $_\n";
   }
#
   print "\nCalling system.methodSignature...\n";
   my $req = RPC::XML::request->new('system.methodSignature',
      RPC::XML::string->new('system.listMethods'));
   $res = $client->send_request($req);
   print "   Response type = ".$res->type."\n";
   print "   Response string = ".$res->as_string."\n";
   print "   Response value = ".$res->value."\n";
   foreach (@{$res->value}) {
      print "      ".join(', ',@$_)."\n";
   }
#
   print "\nCalling com.herong.hello...\n";
   my $res = $client->send_request('com.herong.hello');
   print "   Response string = ".$res->as_string."\n";
   print "   Response value = ".$res->value."\n";
#
   print "\nCalling com.herong.getCelsius...\n";
   my $res = $client->send_request('com.herong.getCelsius');
   print "   Response value = ".$res->value."\n";
#
   print "\nCalling com.herong.getCelsius...\n";
   my $req = RPC::XML::request->new('com.herong.getCelsius',
      RPC::XML::double->new('100.0'));
   $res = $client->send_request($req);
   print "   Response value = ".$res->value."\n";
#
   print "\nCalling com.herong.getInfo...\n";
   my $res = $client->send_request('com.herong.getInfo');
   print "   Response value = ".$res->value."\n";
   foreach (@{$res->value}) {
      print "      $_\n";
   }
exit;

If you run the RpcXmlClient.pl in another command window, you will see:

Calling system.listMethods...
   Response class = RPC::XML::array
   Response type = array
   Response string = <array><data><value><string>com.herong.getCelsiu
s</string></value><value><string>com.herong.getInfo</string></value><
value><string>com.herong.hello</string></value><value><string>system.
identity</string></value><value><string>system.introspection</string>
</value><value><string>system.listMethods</string></value><value><str
ing>system.methodHelp</string></value><value><string>system.methodSig
nature</string></value><value><string>system.multicall</string></valu
e><value><string>system.status</string></value></data></array>
   Response value = ARRAY(0x232ddc8)
      com.herong.getCelsius
      com.herong.getInfo
      com.herong.hello
      system.identity
      system.introspection
      system.listMethods
      system.methodHelp
      system.methodSignature
      system.multicall
      system.status

Calling system.methodSignature...
   Response type = array
   Response string = <array><data><value><array><data><value><string>
array</string></value></data></array></value><value><array><data><val
ue><string>array</string></value><value><string>string</string></valu
e></data></array></value></data></array>
   Response value = ARRAY(0x232d338)
      array
      array, string

Calling com.herong.hello...
   Response string = <string>Hello world!</string>
   Response value = Hello world!

Calling com.herong.getCelsius...
   Response value = -17.7777777777778

Calling com.herong.getCelsius...
   Response value = 37.7777777777778

Calling com.herong.getInfo...
   Response value = ARRAY(0x2244aa8)
      Author
      Herong Yang
      Version
      2005

(Continued on next part...)

Part:   1  2  3   4 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part B - RPC::XML Module