ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/modules/rpc/json/rpc.C
Revision: 1.4
Committed: Wed Jul 25 00:03:21 2007 UTC (19 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +1 -0 lines
Log Message:
initial import

File Contents

# User Rev Content
1 pippijn 1.1 //>>>>>>>>>>> rpc.C <<<<<<<<<<<//
2     #include <map>
3    
4 pippijn 1.4 #include <svsconfig.h>
5 pippijn 1.2 #include <common/util.h>
6 pippijn 1.1
7     #include "json/json.h"
8     #include "json/rpc.h"
9    
10     namespace json
11     {
12     typedef std::map<char const *, void (*) (void *, Value &, Value &), str_lt> method_map;
13    
14     method_map methods;
15    
16     void
17     jsonrpc_add_method (char *name, jsonrpc_method method)
18     {
19     methods[name] = method;
20     }
21    
22     void
23     jsonrpc_system_list_methods (void *userdata, Value &request, Value &response)
24     {
25     unsigned i = 0;
26     Value method_list (arrayValue);
27    
28     method_map::iterator it;
29 pippijn 1.3 for (it = methods.begin (); it != methods.end (); it++)
30 pippijn 1.1 {
31     method_list[i] = Value (it->first);
32     i++;
33     }
34    
35     response["result"] = method_list;
36     }
37    
38     void
39     jsonrpc_service (void *userdata, Value &request, Value &response)
40     {
41     char const *methodName = static_cast<char const *> (request["method"]);
42    
43     if (strcmp (methodName, "system.listMethods") == 0)
44     jsonrpc_system_list_methods (userdata, request, response);
45     else
46     {
47     jsonrpc_method method = methods[methodName];
48     if (method)
49     (*method) (userdata, request, response);
50     }
51     }
52    
53     void
54     jsonrpc_process (void *userdata, char *response_text, char const *request_text, size_t response_len)
55     {
56     std::string text;
57     bool parse_success;
58 pippijn 1.3 Value request (objectValue);
59     Value response (objectValue);
60     Reader *reader = new Reader; // XXX: leak but if not, it corrupts LOOK AT THIS!
61 pippijn 1.1 FastWriter writer;
62    
63 pippijn 1.3 parse_success = reader->parse (request_text, request_text + strlen (request_text), request);
64 pippijn 1.1
65     jsonrpc_service (userdata, request, response);
66    
67     text = writer.write (response);
68    
69 pippijn 1.3 memcpy (response_text, text.c_str (), text.length () + 1);
70 pippijn 1.1 }
71     }