|
Tcl XML-RPC Server Utility Package |
![]() |
|||||||||||||||||
|
The SOAP::Domain package makes in easy to provide a web service implementation in TclHTTPD and to provide both SOAP and XML-RPC access. Here is a simple example that can be placed in the custom/ subdirectory of tclhttpd that will provide a simple web service. package require SOAP::Domain SOAP::Domain::register -prefix /rpc proc square {num} { if { [catch {expr $num + 0}] } { error "parameter num must be a number" } return [expr $num * $num] } SOAP::export square XMLRPC::export square The first line loads in SOAP service support for Tclhttpd. Then we register a url prefix with the server that will be redirected to call our web service methods. We then define a method and then we use the export commands to state that this is a public procedure. We do not, after all, want people to call any command in the interpreter. More complex methods are likely to need to specify the return variable type. As tcl doesn't have types we have a package to assist us and to tag the return value with an XML-RPC type. For instance, one of the XML-RPC types is a date-time point: package require rpcvar proc date {} { set result [clock format [clock seconds] -format {%Y%m%dT%H:%M:%S}] return [rpcvar::rpcvar "dateTime.iso8601" $result] } SOAP::export date XMLRPC::export date The result here is initially a string formatted in the iso8601 point in time format. As there is no real way for Tcl to distinguish between this string and a non-date string we have to provide a hint so that the XML-RPC reply has the correct type information. For simple types: int, double, string the framework can guess the correct type. For other types, you will need to return a TypedVariable as the result. A more significant example of this is a method returning an array: proc sort {lst} { return [rpcvar::rpcvar array [lsort $lst]] }Here, we are returning an array of sorted elements. The type of each element will have to be guessed by the framework as the elements themselves cannot be provided with a type. XML-RPC supports struct results as well as arrays. This is supported from Tcl as Tcl arrays. The struct element name is set from the array element name. For more details about writing services look at the SOAP::Domain documentation. | ||||||||||||||||||
$Id: XMLRPCDomain.html,v 1.3.2.1 2004/03/08 12:07:46 patthoyts Exp $ |