File: Validator.cpp

package info (click to toggle)
flxmlrpc 0.1.4-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,056 kB
  • sloc: sh: 11,511; cpp: 3,648; makefile: 63
file content (415 lines) | stat: -rw-r--r-- 6,438 bytes parent folder | download | duplicates (3)
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
404
405
406
407
408
409
410
411
412
413
414
415
// Validator.cpp : XMLRPC server based on the compliancy test at validator.xmlrpc.com.

//

#include "XmlRpc.h"

using namespace XmlRpc;



#include <iostream>
#include <stdlib.h>





XmlRpcServer s;





// One argument is passed, an array of structs, each with a member named curly with 

// an integer value. Return the sum of those values.



class ArrayOfStructsTest : public XmlRpcServerMethod

{

public:

  ArrayOfStructsTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.arrayOfStructsTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "ArrayOfStructsTest\n";

    XmlRpcValue& arg1 = params[0];

    int n = arg1.size(), sum = 0;

    for (int i=0; i<n; ++i) 

      sum += int(arg1[i]["curly"]);



    result = sum;

  }

} arrayOfStructsTest(&s);





// This handler takes a single parameter, a string, that contains any number of predefined 

// entities, namely <, >, &, ' and ".

// The handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets, 

// ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes. 

// To validate, the numbers must be correct.



class CountTheEntities : public XmlRpcServerMethod

{

public:

  CountTheEntities(XmlRpcServer* s) : XmlRpcServerMethod("validator1.countTheEntities", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "CountTheEntities\n";

    std::string& arg = params[0];

    int ctLeftAngleBrackets = 0;

    int ctRightAngleBrackets = 0;

    int ctAmpersands = 0;

    int ctApostrophes = 0;

    int ctQuotes = 0;



    int n = int(arg.length());

    for (int i=0; i<n; ++i)

      switch (arg[i])

      {

        case '<': ++ctLeftAngleBrackets; break;

        case '>': ++ctRightAngleBrackets; break;

        case '&': ++ctAmpersands; break;

        case '\'': ++ctApostrophes; break;

        case '\"': ++ctQuotes; break;

      }



    result["ctLeftAngleBrackets"] = ctLeftAngleBrackets;

    result["ctRightAngleBrackets"] = ctRightAngleBrackets;

    result["ctAmpersands"] = ctAmpersands;

    result["ctApostrophes"] = ctApostrophes;

    result["ctQuotes"] = ctQuotes;

  }

} countTheEntities(&s);







// This handler takes a single parameter, a struct, containing at least three elements 

// named moe, larry and curly, all <i4>s. Your handler must add the three numbers and 

// return the result.



class EasyStructTest : public XmlRpcServerMethod

{

public:

  EasyStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.easyStructTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "EasyStructTest\n";

    XmlRpcValue& arg1 = params[0];

    int sum = int(arg1["moe"]) + int(arg1["larry"]) + int(arg1["curly"]);

    result = sum;

  }

} easyStructTest(&s);





// This handler takes a single parameter, a struct. Your handler must return the struct.



class EchoStructTest : public XmlRpcServerMethod

{

public:

  EchoStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.echoStructTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "EchoStructTest\n";

    result = params[0];

  }

} echoStructTest(&s);







// This handler takes six parameters, and returns an array containing all the parameters.



class ManyTypesTest : public XmlRpcServerMethod

{

public:

  ManyTypesTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.manyTypesTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "ManyTypesTest\n";

    result = params;

  }

} manyTypesTest(&s);







// This handler takes a single parameter, which is an array containing between 100 and 

// 200 elements. Each of the items is a string, your handler must return a string 

// containing the concatenated text of the first and last elements.





class ModerateSizeArrayCheck : public XmlRpcServerMethod

{

public:

  ModerateSizeArrayCheck(XmlRpcServer* s) : XmlRpcServerMethod("validator1.moderateSizeArrayCheck", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "ModerateSizeArrayCheck\n";

    std::string s = params[0][0];

    s.append(static_cast<std::string>(params[0][params[0].size()-1]));

    result = s;

  }

} moderateSizeArrayCheck(&s);





// This handler takes a single parameter, a struct, that models a daily calendar.

// At the top level, there is one struct for each year. Each year is broken down

// into months, and months into days. Most of the days are empty in the struct

// you receive, but the entry for April 1, 2000 contains a least three elements

// named moe, larry and curly, all <i4>s. Your handler must add the three numbers

// and return the result.



class NestedStructTest : public XmlRpcServerMethod

{

public:

  NestedStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.nestedStructTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "NestedStructTest\n";

    XmlRpcValue& dayStruct = params[0]["2000"]["04"]["01"];

    int sum = int(dayStruct["moe"]) + int(dayStruct["larry"]) + int(dayStruct["curly"]);

    result = sum;

  }

} nestedStructTest(&s);







// This handler takes one parameter, and returns a struct containing three elements, 

// times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.



class SimpleStructReturnTest : public XmlRpcServerMethod

{

public:

  SimpleStructReturnTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.simpleStructReturnTest", s) {}



  void execute(XmlRpcValue& params, XmlRpcValue& result)

  {

    std::cerr << "SimpleStructReturnTest\n";

    int n = params[0];

    result["times10"] = n * 10;

    result["times100"] = n * 100;

    result["times1000"] = n * 1000;

  }

} simpleStructReturnTest(&s);







int main(int argc, char* argv[])

{

  if (argc != 2) {

    std::cerr << "Usage: Validator port\n";

    return -1;

  }

  int port = atoi(argv[1]);



  XmlRpc::setVerbosity(5);



  // Create the server socket on the specified port

  s.bindAndListen(port);



  // Wait for requests indefinitely

  s.work(-1.0);



  return 0;

}