Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC."— Presentation transcript:

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

2 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 II 2. XML-RPC

3 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 Linux Windows Private Network App A App B App A App B Client Server Client XML-RPC EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

4 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 Response Headline information Other App Headline EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

5 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 II 2. XML-RPC Three Main Parts of XML-RPC

6 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 II 2. XML-RPC Basic Type namesDescription int or i432-bit integers double64-bit floating pt booleantrue(1) or false(0) stringASCII text, usually support Unicode dateTime.iso8601Dates in ISO8601 format: CCYYMMDDTHH:MM:SS base64Binary defined as in RFC2045

7 7 Some examples: – 27 – 27.31415 – 1 – Hello – 20020125T02:20:04 – SGVsbG8sIFdvcmxkIQ== These basic types are always enclosed in value elements -12.45 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

8 8 These basic types can be combined into two more complex types: Array and Struct EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC This is an array. This is an array.

9 9 Arrays can contain mixtures of different types 1 Chan Tai-Man -91 0.1234 1 Chan Tai-Man -91 0.1234 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

10 10 Arrays can be multi- dimensional E.g. 10 20 30 40 10 20 30 40 First Row Second Row EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC

11 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 II 2. XML-RPC

12 12 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC givenName Tai-Man familyName Chan age 27 givenName Tai-Man familyName Chan age 27 First member of the struct Second member of the struct Third member of the struct

13 13 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC Name a attributes href http://ex.com target _top Name a attributes href http://ex.com target _top Nest a struct inside a struct We can also nest an array inside a struct

14 14 XML-RPC Request Structure EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC XML-RPC requests are a combination of XML content and HTTP headers – HTTP header: a wrapper for passing the request over the Web POST /xmlrpc HTTP 1.0 User-Agent: myXMLRPCClient/1.0 Host: 192.168.124.2 Content-Type: text/html Content-Length: 169 : XML statements : POST /xmlrpc HTTP 1.0 User-Agent: myXMLRPCClient/1.0 Host: 192.168.124.2 Content-Type: text/html Content-Length: 169 : XML statements : – XML content: pass parameters and identify the procedure to be called For example

15 15 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC circleArea 2.42 10 20 circleArea 2.42 10 20 The XML statements contain the method name and passed parameters Input parameters: a double and an array Method to be called

16 16 XML-RPC Response Structure EIE424 Distributed Systems and Networking Programming –Part II 2. 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 OK Date: Sat, 06 Oct 2004 23:20:04 GMT Server: Apache.1.3.12 (Unix) Connection: close Content-Type: text/html Content-Length: 124 : XML statements : HTTP/1.1 200 OK Date: Sat, 06 Oct 2004 23:20:04 GMT Server: Apache.1.3.12 (Unix) Connection: close Content-Type: text/html Content-Length: 124 : XML statements : For example

17 17 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC 2.42 2.42 No such method! No such method! Return a double number Return a fault, no standardized error code

18 18 EIE424 Distributed Systems and Networking Programming –Part II 2. XML-RPC code 26 message No such method! code 26 message No such method! Return a struct The error code is created by the app. Can only be understood in this app. Return a struct The error code is created by the app. Can only be understood in this app.

19 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 II 2. XML-RPC

20 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 II 2. XML-RPC

21 21 EIE424 Distributed Systems and Networking Programming –Part II 2. 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

22 22 EIE424 Distributed Systems and Networking Programming –Part II 2. 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)

23 23 EIE424 Distributed Systems and Networking Programming –Part II 2. 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

24 24 EIE424 Distributed Systems and Networking Programming –Part II 2. 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

25 25 EIE424 Distributed Systems and Networking Programming –Part II 2. 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 port number for the server

26 26 EIE424 Distributed Systems and Networking Programming –Part II 2. 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;

27 27 EIE424 Distributed Systems and Networking Programming –Part II 2. 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

28 28 EIE424 Distributed Systems and Networking Programming –Part II 2. 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

29 29 EIE424 Distributed Systems and Networking Programming –Part II 2. 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/jpeg Connection: keep-alive area.circleArea 3.0 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/jpeg Connection: keep-alive area.circleArea 3.0 HTTP header XML messages

30 30 EIE424 Distributed Systems and Networking Programming –Part II 2. 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 3 The area of the circle would be: 28.274 … >java AreaClient 4 The area of the circle would be: 50.265 … >java AreaClient 3 The area of the circle would be: 28.274 … >java AreaClient 4 The area of the circle would be: 50.265 …

31 31 EIE424 Distributed Systems and Networking Programming –Part II 2. 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


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

Similar presentations


Ads by Google