Showing posts with label XML-RPC. Show all posts
Showing posts with label XML-RPC. Show all posts

Sunday, June 10, 2007

Keyword arguments in XML-RPC

This isn't the least bit novel, for example I know I've been using this trick for years, but nonetheless here is a way to simulate named arguments in XML-RPC. XML-RPC only natively supports positional parameters, but by passing a single positional argument that is itself an XML-RPC struct (which is actually a mapping), you can simulate named and/or optional arguments. Rather than reproduce a sample XML-RPC document demonstrating this usage, I'll refer you to one of my earlier posts that utilized this technique; you'll see that the method is called with two named parameters: path and args.

If you are familiar with perl, you may also be aware of the trick perl 5, which also only natively supports positional arguments, uses to simulate named parameters. In perl 5, it is common to pass a hash of name/value pairs as arguments. However, what perl actually does under the scenes, and which is different from this XML-RPC trick, is to serialize the hash into an array of alternating names and values; it then passes this array as the positional argument list for the subroutine being called. The called subroutine then de-serializes the name and value pairs from the argument array, reconstructing the original hash. This flattening of a hash has to be a documented protocol between the subroutine and its callers.

Of course, you could do exactly the same thing using XML-RPC: serialize the argument dictionary into an array of alternating names and values and populate the method's param list with this array's elements. The XML-RPC server method could then reconstruct the original dictionary from the param list.

But XML-RPC also supports passing dictionaries and dictionaries: using the struct data type. Hence my original suggestion. Since to support named (or generic optional arguments) we have to document a protocol between the caller and the method, we might as well make the protocol as straightforward as possible. Rather than serialize and deserialize a dictionary of named arguments, just pass the dictionary as-is, as the one and only positional argument.

Thursday, May 24, 2007

XML-RPC patented

Ever being the astute one, I just now discovered that webMethods was awarded a patent for XML-RPC back in April of last year. They don't seem to be cracking down yet, but could XML-RPC be the next GIF/LZW controversy?

It would be hard to compose an unencumbered alternative to XML-RPC when the first claim of the patent reads:
A method of communicating between first and second machines, said method comprising the steps of: generating a message at a first machine including at least one argument and a type label for said argument; and transmitting said message from said first machine.

Since S.O.A.P was developed by Microsoft it goes without saying but that is patented too. I guess I need to start converting my XML-RPC clients and servers to JSON-RPC to be on the safe side.

Thursday, May 17, 2007

XML-RPC namespace

Is it just me or does XML-RPC appear to not have a defined XML namespace?

I realize that most people are using XML-RPC as the entire XML document, so namespaces probably don't matter to them. But I'm implementing a job scheduler in which I am separating the scheduling logic from the job handling logic. I do this by defining a job as an XML document consisting of scheduling parameters and an XML-RPC method invocation. When it is time to execute the job, the scheduler acts as an XML-RPC client, making the method call to the job handler process (which is an XML-RPC server). In this context, my job's XML document consists of tags from two namespaces: my job scheduler tags and XML-RPC tags. XML namespaces solve the ambiguity, but XML-RPC doesn't appear to have a well-defined namespace.

Since this is a slighty out-of-the-ordinary usage of XML-RPC, perhaps an example would help illustrate the problem:

<?xml version="1.0" ?>
<job xmlns="http://www.nttmcl.com/kelly/jobsched">
<!-- Schedules are expressed in ISO-8601 notation. -->
<!-- For example, the following schedule says to
repeat 5 times 1 minute apart, starting on May
18th 2007 at midnight UTC. -->
<schedule>
R5/2007-05-18T00:00:00Z/PT1M
</schedule>
<action>
<methodCall xmlns="">
<methodName>ExecProcess</methodName>
<params><param><value>
<struct>
<member>
<name>path</name>
<value>/bin/echo</value>
</member>
<member>
<name>args</name>
<value>
<array><data>
<value>This is a test</value>
</data></array>
</value>
</member>
</struct>
</value></param></params>
</methodCall>
</action>
</job>

As you can see, currently, I'm just resetting the XML namespace for the XML-RPC <methodCall> tag (and all nested sub-tags) to an empty namespace. If XML-RPC has a well-defined namespace, however, I would feel much better using that. Please, if anyone is aware of the proper namespace declaration for XML-RPC please let me know. If not, I will gladly offer a unique URL in my personal domain for use as the official XML-RPC identifier: http://www.posi.net/xml-rpc/.