• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // FileClient.cpp : A simple xmlrpc client. Usage: FileClient serverHost serverPort xmlfile
2 // Reads an xmlrpc request from the specified xmlfile and calls the method on the server.
3 //
4 // Link against xmlrpc lib and whatever socket libs your system needs (ws2_32.lib on windows)
5 
6 #include "XmlRpc.h"
7 #include <iostream>
8 #include <fstream>
9 #include <stdlib.h>
10 
11 using namespace XmlRpc;
12 
13 std::string parseRequest(std::string const& xml, XmlRpcValue& params);
14 
15 
main(int argc,char * argv[])16 int main(int argc, char* argv[])
17 {
18   if (argc != 4) {
19     std::cerr << "Usage: FileClient serverHost serverPort requestXmlFile\n";
20     return -1;
21   }
22   int port = atoi(argv[2]);
23 
24   XmlRpc::setVerbosity(5);
25   XmlRpcClient c(argv[1], port);
26 
27   //
28   std::ifstream infile(argv[3]);
29   if (infile.fail()) {
30     std::cerr << "Could not open file '" << argv[3] << "'.\n";
31     return -1;
32   }
33 
34   // Suck in the file. This is a one-liner in good compilers (which vc++ 6 is not)...
35   infile.seekg(0L, std::ios::end);
36   long nb = infile.tellg();
37   infile.clear();
38   infile.seekg(0L);
39   char* b = new char[nb+1];
40   infile.read(b, nb);
41   b[nb] = 0;
42 
43   std::cout << "Read file.\n";
44 
45   // Find the methodName and parse the params
46   std::string s(b);
47   XmlRpcValue params;
48   std::string name = parseRequest(s, params);
49 
50   if (name.empty()) {
51     std::cerr << "Could not parse file\n";
52     return -1;
53   }
54 
55   for (;;) {
56     XmlRpcValue result;
57     std::cout << "Calling " << name << std::endl;
58     if (c.execute(name.c_str(), params, result))
59       std::cout << result << "\n\n";
60     else
61       std::cout << "Error calling '" << name << "'\n\n";
62     std::cout << "Again? [y]: ";
63     std::string ans;
64     std::cin >> ans;
65     if (ans != "" && ans != "y") break;
66   }
67 
68   return 0;
69 }
70 
71 
72 //
73 std::string
parseRequest(std::string const & xml,XmlRpcValue & params)74 parseRequest(std::string const& xml, XmlRpcValue& params)
75 {
76   const char METHODNAME_TAG[] = "<methodName>";
77   const char PARAMS_TAG[] = "<params>";
78   const char PARAMS_ETAG[] = "</params>";
79   const char PARAM_TAG[] = "<param>";
80   const char PARAM_ETAG[] = "</param>";
81 
82   int offset = 0;   // Number of chars parsed from the request
83 
84   std::string methodName = XmlRpcUtil::parseTag(METHODNAME_TAG, xml, &offset);
85   XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed methodName %s.", methodName.c_str());
86 
87   if (! methodName.empty() && XmlRpcUtil::findTag(PARAMS_TAG, xml, &offset))
88   {
89     int nArgs = 0;
90     while (XmlRpcUtil::nextTagIs(PARAM_TAG, xml, &offset)) {
91       std::cout << "Parsing arg " << nArgs+1 << std::endl;
92       XmlRpcValue arg(xml, &offset);
93       if ( ! arg.valid()) {
94         std::cerr << "Invalid argument\n";
95         return std::string();
96       }
97       std::cout << "Adding arg " << nArgs+1 << " to params array." << std::endl;
98       params[nArgs++] = arg;
99       (void) XmlRpcUtil::nextTagIs(PARAM_ETAG, xml, &offset);
100     }
101 
102     XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed %d params.", nArgs);
103 
104     (void) XmlRpcUtil::nextTagIs(PARAMS_ETAG, xml, &offset);
105   }
106 
107   return methodName;
108 }
109