ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/json/rpc.C
Revision: 1.7
Committed: Sat Sep 22 14:27:30 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +3 -2 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

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.7 * $Id: rpc.C,v 1.6 2007-09-05 11:23:15 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.7 #include <util/containers.h>
16     #include <util/predicates.h>
17 pippijn 1.1
18     #include "json/json.h"
19     #include "json/rpc.h"
20    
21     namespace json
22     {
23     typedef std::map<char const *, void (*) (void *, Value &, Value &), str_lt> method_map;
24    
25     method_map methods;
26    
27     void
28 pippijn 1.5 jsonrpc_add_method (char const * const name, jsonrpc_method method)
29 pippijn 1.1 {
30     methods[name] = method;
31     }
32    
33     void
34     jsonrpc_system_list_methods (void *userdata, Value &request, Value &response)
35     {
36     unsigned i = 0;
37     Value method_list (arrayValue);
38    
39     method_map::iterator it;
40 pippijn 1.3 for (it = methods.begin (); it != methods.end (); it++)
41 pippijn 1.1 {
42     method_list[i] = Value (it->first);
43     i++;
44     }
45    
46     response["result"] = method_list;
47     }
48    
49     void
50     jsonrpc_service (void *userdata, Value &request, Value &response)
51     {
52     char const *methodName = static_cast<char const *> (request["method"]);
53    
54     if (strcmp (methodName, "system.listMethods") == 0)
55     jsonrpc_system_list_methods (userdata, request, response);
56     else
57     {
58     jsonrpc_method method = methods[methodName];
59     if (method)
60     (*method) (userdata, request, response);
61     }
62     }
63    
64     void
65     jsonrpc_process (void *userdata, char *response_text, char const *request_text, size_t response_len)
66     {
67     std::string text;
68     bool parse_success;
69 pippijn 1.3 Value request (objectValue);
70     Value response (objectValue);
71 pippijn 1.5 Reader reader;
72 pippijn 1.1 FastWriter writer;
73    
74 pippijn 1.5 parse_success = reader.parse (request_text, request_text + strlen (request_text), request);
75 pippijn 1.1
76     jsonrpc_service (userdata, request, response);
77    
78     text = writer.write (response);
79    
80 pippijn 1.3 memcpy (response_text, text.c_str (), text.length () + 1);
81 pippijn 1.1 }
82     }