Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "httpsrv.h"
00038
00039 #include <iostream>
00040
00041
00042 static void server_test()
00043 {
00044 HttpServer server;
00045
00046
00047
00048
00049
00050 server.register_url("http://+:8080/");
00051
00052
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
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
00088 TypeList types;
00089
00090 rpc_req.get_parameter_types(types);
00091
00092 if (types == int_types)
00093
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
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 }