aboutsummaryrefslogtreecommitdiff
path: root/test/Validator.cpp
blob: 16d2e791895e5f8fbbf64ba9dd036d02868148bc (plain)
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
// Validator.cpp : XMLRPC server based on the compliancy test at validator.xmlrpc.com.
//
#include "XmlRpc.h"
using namespace XmlRpc;

#include <iostream>


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 += 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;
}