FileClient.cpp
Go to the documentation of this file.
00001 // FileClient.cpp : A simple xmlrpc client. Usage: FileClient serverHost serverPort xmlfile
00002 // Reads an xmlrpc request from the specified xmlfile and calls the method on the server.
00003 // 
00004 // Link against xmlrpc lib and whatever socket libs your system needs (ws2_32.lib on windows)
00005 
00006 #include "XmlRpc.h"
00007 #include <iostream>
00008 #include <fstream>
00009 #include <stdlib.h>
00010 
00011 using namespace XmlRpc;
00012 
00013 std::string parseRequest(std::string const& xml, XmlRpcValue& params);
00014 
00015 
00016 int main(int argc, char* argv[])
00017 {
00018   if (argc != 4) {
00019     std::cerr << "Usage: FileClient serverHost serverPort requestXmlFile\n";
00020     return -1;
00021   }
00022   int port = atoi(argv[2]);
00023 
00024   XmlRpc::setVerbosity(5);
00025   XmlRpcClient c(argv[1], port);
00026 
00027   //
00028   std::ifstream infile(argv[3]);
00029   if (infile.fail()) {
00030     std::cerr << "Could not open file '" << argv[3] << "'.\n";
00031     return -1;
00032   }
00033 
00034   // Suck in the file. This is a one-liner in good compilers (which vc++ 6 is not)...
00035   infile.seekg(0L, std::ios::end);
00036   long nb = infile.tellg();
00037   infile.clear();
00038   infile.seekg(0L);
00039   char* b = new char[nb+1];
00040   infile.read(b, nb);
00041   b[nb] = 0;
00042 
00043   std::cout << "Read file.\n";
00044 
00045   // Find the methodName and parse the params
00046   std::string s(b);
00047   XmlRpcValue params;
00048   std::string name = parseRequest(s, params);
00049 
00050   if (name.empty()) {
00051     std::cerr << "Could not parse file\n";
00052     return -1;
00053   }
00054 
00055   for (;;) {
00056     XmlRpcValue result;
00057     std::cout << "Calling " << name << std::endl;
00058     if (c.execute(name.c_str(), params, result))
00059       std::cout << result << "\n\n";
00060     else
00061       std::cout << "Error calling '" << name << "'\n\n";
00062     std::cout << "Again? [y]: ";
00063     std::string ans;
00064     std::cin >> ans;
00065     if (ans != "" && ans != "y") break;
00066   }
00067 
00068   return 0;
00069 }
00070 
00071 
00072 // 
00073 std::string
00074 parseRequest(std::string const& xml, XmlRpcValue& params)
00075 {
00076   const char METHODNAME_TAG[] = "<methodName>";
00077   const char PARAMS_TAG[] = "<params>";
00078   const char PARAMS_ETAG[] = "</params>";
00079   const char PARAM_TAG[] = "<param>";
00080   const char PARAM_ETAG[] = "</param>";
00081 
00082   int offset = 0;   // Number of chars parsed from the request
00083 
00084   std::string methodName = XmlRpcUtil::parseTag(METHODNAME_TAG, xml, &offset);
00085   XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed methodName %s.", methodName.c_str()); 
00086 
00087   if (! methodName.empty() && XmlRpcUtil::findTag(PARAMS_TAG, xml, &offset))
00088   {
00089     int nArgs = 0;
00090     while (XmlRpcUtil::nextTagIs(PARAM_TAG, xml, &offset)) {
00091       std::cout << "Parsing arg " << nArgs+1 << std::endl;
00092       XmlRpcValue arg(xml, &offset);
00093       if ( ! arg.valid()) {
00094         std::cerr << "Invalid argument\n";
00095         return std::string();
00096       }
00097       std::cout << "Adding arg " << nArgs+1 << " to params array." << std::endl;
00098       params[nArgs++] = arg;
00099       (void) XmlRpcUtil::nextTagIs(PARAM_ETAG, xml, &offset);
00100     }
00101 
00102     XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed %d params.", nArgs); 
00103 
00104     (void) XmlRpcUtil::nextTagIs(PARAMS_ETAG, xml, &offset);
00105   }
00106 
00107   return methodName;
00108 }


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