xmlrpc_clnt.cpp

Go to the documentation of this file.
00001 
00002  //
00003  // XMLStorage XML-RPC client extension
00004  //
00005  // Copyright (c) 2009 Martin Fuchs <martin-fuchs@gmx.net>
00006  //
00007 
00008 
00009 /*
00010 
00011   All rights reserved.
00012 
00013   Redistribution and use in source and binary forms, with or without
00014   modification, are permitted provided that the following conditions are met:
00015 
00016   * Redistributions of source code must retain the above copyright
00017     notice, this list of conditions and the following disclaimer.
00018   * Redistributions in binary form must reproduce the above copyright
00019     notice, this list of conditions and the following disclaimer in
00020     the documentation and/or other materials provided with the
00021     distribution.
00022 
00023   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00027   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00029   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033   POSSIBILITY OF SUCH DAMAGE.
00034 
00035 */
00036 
00037 #include "httpclnt.h"
00038 
00039 #include <iostream>
00040 
00041 
00042 namespace XMLRPC {
00043 
00044 
00045 XMLRPCCall::XMLRPCCall(const char* name)
00046 {
00047     init(name);
00048 }
00049 
00050 void XMLRPCCall::init(const char* name)
00051 {
00052     _writer.create("methodCall");
00053         _writer.create("methodName");
00054             _writer.set_content(name);
00055         _writer.back();
00056     // The <methodCall> tag is closed in XMLRPCWriter::flush().
00057 }
00058 
00059 
00060 XMLRPCResult::XMLRPCResult(const string& xml_str)
00061  :  XMLRPCParser(xml_str, "methodResponse")
00062 {
00063     if (_pos.go_down("params")) {
00064         // We are now prepared to call the get_int/double/string/...() functions
00065     } else if (_pos.go_down("fault")) {
00066         if (_pos.go("value/struct")) {
00067             XMLRPCStruct members(*_pos);
00068 
00069             int faultCode = atoi(members["faultCode"].c_str());
00070             string faultString = members["faultString"];
00071 
00072             throw XMLRPCFault(faultCode, faultString);
00073 
00074             _pos.back();
00075         }
00076 
00077         _pos.back();
00078     }
00079 }
00080 
00081 
00082 XMLRPCResult XMLRPCClient::call(XMLRPCCall& call)
00083 {
00084     string xml_str = call.flush();
00085 
00086 #ifdef _DEBUG
00087     cout << xml_str << endl;
00088 #endif
00089 
00090     string out = request_post(xml_str);
00091 
00092 #ifdef _DEBUG
00093     cout << out << endl;
00094 #endif
00095 
00096     return XMLRPCResult(out);
00097 }
00098 
00099 
00100 } // namespace XMLRPC