initial load of upstream version 1.06.32
[xmlrpc-c] / examples / xmlrpc_sample_add_server.c
1 /* A simple standalone XML-RPC server written in C. */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #ifndef WIN32
6 #include <unistd.h>
7 #endif
8
9 #include <xmlrpc-c/base.h>
10 #include <xmlrpc-c/server.h>
11 #include <xmlrpc-c/server_abyss.h>
12
13 #include "config.h"  /* information about this build environment */
14
15 static xmlrpc_value *
16 sample_add(xmlrpc_env *   const env, 
17            xmlrpc_value * const param_array, 
18            void *         const user_data ATTR_UNUSED) {
19
20     xmlrpc_int32 x, y, z;
21
22     /* Parse our argument array. */
23     xmlrpc_decompose_value(env, param_array, "(ii)", &x, &y);
24     if (env->fault_occurred)
25         return NULL;
26
27     /* Add our two numbers. */
28     z = x + y;
29
30     /* Sometimes, make it look hard (so client can see what it's like
31        to do an RPC that takes a while).
32     */
33     if (y == 1)
34         sleep(2);
35
36     /* Return our result. */
37     return xmlrpc_build_value(env, "i", z);
38 }
39
40
41
42 int 
43 main(int           const argc, 
44      const char ** const argv) {
45
46     xmlrpc_server_abyss_parms serverparm;
47     xmlrpc_registry * registryP;
48     xmlrpc_env env;
49
50     if (argc-1 != 1) {
51         fprintf(stderr, "You must specify 1 argument:  The TCP port "
52                 "number on which the server will accept connections "
53                 "for RPCs.  You specified %d arguments.\n",  argc-1);
54         exit(1);
55     }
56     
57     xmlrpc_env_init(&env);
58
59     registryP = xmlrpc_registry_new(&env);
60
61     xmlrpc_registry_add_method(
62         &env, registryP, NULL, "sample.add", &sample_add, NULL);
63
64     /* In the modern form of the Abyss API, we supply parameters in memory
65        like a normal API.  We select the modern form by setting
66        config_file_name to NULL: 
67     */
68     serverparm.config_file_name = NULL;
69     serverparm.registryP        = registryP;
70     serverparm.port_number      = atoi(argv[1]);
71     serverparm.log_file_name    = "/tmp/xmlrpc_log";
72
73     printf("Running XML-RPC server...\n");
74
75     xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(log_file_name));
76
77     /* xmlrpc_server_abyss() never returns */
78
79     return 0;
80 }