ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/json/rpc.C
Revision: 1.6
Committed: Wed Sep 5 11:23:15 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +2 -2 lines
Log Message:
removed GPLed code and put license back to BSD

File Contents

# User Rev Content
1 pippijn 1.5 /*
2     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
3 pippijn 1.6 * Rights to this code are as documented in doc/pod/license.pod.
4 pippijn 1.5 *
5     * JSON-RPC for Ermyth
6     *
7 pippijn 1.6 * $Id: rpc.C,v 1.5 2007-08-28 17:08:12 pippijn Exp $
8 pippijn 1.5 */
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     }