| 61 | |
| 62 | == Example method == |
| 63 | |
| 64 | This example will show you how to create a method for the rest interface. |
| 65 | For this example we add the ''hello_client'' method to the collectors interface. |
| 66 | The function_wrapper.hpp and function_wrapper.cpp are where the method has to be defined and implemented. |
| 67 | |
| 68 | We add {{{std::string hello_client(std::vector<std::strings>);}}} to the header file and implement the method. |
| 69 | Note that all arguments are saved into a vector of string, the responsebility of converting the arguments into the right data type is part of the method as well as the creation if the XML document for the reply. |
| 70 | |
| 71 | |
| 72 | {{{ |
| 73 | |
| 74 | std::string function_wrapper::hello_client(std::vector<std::string> args){ |
| 75 | if(args.size() > 1){ |
| 76 | return std::string(); |
| 77 | } |
| 78 | else{ |
| 79 | std::stringstream output; |
| 80 | boost::property_tree::ptree pt; |
| 81 | pt.add("method","/hello_client"); |
| 82 | try{ |
| 83 | int end = boost::lexical_cast<int>(args.first()); |
| 84 | for(int i = 1 ; i <= end; i++){ |
| 85 | pt.add("count.num",i); |
| 86 | } |
| 87 | } |
| 88 | catch(exception &e){ |
| 89 | std::cout << e.what() << std::endl; |
| 90 | return std::string(); |
| 91 | } |
| 92 | try{ |
| 93 | boost::property_tree::xml_parser::xml_writer_settings<char> w(' ', 2); |
| 94 | boost::property_tree::xml_parser::write_xml( output, pt,w ); |
| 95 | } |
| 96 | catch (std::exception& e){ |
| 97 | std::cout << e.what() << std::endl; |
| 98 | return std::string(); |
| 99 | } |
| 100 | return output.str(); |
| 101 | } |
| 102 | } |
| 103 | }}} |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |