xmlrpc_srv_example.cpp

Go to the documentation of this file.
00001 
00002  //
00003  // XMLStorage XML-RPC server example
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 "httpsrv.h"
00038 
00039 #include <iostream>
00040 
00041 
00042 static void server_test()
00043 {
00044     HttpServer server;
00045 
00046      // allow persistent HTTP connections
00047 //  server.enable_extensions();
00048 
00049 //  server.register_url("http://localhost:8080/");
00050     server.register_url("http://+:8080/");
00051 
00052      // initialization for the method "Calculator.subtract": two int parameters
00053     TypeList int_types;
00054     int_types.push_back(XRT_INT);
00055     int_types.push_back(XRT_INT);
00056 
00057     for(;;) {
00058         HttpRequest request;
00059 
00060         server.receive_request(request);
00061 
00062         if (request._command == HTTP_POST) {
00063 #ifdef _DEBUG
00064             cout << request._data << endl;
00065 #endif
00066 
00067             XMLRPCRequest rpc_req(request._data);
00068             XMLRPCResponse response;
00069 
00070              // execute the different methods:
00071 
00072             if (rpc_req._method_name == "examples.getStateName") {
00073                 int state = rpc_req.get_int();
00074 
00075                 char buffer[20];
00076                 sprintf(buffer, "State %d", state);
00077 
00078                 response.set_string(buffer);
00079             }
00080             else if (rpc_req._method_name == "Calculator.add") {
00081                 int x = rpc_req.get_int();
00082                 int y = rpc_req.get_int();
00083 
00084                 response.set_double(x+y+.5);
00085             }
00086             else if (rpc_req._method_name == "Calculator.subtract") {
00087                  // look at the type of the recevied call parameters
00088                 TypeList types;
00089 
00090                 rpc_req.get_parameter_types(types);
00091 
00092                 if (types == int_types)
00093 //              if (rpc_req.get_next_type() == XRT_INT)
00094                 {
00095                     int x = rpc_req.get_int();
00096                     int y = rpc_req.get_int();
00097 
00098                     response.set_int(x-y);
00099                 } else {
00100                     const ISODate& d1 = rpc_req.get_date();
00101                     const ISODate& d2 = rpc_req.get_date();
00102 
00103                     response.set_int(d1.diff(d2));
00104                 }
00105             }
00106             else if (rpc_req._method_name == "examples.getArray") {
00107                 int i = rpc_req.get_int();
00108                 string s = rpc_req.get_string();
00109                 double d = rpc_req.get_double();
00110                 bool b = rpc_req.get_bool();
00111 
00112                 XMLRPCArray arr;
00113                 arr.push_back(s);
00114                 arr.push_back("TEST");
00115 
00116                 response.set_array(arr);
00117             }
00118             else if (rpc_req._method_name == "examples.getStruct") {
00119                 string a = rpc_req.get_string();
00120                 string b = rpc_req.get_string();
00121                 string c = rpc_req.get_string();
00122 
00123                 XMLRPCStruct str;
00124                 str["a"] = a;
00125                 str["b"] = b;
00126                 str["c"] = c;
00127 
00128                 response.set_struct(str);
00129             }
00130             else if (rpc_req._method_name == "examples.transferBinary") {
00131                 const Binary& bin = rpc_req.get_binary();
00132 
00133                 response.set_string(bin.str());
00134             }
00135             else if (rpc_req._method_name == "examples.readBinary") {
00136                 response.set_binary(Binary("Hello binary world", 18));
00137             }
00138             else if (rpc_req._method_name == "examples.getTime") {
00139                 response.set_date(ISODate());
00140             }
00141             else
00142                 response.set_failure(0, string("unknown method name: ")+rpc_req._method_name);
00143 
00144             if (rpc_req.more_parameters())
00145                 response.set_failure(0, string("too many parameters: ")+rpc_req.get_string());
00146 
00147 #ifdef _DEBUG
00148             cout << response.flush() << endl;
00149 #endif
00150 
00151             server.send_response(request, response.flush(), "text/xml");
00152         } else
00153             server.send_error(request, 405, "unknown or unhandled HTTP command");
00154             //throw exception("unknown HTTP command");
00155     }
00156 }
00157 
00158 
00159 int main()
00160 {
00161     try {
00162         server_test();
00163     } catch(exception& e) {
00164         cerr << e.what() << endl;
00165     }
00166 
00167     return 0;
00168 }