CS 580 Client-Server Spring Semester, 2004 XML-RPC |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 15-Jan-04 |
RPC
Remote Procedure Call
A client can "directly" call a function or procedure on the server
Issues
XML-RPC
RPC using
Smalltalk Example
| client sum | client :=XmlRpcClient url: 'http://xmlrpc.usefulinc.com/demo/server.php'. sum := client perform: 'examples.addtwo' with: 5 with: 3. ^sum
Java Example import java.util.*; import org.apache.xmlrpc.*; public class XmlRpcExample { public static void main (String args[]) { try { XmlRpcClient xmlrpc = new XmlRpcClientLite ("http://xmlrpc.usefulinc.com/demo/server.php"); Vector parameters = new Vector (); parameters.addElement (new Integer(5) ); parameters.addElement (new Integer(3) ); Integer sum = (Integer) xmlrpc.execute ("examples.addtwo", parameters); System.out.println( sum.intValue() ); } catch (java.net.MalformedURLException badAddress) { badAddress.printStackTrace( System.out); } catch (java.io.IOException connectionProblem) { connectionProblem.printStackTrace( System.out); } catch (Exception serverProblem) { serverProblem.printStackTrace( System.out); } }}
Some Client Details Java Client
Main Page: http://www.xmlrpc.com/
Download page: http://xml.apache.org/xmlrpc/download.html
JavaDoc: http://xml.apache.org/xmlrpc/apidocs/index.html
Two Clients
XmlRpcClient
Important Methods
XmlRpcClient(String serverURL)
Smalltalk Client
Download page: http://www.eli.sdsu.edu/SmalltalkCode/xmlrpc/index.html
XmlRpcClient class>>url: serverURL
XmlRpcClient>>perform: aMethodName XmlRpcClient>>perform: aMethodName with: oneArgument XmlRpcClient>> perform: aMethodName with: firstArgument with: secondArgument XmlRpcClient>> perform: aMethodName withArguments: collectionOfArguments
Issues
Client program has to know
XML-RPC
data type
|
Java
|
Smalltalk |
<i4>
or <int>
|
java.lang.Integer
|
SmalltInteger |
<boolean>
|
java.lang.Boolean
|
true,
false
|
<string>
|
java.lang.String
|
String
|
<double>
|
java.lang.Double
|
Float
|
<dateTime.iso8601>
|
java.util.Date
|
Timestamp |
<struct>
|
java.util.Hashtable
|
Dictionary
|
<array>
|
java.util.Vector
|
OrderedCollection
|
<base64>
|
byte[
]
|
ByteArray |
Smalltalk
|
XML-RPC |
Integer
(including Large)
|
<int> |
Number |
<double> |
SequenceableCollection |
<array> |
Smalltalk |
XML-RPC |
Object |
<object> |
Nil |
<nil> |
How do you know about methods on the Server?
The server:
How does this work?
Client marshals (serialize) the rpc request
Complete Request sent to Server
POST /demo/server.php HTTP/1.1 Host: xmlrpc.usefulinc.com Content-length: 190 Content-type: text/xml;charset=iso-8859-1 User-Agent: Smalltalk XMLRPC version 0.5 (VisualWorksÆ NonCommercial, Release 7 of June 14, 2002) Connection: keep-alive <?xml version="1.0"?> <methodCall> <methodName>examples.addtwo</methodName> <params> <param> <value><int>5</int></value> </param> <param> <value><int>3</int></value> </param> </params> </methodCall>
XmlRpc Servers
Java Example
The following starts an addtwo server on port 8080
Server URL is serverMachinename:8080
Method name is: examples.addtwo
How come the server is still running after the last println?
import org.apache.xmlrpc.*; public class JavaServer { public Integer addtwo(int x, int y) { return new Integer( x + y); } public static void main( String[] args) { try { System.out.println("Starting server on port 8080"); WebServer addTwoServer = new WebServer(8080); addTwoServer.addHandler("examples", new JavaServer()); System.out.println("server running"); } catch (Exception webServerStartError) { System.err.println( "JavaServer " + webServerStartError.toString()); } } }
Smalltalk Server Example
Smalltalk defineClass: #WaveXmlRpcAddtwo superclass: #{Smalltalk.WaveXmlRpcServer} indexedType: #none private: false instanceVariableNames: '' classInstanceVariableNames: '' imports: '' category: 'Network-XMLRPC-Server' WaveXmlRpcAddtwo class methodsFor: 'utility' allowedServerMethods "Answer a dictionary Key is the string the client will use to request the method Value is the symbol for the method to actually call" ^(Dictionary new) at: 'examples.addtwo' put: #add:to:; yourself WaveXmlRpcAddtwo methodsFor: 'accessing' add: anInteger to: aSecondInteger ^anInteger + aSecondInteger
Notice
We have not explicitly handled sockets in any example
How to Start the Smalltalk Server
File in the XmlRpcServer parcel
The parcel contains the WaveXmlRpcAddtwo class and loads the Wave web server. When this is done the Server Console window will open up. It looks like:
Click on the “Create Server” button. The window will expand as seen below.
Set the server type to “TinyHttpServer”, set the Hostname to the name or IP of the machine you are running the server on, set the port to the port you wish the server to listen to. Then click the “Create and Start” button on the bottom of the window.
Now click on the “Edit Resolver” button. The bottom of the window will expand to look like:
Click on the “Add Path” button.
Set the “Resolver type” to WaveXmlRpcAddtwo. Note any subclass of WaveXmlRpcServer will appear in the resolver type drop down menu. Add a path. I used RPC2, which was common when XML-RCP was new. Click on the “Accept” button.
In the above example
Server URL is: http://192.168.1.101:8008/RPC2
Method name is: examples.addtwo
This sever is not running so you will not be able to connect to it.
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 15-Jan-04    Next