顺德碧桂园小区那个好:Asterisk模块编写1

来源:百度文库 编辑:九乡新闻网 时间:2024/04/26 15:44:39
作者/ Russell Bryant  翻译/ 高志  d  H ;  
        是否有过想编写Asterisk模块的想法?在Asterisk中有的模块相当的复杂,但是其结构却非常的简单,让我们来从“Hello World”Asterisk模块开始:res_helloworld.该模块是基于Asterisk1.6的,为Asterisk1.4编写模块几乎一样。创建的文件名为res_helloworld.c,存放在Asterisk的源代码树/res目录下。 U1y8Y/  
        首先每个Asterisk模块都包含主要的Asterisk头文件,asterisk.h X?gH(mn  
                #include "asterisk.h“  (/;        接下来,包含ASTERISK_FILE_VERSION宏,该宏用于注册该文件的版本,通过CLI命令“core show file version like filename”命令查看文件SVN版本。     7E]qP 5  
        包含Asterisk模块头文件,包含该头文件是定义实现Asterisk模块所必须的。 (aUdPo8H^  
                #include "asterisk/module.h" &WV&_z  
        让我们继续进行同时包含使用Asteisk日志模块接口,用于显示Asterisk日志信息,显示日志信息也是本模块所要做的事情。 ,u@Vi0  
                #include "asterisk/logger.h" AbA_s I<;  
        现在包含每个Asterisk模块必须的使用的两个函数,load_module()和unload_module().当Asterisk加载和卸载模块时会调用他们。 ^/M-*U8ab  
        static int load_module(void) SsBiCc tn  
        { QJ^'Uyfdn  
                ast_log(LOG_NOTICE, "Hello World!\n"); y3Qb2l  
                return AST_MODULE_LOAD_SUCCESS; Y=$PsDh!  
        } L} Rsg'U  
        static int unload_module(void) T)lkT?  
        { $VNj0i. Pr  
                ast_log(LOG_NOTICE, "Goodbye World!\n"); g/(3D  
                return 0; `3?5Z/,y  
        } mzm{p(.  
        static int unload_module(void) &\m=|S  
        { r~YxtBZH+  
                ast_log(LOG_NOTICE, "Goodbye World!\n"); 1%$Z%?  
                return 0; ZE:!>VXa87  
        } zFm:=,9  
        最后,每个模块必须包含AST_MODULE_INFO宏实例。该宏包含模块必要代码是用于该模块被加载时向Asterisk core注册自己。 CNl @8&R  
                AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hello World"); V{C{y5  
        最终的结果构成res_helloworld.c文件。 HB*H%>L{"B  
        重新编译Asterisk,编译系统将自动发现该模块,该模块像其他模块一样,也会被编译,最后安装。通过编译,安装,运行Asterisk,这时可以确认你的模块是否被正确的加载: [9d4 0>e  
        *CLI> module show like helloworld 0q o]nw  
        Module Description Use Count a)QSq<2*  
        res_helloworld.so Hello World 0 2#Du5d   
        1 modules loaded z#*> u  
        通过CLI命令可以自己卸载、加载你的模块,可以观测到日志信息。 (>C$8)v  
        *CLI> module unload res_helloworld.so UIC~%?oIA  
          [Jun 19 10:50:57] NOTICE[26612]: res_helloworld.c:35 unload_module: Goodbye World! Eg(.L,dj  
        *CLI> module load res_helloworld.so 2" u,f  
          [Jun 19 10:51:05] NOTICE[26612]: res_helloworld.c:42 load_module: Hello World! Xbu P_U'  
          Loaded res_helloworld.so => (Hello World) 9`td_qh  
        祝贺你,你已经成功的完成了Asteisk模块编写! k]rc -c-  
        下一步,我们将开始实现在Asteirsk模块中更有用的应用。请看下一篇《Asterisk模块编写2--进阶》。下面是本文英文原文。 J1Run0  
How-to: Write an Asterisk Module, Part 1 ldt]=Sqy  
文/  Russell Bryant   摘自:http://www.russellbryant.net/blog/  elz0t        Have you ever wanted to write an Asterisk module? While some of the Asterisk modules are quite complicated, the anatomy of an Asterisk module is pretty simple. Let’s start with the “Hello World” of Asterisk modules: res_helloworld. RT        This example is based on Asterisk 1.6. However, creating Asterisk modules for Asterisk 1.4 is almost the exact same. Y+{jG(rg.F  
        Create a file called res_helloworld.c in the res/ directory of an Asterisk source tree. ;_#<a*f  
        The first thing in every Asterisk module is to include the main Asterisk header file, asterisk.h. dOVu D(  
                #include "asterisk.h" |t~*!0>3  
        Next, include the ASTERISK_FILE_VERSION macro. This registers the file with the “core show file version” CLI command. This CLI command lists the last SVN revision where that file changed. zEt!Pug  
                ASTERISK_FILE_VERSION(__FILE__, "$Revision: $") o,RiAtdk  
        Include the Asterisk module header file. This includes the definitions necessary for implementing an Asterisk module. z"-oD*ICw  
                #include "asterisk/module.h" s8dP=_ `  
        Let’s go ahead and include the header file that lets us use the Asterisk logging interface, as well. This will let us print messages to the Asterisk log so that our new module actually does something. Q CO,f  
                #include "asterisk/logger.h" Vfw H:  
-$$mrU  
        It is now time to include the two functions that every Asterisk module must implement. Those are load_module() and unload_module(). These functions get called when Asterisk loads and unloads the module. *fz#B/ _o  
fZcA{$Vc]N  
static int load_module(void) A4(k<{ }X. Fm'`  
ast_log(LOG_NOTICE, "Hello World!\n"); Bo\~PV [  
return AST_MODULE_LOAD_SUCCESS; } 5H 1N]v+  
4o"?Q V:  
aKE`nA0\B  
static int unload_module(void) 5 D=r7  
{ OZC yg/K  
ast_log(LOG_NOTICE, "Goodbye World!\n"); K]uH7-YvL/  
return 0; pjl>ZoOM  
} #eX<=H]  
>        Finally, every module must include an instance of one of the AST_MODULE_INFO macros. This macro includes the necessary code for the module to properly register itself with the Asterisk core when it gets loaded. f:y1eLl3  
}"SqB{5e(  
                AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hello World"); |,~ )/o_R  
QPh3(K1w^  
        The final result should look something like this: res_helloworld.c qozvNJm)  
        When you run make to compile Asterisk, the build system should automatically find your module. It will be compiled and installed with the rest of Asterisk. Compile, install, and run Asterisk. Then, verify that your module has been loaded: FD&^nJ_{  
La3rX  
*CLI> module show like helloworld 1e7I2g  
Module Description Use Count !s?SI=B8  
res_helloworld.so Hello World 0 tbiM>qxB  
1 modules loaded 3"< 0_3?W  
gu%i|-}  
        You should also be able to unload and load your module, and see the appropriate message in the Asterisk logger. hqlQ-aytS  
{ PJ>gX$  
*CLI> module unload res_helloworld.so T:Ee6I 3l  
[Jun 19 10:50:57] NOTICE[26612]: res_helloworld.c:35 unload_module: Goodbye World! K)se$vb6  
*CLI> module load res_helloworld.so iU37LODa2T  
[Jun 19 10:51:05] NOTICE[26612]: res_helloworld.c:42 load_module: Hello World! xy4+ [u  
Loaded res_helloworld.so => (Hello World) QIlZZ  
hjM?D`5x  
        Congratulations! You have successfully written an Asterisk module! '6WS<@%}  
        Next, we will start looking at how to start implementing more useful things inside of this module structure. ed}#S~4q  
e0(aRN{W