freeswitch的mod_xml_curl模块动态获取dialplan

freeswitch的mod_xml_curl模块动态获取dialplan_第1张图片

 

概述

freeswitch是一款简单好用的VOIP开源软交换平台。

mod_xml_curl模块支持从web服务获取xml配置,本文介绍如何动态获取dialplan配置。

环境

centos:CentOS  release 7.0 (Final)或以上版本

freeswitch:v1.6.20

GCC:4.8.5

dialplan查找流程

我们在“switch_xml_parse_file”函数中设置一个断点,并打印堆栈。

Breakpoint 1, switch_xml_parse_file (file=file@entry=0x7fdda1a015f0 "/tmp/34096cac-311f-11ee-b31e-672fb2a27310.tmp.xml") at src/switch_xml.c:1625

1625                           if ((xml = switch_xml_parse_fd(fd))) {

(gdb) bt

#0  switch_xml_parse_file (file=file@entry=0x7fdda1a015f0 "/tmp/34096cac-311f-11ee-b31e-672fb2a27310.tmp.xml") at src/switch_xml.c:1625

#1  0x00007fddee4d5258 in xml_url_fetch (section=, tag_name=, key_name=, key_value=, params=0x7fdd68004d10, user_data=0x1ad1ae8) at mod_xml_curl.c:311

#2  0x00007fddf71e0ec0 in switch_xml_locate (section=section@entry=0x7fddae9fc7e8 "dialplan", tag_name=tag_name@entry=0x0, key_name=key_name@entry=0x0, key_value=key_value@entry=0x0, root=root@entry=0x7fdda1a01b00,

    node=node@entry=0x7fdda1a01af8, params=0x7fdd68004d10, clone=clone@entry=SWITCH_FALSE) at src/switch_xml.c:1675

#3  0x00007fddae9fc214 in dialplan_xml_locate (node=0x7fdda1a01af8, root=0x7fdda1a01b00, caller_profile=0x7fddd004d690, session=0x7fddd0030328) at mod_dialplan_xml.c:613

#4  dialplan_hunt (session=0x7fddd0030328, arg=, caller_profile=0x7fddd004d690) at mod_dialplan_xml.c:658

#5  0x00007fddf71342ef in switch_core_standard_on_routing (session=0x7fddd0030328) at src/switch_core_state_machine.c:281

#6  switch_core_session_run (session=0x7fddd0030328) at src/switch_core_state_machine.c:643

#7  0x00007fddf712c9fe in switch_core_session_thread (thread=, obj=0x7fddd0030328) at src/switch_core_session.c:1648

#8  0x00007fddf71286f3 in switch_core_session_thread_pool_worker (thread=0x7fddd004f5e0, obj=) at src/switch_core_session.c:1711

#9  0x00007fddf7213e10 in dummy_worker (opaque=0x7fddd004f5e0) at threadproc/unix/thread.c:151

#10 0x00007fddf4a11ea5 in start_thread () from /lib64/libpthread.so.0

#11 0x00007fddf4065b0d in clone () from /lib64/libc.so.6

(gdb)

从堆栈中可以看出,一通呼叫查找动态dialplan的流程如下。

switch_core_session_thread

switch_core_session_run

switch_core_standard_on_routing

dialplan_hunt

dialplan_xml_locate

switch_xml_locate

xml_url_fetch

switch_xml_parse_file

web服务

mod_xml_curl模块依赖于web服务,需要自己创建一个web接口并动态的返回xml配置。

下面是python3.10实现的一个简单的web服务接口函数,从基类“BaseHTTPRequestHandler”继承并实现简单的返回逻辑。

    def fsDialplan(self):

        length = int(self.headers['content-length']) 

        datas = self.rfile.read(length)

        logging.info('/fs/dialplan request, data=%s' % urllib.parse.unquote(datas))

        respcode = '''

   

       

           

               

                    

                   

               

           

       

   

'''

        self.send_response(200)

        self.send_header('Content-Type', 'application;charset=utf-8')

        self.end_headers()

        self.wfile.write(respcode.encode('utf-8'))

        return

web服务响应消息格式注意事项,必须有“section”段,xml格式不能使用压缩格式,否则会解析错误。

配置

检查conf/autoload_configs/modules.conf.xml文件,mod_xml_curl模块要放在配置的顶部。

修改conf/autoload_configs/xml_curl.conf.xml文件。

 

   

     

   

 

测试

使用10011发起呼叫,日志如下。

2023-08-03 11:08:27.438976 [INFO] mod_dialplan_xml.c:637 Processing 10011 <10011>->13712345678 in context public

2023-08-03 11:08:27.478976 [CONSOLE] mod_xml_curl.c:323 XML response is in /tmp/042fcd1e-31ab-11ee-b34d-672fb2a27310.tmp.xml

Dialplan: sofia/external/[email protected] parsing [public->test-url] continue=false

Dialplan: sofia/external/[email protected] Regex (PASS) [test-url] destination_number(13712345678) =~ /^(\d+)$/ break=on-false

Dialplan: sofia/external/[email protected] Action answer()

Dialplan: sofia/external/[email protected] Action playback(/usr/local/freeswitch/sounds/dialplan-test-url.wav)

呼叫结果符合预期。

问题

网友的问题:当使用 dialplan时,这个动态获取的dialplan优先级最高,每通电话都会执行到这个dialplan。 而有些电话我不想走xml动态获取的,就想走本地配置的,这个问题怎解决呢?

方案1,mod_xml_curl模块内部已实现的逻辑,当web服务返回“NOT FOUND”时,自动走本地配置。缺点是对web服务依赖,每通呼叫都会到web服务查询。

方案2,修改“switch_xml_locate”函数,增加条件控制,比如caller是“123456”时直接使用本地配置。缺点是不够灵活,配置条件修改较困难。

方案3,修改mod_xml_curl模块的“xml_url_fetch”函数,对web响应的xml结果进行缓存,设置过期时间,减少了对web服务的依赖,又有动态配置的优点。缺点是web配置的生效会增加缓存的延迟,弱一致性。

以上方案仅仅从测试结果推导,并未真正实现,实现细节有不确定性。

总结

mod_xml_curl模块动态获取dialplan数据,控制呼叫流程。

未解决问题。

如何更灵活的控制从web服务和本地xml文件获取配置。

如何缓存web服务的xml配置。

如何解决web服务不可用的时候,呼叫流程问题。

空空如常

求真得真

你可能感兴趣的:(技术-VOIP,xml,freeswitch,voip)