23 XML RPC

Dr M. Vijayalakshmi

    RPC

 

Remote Procedure Call (RPC) is a model that specifies how cooperating processes on different nodes in a heterogeneous computing environment can communicate and coordinate activities. The paradigm of RPC is based on the concept of a procedure call in a higher level programming language. The semantics of RPC are almost identical to the semantics of the traditional procedure call. The major difference is that while a normal procedure call takes place between procedures of a single process in the same memory space on a single system, RPC takes place between processes on clients and servers in a heterogeneous computing environment.

 

Ordinary procedure call

 

The function calls in any programming paradigm corresponds to an ordinary procedure call. There are two ways of passing parameters while making these function or procedure calls.

 

Value parameters

     The actual value of the parameter is placed on the stack. This can then be used and modified by the procedure without any change to any original variable.

 

Reference parameters

 

The address of the parameter is passed into the procedure. Any use of the parameter within the procedure uses the address to access or change the value.

 

Remote Procedure Call

 

The remote procedure call acts like a procedure call, but act across the network. The process makes a remote procedure call by pushing its parameters and a return address onto the stack, and jumping to the start of the procedure. The procedure itself is responsible for accessing and using the network. After the remote execution is over, the procedure jumps back to the return address. The calling process then continues.

 

What is XML-RPC?

 

It is a simple protocol that uses XML messages to perform RPC. Request is encoded in XML and send via HTTP. Response are encoded in XML and received via HTTP. It is an easiest way to get started with web services.

 

XML-RPC provides an XML and HTTP-based mechanism for making method or function calls across a network. It uses XML for messaging (use only a small XML vocabulary set) and HTTP to pass information from a client computer to a server computer. There is no notion of objects and no mechanism for including information that uses other XML vocabularies. It emerged in early 1998.

 

Developing With XML-RPC

 

In real applications, we do not need to directly program the XML statements. We only need to add an XML-RPC library and making some function calls through this library. For example, the Apache XML Project’s Apache XML-RPC has provided packages that make integrating XML-RPC with Java easier.

 

Apache XML-RPC

 

It provides an automated registration process for adding methods to the XML-RPC server. It provides a built-in server that only speaks XML-RPC, reducing the need to create full-blown servlets. A client package that makes calling remote methods fairly simple.

   Prepare for XML-RPC Services

 

 

Step 1: Develop the Function

 

 

We have developed a normal function that returns the area (double) of a circle based on the input radius.The name of the Function is circleArea. Input parameter to the function is the radius (double).

 

Step2 : Import Libraries

 

First, import the following libraries to work with XML RPC.

 

 

Step3: Create Server and Register Function

 

Then develop the server program that would create the Web server. We can then register the class to the server and start the server.

 

 

Step 4: Execute the Server

 

To fire up the server, just execute the class from the command line, specifying a port number.Assume that the class has been compiled and the file AreaServer.class has been generated.

 

Step5: Developing the Client Program

 

To call the function registered in the server, a tailor-made client program is required.

We also need to use the XML-RPC libraries:

 

import java.io.IOException;

import java.util.Vector;

import org.apache.xmlrpc.XmlRpc;

import org.apache.xmlrpc.XmlRpcClient;

import org.apache.xmlrpc.XmlRpcExce ption;

  Client Program

 

Example

 

 

First we can create an XMLRPC client and connect to the server at port 8899. We then call the server function using the registered name “area” and with the function name “circeArea”. The area returned from the server is then printed to the client.

 

 

HTTP Request

 

By executing this program, a HTTP request will be generated. Following are the parameters in the HTTP request and the value is padded from the client to the server as XML messages.

 

 

Results

 

If everything is fine, the server will send back the method Response and embedded inside the HTTP response. Assume the client program is compiled and the AreaClient.class is generated. The result looks pretty simple.

 

 

The Execution Model

 

XML-RPC works in the principle of the following model as shown in Figure 25.2.

 

 

       Steps in the Model

  • The client calls the local stub procedure. The stub packages up the parameters into a network message. This is called marshalling.
  • Networking functions in the O/S kernel are called by the stub to send the message.
  • The kernel sends the message(s) to the remote system. This may be connection-oriented or connectionless.
  • A server stub unmarshals the arguments from the network message.
  • The server stub executes a local procedure call.
  • The procedure completes, returning execution to the server stub.
  • The server stub marshals the return values into a network message.
  • The return messages are sent back.
  • The client stub reads the messages using the network functions.
  • The message is unmarshalled and the return values are set on the stack for the local process.

    Basic process for building server

 

A server program defines the server’s interface using an Interface Definition Language (IDL). The IDL specifies the names, parameters, and type for all server procedures. A stub compiler reads the IDL and produces two stub procedures for each server procedure: a client-side stub and a server-side stub. In the server side, the server program is linked with the server-side stub; and in the client side, the client program is linked with the client-side stub.

 

Stub

 

The Client side stub

 

The Client Stub is the one that connects to the remote machine, send all the parameters down to it, wait for replies, do the right thing to the stack and returns.

 

The Server side stub

 

The Server Stub reads the parameters, and present them in a suitable form to execute the procedure locally. After execution, it has to send the results back to the calling process.

 

RPC Binding

 

The server, when starting up, exports its interface, identifying itself to a network name server and telling the local runtime its dispatcher address. The client, before issuing any calls, imports the server, which causes the RPC runtime to look up the server through the name service and contact the requested server to setup a connection.

 

Request example

 

The basic template for an XML-RPC request would look like this,

 

POST /target HTTP 1.0

User-Agent: Identifier

Host: host.making.request

Conte nt-Type: text/xml

Content-length: 181

An example request could be

POST /xmlrpc HTTP 1.0

User-Agent: myXMLRPCClient/1.0

Host: 192.168.124.2

Content-Type: text/xml

Content-Length: 169

<?xml version=”1.0″?>

<methodCall>

<methodName>examples.getStateName</methodName>

<params> <param> <value><i4>41</i4></value></param></params> </methodCall>

 

Header requirements

 

The format of the URI is not specified. For example, it could be empty, a single slash, if the server is only handling XML-RPC calls. However, if the server is handling a mix of incoming HTTP requests, we allow the URI to help route the request to the code that handles XML-RPC requests ( Here the URI is mentioned as RPC2, telling the server to route the request to the “RPC2” responder). A User-Agent and Host must be specified. The Content-Type is text/xml. The Content-Length must be specified and must be correct.

 

Advantages of XML-RPC

 

XML-RPC provides the following advantages,

 

•      Firewalls.

 

Firewall software can watch for POSTs whose Content-Type is text/xml.

 

•      Discoverability.

 

It should be possible for an HTML coder to be able to look at a file containing an XML-RPC procedure call, understand what it’s doing.

 

•      Easy to implement.

 

It should be easy to implement protocol that could quickly be adapted to run in other environments or on other operating systems.

 

Disadvantages of XML-RPC

 

XML-RPC provides the following disadvantages,

  • XML-RPC is impoverished in the type of data it can transmit and obese in its message size.
  • XML-RPC transmits messages lacking statefulness and incurs channel bottlenecks.
  • Compared to SOAP, XML-RPC lacks both important security mechanisms and a robust object model.
  • As a data representation, XML-RPC is slow, cumbersome, and incomplete compared to native programming language mechanisms like Java.

Summary

 

This module explains in detail about XML-RPC services. It has explored the mechanism of XML-RPC with understandable examples. Finally, we have discussed about the advantages and disadvantages of XML-RPC compared to other models.

 

References

  1. http://www.scottandrew.com/xml-rpc/blogger/
  2. http://www.xmlrpc.com