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 208 209 210 211 212 213 214 215 216
|
// FileClient.cpp : A simple xmlrpc client. Usage: FileClient serverHost serverPort xmlfile
// Reads an xmlrpc request from the specified xmlfile and calls the method on the server.
//
// Link against xmlrpc lib and whatever socket libs your system needs (ws2_32.lib on windows)
#include "XmlRpc.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace XmlRpc;
std::string parseRequest(std::string const& xml, XmlRpcValue& params);
int main(int argc, char* argv[])
{
if (argc != 4) {
std::cerr << "Usage: FileClient serverHost serverPort requestXmlFile\n";
return -1;
}
int port = atoi(argv[2]);
XmlRpc::setVerbosity(5);
XmlRpcClient c(argv[1], port);
//
std::ifstream infile(argv[3]);
if (infile.fail()) {
std::cerr << "Could not open file '" << argv[3] << "'.\n";
return -1;
}
// Suck in the file. This is a one-liner in good compilers (which vc++ 6 is not)...
infile.seekg(0L, std::ios::end);
long nb = infile.tellg();
infile.clear();
infile.seekg(0L);
char* b = new char[nb+1];
infile.read(b, nb);
b[nb] = 0;
std::cout << "Read file.\n";
// Find the methodName and parse the params
std::string s(b);
XmlRpcValue params;
std::string name = parseRequest(s, params);
if (name.empty()) {
std::cerr << "Could not parse file\n";
return -1;
}
for (;;) {
XmlRpcValue result;
std::cout << "Calling " << name << std::endl;
if (c.execute(name.c_str(), params, result))
std::cout << result << "\n\n";
else
std::cout << "Error calling '" << name << "'\n\n";
std::cout << "Again? [y]: ";
std::string ans;
std::cin >> ans;
if (ans != "" && ans != "y") break;
}
return 0;
}
//
std::string
parseRequest(std::string const& xml, XmlRpcValue& params)
{
const char METHODNAME_TAG[] = "<methodName>";
const char PARAMS_TAG[] = "<params>";
const char PARAMS_ETAG[] = "</params>";
const char PARAM_TAG[] = "<param>";
const char PARAM_ETAG[] = "</param>";
int offset = 0; // Number of chars parsed from the request
std::string methodName = XmlRpcUtil::parseTag(METHODNAME_TAG, xml, &offset);
XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed methodName %s.", methodName.c_str());
if (! methodName.empty() && XmlRpcUtil::findTag(PARAMS_TAG, xml, &offset))
{
int nArgs = 0;
while (XmlRpcUtil::nextTagIs(PARAM_TAG, xml, &offset)) {
std::cout << "Parsing arg " << nArgs+1 << std::endl;
XmlRpcValue arg(xml, &offset);
if ( ! arg.valid()) {
std::cerr << "Invalid argument\n";
return std::string();
}
std::cout << "Adding arg " << nArgs+1 << " to params array." << std::endl;
params[nArgs++] = arg;
(void) XmlRpcUtil::nextTagIs(PARAM_ETAG, xml, &offset);
}
XmlRpcUtil::log(3, "XmlRpcServerConnection::parseRequest: parsed %d params.", nArgs);
(void) XmlRpcUtil::nextTagIs(PARAMS_ETAG, xml, &offset);
}
return methodName;
}
|