Functions

xmlrpc_srv_example.cpp File Reference

#include "httpsrv.h"
#include <iostream>
Include dependency graph for xmlrpc_srv_example.cpp:

Go to the source code of this file.

Functions

static void server_test ()
int main ()

Function Documentation

int main (  )

Definition at line 159 of file xmlrpc_srv_example.cpp.

References server_test().

{
    try {
        server_test();
    } catch(exception& e) {
        cerr << e.what() << endl;
    }

    return 0;
}

Here is the call graph for this function:

static void server_test (  ) [static]

Definition at line 42 of file xmlrpc_srv_example.cpp.

References HttpRequest::_command, HttpRequest::_data, HTTP_POST, HttpServer::receive_request(), HttpServer::register_url(), HttpServer::send_error(), HttpServer::send_response(), and XMLRPC::XRT_INT.

Referenced by main().

{
    HttpServer server;

     // allow persistent HTTP connections
//  server.enable_extensions();

//  server.register_url("http://localhost:8080/");
    server.register_url("http://+:8080/");

     // initialization for the method "Calculator.subtract": two int parameters
    TypeList int_types;
    int_types.push_back(XRT_INT);
    int_types.push_back(XRT_INT);

    for(;;) {
        HttpRequest request;

        server.receive_request(request);

        if (request._command == HTTP_POST) {
#ifdef _DEBUG
            cout << request._data << endl;
#endif

            XMLRPCRequest rpc_req(request._data);
            XMLRPCResponse response;

             // execute the different methods:

            if (rpc_req._method_name == "examples.getStateName") {
                int state = rpc_req.get_int();

                char buffer[20];
                sprintf(buffer, "State %d", state);

                response.set_string(buffer);
            }
            else if (rpc_req._method_name == "Calculator.add") {
                int x = rpc_req.get_int();
                int y = rpc_req.get_int();

                response.set_double(x+y+.5);
            }
            else if (rpc_req._method_name == "Calculator.subtract") {
                 // look at the type of the recevied call parameters
                TypeList types;

                rpc_req.get_parameter_types(types);

                if (types == int_types)
//              if (rpc_req.get_next_type() == XRT_INT)
                {
                    int x = rpc_req.get_int();
                    int y = rpc_req.get_int();

                    response.set_int(x-y);
                } else {
                    const ISODate& d1 = rpc_req.get_date();
                    const ISODate& d2 = rpc_req.get_date();

                    response.set_int(d1.diff(d2));
                }
            }
            else if (rpc_req._method_name == "examples.getArray") {
                int i = rpc_req.get_int();
                string s = rpc_req.get_string();
                double d = rpc_req.get_double();
                bool b = rpc_req.get_bool();

                XMLRPCArray arr;
                arr.push_back(s);
                arr.push_back("TEST");

                response.set_array(arr);
            }
            else if (rpc_req._method_name == "examples.getStruct") {
                string a = rpc_req.get_string();
                string b = rpc_req.get_string();
                string c = rpc_req.get_string();

                XMLRPCStruct str;
                str["a"] = a;
                str["b"] = b;
                str["c"] = c;

                response.set_struct(str);
            }
            else if (rpc_req._method_name == "examples.transferBinary") {
                const Binary& bin = rpc_req.get_binary();

                response.set_string(bin.str());
            }
            else if (rpc_req._method_name == "examples.readBinary") {
                response.set_binary(Binary("Hello binary world", 18));
            }
            else if (rpc_req._method_name == "examples.getTime") {
                response.set_date(ISODate());
            }
            else
                response.set_failure(0, string("unknown method name: ")+rpc_req._method_name);

            if (rpc_req.more_parameters())
                response.set_failure(0, string("too many parameters: ")+rpc_req.get_string());

#ifdef _DEBUG
            cout << response.flush() << endl;
#endif

            server.send_response(request, response.flush(), "text/xml");
        } else
            server.send_error(request, 405, "unknown or unhandled HTTP command");
            //throw exception("unknown HTTP command");
    }
}

Here is the call graph for this function:

Here is the caller graph for this function: