Debian Codegraph @

src/modules/janssonrpcc/README

raw
1 JANSSONRPCC (jsonrpc client) Module
2
3 Joe Hillenbrand
4
5 <[email protected]>
6
7 Edited by
8
9 Matthew Williams
10
11 <[email protected]>
12
13 Copyright © 2013 Flowroute LLC (flowroute.com)
14 __________________________________________________________________
15
16 Table of Contents
17
18 1. Admin Guide
19
20 1. Overview
21 2. Dependencies
22
23 2.1. Kamailio Modules
24 2.2. External Libraries or Applications
25
26 3. Parameters
27
28 3.1. min_srv_ttl (integer)
29 3.2. result_pv (string)
30 3.3. server (string)
31 3.4. retry_codes (string)
32 3.5. keep_alive (integer)
33
34 4. Functions
35
36 4.1. janssonrpc_notification(conn, method, parameters)
37 4.2. janssonrpc_request(conn, method, params[, options]])
38
39 5. Error Handling
40
41 List of Examples
42
43 1.1. Set min_srv_ttl parameter
44 1.2. Set result_pv parameter
45 1.3. Set server parameter
46 1.4. Set retry_codes parameter
47 1.5. Set keep_alive parameter
48 1.6. janssonrpc_notification usage
49 1.7. janssonrpc_request usage
50 1.8. route example with internal_error handling
51
52 Chapter 1. Admin Guide
53
54 Table of Contents
55
56 1. Overview
57 2. Dependencies
58
59 2.1. Kamailio Modules
60 2.2. External Libraries or Applications
61
62 3. Parameters
63
64 3.1. min_srv_ttl (integer)
65 3.2. result_pv (string)
66 3.3. server (string)
67 3.4. retry_codes (string)
68 3.5. keep_alive (integer)
69
70 4. Functions
71
72 4.1. janssonrpc_notification(conn, method, parameters)
73 4.2. janssonrpc_request(conn, method, params[, options]])
74
75 5. Error Handling
76
77 1. Overview
78
79 This module provides access to JSON-RPC 2.0 services (operating over
80 TCP/Netstrings) in accordance with
81 http://www.jsonrpc.org/specification. It uses JANSSON library for JSON
82 document management.
83
84 It uses t_suspend() and t_continue() from the TM module for
85 asynchronous processing.
86
87 Note that after invoking an asynchronous operation, the processing will
88 continue later, in another application process. Therefore, do not rely
89 on variables stored in private memory, use shared memory if you want to
90 get values after the processing is resumed (e.g., $shv(...) or htable
91 $sht(...)).
92
93 2. Dependencies
94
95 2.1. Kamailio Modules
96 2.2. External Libraries or Applications
97
98 2.1. Kamailio Modules
99
100 The following modules must be loaded before this module:
101 * jansson - jansson json handling.
102 * tm - transaction management.
103
104 2.2. External Libraries or Applications
105
106 The following libraries or applications must be installed before
107 running Kamailio with this module loaded:
108 * jansson (http://www.digip.org/jansson/), tested with: 2.2+
109 * libevent 2.0.5+ (http://libevent.org/), tested with: 2.0.16
110
111 3. Parameters
112
113 3.1. min_srv_ttl (integer)
114 3.2. result_pv (string)
115 3.3. server (string)
116 3.4. retry_codes (string)
117 3.5. keep_alive (integer)
118
119 3.1. min_srv_ttl (integer)
120
121 The minimum acceptable TTL in seconds for SRV DNS entries. This means
122 that TTLs from the DNS will be ignored if they are lower than this
123 value. It cannot be set lower than 1 second.
124
125 Default is 5 seconds.
126
127 Example 1.1. Set min_srv_ttl parameter
128 ...
129 modparam("janssonrpcc", "min_srv_ttl", 30)
130 ...
131
132 This will set any SRV TTL lower than 30 seconds to 30 seconds.
133
134 3.2. result_pv (string)
135
136 The PV spec where to store the result of a call to
137 janssonrpc_request(). It can be any writable PV.
138
139 Default value is “$var(jsrpc_result)”.
140
141 Example 1.2. Set result_pv parameter
142 ...
143 modparam("janssonrpcc", "result_pv", "$var(result)")
144 ...
145
146 3.3. server (string)
147
148 The server providing the remote jsonrpc service. Format can be
149 "conn=example;addr=localhost;port=9999;priority=10;weight=20" or
150 "conn=bar;srv=_sip._tcp.example.net".
151
152 * conn - name for a collection of servers (required).
153 * srv - DNS SRV domain name (optional).
154 * addr - host address (required, except when using srv).
155 * port - host port (required, except when using srv).
156 * priority - server are grouped by priority. Servers with higher
157 priority (lower number) are used first. Default is 0. (optional
158 when using addr, invalid otherwise).
159 * weight - functions the same as a DNS SRV weight. Requests are
160 distributed between servers of the same priority proportional to
161 their weight. Default is 1. (optional when using addr, invalid
162 otherwise).
163
164 Example 1.3. Set server parameter
165 ...
166 modparam("janssonrpcc", "server", "conn=tests;srv=_test1._tcp.example.net");
167 modparam("janssonrpcc", "server", "conn=tests;srv=_test2._tcp.example.net");
168 modparam("janssonrpcc", "server", "conn=local;addr=localhost;port=8080;priority=
169 10;weight=10");
170 modparam("janssonrpcc", "server", "conn=user_db;addr=rpc.prod.example.net;port=5
171 060;priority=10;weight=10");
172 ...
173
174 3.4. retry_codes (string)
175
176 A comma delimited list of error codes or error code ranges to
177 automatically schedule a request retry if received.
178
179 This will only be used if there is no route specified for the request.
180
181 An error code can be any integer, but is typically a negative number.
182
183 An error code range is delimited by ".." . For example,
184 "-32099..-32000".
185
186 Spaces are ignored.
187
188 Example 1.4. Set retry_codes parameter
189 ...
190 modparam("janssonrpcc", "retry_codes", "-32603, -32000..-32099");
191 ...
192
193 3.5. keep_alive (integer)
194
195 number of seconds to send a tcp keep-alive to the server connection
196
197 Default is 0 (disabled)
198
199 Example 1.5. Set keep_alive parameter
200 ...
201 modparam("janssonrpcc", "keep_alive", 10)
202 ...
203
204 4. Functions
205
206 4.1. janssonrpc_notification(conn, method, parameters)
207 4.2. janssonrpc_request(conn, method, params[, options]])
208
209 4.1. janssonrpc_notification(conn, method, parameters)
210
211 * conn - name for a collection of servers (required)
212 * method - jsonrpc method (required)
213 * params - jsonrpc request params (required) Use $null or empty
214 string to not send any parameters in the jsonrpc notification.
215
216 Unlike janssonrpc_request (below), notifications do not receive a
217 response. Script processing continues in the usual fashion as soon as
218 the notification has been sent.
219
220 If no servers can be reached, a message is sent to the logs.
221
222 The 'method' and 'params' can be a static string or dynamic string
223 value with config variables.
224
225 Example 1.6. janssonrpc_notification usage
226 ...
227 janssonrpc_notification("user_db", "update_user", '{"id": 1234, "name": "Daniel"
228 }');
229 ...
230
231 4.2. janssonrpc_request(conn, method, params[, options]])
232
233 The conn, method, params, and options can be a static string or a
234 dynamic string value with config variables.
235
236 * conn - name for a collection of servers (required)
237 * method - jsonrpc method (required)
238 * params - jsonrpc request params (required) Use $null or empty
239 string to not send any parameters in the jsonrpc request.
240 * options
241 Options for the janssonrpc_request function. Format can be
242 "route=RESPONSE;retry=2;timeout=100". All these parameters are
243 optional.
244 + retry - number of times you retry a failed request. -1 means
245 retry forever. Default is 0. Request will be retried if they
246 either timeout or fail to send. Retries utilize exponential
247 back off between successive retries, up to 60 seconds. The
248 equation for time between retries is:
249 time = n^2 * timeout (for time < 60 seconds)
250 where n is the number of times a request has been tried.
251 + timeout - request timeout in milliseconds. Default is 500.
252 + route - resume script execution at this route.
253
254 When a response is received, processing continues for the SIP request
255 in the route specified.
256
257 If no route is specified, then any errors are logged and successes are
258 ignored. The function will also not interrupt script execution.
259
260 Since the SIP request handling is resumed in another process, the
261 config file execution is lost. Only shared variables ($shv, $avp, etc)
262 should be used for any value that will be needed when the script is
263 resumed.
264
265 The result is stored in the pseudo-variable specified in the module
266 parameter 'result_pv'. This pseudo-variable is set after the response
267 is received.
268
269 Example 1.7. janssonrpc_request usage
270 ...
271 janssonrpc_request("user_db", "get_user", '{"id": 1234}', "route=RESPONSE;retry=
272 1");
273 ...
274
275 route[RESPONSE] {
276 xlog("Result received: $var(result)");
277 ...
278 }
279 ...
280
281 5. Error Handling
282
283 When a route is specified as part of the janssonrpc_request() function,
284 a JSON object is stored in the result pseudo-variable (see
285 'Parameters').
286
287 The JSON object can be accessed using the jansson_get() function from
288 the jansson module and is of the form:
289 ...
290 {
291 "result" : {...},
292 "error": {...},
293 "internal_error": { "code": ..., "message": ..., "data": ... }
294 }
295 ...
296
297 'result' or 'error' come from the server and should follow the JSONRPC
298 specification. Keep in mind a server's 'error' might not follow the
299 JSONRPC specification and not include a 'code' and/or 'message', so be
300 sure to check that they are there before trying to use them.
301
302 When 'internal_error' is present, that means there was a problem with
303 sending or receiving the request. 'internal_error' contains a 'code'
304 which is an integer representing the type of error, a 'message' which
305 is the error in string form, and possibly 'data' which is usually the
306 failed request, which is optional and can be useful for debugging.
307
308 Here are the possible values for internal error codes:
309 * -1: "Failed to build request"
310 * -5: "Failed to send request"
311 * -10: "JSON parse error"
312 * -11: "Failed to convert response to a pseudo-variable"
313 * -20: "Bad response from server"
314 * -50: "Request retry failed"
315 * -75: "Request dropped for server disconnection"
316 * -100: "Message timeout"
317 * -1000: "There is a bug". Please report these errors to the module
318 maintainers.
319
320 Example 1.8. route example with internal_error handling
321 ...
322 route {
323 janssonrpc_request("user_db", "get_user", '{"id": 1234}', "route=RESPONS
324 E;retry=1");
325 }
326
327 route[RESPONSE] {
328 if(jansson_get("internal_error", $var(jsrpc_result), "$var(internal)"))
329 {
330 route(INTERNAL);
331 } else if(jansson_get("error", $var(jsrpc_result), "$var(error)")) {
332 route(ERROR);
333 } else if(jansson_get("result", $var(jsrpc_result), "$var(result)")) {
334 route(RESULT);
335 }
336 t_reply("200", "OK");
337 }
338
339 route[RESULT] {
340 xlog("result is $var(result)\n");
341 xlog("success\n");
342 }
343
344 route[ERROR] {
345 xlog("There was an error\n");
346 if(jansson_get("code", $var(error), "$var(c)")) {
347 xlog("code is $var(c)\n");
348 }
349
350 if(jansson_get("message", $var(error), "$var(r)")) {
351 xlog("error is $var(r)\n");
352 }
353
354 if(jansson_get("data", $var(error), "$var(d)")) {
355 xlog("data is $var(d)\n");
356 }
357 }
358
359 route[INTERNAL] {
360 xlog("There was an internal error\n");
361
362 jansson_get("code", $var(internal), "$var(c)");
363 xlog("code is $var(c)\n");
364
365 jansson_get("message", $var(internal), "$var(r)");
366 xlog("error is $var(r)\n");
367
368 if(jansson_get("data", $var(internal), "$var(d)")) {
369 xlog("request is $var(d)\n");
370 }
371 }
372 ...