xml-rpc c++ 测试代码

//server.cpp
#include 
#include 

#include "../include/rpc.h"
#include "../include/rpc_value.h"
using namespace XmlRpc;
using namespace std;

XmlRpcServer s;

class sum_c : public XmlRpcServerMethod {
public:
        sum_c(XmlRpcServer *s) : XmlRpcServerMethod("sum", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {
                int nArgs = params.size();
                double sum = 0.0;
                cout << "--------------------------------- sum nArgs--" << params.getType() << params.size() << endl;
                for (int i = 0; i < nArgs; ++i) {
                        if (params[i].getType() == 3)
                                sum += double(params[i]);
                        else if (params[i].getType() == 2)
                                sum += int(params[i]);
                }
                result = sum;
        }

}sum (&s);

class hello_name_c : public XmlRpcServerMethod {
public:
        hello_name_c(XmlRpcServer *s) : XmlRpcServerMethod("hello_name", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {
                cout << "---------------------------------nArgs--" << params.getType() << endl;
                std::string resultString = "Hello, ";
                resultString += std::string(params[0]);
                result = resultString;
        }
} hello_name(&s);

class struct_get_c : public XmlRpcServerMethod {
public:
        struct_get_c(XmlRpcServer *s) : XmlRpcServerMethod("struct_get", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {
                XmlRpcValue A;
                cout << "---------------------------------nArs" << endl;
                //A.setSize(2);
                A["a"] = 123;
                A["b"] = 456;
                A["c"] = 43;
                A["d"] = 425;
                result["a"] = A;
                result["b"] = 123;
                cout << "---nArs------------------------------" << endl;
        }
} struct_get(&s);

class struct_set_c : public XmlRpcServerMethod {
public:
        struct_set_c(XmlRpcServer *s) : XmlRpcServerMethod("struct_set1", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {

                int nArgs = params[0].size();
                double sum = 0.0;
                cout << "---------------------------------nArgs--" << int(params[0]["c"]) << params[0].getType() << params[0].size() << endl;

                for (int i = 0; i < nArgs; ++i) {
                        sum += int(params[i]["a"]);
                        sum += int(params[i]["b"]);
                }


                cout << "d" << std::string(params[0]["d"]) << endl;
                cout << "nArgs---------------------------------" << nArgs << endl;
                result = sum;
        }
} struct_set(&s);

class struct_buf_get_c : public XmlRpcServerMethod {
public:
        struct_buf_get_c(XmlRpcServer *s) : XmlRpcServerMethod("struct_buf_get", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {
                XmlRpcValue A;
                A.setSize(2);
                A[0]["a"] = 123;
                A[0]["b"] = 456;
                A[1]["a"] = 43;
                A[1]["b"] = 425;
                result["a"] = A;
                result["b"] = 123;
        }
} struct_buf_get(&s);

class struct_buf_set_c : public XmlRpcServerMethod {
public:
        struct_buf_set_c(XmlRpcServer *s) : XmlRpcServerMethod("struct_buf_set", s)
        { }
        void execute(XmlRpcValue ¶ms, XmlRpcValue &result)
        {

                int nArgs = params.size();
                double sum = 0.0;

                for (int i = 0; i < nArgs; ++i) {
                        sum += int(params[i]["a"]);
                        sum += int(params[i]["b"]);
                }
                result = sum;
        }
} struct_buf_set(&s);

int main()
{
        XmlRpc::setVerbosity(3);
        s.bindAndListen(3838);
        s.enableIntrospection(true);
        s.work(-1);

        return 0;

}

 
  
//client.h
 
  
#ifndef RPC_CLIENT_H
#define RPC_CLIENT_H

#include "../include/rpc.h"
using namespace XmlRpc;

class c_client : public XmlRpcClient {
public:
        c_client(const char *host, int port, const char *uri = 0);
        ~c_client();
};

#endif //RPC_CLIENT_H

 
  
 
  
//client_cpp
 
  

//
#include 
#include 
#include "client.h"
#include "../include/rpc.h"
using namespace XmlRpc;
using  namespace std;

c_client::c_client(const char *host, int port, const char *uri) : XmlRpcClient(host, port, uri)
{

}
c_client::~c_client()
{

}

int main()
{
        const char *server = "localhost";
        const int port = 3838;
        const char *uri = NULL;
        XmlRpcValue args, res;

        XmlRpc::setVerbosity(3);
#if 1
        std::cout<<"1--- "<;
        c_client client(server, port, uri);
#if 0
        args.setSize(3);

        //*((double *)(&(args[0])))  = 1;
       // *((double *)(&(args[1])))  = 2;
        //*((double *)(&(args[2])))  = 3;
        cout <<  args.getType() << endl;

        args[0]  = 1;
        cout <<  args[0].getType() << endl;
        //args[0]  = 1;
       // cout << "22222" << endl;
        args[1]  = 2;
        cout <<  args[1].getType() << endl;
        args[2]  = 3.0;
        cout <<  args[2].getType() << endl;

        client.execute("sum", args, res);
        client.execute("struct_get", args, res);
#endif

#if 0
        args.setSize(2);
        args[0]["a"] = 123;
        args[0]["b"] = 456;
        args[1]["a"] = 43;
        args[1]["b"] = 425;

        client.execute("struct_buf_set", args, res);
#endif
#if 0
        args[0] = 3;
        args[1] = 3;
        client.execute("sum", args, res);
#endif
        args["a"] = 1;
        args["b"] = 2;
        args["c"] = 3;
        args["d"] = "abcdefg";
        //std::cout<<"size is "<        client.execute("struct_set1", args, res);
        //std::cout<<"size is "<        //client.execute("sum", args, res);
#else
        std::cout<<"2--- "<;
        XmlRpcClient c(server, port, uri);
        c.execute("system.listMethods", args, res);
#endif
        std::cout<<"result is "<;

        return 0;

}



 
  
 
 

你可能感兴趣的:(cpp)