-
Notifications
You must be signed in to change notification settings - Fork 909
/
erlang_admin.xml
700 lines (640 loc) · 21.3 KB
/
erlang_admin.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../../doc/docbook/entities.xml">
%docentities;
]>
<!-- Module User's Guide -->
<chapter>
<title>&adminguide;</title>
<section>
<title>Overview</title>
<para>
Erlang is a general-purpose programming language and runtime environment.
Erlang has built-in support for concurrency, distribution and fault tolerance.
This module provides interact with Erlang node. The module allows sending,
receiving Erlang messages and RPC calls between each other.
</para>
</section>
<section>
<title>Dependencies</title>
<section>
<title>&kamailio; Modules</title>
<para>
The following modules must be loaded before this module:
<itemizedlist mark="none">
<listitem>
<para>
<emphasis>None</emphasis>
</para>
</listitem>
</itemizedlist>
</para>
</section>
<section>
<title>External Libraries or Applications</title>
<para>
The following libraries or applications must be installed before running
&kamailio; with this module loaded:
<itemizedlist mark="none">
<listitem>
<para>
<emphasis>ei</emphasis> - Erlang interface C library.
<para>
The routines for handling the Erlang binary term format
and communicate with with distributed Erlang.
For more details see <ulink url="http://www.erlang.org/doc/apps/erl_interface/index.html">Erlang Interface Reference Manual</ulink>
</para>
</para>
</listitem>
<listitem>
<para>
<emphasis>epmd</emphasis> - Erlang Port Mapper Daemon.
<para>
This daemon acts as a name server on all hosts involved in distributed
Erlang computations. For more details see <ulink url="http://www.erlang.org/doc/man/epmd.html">
Erlang Port Mapper Daemon Manual</ulink>.
</para>
</para>
</listitem>
</itemizedlist>
</para>
<para>
<emphasis>epmd</emphasis> must running on the same host where &kamailio; is running.
Erlang does not need to be installed on the same host where &kamailio; is running.
</para>
</section>
</section>
<section>
<title>Parameters</title>
<section id="erlang.p.no_cnodes">
<title><varname>no_cnodes</varname> (int)</title>
<para>
Number of erlang C node processes to be started to handle the communication
tasks. A C node is a C program written to act as a hidden node in a distributed Erlang system.
</para>
<para>
<emphasis>
Default value is 1.
</emphasis>
</para>
<example>
<title>Set <varname>no_cnodes</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "no_cnodes", 2)
...
</programlisting>
</example>
</section>
<section id="erlang.p.cnode_alivename">
<title><varname>cnode_alivename</varname> (str)</title>
<para>
<emphasis>alivename</emphasis> is the registered name of the &kamailio; process.
</para>
<para>
<emphasis>Note,</emphasis> if the <varname>no_cnodes</varname> greater then 1,
then <emphasis>alivename</emphasis> of &kamailio; process will be suffixed with number.
</para>
<example>
<title>Set <varname>cnode_alivename</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "cnode_alivename", "proxy")
...
</programlisting>
</example>
</section>
<section id="erlang.p.cnode_host">
<title><varname>cnode_host</varname> (str)</title>
<para>
C node <emphasis>host</emphasis> is the name of the machine we're running on.
If long names are to be used, it should be fully qualified.
</para>
<example>
<title>Set <varname>cnode_host</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "cnode_host", "kamailio.lan")
...
</programlisting>
</example>
</section>
<section id="erlang.p.erlang_nodename">
<title><varname>erlang_nodename</varname> (str)</title>
<para>
The format of the node name is a <emphasis>name@host</emphasis>
where name is the name given by the user and host is the full
host name if long names are used, or the first part of the host
name if short names are used.
</para>
<example>
<title>Set <varname>erlang_nodename</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "erlang_nodename", "node1@erlang.lan")
...
</programlisting>
</example>
</section>
<section id="erlang.p.cookie">
<title><varname>cookie</varname> (str)</title>
<para>
Each node has its own magic cookie. When a nodes tries to connect
to another node, the magic cookies are compared If they do not match,
the connected node rejects the connection.
</para>
<example>
<title>Set <varname>cookie</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "cookie", "secretcookie")
...
</programlisting>
</example>
</section>
<section id="erlang.p.trace_level">
<title><varname>trace_level</varname> (int)</title>
<para>
Used to set tracing on the distribution. The parameter is different
verbosity level. A higher level means more information. Useful in
development, but in production should be disabled.
</para>
<para>
The different tracelevels has the following messages:
<itemizedlist>
<orderedlist>
<listitem>
Verbose error messages
</listitem>
<listitem>
Above messages and verbose warning messages
</listitem>
<listitem>
Above messages and progress reports for connection handling
</listitem>
<listitem>
Above messages and progress reports for communication
</listitem>
<listitem>
Above messages and progress reports for data conversion
</listitem>
</orderedlist>
</itemizedlist>
</para>
<emphasis>
Default value is 0, no verbose is set.
</emphasis>
<emphasis>
To see trace log on stdout, &kamailio; must be started with <emphasis>-E</emphasis> option.
</emphasis>
<example>
<title>Set <varname>trace_level</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("erlang", "trace_level", 5)
...
</programlisting>
</example>
</section>
<section>
<title><varname>rpc_reply_with_struct</varname> (int)</title>
<para>
Prepend Erlang atom struct in RPC &kamailio; reply where RPC struct is generated.
Some libraries for converting erlang term in JSON require atom struct
as first element in tuple to convert list of properties into javscript object.
</para>
<emphasis>
Default value is 0 (disabled).
</emphasis>
</section>
</section>
<section>
<title>Exported pseudo variables</title>
<section>
<title>Overview</title>
<para>
Erlang provides a number of data types but not all of them are supported by &kamailio;.
This module provides several of them as pseudo variables. These pseudo variables
allow user to create Erlang message or function arguments and receive result from
Erlang node. Some Erlang variables can contain other data types, so we introduced several
attributes to determine type, length, or get formatted output useful for debug,
quick compare or put into the log.
</para>
</section>
<section>
<title>Attributes</title>
<para>
Exported pseudo variable has several attributes to get type, length and formatted output
readable by transformations or user. Pseudo variable by self determine variable type, but
we need to know what type on some position is.
</para>
<itemizedlist>
<listitem>
<varname>type</varname>
<para>
get variable type. Possible types are: <emphasis>atom, integer,
list, string</emphasis>, <emphasis>tuple</emphasis>,
<emphasis>pid</emphasis> and <emphasis>ref</emphasis>.
</para>
</listitem>
<listitem>
<varname>length</varname>
<para>
get length of list or tuple.
</para>
</listitem>
<listitem>
<varname>format</varname>
<para>
Prints a term, in clear text. It tries to resemble the
term printing in the Erlang shell.
</para>
</listitem>
</itemizedlist>
<example>
<title>Example of using attribute <emphasis>length</emphasis></title>
<programlisting format="linespecific">
...
xlogl("L_DEBUG","The number of elements in list L: $erl_list(L=>length)\n");
xlogl("L_DEBUG","The number of elements in tuple T: $erl_tuple(T=>length)\n");
...
> example of log output:
DEBUG: <script>: 120:The number of elements in list L: 4
DEBUG: <script>: 121:The number of elements in tuple T: 2
...
</programlisting>
</example>
<example>
<title>Example of using attributes <emphasis>type</emphasis> and <emphasis>format</emphasis></title>
<programlisting format="linespecific">
...
xlogl("L_DEBUG","list L in clear text: $erl_list(L=>format), the type at index 2: $erl_list(L[2]=>type)\n");
xlogl("L_DEBUG","tuple T in clear text: $erltuple(T=>format), the type at index 1: $erl_tuple[T[1]=>type)\n");
...
> example of log output:
DEBUG: <script>: 130:list L in clear text: [example, list, 4, "items"], the type at index 2: integer
DEBUG: <script>: 131:tuple T in clear text: {example, tuple}, the type at index 1: atom
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_atom">
<title><varname>$erl_atom(name)</varname></title>
<para>
<emphasis>erl_atom</emphasis> pseudo variable allows create analog to Erlang atom data type.
Erlang atom is a literal, a constant with name. Formatted output pseudo
variable atom could be enclosed in single quotes (') if it does
not begin with a lower-case letter or if it contains other characters
than alphanumeric characters, underscore (_), or @.
</para>
<para>
</para>
<example>
<title>Example set and print to log $erl_atom</title>
<programlisting format="linespecific">
...
$erl_atom(A) = "badrpc";
xlogl("L_DEBUG","$$erl_atom(A): $erl_atom(A=>format)\n");
...
> log output is:
DEBUG: <script>: 123:$erl_atom(A): badrpc
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_list">
<title><varname>$erl_list(name)</varname></title>
<para>
Compound data type with a variable number of terms. Formally,
a list is either the empty list [] or consists of one or more
elements.
</para>
<para>
Behavior of the list is the same as AVP variable. Set value without
index prepends element on list. You are able to replace element at
given index. List supports [*] index to get whole list or reset all
elements in the list. To create empty list set whole list to
<emphasis>$null</emphasis>.
</para>
<example>
<title>Example of using $erl_list</title>
<programlisting format="linespecific">
...
$erl_atom(E) = "example";
$erl_list(L) = "list";
$erl_list(L) = "of";
$erl_list(L) = $erl_atom(E);
xlogl("L_DEBUG","length(L): $erl_list(L=>length), format(L): $erl_list(L=>format)\n");
# empty list
$erl_tuple(E[*]) = $null;
xlogl("L_DEBUG","length(E): $erl_list(E=>length), format(E): $erl_list(L=>format)\n");
...
> log output is:
DEBUG: <script>: 143:length(L): 3, format(L): [example, "of", "list"]
DEBUG: <script>: 148:length(E): 0, format(E): []
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_tuple">
<title><varname>$erl_tuple(name)</varname></title>
<para>
From the Erlang point of view the tuple compound data type with
a fixed number of terms. The module implementation of tuple has
the same behavior as the list.
</para>
<example>
<title>Example of using erl_tuple</title>
<programlisting format="linespecific">
...
$erl_atom(line) = "line";
$erl_atom(id) = "id";
$erl_atom(owner)= "owner";
$var(user) = "Bob";
$erl_tuple(owner) = $var(user);
$erl_tuple(owner) = $erl_atom(owner);
$erl_tuple(id) = 23;
$erl_tuple(id) = $erl_atom(id);
$erl_list(L) = $erl_tuple(owner);
$erl_list(L) = $erl_tuple(id);
$erl_tuple(T) = $erl_list(L);
$erl_tuple(T) = $erl_atom(line);
xlogl("L_DEBUG","length(T): $erl_tuple(T=>length), format(T): $erl_tuple(T=>format)\n");
...
> log output is:
...
DEBUG: <script>: 153:length(T): 2, format(T): {line, [{id, 23}, {owner, "Bob"}]}
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_pid">
<title><varname>$erl_pid(name)</varname></title>
<para>
<emphasis>erl_pid</emphasis> holds Eralng process identifier. Provides
access to Erlang PID value and could be used in send message.
</para>
<example>
<title>Example of using $erl_pid</title>
<programlisting format="linespecific">
...
if ($erl_xbuff(msg[0]=>type) == "pid") {
$erl_pid(pid) = $erl_xbuff(msg[0]);
xlogl("L_INFO","Received PID: $erl_pid(pid=>format)\n");
}
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_ref">
<title><varname>$erl_ref(name)</varname></title>
<para>
<emphasis>erl_ref</emphasis> holds Erlang reference. Provides
access to reference value and could be used in send message.
</para>
<example>
<title>Example of using erl_ref</title>
<programlisting format="linespecific">
...
if ($erl_xbuff(msg[0]=>type) == "ref") {
$erl_ref(ref) = $erl_xbuff(msg[0]);
xlogl("L_INFO","Reference: $erl_ref(ref=>format)\n");
}
...
</programlisting>
</example>
</section>
<section id="erlang.v.erl_xbuff">
<title><varname>$erl_xbuff(name)</varname></title>
<para>
<emphasis>erl_xbuff</emphasis> is created as generic pseudo variable
to acts as other pseudo variables exported from module. It's useful
when in advance we don't know what variable type to expect.
The behavior of variable depends of initialization. Module
functions expect this PV as return value, and PV for incoming
Erlang message.
</para>
<example>
<title>Example of using $erl_xbuff</title>
<programlisting format="linespecific">
...
# Tuple T from previous example
$erl_xbuff(X) = $erl_tuple(T);
xlogl("L_DEBUG","typeof(X): $erl_xbuff(X=>type), length(X): $erl_xbuff(X=>length), format(X): $erl_xbuff(X=>format)\n");
...
> log output is:
...
DEBUG: <script>: 410:typeof(X): tuple, length(X): 2, format(X): {line, [{id, 23}, {owner, "Bob"}]}
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Functions</title>
<section id="erlang.f.erl_rpc">
<title>
<function moreinfo="none">erl_rpc(mod,fun,args,reply)</function>
</title>
<para>
This function supports calling Erlang functions on remote nodes.
</para>
<para>
The parameter <emphasis>mod</emphasis> and <emphasis>fun</emphasis>
are module and function name respectively. It can be a static
string or a dynamic string value with config variables.
</para>
<para>
The parameter <emphasis>args</emphasis> is list of arguments passed to
function, so it must be <emphasis>list</emphasis>, or <emphasis>xbuff</emphasis> that contains list.
</para>
<para>
The parameter <emphasis>reply</emphasis> is result from RPC call. It must
be <emphasis>xbuff</emphasis> to accept any result.
</para>
<para>
Function returns false on error to send or wrong arguments passed to function.
If executing remote function caused error function still returns
true but error is encoded into <emphasis>repl</emphasis> parameter.
</para>
<example>
<title>Example of using erl_rpc</title>
<programlisting format="linespecific">
...
# example of call erlang:list_to_tuple(["one","two"])
# on remote node
$erl_list(L) = "two";
$erl_list(L) = "one";
# put list into list
$erl_list(args) = $erl_list(L);
erl_rpc("erlang", "list_to_tuple", "$erl_list(args)", "$erl_xbuff(repl)");
xlogl("L_DEBUG","type(repl): $erl_xbuff(repl=>type), format(repl): $erl_xbuff(repl=>format)\n");
> log output:
...
DEBUG: <script>: 386:type(repl): tuple, format(repl): {"one", "two"}
...
</programlisting>
</example>
</section>
<section id="erlang.f.erl_reg_send">
<title><function moreinfo="none">erl_reg_send(server,msg)</function></title>
<para>
This function sends an Erlang term to a registered process.
</para>
<para>
The argument <emphasis>server</emphasis> is the registered name
of the intended recipient process on remote node.
</para>
<para>
The argument <emphasis>msg</emphasis> is containing the message to be sent.
</para>
<example>
<title>Example of using erl_reg_send</title>
<programlisting format="linespecific">
...
# example of send message to registered process
# {notifier,'node1@erlang.lan'} ! {example,message}
$erl_atom(example) = "example";
$erl_atom(message) = "message";
$erl_tuple(M) = $erl_atom(message);
$erl_tuple(M) = $erl_atom(example);
erl_reg_send("notifier","$erl_tuple(M)");
...
</programlisting>
</example>
</section>
<section id="erlang.f.erl_send">
<title><function moreinfo="none">erl_send(pid,msg)</function></title>
<para>
This function sends an Erlang term to a process. This function
can be used from ANY_ROUTE. The argument <emphasis>pid</emphasis>
is the Erlang process id of the intended recipient process on
remote node. The argument <emphasis>msg</emphasis> is containing
the message to be sent.
</para>
<example>
<title>Example of using erl_send</title>
<programlisting format="linespecific">
...
# example of send message to process
# Pid ! {example,message}
$erl_atom(notifier) = "notifier";
$erl_list(args) = $erl_atom(notifier);
erl_rpc("erlang", "whereis", "$erl_list(args)", "$erl_xbuff(pid)");
$erl_atom(example) = "example";
$erl_atom(message) = "message";
$erl_tuple(M) = $erl_atom(message);
$erl_tuple(M) = $erl_atom(example);
erl_send("$erl_xbuff(pid)","$erl_tuple(M)");
...
</programlisting>
</example>
</section>
<section id="erlang.f.erl_reply">
<title><function>erl_reply(msg)</function></title>
<para>
Function to send message from event route (pseudo process).
Function sends reply message <emphasis>msg</emphasis> to the sender process.
</para>
<example>
<title>Example of use erl_reply</title>
<programlisting format="linespecific">
...
# event route acts as registered process
event_route[erlang:greetings] {
xlogl("L_INFO","Received message: $erl_xbuff(msg=>format)\n");
$erl_atom(hello) = "hello";
$erl_tuple(reply) = "Erlang";
$erl_tuple(reply) = $erl_atom(hello);
# reply greeting
erl_reply("$erl_tuple(reply)");
}
...
%% in erlang shell
(node1@erlang.lan)24> {greetings,'proxy@kamailio.lan'} ! {hello,"Kamailio"}.
{hello,"Kamailio"}
(node1@erlang.lan)25> flush().
Shell got {hello,"Erlang"}
ok
> logged info message:
INFO: <script>: 951:Received message: {"hello", "Kamailio"}
>
</programlisting>
</example>
</section>
</section>
<section>
<title>Event routes</title>
<para>
To allow &kamailio; C node to have similar to erlang registered processes
we can use event routes. Event routes executed when asynchronous message received
from erlang node.
</para>
<para>
Event route receives message in <varname>$erl_xbuff(msg)</varname> and sender
process in <varname>$erl_xbuff(pid)</varname>. Reply message is optional
and can be sent with <emphasis>erl_reply</emphasis>.
</para>
<section>
<title>Registered pseudo process</title>
<para>
To create pseudo erlang registered process in &kamailio; scrip create event route in form of
<varname>event_route[erlang:<emphasis><my_process_name></emphasis>]</varname>. Where
<emphasis><my_process_name></emphasis> is the name of pseudo process.
</para>
<example>
<title>Example of registered process</title>
<programlisting format="linespecific">
...
# event route acts as registered process
event_route[erlang:handler] {
xlogl("L_INFO","Received message: $erl_xbuff(msg=>format)\n");
}
...
%% in erlang shell send message to registered process
(node1@erlang.lan)12> {handler,'proxy@kamailio.lan'} ! {"hello","Kamailio"}.
{"hello","Kamailio"}
> logged info message:
INFO: <script>: 951:Received message: {"hello", "Kamailio"}
>
</programlisting>
</example>
</section>
<section id="erlang.e.self">
<title><varname>event_route[erlang:self]</varname></title>
<para>
The reserved pseudo process name to receive messages sent to &kamailio; C node.
The message are sent to non registered process.
</para>
<example>
<title>Example of using default event route</title>
<programlisting format="linespecific">
...
# default event route from erlang
event_route[erlang:self] {
xlogl("L_INFO","Received message: $erl_xbuff(msg=>format)\n");
if(pv_isset("$erl_xbuff(msg[1])") && $erl_xbuff(msg[1]=>type) == "pid") {
xlogl("L_INFO","Echo reply to: $erl_xbuff(msg[1]=>format)\n");
erl_send("$erl_xbuff(msg[1])","$erl_xbuff(msg[0])");
}
}
...
%% in erlang shell send message to registered process
(node1@erlang.lan)12> Pid=rpc:call('proxy@kamailio.lan',erlang,whereis,[self]).
<14808.9.0>
(node1@erlang.lan)14> Pid ! ["hello from",self()].
["hello from",<0.247.0>]
(node1@erlang.lan)15> flush().
Shell got "hello from"
ok
> logged info messages:
INFO: <script>: 653:Received message: ["hello from", <node1@erlang.lan.247.0>]
INFO: <script>: 656:Echo reply to: <node1@erlang.lan.247.0>]
>
</programlisting>
</example>
</section>
</section>
</chapter>