initial load of upstream version 1.06.32
[xmlrpc-c] / examples / xmlrpc_socket_server.c
1 /* A simple standalone XML-RPC server written in C as an example of use of
2    the Xmlrpc-c libraries.
3
4    This example expects an already bound socket on Standard Input, ready to
5    be listened on for client connections.  Also see xmlrpc_sample_add_server,
6    which is the same thing, except you tell it a TCP port number and it
7    creates the socket itself.
8  */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #ifndef WIN32
13 #include <unistd.h>
14 #endif
15
16 #include <xmlrpc-c/base.h>
17 #include <xmlrpc-c/server.h>
18 #include <xmlrpc-c/server_abyss.h>
19
20 #include "config.h"  /* information about this build environment */
21
22 static xmlrpc_value *
23 sample_add(xmlrpc_env *   const env, 
24            xmlrpc_value * const param_array, 
25            void *         const user_data ATTR_UNUSED) {
26
27     xmlrpc_int32 x, y, z;
28
29     /* Parse our argument array. */
30     xmlrpc_decompose_value(env, param_array, "(ii)", &x, &y);
31     if (env->fault_occurred)
32         return NULL;
33
34     /* Add our two numbers. */
35     z = x + y;
36
37     /* Sometimes, make it look hard (so client can see what it's like
38        to do an RPC that takes a while).
39     */
40     if (y == 1)
41         sleep(2);
42
43     /* Return our result. */
44     return xmlrpc_build_value(env, "i", z);
45 }
46
47
48
49 int 
50 main(int           const argc, 
51      const char ** const argv) {
52
53     xmlrpc_server_abyss_parms serverparm;
54     xmlrpc_registry * registryP;
55     xmlrpc_env env;
56
57     if (argc-1 != 0) {
58         fprintf(stderr, "There are no arguments.  You must supply a "
59                 "bound socket on which to listen for client connections "
60                 "as Standard Input\n");
61         if (argv) {} /* silence unused parameter warning */
62         exit(1);
63     }
64     
65     xmlrpc_env_init(&env);
66
67     registryP = xmlrpc_registry_new(&env);
68
69     xmlrpc_registry_add_method(
70         &env, registryP, NULL, "sample.add", &sample_add, NULL);
71
72     /* In the modern form of the Abyss API, we supply parameters in memory
73        like a normal API.  We select the modern form by setting
74        config_file_name to NULL: 
75     */
76     serverparm.config_file_name   = NULL;
77     serverparm.registryP          = registryP;
78     serverparm.log_file_name      = "/tmp/xmlrpc_log";
79     serverparm.keepalive_timeout  = 0;
80     serverparm.keepalive_max_conn = 0;
81     serverparm.timeout            = 0;
82     serverparm.dont_advertise     = FALSE;
83     serverparm.socket_bound       = TRUE;
84     serverparm.socket_handle      = STDIN_FILENO;
85
86     printf("Running XML-RPC server...\n");
87
88     xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(socket_handle));
89
90     return 0;
91 }