31
1 EIE424 Distributed Systems and Networking Programming – Part II 2. XML-RPC 2. XML-RPC

1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

Embed Size (px)

Citation preview

Page 1: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

1

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

2. XML-RPC

Page 2: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

2

What is XML-RPC?

XML-RPC provides an XML- and HTTP-based mechanism for making method or function calls across a network– Use XML for messaging (use only a small XML

vocabulary set) – Use HTTP to pass information from a client computer

to a server computer No notion of objects No mechanism for including information that

uses other XML vocabularies Emerged in early 1998

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 3: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

3

Why XML-RPC? As distributed systems become more common,

there is a need to integrate the applications in different computers, even within a company

XML-RPC provides a simple tool to connect disparate parts inside a private network

LinuxWindowsPrivate

Network

App A

App B

App A

App B

Client

Server

Server

Client

XML-RPC XML-RPC

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 4: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

4

XML-RPC can also provide a simple interface to a computer for other computers in the world to access

Similar to the case that a Web page is a human readable interface of a computer

Web Server

HTTP GET

HTTP response with HTML page

XML-RPC Server

XML-RPC Client

oreillynet.com

XML-RPC Request

XML-RPC ResponseHeadline information

Other App

Headline

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 5: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

5

Data Model– A set of type for passing parameters, return values

and faults (error messages)– Use by both request and response

Request Structure– An HTTP post request containing method name and

parameter information Response Structure

– An HTTP response containing return values or fault information

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Three Main Parts of XML-RPC

Page 6: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

6

XML-RPC Data Model Define only 6 basic data types and 2 compound

data types Seems to be enough for most practical applications

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Basic Type names Description

int or i4 32-bit integers

double 64-bit floating pt

boolean true(1) or false(0)

string ASCII text, usually support Unicode

dateTime.iso8601 Dates in ISO8601 format: CCYYMMDDTHH:MM:SS

base64 Binary defined as in RFC2045

Page 7: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

7

Some examples:– <int>27</int>– <double>27.31415</double>– <boolean>1</boolean>– <string>Hello</string>– <dateTime.iso8601>20020125T02:20:04

</dateTime.iso8601>– <base64>SGVsbG8sIFdvcmxkIQ==</base64>

These basic types are always enclosed in value elements <value>

<double> -12.45 </double>

</value>

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 8: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

8

These basic types can be combined into two more complex types: Array and Struct

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<value> <array> <data> <value><string>This </string></value> <value><string>is </string></value> <value><string>an </string></value> <value><string>array.</string></value> </data> </array></value>

<value> <array> <data> <value><string>This </string></value> <value><string>is </string></value> <value><string>an </string></value> <value><string>array.</string></value> </data> </array></value>

Page 9: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

9

Arrays can contain mixtures of different types

<value> <array> <data> <value><boolean>1</boolean></value> <value><string>Chan Tai-Man </string></value> <value><int> -91 </int></value> <value><double>0.1234</double></value> </data> </array></value>

<value> <array> <data> <value><boolean>1</boolean></value> <value><string>Chan Tai-Man </string></value> <value><int> -91 </int></value> <value><double>0.1234</double></value> </data> </array></value>

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 10: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

10

Arrays can be multi-dimensional

E.g.

<value> <array> <data> <value> <array> <data> <value><int>10</int></value> <value><int>20</int></value> </data> </array> </value> <value> <array> <data> <value><int>30</int></value> <value><int>40</int></value> </data> </array> </value> </data> </array></value>

<value> <array> <data> <value> <array> <data> <value><int>10</int></value> <value><int>20</int></value> </data> </array> </value> <value> <array> <data> <value><int>30</int></value> <value><int>40</int></value> </data> </array> </value> </data> </array></value>

First Row

Second Row

4030

2010

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 11: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

11

Struct contains unordered content, identified by name

Names are string, although it is not necessary to enclose them by string elements

Each struct element contains a list of member elements

Member elements each contain one name element and one value element

The order of members is not important

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 12: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

12

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<value> <struct> <member> <name>givenName</name> <value><string>Tai-Man </string></value> </member> <member> <name>familyName</name> <value><string>Chan </string></value> </member> <member> <name>age</name> <value><int>27</int></value> </member> </struct></value>

<value> <struct> <member> <name>givenName</name> <value><string>Tai-Man </string></value> </member> <member> <name>familyName</name> <value><string>Chan </string></value> </member> <member> <name>age</name> <value><int>27</int></value> </member> </struct></value>

First member of the struct

Second member of the struct

Third member of the struct

Page 13: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

13

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<value> <struct> <member> <name>Name</name> <value><string>a</string></value> </member> <member> <name>attributes</name> <value><struct>

<member><name>href</name> <value><string>http://ex.com</string></value> </member> <member><name>target</name> <value><string>_top</string></value>

</member> </struct></value> </member> </struct></value>

