ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/modules/rpc/json/rpc.C
Revision: 1.2
Committed: Sat Jul 21 01:29:10 2007 UTC (19 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
- moved to new documentation system
- fixed small build error

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     for (it = methods.begin(); it != methods.end(); it++)
29     {
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     Value request(objectValue);
58     Value response(objectValue);
59     Reader reader;
60     FastWriter writer;
61    
62     parse_success = reader.parse (request_text, request_text + strlen (request_text), request);
63    
64     jsonrpc_service (userdata, request, response);
65    
66     text = writer.write (response);
67    
68     memcpy (response_text, text.c_str(), text.length() + 1);
69    
70     return;
71     }
72     }