24,854
社区成员
发帖
与我相关
我的任务
分享
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 }
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 }
while(true)
{
cout<<"I am sorry...."<<endl;
}
bool XmlRpcClient::execute ( const char * method,
XmlRpcValue const & params,
XmlRpcValue & result
)
Execute the named procedure on the remote server.
Parameters:
method The name of the remote procedure to execute
params An array of the arguments for the method
result The result value to be returned to the client
Returns:
true if the request was sent and a result received (although the result might be a fault).
Currently this is a synchronous (blocking) implementation (execute does not return until it receives a response or an error). Use isFault() to determine whether the result is a fault response.
Definition at line 165 of file XmlRpcClient.cpp.
References XmlRpc::XmlRpcValue::clear(), XmlRpc::XmlRpcUtil::log(), and XmlRpc::XmlRpcDispatch::work().
00166 {
00167 XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s (_connectionState %d).", method, _connectionState);
00168
00169 // This is not a thread-safe operation, if you want to do multithreading, use separate
00170 // clients for each thread. If you want to protect yourself from multiple threads
00171 // accessing the same client, replace this code with a real mutex.
00172 if (_executing)
00173 return false;
00174
00175 _executing = true;
00176 ClearFlagOnExit cf(_executing);
00177
00178 _sendAttempts = 0;
00179 _isFault = false;
00180
00181 if ( ! setupConnection())
00182 return false;
00183
00184 if ( ! generateRequest(method, params))
00185 return false;
00186
00187 result.clear();
00188 double msTime = -1.0; // Process until exit is called
00189 _disp.work(msTime);
00190
00191 if (_connectionState != IDLE || ! parseResponse(result))
00192 return false;
00193
00194 XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s completed.", method);
00195 _response = "";
00196 return true;
00197 }