吴伟贤のBlog

Feed Rss

freeswitch cookbook 第1章: Routing Calls

07.21.2013, freeswitch, by .

转自:http://www.voip123.cn/?p=242

本章我们将从多个方面讨论呼叫路由相关的知识,主要包括:

* 内部呼叫

* DID内呼

* 外呼

* 同振

* 顺振

*

* 定时路由呼叫

*

介绍

呼叫路由是每一个FREESWITCH服务器的核心。虽然有若干处理复杂呼叫建立的方法,但它可以提供基本的工具,确保解决这些问题。

呼叫路由的最基本内容是拨码计划(dialplan)。拨码计划由一系列动作组成,以完成各种拨码(就像书中诸多章节提到的,拨码计划还有许多特性能影响呼叫路由)。拨码计划包含若干个contexts,每个contexts包含若干个extensions,每个extensions由若干个actions组成,每个actions完成特定的呼叫路由。呼叫路由的解析通过regular expressions完成。regular expressions是正则表达式匹配模式,以完成各个extensionsactions的执行。

为了确保能正确理解本章的内容,了解默认配置中的正则表达式匹配和三个contexts非常必要。

Regular expressions

FreeSWITCH 使用Perl-compatible regular expressions(PCRE)进行正则表达式匹配。请看如下拨码计划的片段,

<extension name=”example”>

<condition field=”destination_number” expression=”^(10\d\d)$”>

<action application=”log” data=”INFO dialed number is [$1]“/>

该例子是包含了拨码计划的大部分内容。匹配destination_number(是指用户要进行呼叫的号码)字段并把该值保存在一个名为$1的变量里。假如一个用户拨打1025,该号码正好和我们例子中的^(10\d\d)$相符合。所有在action中的动作将执行。在我们的例子中将执行log输出。将在终端上显示一条绿色信息,并将变量$1的值用1025代替。打印出的内容如下:

2011-01-09 13:38:31.864281 [INFO] mod_dptools.c:1152 dialed number is [1025]

理解了这个基础的例子,你可以写出更为出色的拨码计划。更多关于正则表达式匹配相关的内容,可以参考如下链接:http://wiki.freeswitch. org/wiki/Regex.

[wiki部分翻译开始]

介绍

在FreeSWITCH中大量使用了正则表达式匹配。具体可以参见http://wiki.freeswitch.org/wiki/Dialplan_XML中的具体应用。本WIKI(http://wiki.freeswitch.org/wiki/Regular_Expression)中分享了许多有用的正则表达式匹配。

 

可以通过在CLI中运行regex命令测试所写正则表达式是否符合您的想法。

regex

Evaluate a regex (regular expression). This command behaves differently depending upon whether or not a substitution string is supplied:

  • If a subst is not supplied, regex returns either true or false
  • If a subst is supplied, regex returns the subst value on a true condition, or the source string on a false condition (this is an update as of revision 14727, previously the regex would return “false” on a failed match.)

The regex delimiter defaults to the | (pipe) character. The delimiter may be changed to ~ (tilde) or / (forward slash) by prefixing the regex with m: (new behavior as of 14727).

Usage: regex <data>|<pattern>[|<subst string>]

       regex m:/<data>/<pattern>[/<subst string>]

       regex m:~<data>~<pattern>[~<subst string>]

Examples:

regex test1234|\d                  <== Returns “true”

regex m:/test1234/\d               <== Returns “true”

regex m:~test1234~\d               <== Returns “true”

regex test|\d                      <== Returns “false”

regex test1234|(\d+)|$1            <== Returns “1234″

regex sip:foo@bar.baz|^sip:(.*)|$1 <== Returns “foo@bar.baz”

regex testingonetwo|(\d+)|$1       <== Returns “testingonetwo” (no match)

[wiki部分翻译结束]

默认配置中的重要拨码计划

如前所述,contexts是由若干个extensions组成。FreeSWITCH默认的配置包括三类contexts。

*  default

*  public

*  features

 

每一类都有其特定的作用,了解他们,有助于您更好地实现您想要实现的功能。具体可参见http://wiki.freeswitch.org/wiki/Dialplan_XML

 

在默认配置中使用最多的context是 default context。默认不修改的情况下,所有在FREESWITCH上认证的用户之间的通话都将使用该context。一般的修改包括使用ACLs以及禁用认证。Default context可以看作是一种“内部的”context,即它管理所有在FreeSWITCH注册的用户之间的路由,与之相对应的是“外部的”呼叫。

 

许多传统PBX所拥有的特性在default context中都有所体现。在 conf/dialplan/default. Xml 中有许多值得学习的实例。可以通过简单的实例开始学习,例如 show_info,可以在终端打印一些提示信息; vmain, which allows a user to log into his/her voicemail box.

 

Local_Extension非常有用,它包含如下内容:

* 内部用户之间的呼叫路由

* 语音留言功能

* 使用 bind_meta_app实现in-call特性

* 更新本地数据库,实现回呼、监听等功能

本章讨论的许多内容都可以在Local_Extension中找到。

评论已关闭。