Menu

[r3316]: / advanced / examples / compound_value_server.c  Maximize  Restore  History

Download this file

202 lines (142 with data), 5.7 kB

  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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* An XML-RPC server program written in C, as an example of using
compound XML-RPC values.
For a simple server program that just deals with integer values,
see xmlrpc_sample_add_server.c. This example focuses just on the
compound XML-RPC values and not the server functions.
This server provides the example.divide XML-RPC method that the example
client program compound_value_client.c invokes. See that program for
details on what the method does.
The program takes one argument: the HTTP port number on which the server
is to accept connections, in decimal.
Example:
$ ./compound_value_server 8080&
$ ./compound_value_client
*/
#define WIN32_LEAN_AND_MEAN /* required by xmlrpc-c/server_abyss.h */
#include <stdlib.h>
#include <stdio.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/server.h>
#include <xmlrpc-c/server_abyss.h>
#include "config.h" /* information about this build environment */
static void
computeQuotient(xmlrpc_env * const envP,
xmlrpc_value * const ratioP,
xmlrpc_double * const quotientP) {
xmlrpc_value * dividendP;
xmlrpc_struct_find_value(envP, ratioP, "DIVIDEND", &dividendP);
if (!envP->fault_occurred) {
if (!dividendP)
xmlrpc_env_set_fault(
envP, 0, "Structure is missing 'DIVIDEND' member");
else {
xmlrpc_value * divisorP;
xmlrpc_struct_find_value(envP, ratioP, "DIVISOR", &divisorP);
if (!envP->fault_occurred) {
if (!divisorP)
xmlrpc_env_set_fault(
envP, 0, "Structure is missing 'DIVISOR' member");
else {
xmlrpc_double dividend;
xmlrpc_read_double(envP, dividendP, &dividend);
if (!envP->fault_occurred) {
xmlrpc_double divisor;
xmlrpc_read_double(envP, divisorP, &divisor);
if (!envP->fault_occurred)
*quotientP = dividend / divisor;
}
xmlrpc_DECREF(divisorP);
}
}
xmlrpc_DECREF(dividendP);
}
}
}
static void
computeQuotients(xmlrpc_env * const envP,
xmlrpc_value * const ratioArrayP,
xmlrpc_value ** const quotientArrayPP) {
xmlrpc_value * quotientArrayP;
quotientArrayP = xmlrpc_array_new(envP);
if (!envP->fault_occurred) {
unsigned int const ratioCt = xmlrpc_array_size(envP, ratioArrayP);
unsigned int i;
for (i = 0; i < ratioCt && !envP->fault_occurred; ++i) {
xmlrpc_value * ratioP;
xmlrpc_array_read_item(envP, ratioArrayP, i, &ratioP);
if (!envP->fault_occurred) {
xmlrpc_double quotient;
computeQuotient(envP, ratioP, &quotient);
if (!envP->fault_occurred) {
xmlrpc_value * quotientP;
quotientP = xmlrpc_double_new(envP, quotient);
if (!envP->fault_occurred) {
xmlrpc_array_append_item(envP, quotientArrayP,
quotientP);
xmlrpc_DECREF(quotientP);
}
}
xmlrpc_DECREF(ratioP);
}
}
if (envP->fault_occurred)
xmlrpc_DECREF(quotientArrayP);
else
*quotientArrayPP = quotientArrayP;
}
}
static xmlrpc_value *
example_divide(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverInfo,
void * const channelInfo) {
xmlrpc_value * retvalP;
xmlrpc_int32 argVersion;
xmlrpc_value * ratioArrayP;
xmlrpc_decompose_value(envP, paramArrayP, "(iA)",
&argVersion, &ratioArrayP);
if (envP->fault_occurred)
return NULL;
if (argVersion != 1) {
xmlrpc_env_set_fault(envP, 0, "Parameter list version must be 1");
return NULL;
}
computeQuotients(envP, ratioArrayP, &retvalP);
xmlrpc_DECREF(ratioArrayP);
if (envP->fault_occurred)
return NULL;
return retvalP;
}
int
main(int const argc,
const char ** const argv) {
struct xmlrpc_method_info3 const methodInfo = {
/* .methodName = */ "example.divide",
/* .methodFunction = */ &example_divide,
};
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
if (argc-1 != 1) {
fprintf(stderr, "You must specify 1 argument: The TCP port "
"number on which the server will accept connections "
"for RPCs (8080 is a common choice). "
"You specified %d arguments.\n", argc-1);
exit(1);
}
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method3(&env, registryP, &methodInfo);
/* In the modern form of the Abyss API, we supply parameters in memory
like a normal API. We select the modern form by setting
config_file_name to NULL:
*/
serverparm.config_file_name = NULL;
serverparm.registryP = registryP;
serverparm.port_number = atoi(argv[1]);
serverparm.log_file_name = "/tmp/xmlrpc_log";
printf("Running XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(log_file_name));
/* xmlrpc_server_abyss() never returns */
return 0;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.