app_mono_admin.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <para>
  14. This module allows the execution of assemblies of managed code, among
  15. the most popular of which is C# (.NET).
  16. It uses the Mono project (http://www.mono-project.com/) to embed the managed
  17. code interpreter inside the SIP server, providing fast execution.
  18. </para>
  19. <para>
  20. Besides C#, other languages can be used to build managed assemblies,
  21. such as: Java, Python, VisualBasic.NET, JavaScript. For more details on
  22. what kind of languages can be used to build managed
  23. assemblies, see: http://www.mono-project.com/Languages
  24. </para>
  25. <para>
  26. A managed assembly can get access to any &kamailio; config variables
  27. and set them. It can also perform many other functions implemented inside
  28. &kamailio; itself, allowing easier handling of SIP from managed code.
  29. </para>
  30. <para>
  31. There are two ways to execute managed code assemblies: load the code at
  32. startup and only execute at runtime, or load and execute at runtime. Only
  33. one mode at a time may be used. The mode is determined by the 'load'
  34. parameter and the function used to execute the code.
  35. </para>
  36. </section>
  37. <section>
  38. <title>Dependencies</title>
  39. <section>
  40. <title>&kamailio; Modules</title>
  41. <para>
  42. The following modules must be loaded before this module:
  43. <itemizedlist>
  44. <listitem>
  45. <para>
  46. <emphasis>none</emphasis>.
  47. </para>
  48. </listitem>
  49. </itemizedlist>
  50. </para>
  51. </section>
  52. <section>
  53. <title>External Libraries or Applications</title>
  54. <para>
  55. The following libraries or applications must be installed before running
  56. &kamailio; with this module loaded:
  57. <itemizedlist>
  58. <listitem>
  59. <para>
  60. <emphasis>mono-devel</emphasis> - Mono devel toolkit.
  61. </para>
  62. </listitem>
  63. </itemizedlist>
  64. </para>
  65. </section>
  66. </section>
  67. <section>
  68. <title>Parameters</title>
  69. <section>
  70. <title><varname>load</varname> (string)</title>
  71. <para>
  72. Set the path to the Mono assembly to be loaded at startup. You
  73. can use mono_run(param) to execute the assembly at runtime.
  74. </para>
  75. <para>
  76. <emphasis>
  77. Default value is <quote>null</quote>.
  78. </emphasis>
  79. </para>
  80. <example>
  81. <title>Set <varname>load</varname> parameter</title>
  82. <programlisting format="linespecific">
  83. ...
  84. modparam("app_mono", "load", "/usr/local/etc/kamailio/mono/myscript.exe")
  85. ...
  86. </programlisting>
  87. </example>
  88. </section>
  89. </section>
  90. <section>
  91. <title>Functions</title>
  92. <section>
  93. <title>
  94. <function moreinfo="none">mono_exec(path [, param])</function>
  95. </title>
  96. <para>
  97. Execute the managed code assembly stored in 'path'. The path can be
  98. a string with pseudo-variables evaluated at runtime. A second parameter
  99. can optionally be provided; it will be passed to the assembly.
  100. </para>
  101. <para>
  102. Note that the assembly is loaded every time from the file, so any change
  103. to it is immediately live. This function cannot be used if 'load'
  104. parameter is set.
  105. </para>
  106. <example>
  107. <title><function>mono_exec</function> usage</title>
  108. <programlisting format="linespecific">
  109. ...
  110. mono_exec("/usr/local/etc/kamailio/mono/myscript.exe");
  111. ...
  112. </programlisting>
  113. </example>
  114. </section>
  115. <section>
  116. <title>
  117. <function moreinfo="none">mono_run([param])</function>
  118. </title>
  119. <para>
  120. Execute the assembly specified by 'load' module parameter. The assembly
  121. is loaded at startup, so changes to it will be effective only after &kamailio;
  122. restart.
  123. </para>
  124. <para>
  125. An optional parameter can be given and it will be passed over to the
  126. assembly. It can be a string with pseudo-variables; these will be
  127. evaluated at runtime.
  128. </para>
  129. <example>
  130. <title><function>mono_run</function> usage</title>
  131. <programlisting format="linespecific">
  132. ...
  133. if(!mono_run("myparam"))
  134. {
  135. xdbg("SCRIPT: failed to execute mono assembly!\n");
  136. }
  137. ...
  138. </programlisting>
  139. </example>
  140. </section>
  141. </section>
  142. <section>
  143. <title>Usage</title>
  144. <para>
  145. First, create a library from the 'SR.cs' file provided
  146. in the folder 'modules/app_mono/lib/'. The examples uses the folder
  147. /usr/local/etc/kamailio/mono/ to store the Mono-specific assemblies
  148. to be used by &kamailio;.
  149. </para>
  150. <programlisting format="linespecific">
  151. ...
  152. mkdir -p /usr/local/etc/kamailio/mono/
  153. cp modules/app_mono/lib/SR.cs /usr/local/etc/kamailio/mono/
  154. gmcs -t:library SR.cs
  155. ...
  156. </programlisting>
  157. <para>
  158. You should see the 'SR.dll' file generated.
  159. </para>
  160. <para>
  161. Create your application in C#/.NET and save it
  162. in the same folder with SR.dll. You have to use the SR package in your
  163. managed source code file -- say you named MySRTest.cs. Also, be
  164. aware that the Main() function from the managed assembly is executed;
  165. thus, be sure that you have it defined.
  166. </para>
  167. <programlisting format="linespecific">
  168. ...
  169. using SR;
  170. namespace MySRTest {
  171. class Test {
  172. static int Main(string[] args) {
  173. SR.Core.Log(1, "Kamailio API Version: " + SR.Core.APIVersion() + "\n");
  174. if (args.Length == 1) {
  175. SR.Core.Log(1, "Parameter from Kamailio config: "
  176. + args[0] + "\n");
  177. }
  178. SR.Core.Dbg("Request URI is: " + SR.PV.GetS("$ru") + "\n");
  179. SR.PV.SetS("$rU", "123");
  180. SR.HDR.AppendToReply("My-Mono-Hdr: ok\r\n");
  181. SR.Core.ModF("sl_reply_error");
  182. return 1;
  183. }
  184. }
  185. }
  186. ...
  187. </programlisting>
  188. <para>
  189. You have to compile the 'SR.dll' file generated.
  190. </para>
  191. <programlisting format="linespecific">
  192. ...
  193. $ gmcs -r:SR.dll MySRTest.cs
  194. ...
  195. </programlisting>
  196. <para>
  197. You should see the file 'MySRTest.exe' generated in the folder.
  198. </para>
  199. <para>
  200. In the &kamailio; config file, load the app_mono.so module and use
  201. its functions inside the routing blocks.
  202. </para>
  203. <programlisting format="linespecific">
  204. ...
  205. loadmodule "app_mono.so"
  206. ...
  207. route {
  208. ...
  209. mono_exec("/usr/local/etc/kamailio/mono/MySRTest.exe");
  210. ...
  211. }
  212. ...
  213. </programlisting>
  214. <para>
  215. The API exported by the SR package to Mono applications is documented
  216. on the &kamailio; wiki site. Also, it is easy to figure out by looking
  217. in the source code tree inside the file modules/app_mono/lib/SR.cs.
  218. </para>
  219. </section>
  220. </chapter>