-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlrpc_client_global.c
375 lines (267 loc) · 10.5 KB
/
xmlrpc_client_global.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <stdarg.h>
#include <stdio.h>
#include "xmlrpc_config.h"
#include "bool.h"
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#include <xmlrpc-c/client_int.h>
#include <xmlrpc-c/client_global.h>
/*=========================================================================
Global Client
=========================================================================*/
static struct xmlrpc_client * globalClientP;
static bool globalClientExists = false;
void
xmlrpc_client_init2(xmlrpc_env * const envP,
int const flags,
const char * const appname,
const char * const appversion,
const struct xmlrpc_clientparms * const clientparmsP,
unsigned int const parmSize) {
/*----------------------------------------------------------------------------
This function is not thread-safe.
-----------------------------------------------------------------------------*/
if (globalClientExists)
xmlrpc_faultf(
envP,
"Xmlrpc-c global client instance has already been created "
"(need to call xmlrpc_client_cleanup() before you can "
"reinitialize).");
else {
/* The following call is not thread-safe */
xmlrpc_client_setup_global_const(envP);
if (!envP->fault_occurred) {
xmlrpc_client_create(envP, flags, appname, appversion,
clientparmsP, parmSize, &globalClientP);
if (!envP->fault_occurred)
globalClientExists = true;
if (envP->fault_occurred)
xmlrpc_client_teardown_global_const();
}
}
}
void
xmlrpc_client_init(int const flags,
const char * const appname,
const char * const appversion) {
/*----------------------------------------------------------------------------
This function is not thread-safe.
-----------------------------------------------------------------------------*/
struct xmlrpc_clientparms clientparms;
/* As our interface does not allow for failure, we just fail silently ! */
xmlrpc_env env;
xmlrpc_env_init(&env);
clientparms.transport = NULL;
/* The following call is not thread-safe */
xmlrpc_client_init2(&env, flags,
appname, appversion,
&clientparms, XMLRPC_CPSIZE(transport));
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_cleanup() {
/*----------------------------------------------------------------------------
This function is not thread-safe
-----------------------------------------------------------------------------*/
XMLRPC_ASSERT(globalClientExists);
xmlrpc_client_destroy(globalClientP);
globalClientExists = false;
/* The following call is not thread-safe */
xmlrpc_client_teardown_global_const();
}
static void
validateGlobalClientExists(xmlrpc_env * const envP) {
if (!globalClientExists)
xmlrpc_faultf(envP,
"Xmlrpc-c global client instance "
"has not been created "
"(need to call xmlrpc_client_init2()).");
}
void
xmlrpc_client_transport_call(
xmlrpc_env * const envP,
void * const reserved ATTR_UNUSED,
/* for client handle */
const xmlrpc_server_info * const serverP,
xmlrpc_mem_block * const callXmlP,
xmlrpc_mem_block ** const respXmlPP) {
validateGlobalClientExists(envP);
if (!envP->fault_occurred)
xmlrpc_client_transport_call2(envP, globalClientP, serverP,
callXmlP, respXmlPP);
}
//Altered for multirpc
xmlrpc_value **
xmlrpc_client_call(xmlrpc_env * const envP,
const char ** const serverUrl,
int number_of_servers,
const char * const methodName,
const char * const format,
...) {
xmlrpc_value ** resultP = malloc(number_of_servers * sizeof(xmlrpc_value *));
int i;
validateGlobalClientExists(envP);
if (!envP->fault_occurred) {
va_list args;
va_start(args, format);
for(i=0; i<number_of_servers; i++)
xmlrpc_client_call2f_va(envP, globalClientP, serverUrl[i],
methodName, format, &resultP[i], args);
va_end(args);
}
return resultP;
}
xmlrpc_value *
xmlrpc_client_call_server(xmlrpc_env * const envP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
const char * const format,
...) {
xmlrpc_value * resultP;
validateGlobalClientExists(envP);
if (!envP->fault_occurred) {
va_list args;
va_start(args, format);
xmlrpc_client_call_server2_va(envP, globalClientP, serverInfoP,
methodName, format, args, &resultP);
va_end(args);
}
return resultP;
}
xmlrpc_value *
xmlrpc_client_call_server_params(
xmlrpc_env * const envP,
const xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_value * const paramArrayP) {
xmlrpc_value * resultP;
validateGlobalClientExists(envP);
if (!envP->fault_occurred)
xmlrpc_client_call2(envP, globalClientP,
serverInfoP, methodName, paramArrayP,
&resultP);
return resultP;
}
xmlrpc_value *
xmlrpc_client_call_params(xmlrpc_env * const envP,
const char * const serverUrl,
const char * const methodName,
xmlrpc_value * const paramArrayP) {
xmlrpc_value * resultP;
validateGlobalClientExists(envP);
if (!envP->fault_occurred) {
xmlrpc_server_info * serverInfoP;
serverInfoP = xmlrpc_server_info_new(envP, serverUrl);
if (!envP->fault_occurred) {
xmlrpc_client_call2(envP, globalClientP,
serverInfoP, methodName, paramArrayP,
&resultP);
xmlrpc_server_info_free(serverInfoP);
}
}
return resultP;
}
void
xmlrpc_client_call_server_asynch_params(
xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
xmlrpc_value * const paramArrayP) {
xmlrpc_env env;
xmlrpc_env_init(&env);
validateGlobalClientExists(&env);
if (!env.fault_occurred)
xmlrpc_client_start_rpc(&env, globalClientP,
serverInfoP, methodName, paramArrayP,
responseHandler, userData);
if (env.fault_occurred) {
/* Unfortunately, we have no way to return an error and the
regular callback for a failed RPC is designed to have the
parameter array passed to it. This was probably an oversight
of the original asynch design, but now we have to be as
backward compatible as possible, so we do this:
*/
(*responseHandler)(serverInfoP->serverUrl,
methodName, paramArrayP, userData,
&env, NULL);
}
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_call_asynch(xmlrpc_env * const envP,
xmlrpc_client * const clientP,
const char ** const serverUrl,
int max_servers,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
const char * const format,
...) {
int i;
va_list args;
XMLRPC_ASSERT_PTR_OK(format);
va_start(args, format);
for(i=0;i<max_servers; i++)
{
xmlrpc_client_start_rpcf_va(envP, clientP,
serverUrl[i], methodName,
responseHandler, userData,
format, args);
}
va_end(args);
}
void
xmlrpc_client_call_asynch_params(const char * const serverUrl,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
xmlrpc_value * const paramArrayP) {
xmlrpc_env env;
xmlrpc_server_info * serverInfoP;
xmlrpc_env_init(&env);
serverInfoP = xmlrpc_server_info_new(&env, serverUrl);
if (!env.fault_occurred) {
xmlrpc_client_call_server_asynch_params(
serverInfoP, methodName, responseHandler, userData, paramArrayP);
xmlrpc_server_info_free(serverInfoP);
}
if (env.fault_occurred)
(*responseHandler)(serverUrl, methodName, paramArrayP, userData,
&env, NULL);
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_call_server_asynch(xmlrpc_server_info * const serverInfoP,
const char * const methodName,
xmlrpc_response_handler responseHandler,
void * const userData,
const char * const format,
...) {
xmlrpc_env env;
validateGlobalClientExists(&env);
if (!env.fault_occurred) {
va_list args;
xmlrpc_env_init(&env);
va_start(args, format);
xmlrpc_client_start_rpcf_server_va(
&env, globalClientP, serverInfoP, methodName,
responseHandler, userData, format, args);
va_end(args);
}
if (env.fault_occurred)
(*responseHandler)(serverInfoP->serverUrl, methodName, NULL,
userData, &env, NULL);
xmlrpc_env_clean(&env);
}
void
xmlrpc_client_event_loop_finish_asynch(void) {
XMLRPC_ASSERT(globalClientExists);
xmlrpc_client_event_loop_finish(globalClientP);
}
void
xmlrpc_client_event_loop_finish_asynch_timeout(
unsigned long const milliseconds) {
XMLRPC_ASSERT(globalClientExists);
xmlrpc_client_event_loop_finish_timeout(globalClientP, milliseconds);
}