4 #include "xmlrpc-c/oldcppwrapper.hpp"
6 #include "DataType.hpp"
7 #include "XmlRpcFunction.hpp"
8 #include "XmlRpcClass.hpp"
9 #include "SystemProxy.hpp"
13 #define NAME "xml-rpc-api2cpp"
17 //=========================================================================
18 // function get_class_info
19 //=========================================================================
20 // Connect to a remote server and extract the information we'll need to
21 // build a proxy class.
23 XmlRpcClass get_class_info (string server_url,
27 // Create a place to store our data.
28 XmlRpcClass info(class_name);
30 // Create a proxy class.
31 SystemProxy system(server_url);
33 // Fetch the full list of methods, and process the ones we want.
34 XmlRpcValue methods = system.listMethods();
35 size_t end = methods.arraySize();
36 for (size_t i = 0; i < end; i++) {
38 // Break the method name into two pieces.
41 string method_name = methods.arrayGetItem(i).getString();
42 size_t last_dot = method_name.rfind('.');
43 if (last_dot == string::npos) {
44 function_name = method_name;
46 method_prefix = string(method_name, 0, last_dot);
47 function_name = string(method_name, last_dot + 1);
50 // Decide whether we care about this function.
51 if (method_prefix == class_prefix) {
53 // Fetch some information about the function.
54 string help = system.methodHelp(method_name);
55 XmlRpcValue signature = system.methodSignature(method_name);
57 // Add this function to our class information.
58 XmlRpcFunction func(function_name, method_name, help, signature);
59 info.addFunction(func);
67 //=========================================================================
68 // function print_header
69 //=========================================================================
70 // Print a complete header for the specified class.
72 void print_header (ostream& out, XmlRpcClass& class_info) {
73 string class_name = class_info.className();
74 out << "// " << class_name << ".h - xmlrpc-c C++ proxy class" << endl;
75 out << "// Auto-generated by xml-rpc-api2cpp." << endl;
78 string header_symbol = "_" + class_name + "_H_";
79 out << "#ifndef " << header_symbol << endl;
80 out << "#define " << header_symbol << " 1" << endl;
82 out << "#include <XmlRpcCpp.h>" << endl;
85 class_info.printDeclaration(cout);
88 out << "#endif /* " << header_symbol << " */" << endl;
92 //=========================================================================
93 // function print_cc_file
94 //=========================================================================
95 // Print a complete header for the specified class.
97 void print_cc_file (ostream& out, XmlRpcClass& class_info) {
98 string class_name = class_info.className();
99 out << "// " << class_name << ".cc - xmlrpc-c C++ proxy class" << endl;
100 out << "// Auto-generated by xml-rpc-api2cpp." << endl;
103 out << "#include <XmlRpcCpp.h>" << endl;
104 out << "#include \"" << class_name << ".h\"" << endl;
106 class_info.printDefinition(cout);
110 //=========================================================================
112 //=========================================================================
113 // For now, just a test harness.
115 int main (int argc, char **argv) {
117 /* Parse our command-line arguments. */
119 cerr << argv[0] << ": Usage:" << endl
120 << " xml-rpc-api2cpp <server_url> <method_prefix> <local_class>"
122 << "Sample arguments:" << endl
123 << " server_url = http://localhost/RPC2" << endl
124 << " method_prefix = system" << endl
125 << " local_class = SystemProxy" << endl;
128 string server_url = argv[1];
129 string method_prefix = argv[2];
130 string local_class = argv[3];
133 XmlRpcClient::Initialize(NAME, VERSION);
136 XmlRpcClass system = get_class_info(server_url,
139 print_header(cout, system);
141 print_cc_file(cout, system);
142 } catch (XmlRpcFault& fault) {
143 cerr << argv[0] << ": XML-RPC fault #" << fault.getFaultCode()
144 << ": " << fault.getFaultString() << endl;
146 } catch (logic_error& err) {
147 cerr << argv[0] << ": " << err.what() << endl;
150 cerr << argv[0] << ": Unknown exception" << endl;
154 XmlRpcClient::Terminate();