This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_C.cc
403 lines (311 loc) · 10.1 KB
/
client_C.cc
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
Redistribution and use of this software in source and binary forms, with or
without modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Yahoo! Inc. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission of Yahoo! Inc.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//////////////////////////////////////////////////////////////////////
// A C wrapper around the libpluton client C++ API
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <pluton/fault.h>
#include <pluton/client.h>
#include <pluton/client_C.h>
static bool GMyDebugFlag = false;
extern "C" {
// The C++ objects are stored in a standard C structure
struct __spluton_request_C_obj {
pluton::clientRequest* _R;
void* _clientHandle;
char* _requestData;
int _requestDataSize; // malloc size of _requestData
mutable std::string* _faultText;
mutable std::string* _serviceName;
};
struct __spluton_client_C_obj {
pluton::client* _C;
mutable std::string* _faultMessage;
};
static void
internal_reset(pluton_request_C_obj* R)
{
R->_clientHandle = 0;
R->_requestData = 0;
R->_requestDataSize = 0;
R->_faultText = 0;
R->_serviceName = 0;
}
pluton_request_C_obj*
pluton_request_C_new()
{
pluton_request_C_obj* R;
R = (pluton_request_C_obj*) malloc(sizeof(pluton_request_C_obj));
if (R) {
internal_reset(R);
R->_R = new pluton::clientRequest();
R->_R->setClientHandle(R);
}
if (GMyDebugFlag) fprintf(stderr, "C New clientRequest %p %p\n", (void*)R, (void*)R->_R);
return R;
}
void
pluton_request_C_reset(pluton_request_C_obj* R)
{
if (GMyDebugFlag) fprintf(stderr, "C reset clientRequest %p %p\n", (void*)R, (void*)R->_R);
void* myCH = R->_R->getClientHandle(); // Need to retain this across reset
R->_R->reset();
R->_R->setClientHandle(myCH);
if (R->_requestData) free(R->_requestData);
if (R->_faultText) delete R->_faultText;
if (R->_serviceName) delete R->_serviceName;
internal_reset(R);
}
void
pluton_request_C_delete(pluton_request_C_obj* R)
{
if (GMyDebugFlag) fprintf(stderr, "C delete clientRequest %p %p\n", (void*)R, (void*)R->_R);
delete R->_R;
if (R->_requestData) free(R->_requestData);
if (R->_faultText) delete R->_faultText;
if (R->_serviceName) delete R->_serviceName;
free(R);
}
pluton_client_C_obj*
pluton_client_C_new(const char* yourName, int defaultTimeoutMilliSeconds)
{
pluton_client_C_obj* C;
C = (pluton_client_C_obj*) malloc(sizeof(pluton_client_C_obj));
if (C) {
C->_C = new pluton::client(yourName, defaultTimeoutMilliSeconds);
C->_faultMessage = new std::string();
}
if (GMyDebugFlag) fprintf(stderr, "C new client %p %p\n", (void*)C, (void*)C->_C);
return C;
}
void
pluton_client_C_reset(pluton_client_C_obj* C)
{
if (GMyDebugFlag) fprintf(stderr, "C reset client %p %p\n", (void*)C, (void*)C->_C);
C->_C->reset();
}
void
pluton_client_C_delete(pluton_client_C_obj* C)
{
if (GMyDebugFlag) fprintf(stderr, "C delete client %p %p\n", (void*)C, (void*)C->_C);
delete C->_faultMessage;
delete C->_C;
free(C);
}
//////////////////////////////////////////////////////////////////////
// If the caller signifies that the data is volatile, take a copy so
// that we don't have to rely on the caller to keep it around.
//////////////////////////////////////////////////////////////////////
void
pluton_request_C_setRequestData(pluton_request_C_obj* R,
const char* p, int len,
int copyData)
{
if (GMyDebugFlag) fprintf(stderr, "C setRequestData %p %p\n", (void*)R, (void*)R->_R);
if (!copyData) {
R->_R->setRequestData(p, len);
return;
}
// Does the new requestData fit in the malloced buffer?
if (R->_requestData) {
if (R->_requestDataSize < len) {
delete R->_requestData;
R->_requestData = 0;
}
}
// If there is no malloc buffer, allocate one
if (!R->_requestData) {
R->_requestData = (char*) malloc(len);
R->_requestDataSize = len;
}
memcpy(R->_requestData, p, len);
R->_R->setRequestData(R->_requestData, len);
}
void
pluton_request_C_setAttribute(pluton_request_C_obj* R, int attrs)
{
R->_R->setAttribute(attrs);
}
int
pluton_request_C_getAttribute(const pluton_request_C_obj* R, int attrs)
{
return R->_R->getAttribute(attrs) ? 1 : 0;
}
void
pluton_request_C_clearAttribute(pluton_request_C_obj* R, int attrs)
{
R->_R->clearAttribute(attrs);
}
int
pluton_request_C_setContext(pluton_request_C_obj* R,
const char* key, const char* value, int valueLength)
{
if (GMyDebugFlag) fprintf(stderr, "C setContext %p %p %s\n", (void*)R, (void*)R->_R, key);
std::string V;
V.assign(value, valueLength);
return R->_R->setContext(key, V);
}
int
pluton_request_C_getResponseData(const pluton_request_C_obj* R,
const char** responsePointer, int* responseLength)
{
if (GMyDebugFlag) fprintf(stderr, "C getResponseData %p %p\n", (void*)R, (void*)R->_R);
const char* rp;
int rl;
int res;
res = R->_R->getResponseData(rp, rl);
*responsePointer = rp;
*responseLength = rl;
return res;
}
int
pluton_request_C_inProgress(const pluton_request_C_obj* R)
{
return R->_R->inProgress() ? 1 : 0;
}
int
pluton_request_C_hasFault(const pluton_request_C_obj* R)
{
return R->_R->hasFault() ? 1 : 0;
}
int
pluton_request_C_getFaultCode(const pluton_request_C_obj* R)
{
return R->_R->getFaultCode();
}
const char*
pluton_request_C_getFaultText(const pluton_request_C_obj* R)
{
if (!R->_faultText) R->_faultText = new std::string;
*R->_faultText = R->_R->getFaultText();
return R->_faultText->c_str();
}
const char*
pluton_request_C_getServiceName(const pluton_request_C_obj* R)
{
if (!R->_serviceName) R->_serviceName = new std::string;
*R->_serviceName = R->_R->getServiceName();
return R->_serviceName->c_str();
}
//////////////////////////////////////////////////////////////////////
// We manage the client handle as we use the pluton::clientRequest
// handle to identify ourselves.
//////////////////////////////////////////////////////////////////////
void
pluton_request_C_setClientHandle(pluton_request_C_obj* R, void* H)
{
if (GMyDebugFlag) fprintf(stderr, "C SCH %p/%p to %p\n", (void*)R, (void*)R->_R, H);
R->_clientHandle = H;
}
void*
pluton_request_C_getClientHandle(const pluton_request_C_obj* R)
{
if (GMyDebugFlag) fprintf(stderr, "C GCH %p/%p from %p\n", (void*)R, (void*)R->_R, R->_clientHandle);
return R->_clientHandle;
}
//////////////////////////////////////////////////////////////////////
const char*
pluton_client_C_getAPIVersion()
{
return pluton::client::getAPIVersion();
}
int
pluton_client_C_initialize(pluton_client_C_obj* C, const char* lookupMapPath)
{
if (GMyDebugFlag) fprintf(stderr, "C initialize %p %p\n", (void*)C, (void*)C->_C);
return C->_C->initialize(lookupMapPath) ? 1 : 0;
}
int
pluton_client_C_hasFault(const pluton_client_C_obj* C)
{
return C->_C->hasFault() ? 1 : 0;
}
extern int
pluton_client_C_getFaultCode(const pluton_client_C_obj* C)
{
return C->_C->getFault().getFaultCode();
}
extern const char*
pluton_client_C_getFaultMessage(const pluton_client_C_obj* C, const char* prefix, int longFormat)
{
*(C->_faultMessage) = C->_C->getFault().getMessage(prefix, longFormat ? true : false);
return C->_faultMessage->c_str();
}
void
pluton_client_C_setDebug(pluton_client_C_obj* C, int nv)
{
C->_C->setDebug(nv ? true : false);
GMyDebugFlag = nv ? true : false;
}
void
pluton_client_C_setTimeoutMilliSeconds(pluton_client_C_obj* C, int timeoutMilliSeconds)
{
C->_C->setTimeoutMilliSeconds(timeoutMilliSeconds);
}
int
pluton_client_C_getTimeoutMilliSeconds(const pluton_client_C_obj* C)
{
return C->_C->getTimeoutMilliSeconds();
}
int
pluton_client_C_addRequest(pluton_client_C_obj* C, const char* serviceKey, pluton_request_C_obj* R)
{
if (GMyDebugFlag) fprintf(stderr, "C addRequest %p %p : %p %p ch=%p %p\n",
(void*)C, (void*)C->_C, (void*)R, (void*)R->_R,
R->_clientHandle, R->_R->getClientHandle());
return C->_C->addRequest(serviceKey, R->_R) ? 1 : 0;
}
int
pluton_client_C_executeAndWaitSent(pluton_client_C_obj* C)
{
if (GMyDebugFlag) fprintf(stderr, "C executeAndWaitSent %p %p\n",(void*)C, (void*)C->_C);
return C->_C->executeAndWaitSent();
}
int
pluton_client_C_executeAndWaitAll(pluton_client_C_obj* C)
{
if (GMyDebugFlag) fprintf(stderr, "C executeAndWaitAll %p %p\n",(void*)C, (void*)C->_C);
return C->_C->executeAndWaitAll();
}
int
pluton_client_C_executeAndWaitOne(pluton_client_C_obj* C, pluton_request_C_obj* R)
{
if (GMyDebugFlag) fprintf(stderr, "C executeAndWaitOne %p %p : %p %p\n",
(void*)C, (void*)C->_C, (void*)R, (void*)R->_R);
return C->_C->executeAndWaitOne(R->_R);
}
pluton_request_C_obj*
pluton_client_C_executeAndWaitAny(pluton_client_C_obj* C)
{
if (GMyDebugFlag) fprintf(stderr, "C executeAndWaitOne %p %p\n", (void*)C, (void*)C->_C);
pluton::clientRequest* R = C->_C->executeAndWaitAny();
if (R) return (pluton_request_C_obj*) R->getClientHandle();
return 0;
}
// Terminate extern "C"
}