An Embeddable Lightweight XML-RPC Server
by M. Tim Jones

Listing One

result = xmlrpc_client_call( &env, "http://192.168.2.151/device",
           "getDeviceStats", "(ssi)", "username", "password", 0 );

Listing Two

POST /device HTTP/1.1
Accept: */*
TE: trailers
Host: 192.168.2.151
User-Agent: XML-RPC_Device_Data_Gathering_C_Client/1.0 libwww/5.3.2
Connection: TE,Keep-Alive
Date: Tue, 05 Mar 2002 11:24:53 GMT
Content-Length: 287
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>getDeviceStats</methodName>
<params>
<param><value><string>username</string></value></param>
<param><value><string>password</string></value></param>
<param><value><i4>0</i4></value></param>
</params>
</methodCall>

Listing Three

HTTP/1.1 200 OK
Connection: close
Content-length:  418
Content-Type: text/xml
Server: Lightweight XMLRPC

<?xml version="1.0"?>
<methodResponse>
  <params><param>
  <value><struct>
  <member>
    <name>status</name>
    <value><int>1</int></value>
  </member>
  <member>
    <name>lastCommand</name>
    <value><string>reboot</string></value>
  </member>
  <member>
    <name>currentState</name>
    <value><string>Normal Operation</string></value>
  </member>
  </struct></value>
  </param></params>
</methodResponse>


Listing Four
xmlrpc_parse_value( &env, result, "{s:i,s:s,s:s,*}",
                         "status", &status, "lastCommand", &lastCommand,
                         "currentState", &currentState );

Listing Five

extern int addMethod( int (*func)(xmlCall_t *), char *name );
extern int getDeviceStats( xmlCall_t *xmlCall );
ret = addMethod( getDeviceStats, "getDeviceStats" );

Listing Six

int getDeviceStats( xmlCall_t *xmlCall )
{
  char username[80], password[80];
  char lastCommand[80], curState[80];
  int request = 0, status, ret;

  /* Grab the arguments */
  do {
    ret = getStringArg( xmlCall, username );
    if (ret != XML_NO_ERROR) break;

    ret = getStringArg( xmlCall, password );
    if (ret != XML_NO_ERROR) break;

    ret = getIntegerArg( xmlCall, &request );
    if (ret != XML_NO_ERROR) break;

  } while (0);
  if (ret == XML_NO_ERROR) {

    /* Processing */
    /* Just dummy up some data... */
    status = 1;
    strcpy(lastCommand, "reboot");
    strcpy(curState, "Normal Operation");

    /* Generate XML response */
    ret = xmlBuildResponse( xmlCall, "{iss}", "status", status,
                    "lastCommand", lastCommand, "currentState", curState );
  }
  return ret;
}

Listing Seven

#!/bin/sh
PATH=$PATH:/usr/local/bin
CLIENT_CFLAGS=`xmlrpc-c-config libwww-client --cflags`
CLIENT_LIBS=`xmlrpc-c-config libwww-client --libs`
gcc $CLIENT_CFLAGS -o client client.c $CLIENT_LIBS


Listing Eight

#!/usr/bin/python
import xmlrpclib
server_url = 'http://192.168.2.151/device'
server = xmlrpclib.Server(server_url);

result = server.getDeviceStats( "username", "password", 0 );

print "Status : ", result['status']
print "Last Command was : ", result['lastCommand'];
print "Current State is : ", result['currentState'];




3