<value> <struct> <member> <name>Name</name> <value><string>a</string></value> </member> <member> <name>attributes</name> <value><struct>

<member><name>href</name> <value><string>http://ex.com</string></value> </member> <member><name>target</name> <value><string>_top</string></value>

</member> </struct></value> </member> </struct></value>

Nest a struct inside a struct

We can also nest an array inside a struct

Page 14: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

14

XML-RPC Request Structure

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

XML-RPC requests are a combination of XML content and HTTP headers– HTTP header: a wrapper for passing the request

over the WebPOST /xmlrpc HTTP 1.0User-Agent: myXMLRPCClient/1.0Host: 192.168.124.2Content-Type: text/htmlContent-Length: 169

: XML statements

:

POST /xmlrpc HTTP 1.0User-Agent: myXMLRPCClient/1.0Host: 192.168.124.2Content-Type: text/htmlContent-Length: 169

: XML statements

:

– XML content: pass parameters and identify the procedure to be called

For example

Page 15: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

15

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<?xml version=“1.0”?><methodCall> <methodName>circleArea</methodName> <params> <param> <value><double>2.42</double></value> </param> <param> <value> <array> <data> <value><int>10</int></value> <value><int>20</int></value> </data> </array>

</value> </param> </params></methodCall>

<?xml version=“1.0”?><methodCall> <methodName>circleArea</methodName> <params> <param> <value><double>2.42</double></value> </param> <param> <value> <array> <data> <value><int>10</int></value> <value><int>20</int></value> </data> </array>

</value> </param> </params></methodCall>

The XML statements contain the method name and passed parameters

The XML statements contain the method name and passed parameters

Input parameters: a double and an array

Method to be called

Page 16: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

16

XML-RPC Response Structure

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

If a request is successful – the procedure was found, executed correctly, the result will be returned thru the response to the client

Similar to request, a response needs to be attached to a HTTP header for surfing on the Web

HTTP/1.1 200 OKDate: Sat, 06 Oct 2004 23:20:04 GMTServer: Apache.1.3.12 (Unix)Connection: closeContent-Type: text/htmlContent-Length: 124

: XML statements

:

HTTP/1.1 200 OKDate: Sat, 06 Oct 2004 23:20:04 GMTServer: Apache.1.3.12 (Unix)Connection: closeContent-Type: text/htmlContent-Length: 124

: XML statements

:For exampleFor example

Page 17: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

17

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<?xml version=“1.0”?><methodResponse> <params> <param> <value><double>2.42</double></value> </param> </params></methodResponse>

<?xml version=“1.0”?><methodResponse> <params> <param> <value><double>2.42</double></value> </param> </params></methodResponse>

<?xml version=“1.0”?><methodResponse> <fault> <value><string>No such method!</string></value> </fault></methodResponse>

<?xml version=“1.0”?><methodResponse> <fault> <value><string>No such method!</string></value> </fault></methodResponse>

Return a double number

Return a fault, no standardized error code

Page 18: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

18

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

<?xml version="1.0"?><methodResponse> <fault> <value> <struct> <member> <name>code</name> <value><int>26</int></value> </member> <member> <name>message</name> <value><string>No such method!</string> </value> </member> </struct> </value> </fault></methodResponse>

<?xml version="1.0"?><methodResponse> <fault> <value> <struct> <member> <name>code</name> <value><int>26</int></value> </member> <member> <name>message</name> <value><string>No such method!</string> </value> </member> </struct> </value> </fault></methodResponse>

Return a structThe error code is created by the app. Can only be understood in this app.

Return a structThe error code is created by the app. Can only be understood in this app.

Page 19: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

19

Developing With XML-RPC

In real applications, we do not need to directly program the XML statements

Only need to add an XML-RPC library and making some function calls thru this library

Hence can be done by using any programming languages, such as Java

For example, the Apache XML Project’s Apache XML-RPC has provided packages that make integrating XML-RPC with Java easier

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 20: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

20

Apache XML-RPC

Provide an automated registration process for adding methods to the XML-RPC server

Provide 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

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Page 21: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

21

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Prepare for XML-RPC Services

Develop the function

to be called by the client

Develop the function

to be called by the client

Create the server

Create the server

Register the function to the

server

Register the function to the

server

Start the server

Start the server

Page 22: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

22

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Develop The Function

public class AreaHandler {

public double circleArea(double radius) { double value = (radius*radius*Math.PI); return value; }}

public class AreaHandler {

public double circleArea(double radius) { double value = (radius*radius*Math.PI); return value; }}

• Just a normal function that returns the area (double) of a circle based on the input radius

• Function name: circleArea• Input parameter: the radius (double)

Page 23: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

23

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Create Server and Register Function

import java.io.IOException;import org.apache.xmlrpc.WebServer;import org.apache.xmlrpc.XmlRpc;

