ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/json/rpc.C
Revision: 1.3
Committed: Sun Jul 22 18:38:43 2007 UTC (19 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.2: +6 -8 lines
Log Message:
-fixed help
- temporarily work around corruption in jsonrpc

File Contents

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