Line data Source code
1 : /********************************************************************\
2 :
3 : Name: mjsonrpc_user.cxx
4 : Created by: Konstantin Olchanski
5 :
6 : Contents: handler of user-provided and experimental JSON-RPC requests
7 :
8 : \********************************************************************/
9 :
10 : #include <stdio.h>
11 :
12 : #include "mjsonrpc.h"
13 :
14 : //
15 : // example 1: extract request parameters, return up to 3 results
16 : //
17 :
18 0 : static MJsonNode* user_example1(const MJsonNode* params)
19 : {
20 0 : if (!params) {
21 0 : MJSO* doc = MJSO::I();
22 0 : doc->D("example of user defined RPC method that returns up to 3 results");
23 0 : doc->P("arg", MJSON_STRING, "example string argment");
24 0 : doc->P("optional_arg?", MJSON_INT, "optional example integer argument");
25 0 : doc->R("string", MJSON_STRING, "returns the value of \"arg\" parameter");
26 0 : doc->R("integer", MJSON_INT, "returns the value of \"optional_arg\" parameter");
27 0 : return doc;
28 : }
29 :
30 0 : MJsonNode* error = NULL;
31 :
32 0 : std::string arg = mjsonrpc_get_param(params, "arg", &error)->GetString(); if (error) return error;
33 0 : int optional_arg = mjsonrpc_get_param(params, "optional_arg", NULL)->GetInt();
34 :
35 0 : if (mjsonrpc_debug)
36 0 : printf("user_example1(%s,%d)\n", arg.c_str(), optional_arg);
37 :
38 0 : return mjsonrpc_make_result("string", MJsonNode::MakeString(arg.c_str()), "integer", MJsonNode::MakeInt(optional_arg));
39 0 : }
40 :
41 : //
42 : // example 2: extract request parameters, return more than 3 results
43 : //
44 :
45 0 : static MJsonNode* user_example2(const MJsonNode* params)
46 : {
47 0 : if (!params) {
48 0 : MJSO* doc = MJSO::I();
49 0 : doc->D("example of user defined RPC method that returns more than 3 results");
50 0 : doc->P("arg", MJSON_STRING, "example string argment");
51 0 : doc->P("optional_arg?", MJSON_INT, "optional example integer argument");
52 0 : doc->R("string1", MJSON_STRING, "returns the value of \"arg\" parameter");
53 0 : doc->R("string2", MJSON_STRING, "returns \"hello\"");
54 0 : doc->R("string3", MJSON_STRING, "returns \"world!\"");
55 0 : doc->R("value1", MJSON_INT, "returns the value of \"optional_arg\" parameter");
56 0 : doc->R("value2", MJSON_NUMBER, "returns 3.14");
57 0 : return doc;
58 : }
59 :
60 0 : MJsonNode* error = NULL;
61 :
62 0 : std::string arg = mjsonrpc_get_param(params, "arg", &error)->GetString(); if (error) return error;
63 0 : int optional_arg = mjsonrpc_get_param(params, "optional_arg", NULL)->GetInt();
64 :
65 0 : if (mjsonrpc_debug)
66 0 : printf("user_example2(%s,%d)\n", arg.c_str(), optional_arg);
67 :
68 0 : MJsonNode* result = MJsonNode::MakeObject();
69 :
70 0 : result->AddToObject("string1", MJsonNode::MakeString(arg.c_str()));
71 0 : result->AddToObject("string2", MJsonNode::MakeString("hello"));
72 0 : result->AddToObject("string3", MJsonNode::MakeString("world!"));
73 0 : result->AddToObject("value1", MJsonNode::MakeInt(optional_arg));
74 0 : result->AddToObject("value2", MJsonNode::MakeNumber(3.14));
75 :
76 0 : return mjsonrpc_make_result(result);
77 0 : }
78 :
79 : //
80 : // example 3: return an error
81 : //
82 :
83 0 : static MJsonNode* user_example3(const MJsonNode* params)
84 : {
85 0 : if (!params) {
86 0 : MJSO* doc = MJSO::I();
87 0 : doc->D("example of user defined RPC method that returns an error");
88 0 : doc->P("arg", MJSON_INT, "integer value, if zero, throws a JSON-RPC error");
89 0 : doc->R("status", MJSON_INT, "returns the value of \"arg\" parameter");
90 0 : return doc;
91 : }
92 :
93 0 : MJsonNode* error = NULL;
94 :
95 0 : int arg = mjsonrpc_get_param(params, "arg", &error)->GetInt(); if (error) return error;
96 :
97 0 : if (mjsonrpc_debug)
98 0 : printf("user_example3(%d)\n", arg);
99 :
100 0 : if (arg)
101 0 : return mjsonrpc_make_result("status", MJsonNode::MakeInt(arg));
102 : else
103 0 : return mjsonrpc_make_error(15, "example error message", "example error data");
104 : }
105 :
106 : //
107 : // to create your own rpc method handler, copy one of the examples here, register it in user_init below
108 : //
109 :
110 :
111 :
112 : //
113 : // user_init function is called at startup time to register user rpc method handlers
114 : //
115 :
116 0 : void mjsonrpc_user_init()
117 : {
118 0 : if (mjsonrpc_debug) {
119 0 : printf("mjsonrpc_user_init!\n");
120 : }
121 :
122 : // add user functions to the rpc list
123 :
124 0 : mjsonrpc_add_handler("user_example1", user_example1);
125 0 : mjsonrpc_add_handler("user_example2", user_example2);
126 0 : mjsonrpc_add_handler("user_example3", user_example3);
127 0 : }
128 :
129 : /* emacs
130 : * Local Variables:
131 : * tab-width: 8
132 : * c-basic-offset: 3
133 : * indent-tabs-mode: nil
134 : * End:
135 : */
136 :
|