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