/* * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in doc/pod/license.pod. * * JSON-RPC for Ermyth * * $Id: rpc.C,v 1.7 2007/09/22 14:27:30 pippijn Exp $ */ //>>>>>>>>>>> rpc.C <<<<<<<<<< #include #include #include #include #include "json/json.h" #include "json/rpc.h" namespace json { typedef std::map method_map; method_map methods; void jsonrpc_add_method (char const * const name, jsonrpc_method method) { methods[name] = method; } void jsonrpc_system_list_methods (void *userdata, Value &request, Value &response) { unsigned i = 0; Value method_list (arrayValue); method_map::iterator it; for (it = methods.begin (); it != methods.end (); it++) { method_list[i] = Value (it->first); i++; } response["result"] = method_list; } void jsonrpc_service (void *userdata, Value &request, Value &response) { char const *methodName = static_cast (request["method"]); if (strcmp (methodName, "system.listMethods") == 0) jsonrpc_system_list_methods (userdata, request, response); else { jsonrpc_method method = methods[methodName]; if (method) (*method) (userdata, request, response); } } void jsonrpc_process (void *userdata, char *response_text, char const *request_text, size_t response_len) { std::string text; bool parse_success; Value request (objectValue); Value response (objectValue); Reader reader; FastWriter writer; parse_success = reader.parse (request_text, request_text + strlen (request_text), request); jsonrpc_service (userdata, request, response); text = writer.write (response); memcpy (response_text, text.c_str (), text.length () + 1); } }