import java.io.IOException;import org.apache.xmlrpc.WebServer;import org.apache.xmlrpc.XmlRpc;

First, import the libraries

Page 24: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

24

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

public class AreaServer {

public static void main(String[] args) { try {

startServer(args); } catch (IOException e) {;} }

public static void startServer(String[] args) throws IOException { WebServer server = new WebServer(

Integer.parseInt(args[0])); server.addHandler(“area”,new AreaHandler()); server.start(); }}

public class AreaServer {

public static void main(String[] args) { try {

startServer(args); } catch (IOException e) {;} }

public static void startServer(String[] args) throws IOException { WebServer server = new WebServer(

Integer.parseInt(args[0])); server.addHandler(“area”,new AreaHandler()); server.start(); }}

Should input a port number

Convert to an integer

Register the class under the name “area”Start server

Page 25: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

25

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

To fire up the server, just execute the class from the command line – need specifying a port no.

Assume the class has been compiled and the file AreaServer.class has been generated

>java AreaServer 8899>java AreaServer 8899

port number for the server

Page 26: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

26

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Developing the Client Program To call the function registered in the server, a

tailor-made client program is required 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.XmlRpcException;

import java.io.IOException;import java.util.Vector;import org.apache.xmlrpc.XmlRpc;import org.apache.xmlrpc.XmlRpcClient;import org.apache.xmlrpc.XmlRpcException;

Page 27: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

27

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

public class AreaClient {

public static void main(String[] args) { AreaClient client = new AreaClient(); double radius = Double.parseDouble(args[0]); try {

double area = client.areaCircle(radius); System.out.println(“The area of the circle

would be: “ + area); } catch (IOException e) {;} catch (XmlRpcException e) {;} }

public double areaCircle (double radius) throws IOException, XmlRpcException {

: }}

public class AreaClient {

public static void main(String[] args) { AreaClient client = new AreaClient(); double radius = Double.parseDouble(args[0]); try {

double area = client.areaCircle(radius); System.out.println(“The area of the circle

would be: “ + area); } catch (IOException e) {;} catch (XmlRpcException e) {;} }

public double areaCircle (double radius) throws IOException, XmlRpcException {

: }}

Call areaCircle(), if OK, report the result

Cast to double

Page 28: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

28

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

public double areaCircle (double radius) throws IOException, XmlRpcException {

XmlRpcClient client = new XmlRpcClient(http://localhost:8899/); Vector params = new Vector(); params.addElement(new Double (radius));

Object result = client.execute(“area.circleArea”,params); String resultStr = result.toString(); double area = Double.parseDouble(resultStr); return area;}

public double areaCircle (double radius) throws IOException, XmlRpcException {

XmlRpcClient client = new XmlRpcClient(http://localhost:8899/); Vector params = new Vector(); params.addElement(new Double (radius));

Object result = client.execute(“area.circleArea”,params); String resultStr = result.toString(); double area = Double.parseDouble(resultStr); return area;}

Create a client and connect to the server at port 8899

Add a Double object to the Vector object

Call the server function. The registered name “area” and function name need to be known

Just to play safe

Page 29: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

29

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

By executing this program, a HTTP request will be generated. An example is shown as follows:

POST / HTTP/1.1 Content-Length: 175 Content-Type: text/xml User-Agent: Java1.3.0 Host: localhost:8899 Accept: text/html, image/gif, image/jpegConnection: keep-alive

<?xml version="1.0" encoding="ISO-8859-1"?> <methodCall><methodName>area.circleArea</methodName> <params> <param><value><double>3.0</double></value></param> </params> </methodCall>

POST / HTTP/1.1 Content-Length: 175 Content-Type: text/xml User-Agent: Java1.3.0 Host: localhost:8899 Accept: text/html, image/gif, image/jpegConnection: keep-alive

<?xml version="1.0" encoding="ISO-8859-1"?> <methodCall><methodName>area.circleArea</methodName> <params> <param><value><double>3.0</double></value></param> </params> </methodCall>

HTTP header

XML messages

Page 30: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

30

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

If everything is fine, the server will send back the methodResponse and embedded inside the HTTP response

Assume the client program is compiled and the AreaClient.class is generated

The result looks pretty simple

>java AreaClient 3The area of the circle would be: 28.274 …

>java AreaClient 4The area of the circle would be: 50.265 …

>java AreaClient 3The area of the circle would be: 28.274 …

>java AreaClient 4The area of the circle would be: 50.265 …

Page 31: 1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

31

EIE424

Distributed Systems and Networking Programming –Part II2. XML-RPC

Concluding Remarks Java is only a tool for us to generate the XML

messages Other programming languages can be used to do the

same. For example, Perl For XML-RPC, client needs to have some knowledge

of server’s registered functions No way for the server to publicize its functions or to

allow the client to automatically obtain such info Hence does not perfectly fit the requirements of Web

Services