Validator.cpp
Go to the documentation of this file.
00001 // Validator.cpp : XMLRPC server based on the compliancy test at validator.xmlrpc.com.
00002 //
00003 #include "XmlRpc.h"
00004 using namespace XmlRpc;
00005 
00006 #include <iostream>
00007 
00008 
00009 XmlRpcServer s;
00010 
00011 
00012 // One argument is passed, an array of structs, each with a member named curly with 
00013 // an integer value. Return the sum of those values.
00014 
00015 class ArrayOfStructsTest : public XmlRpcServerMethod
00016 {
00017 public:
00018   ArrayOfStructsTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.arrayOfStructsTest", s) {}
00019 
00020   void execute(XmlRpcValue& params, XmlRpcValue& result)
00021   {
00022     std::cerr << "ArrayOfStructsTest\n";
00023     XmlRpcValue& arg1 = params[0];
00024     int n = arg1.size(), sum = 0;
00025     for (int i=0; i<n; ++i) 
00026       sum += int(arg1[i]["curly"]);
00027 
00028     result = sum;
00029   }
00030 } arrayOfStructsTest(&s);
00031 
00032 
00033 // This handler takes a single parameter, a string, that contains any number of predefined 
00034 // entities, namely <, >, &, ' and ".
00035 // The handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets, 
00036 // ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes. 
00037 // To validate, the numbers must be correct.
00038 
00039 class CountTheEntities : public XmlRpcServerMethod
00040 {
00041 public:
00042   CountTheEntities(XmlRpcServer* s) : XmlRpcServerMethod("validator1.countTheEntities", s) {}
00043 
00044   void execute(XmlRpcValue& params, XmlRpcValue& result)
00045   {
00046     std::cerr << "CountTheEntities\n";
00047     std::string& arg = params[0];
00048     int ctLeftAngleBrackets = 0;
00049     int ctRightAngleBrackets = 0;
00050     int ctAmpersands = 0;
00051     int ctApostrophes = 0;
00052     int ctQuotes = 0;
00053 
00054     int n = int(arg.length());
00055     for (int i=0; i<n; ++i)
00056       switch (arg[i])
00057       {
00058         case '<': ++ctLeftAngleBrackets; break;
00059         case '>': ++ctRightAngleBrackets; break;
00060         case '&': ++ctAmpersands; break;
00061         case '\'': ++ctApostrophes; break;
00062         case '\"': ++ctQuotes; break;
00063       }
00064 
00065     result["ctLeftAngleBrackets"] = ctLeftAngleBrackets;
00066     result["ctRightAngleBrackets"] = ctRightAngleBrackets;
00067     result["ctAmpersands"] = ctAmpersands;
00068     result["ctApostrophes"] = ctApostrophes;
00069     result["ctQuotes"] = ctQuotes;
00070   }
00071 } countTheEntities(&s);
00072 
00073 
00074 
00075 // This handler takes a single parameter, a struct, containing at least three elements 
00076 // named moe, larry and curly, all <i4>s. Your handler must add the three numbers and 
00077 // return the result.
00078 
00079 class EasyStructTest : public XmlRpcServerMethod
00080 {
00081 public:
00082   EasyStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.easyStructTest", s) {}
00083 
00084   void execute(XmlRpcValue& params, XmlRpcValue& result)
00085   {
00086     std::cerr << "EasyStructTest\n";
00087     XmlRpcValue& arg1 = params[0];
00088     int sum = int(arg1["moe"]) + int(arg1["larry"]) + int(arg1["curly"]);
00089     result = sum;
00090   }
00091 } easyStructTest(&s);
00092 
00093 
00094 // This handler takes a single parameter, a struct. Your handler must return the struct.
00095 
00096 class EchoStructTest : public XmlRpcServerMethod
00097 {
00098 public:
00099   EchoStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.echoStructTest", s) {}
00100 
00101   void execute(XmlRpcValue& params, XmlRpcValue& result)
00102   {
00103     std::cerr << "EchoStructTest\n";
00104     result = params[0];
00105   }
00106 } echoStructTest(&s);
00107 
00108 
00109 
00110 // This handler takes six parameters, and returns an array containing all the parameters.
00111 
00112 class ManyTypesTest : public XmlRpcServerMethod
00113 {
00114 public:
00115   ManyTypesTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.manyTypesTest", s) {}
00116 
00117   void execute(XmlRpcValue& params, XmlRpcValue& result)
00118   {
00119     std::cerr << "ManyTypesTest\n";
00120     result = params;
00121   }
00122 } manyTypesTest(&s);
00123 
00124 
00125 
00126 // This handler takes a single parameter, which is an array containing between 100 and 
00127 // 200 elements. Each of the items is a string, your handler must return a string 
00128 // containing the concatenated text of the first and last elements.
00129 
00130 
00131 class ModerateSizeArrayCheck : public XmlRpcServerMethod
00132 {
00133 public:
00134   ModerateSizeArrayCheck(XmlRpcServer* s) : XmlRpcServerMethod("validator1.moderateSizeArrayCheck", s) {}
00135 
00136   void execute(XmlRpcValue& params, XmlRpcValue& result)
00137   {
00138     std::cerr << "ModerateSizeArrayCheck\n";
00139     std::string s = params[0][0];
00140     s += std::string(params[0][params[0].size()-1]);
00141     result = s;
00142   }
00143 } moderateSizeArrayCheck(&s);
00144 
00145 
00146 // This handler takes a single parameter, a struct, that models a daily calendar.
00147 // At the top level, there is one struct for each year. Each year is broken down
00148 // into months, and months into days. Most of the days are empty in the struct
00149 // you receive, but the entry for April 1, 2000 contains a least three elements
00150 // named moe, larry and curly, all <i4>s. Your handler must add the three numbers
00151 // and return the result.
00152 
00153 class NestedStructTest : public XmlRpcServerMethod
00154 {
00155 public:
00156   NestedStructTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.nestedStructTest", s) {}
00157 
00158   void execute(XmlRpcValue& params, XmlRpcValue& result)
00159   {
00160     std::cerr << "NestedStructTest\n";
00161     XmlRpcValue& dayStruct = params[0]["2000"]["04"]["01"];
00162     int sum = int(dayStruct["moe"]) + int(dayStruct["larry"]) + int(dayStruct["curly"]);
00163     result = sum;
00164   }
00165 } nestedStructTest(&s);
00166 
00167 
00168 
00169 // This handler takes one parameter, and returns a struct containing three elements, 
00170 // times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.
00171 
00172 class SimpleStructReturnTest : public XmlRpcServerMethod
00173 {
00174 public:
00175   SimpleStructReturnTest(XmlRpcServer* s) : XmlRpcServerMethod("validator1.simpleStructReturnTest", s) {}
00176 
00177   void execute(XmlRpcValue& params, XmlRpcValue& result)
00178   {
00179     std::cerr << "SimpleStructReturnTest\n";
00180     int n = params[0];
00181     result["times10"] = n * 10;
00182     result["times100"] = n * 100;
00183     result["times1000"] = n * 1000;
00184   }
00185 } simpleStructReturnTest(&s);
00186 
00187 
00188 
00189 int main(int argc, char* argv[])
00190 {
00191   if (argc != 2) {
00192     std::cerr << "Usage: Validator port\n";
00193     return -1;
00194   }
00195   int port = atoi(argv[1]);
00196 
00197   XmlRpc::setVerbosity(5);
00198 
00199   // Create the server socket on the specified port
00200   s.bindAndListen(port);
00201 
00202   // Wait for requests indefinitely
00203   s.work(-1.0);
00204 
00205   return 0;
00206 }
00207 


xmlrpcpp
Author(s): Chris Morley and Konstantin Pilipchuk, slight modifications and ROS wrapping by Morgan Quigley
autogenerated on Sat Dec 28 2013 17:42:59