Debian Codegraph @

test/Validator.cpp

raw
1 // Validator.cpp : XMLRPC server based on the compliancy test at validator.xmlrpc.com.
2 //
3 #include "XmlRpc.h"
4 using namespace XmlRpc;
5
6 #include <iostream>
7 #include <stdlib.h>
8
9
10 XmlRpcServer s;
11
12
13 // One argument is passed, an array of structs, each with a member named curly with
14 // an integer value. Return the sum of those values.
15
16 class ArrayOfStructsTest : public XmlRpcServerMethod
17 {
18 public:
19 ArrayOfStructsTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.arrayOfStructsTest", s) {}
20
21 void execute(XmlRpcValue& params, XmlRpcValue& result)
22 {
23 std::cerr << "ArrayOfStructsTest\n";
24 XmlRpcValue& arg1 = params[0];
25 int n = arg1.size(), sum = 0;
26 for (int i=0; i<n; ++i)
27 sum += int(arg1[i]["curly"]);
28
29 result = sum;
30 }
31 } arrayOfStructsTest(&s);
32
33
34 // This handler takes a single parameter, a string, that contains any number of predefined
35 // entities, namely <, >, &, ' and ".
36 // The handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets,
37 // ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes.
38 // To validate, the numbers must be correct.
39
40 class CountTheEntities : public XmlRpcServerMethod
41 {
42 public:
43 CountTheEntities(XmlRpcServer* s) : XmlRpcServerMethod("validator1.countTheEntities", s) {}
44
45 void execute(XmlRpcValue& params, XmlRpcValue& result)
46 {
47 std::cerr << "CountTheEntities\n";
48 std::string& arg = params[0];
49 int ctLeftAngleBrackets = 0;
50 int ctRightAngleBrackets = 0;
51 int ctAmpersands = 0;
52 int ctApostrophes = 0;
53 int ctQuotes = 0;
54
55 int n = int(arg.length());
56 for (int i=0; i<n; ++i)
57 switch (arg[i])
58 {
59 case '<': ++ctLeftAngleBrackets; break;
60 case '>': ++ctRightAngleBrackets; break;
61 case '&': ++ctAmpersands; break;
62 case '\'': ++ctApostrophes; break;
63 case '\"': ++ctQuotes; break;
64 }
65
66 result["ctLeftAngleBrackets"] = ctLeftAngleBrackets;
67 result["ctRightAngleBrackets"] = ctRightAngleBrackets;
68 result["ctAmpersands"] = ctAmpersands;
69 result["ctApostrophes"] = ctApostrophes;
70 result["ctQuotes"] = ctQuotes;
71 }
72 } countTheEntities(&s);
73
74
75
76 // This handler takes a single parameter, a struct, containing at least three elements
77 // named moe, larry and curly, all <i4>s. Your handler must add the three numbers and
78 // return the result.
79
80 class EasyStructTest : public XmlRpcServerMethod
81 {
82 public:
83 EasyStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.easyStructTest", s) {}
84
85 void execute(XmlRpcValue& params, XmlRpcValue& result)
86 {
87 std::cerr << "EasyStructTest\n";
88 XmlRpcValue& arg1 = params[0];
89 int sum = int(arg1["moe"]) + int(arg1["larry"]) + int(arg1["curly"]);
90 result = sum;
91 }
92 } easyStructTest(&s);
93
94
95 // This handler takes a single parameter, a struct. Your handler must return the struct.
96
97 class EchoStructTest : public XmlRpcServerMethod
98 {
99 public:
100 EchoStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.echoStructTest", s) {}
101
102 void execute(XmlRpcValue& params, XmlRpcValue& result)
103 {
104 std::cerr << "EchoStructTest\n";
105 result = params[0];
106 }
107 } echoStructTest(&s);
108
109
110
111 // This handler takes six parameters, and returns an array containing all the parameters.
112
113 class ManyTypesTest : public XmlRpcServerMethod
114 {
115 public:
116 ManyTypesTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.manyTypesTest", s) {}
117
118 void execute(XmlRpcValue& params, XmlRpcValue& result)
119 {
120 std::cerr << "ManyTypesTest\n";
121 result = params;
122 }
123 } manyTypesTest(&s);
124
125
126
127 // This handler takes a single parameter, which is an array containing between 100 and
128 // 200 elements. Each of the items is a string, your handler must return a string
129 // containing the concatenated text of the first and last elements.
130
131
132 class ModerateSizeArrayCheck : public XmlRpcServerMethod
133 {
134 public:
135 ModerateSizeArrayCheck(XmlRpcServer* s) : XmlRpcServerMethod("validator1.moderateSizeArrayCheck", s) {}
136
137 void execute(XmlRpcValue& params, XmlRpcValue& result)
138 {
139 std::cerr << "ModerateSizeArrayCheck\n";
140 std::string s = params[0][0];
141 s.append(static_cast<std::string>(params[0][params[0].size()-1]));
142 result = s;
143 }
144 } moderateSizeArrayCheck(&s);
145
146
147 // This handler takes a single parameter, a struct, that models a daily calendar.
148 // At the top level, there is one struct for each year. Each year is broken down
149 // into months, and months into days. Most of the days are empty in the struct
150 // you receive, but the entry for April 1, 2000 contains a least three elements
151 // named moe, larry and curly, all <i4>s. Your handler must add the three numbers
152 // and return the result.
153
154 class NestedStructTest : public XmlRpcServerMethod
155 {
156 public:
157 NestedStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.nestedStructTest", s) {}
158
159 void execute(XmlRpcValue& params, XmlRpcValue& result)
160 {
161 std::cerr << "NestedStructTest\n";
162 XmlRpcValue& dayStruct = params[0]["2000"]["04"]["01"];
163 int sum = int(dayStruct["moe"]) + int(dayStruct["larry"]) + int(dayStruct["curly"]);
164 result = sum;
165 }
166 } nestedStructTest(&s);
167
168
169
170 // This handler takes one parameter, and returns a struct containing three elements,
171 // times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.
172
173 class SimpleStructReturnTest : public XmlRpcServerMethod
174 {
175 public:
176 SimpleStructReturnTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.simpleStructReturnTest", s) {}
177
178 void execute(XmlRpcValue& params, XmlRpcValue& result)
179 {
180 std::cerr << "SimpleStructReturnTest\n";
181 int n = params[0];
182 result["times10"] = n * 10;
183 result["times100"] = n * 100;
184 result["times1000"] = n * 1000;
185 }
186 } simpleStructReturnTest(&s);
187
188
189
190 int main(int argc, char* argv[])
191 {
192 if (argc != 2) {
193 std::cerr << "Usage: Validator port\n";
194 return -1;
195 }
196 int port = atoi(argv[1]);
197
198 XmlRpc::setVerbosity(5);
199
200 // Create the server socket on the specified port
201 s.bindAndListen(port);
202
203 // Wait for requests indefinitely
204 s.work(-1.0);
205
206 return 0;
207 }
208