// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <iostream>
#include <string>

using namespace std;

#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>

using namespace xmlrpc_c;

struct data
{
    int ival;
    string sval;
};

void test(const string urlstr)
{
    clientSimple cli;
    cout << urlstr << endl;
    {
        value res;
        cli.call(urlstr, string("Test.getInt"), "", &res);
        int v = value_int(res);
        cout << v << endl;
    }
    {
        value res;
        cli.call(urlstr, string("Test.getString"), "", &res);
        string s = value_string(res);
        cout << s << endl;
    }
    {
        value res;
        cli.call(urlstr, string("Test.getData"), "", &res);
        cstruct o = value_struct(res);
        struct data d;
        d.ival = value_int(o["ival"]) ;
        d.sval = value_string(o["sval"]);
        cout << "(" << d.ival << "," << d.sval << ")" << endl;
    }
    {
        value res;        
        cli.call(urlstr, string("Test.getListOfInts"), "", &res);
        carray a = value_array(res).vectorValueValue();
        cout << "[";
        for(carray::iterator it = a.begin(); it != a.end(); ++it)
        {
            if(it != a.begin()) cout << ",";
            int v = value_int(*it); 
            cout << v;
        }
        cout << "]" << endl;
    }
    {
        value res;        
        cli.call(urlstr, string("Test.getListOfStrings"), "", &res);
        carray a = value_array(res).vectorValueValue();
        cout << "[";
        for(carray::iterator it = a.begin(); it != a.end(); ++it)
        {
            if(it != a.begin()) cout << ",";
            string s = value_string(*it); 
            cout << s;
        }
        cout << "]" << endl;
    }
    {
        value res;        
        cli.call(urlstr, string("Test.getListOfData"), "", &res);
        carray a = value_array(res).vectorValueValue();
        cout << "[";
        for(carray::iterator it = a.begin(); it != a.end(); ++it)
        {
            if(it != a.begin()) cout << ",";
            cstruct o = value_struct(*it);
            struct data d;
            d.ival = value_int(o["ival"]) ;
            d.sval = value_string(o["sval"]);
            cout << "(" << d.ival << "," << d.sval << ")";
        }
        cout << "]" << endl;
    }
    {
        int v1 = 123;
        int v2 = 456;
        value res;
        cli.call(urlstr, string("Test.add"), "ii", &res, v1, v2);
        int v3 = value_int(res);
        cout << v3 << endl;
    }
    {
        string s1 = "ABC";
        string s2 = "DEF";
        value res;
        cli.call(urlstr, string("Test.concat"), "ss", &res, s1.c_str(), s2.c_str());
        string s3 = value_string(res);
        cout << s3 << endl;
    }
    /*
    // this does not work
    {
        cstruct o;
        o.insert(pair<string,value>("ival", value_int(123)));
        o.insert(pair<string,value>("sval", value_string("ABC")));
        value res;
        cli.call(urlstr, string("Test.modify"), "S", &res, value_struct(o));
        cstruct o2 = value_struct(res);
        struct data d;
        d.ival = value_int(o2["ival"]) ;
        d.sval = value_string(o2["sval"]);
        cout << "(" << d.ival << "," << d.sval << ")" << endl;
    }
    */
}

int main()
{
    test("http://arnepc5:8001/");
    //test("http://arnepc5:8002/");
    //test("http://arnepc5:8003/");
    test("http://localhost:8004/");
    test("http://localhost:8005/");
    return 0;
}
