ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/json/rpc.C
Revision: 1.5
Committed: Tue Aug 28 17:08:12 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +13 -3 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# User Rev Content
1 pippijn 1.5 /*
2     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
3     * Rights to this code are as documented in doc/pod/gplicense.pod.
4     *
5     * JSON-RPC for Ermyth
6     *
7     * $Id: jsonrpc.C,v 1.4 2007-07-25 00:03:21 pippijn Exp $
8     */
9    
10 pippijn 1.1 //>>>>>>>>>>> rpc.C <<<<<<<<<<<//
11     #include <map>
12    
13 pippijn 1.4 #include <svsconfig.h>
14 pippijn 1.5 #include <stdinc.h>
15 pippijn 1.2 #include <common/util.h>
16 pippijn 1.1
17     #include "json/json.h"
18     #include "json/rpc.h"
19    
20     namespace json
21     {
22     typedef std::map<char const *, void (*) (void *, Value &, Value &), str_lt> method_map;
23    
24     method_map methods;
25    
26     void
27 pippijn 1.5 jsonrpc_add_method (char const * const name, jsonrpc_method method)
28 pippijn 1.1 {
29     methods[name] = method;
30     }
31    
32     void
33     jsonrpc_system_list_methods (void *userdata, Value &request, Value &response)
34     {
35     unsigned i = 0;
36     Value method_list (arrayValue);
37    
38     method_map::iterator it;
39 pippijn 1.3 for (it = methods.begin (); it != methods.end (); it++)
40 pippijn 1.1 {
41     method_list[i] = Value (it->first);
42     i++;
43     }
44    
45     response["result"] = method_list;
46     }
47    
48     void
49     jsonrpc_service (void *userdata, Value &request, Value &response)
50     {
51     char const *methodName = static_cast<char const *> (request["method"]);
52    
53     if (strcmp (methodName, "system.listMethods") == 0)
54     jsonrpc_system_list_methods (userdata, request, response);
55     else
56     {
57     jsonrpc_method method = methods[methodName];
58     if (method)
59     (*method) (userdata, request, response);
60     }
61     }
62    
63     void
64     jsonrpc_process (void *userdata, char *response_text, char const *request_text, size_t response_len)
65     {
66     std::string text;
67     bool parse_success;
68 pippijn 1.3 Value request (objectValue);
69     Value response (objectValue);
70 pippijn 1.5 Reader reader;
71 pippijn 1.1 FastWriter writer;
72    
73 pippijn 1.5 parse_success = reader.parse (request_text, request_text + strlen (request_text), request);
74 pippijn 1.1
75     jsonrpc_service (userdata, request, response);
76    
77     text = writer.write (response);
78    
79 pippijn 1.3 memcpy (response_text, text.c_str (), text.length () + 1);
80 pippijn 1.1 }
81     }