File: hello_server.c

package info (click to toggle)
xmlrpc-epi 0.54.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,588 kB
  • sloc: sh: 12,254; ansic: 5,747; xml: 1,387; makefile: 166; php: 17
file content (91 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* This is a minimalist xmlrpc server.
 * This program reads an XML document from standard input and
 * says hello when the hello() method is called.
 */

#include <stdio.h>
#include <stdlib.h>
#include "xmlrpc.h"

#define VERBOSE 1

/* this function will be called by xmlrpc engine when registered method is found  */
/* typically, most developer time will be spent writing these types of functions. */
XMLRPC_VALUE hello_callback(XMLRPC_SERVER server, XMLRPC_REQUEST request, void* userData)
{
   char buf[1024];
#ifdef VERBOSE
   XMLRPC_VALUE xParams = XMLRPC_RequestGetData(request);   // obtain method params from request
   XMLRPC_VALUE xFirstParam = XMLRPC_VectorRewind(xParams); // obtain first parameter
   const char * name = XMLRPC_GetValueString(xFirstParam);  // get string value
#else
   const char* name = XMLRPC_GetValueString(XMLRPC_VectorRewind(XMLRPC_RequestGetData(request)));
#endif 

   snprintf(buf, sizeof(buf), "hello %s", name ? name : "stranger");
   return XMLRPC_CreateValueString(NULL, buf, 0);
}


/* with the exception of the registration calls, most everything in main
 * only needs to be written once per server.
 */
int main(int argc, char **argv)
{
  XMLRPC_SERVER  server;
  XMLRPC_REQUEST request=0;
  XMLRPC_REQUEST response;

  /* create a new server object */
  server = XMLRPC_ServerCreate();

  /* Register public methods with the server */
  XMLRPC_ServerRegisterMethod(server, "hello", hello_callback);

  /* Now, let's get the client's request from stdin.... */
  {
     char filebuf[4096];  // not that intelligent.  sue me.
     int len = fread(filebuf, sizeof(char), sizeof(filebuf)-1, stdin);

     if(len) {
        filebuf[len] = 0;

        // parse the xml into a request structure
        request = XMLRPC_REQUEST_FromXML((const char*)filebuf, len, NULL);
     }
  }

  if(!request) {
     fprintf(stderr, "bogus xmlrpc request\n");
     return 1;
  }


  /* create a response struct */
  response = XMLRPC_RequestNew();
  XMLRPC_RequestSetRequestType(response, xmlrpc_request_response);

  /* call server method with client request and assign the response to our response struct */
  XMLRPC_RequestSetData(response, XMLRPC_ServerCallMethod(server, request, NULL));

  /* be courteous. reply in same vocabulary/manner as the request. */
  XMLRPC_RequestSetOutputOptions(response, XMLRPC_RequestGetOutputOptions(request) );

  /* serialize server response as XML */
  if(1) {
     char *outBuf = XMLRPC_REQUEST_ToXML(response, 0);

     if(outBuf) {
        printf(outBuf);
        free(outBuf);
     }
  }

  // cleanup.  null safe.
  XMLRPC_RequestFree(request, 1);
  XMLRPC_RequestFree(response, 1);
  XMLRPC_ServerDestroy(server);

  return 0;
}