Opensips Core Variables

opensips Core Variables

Prev

Opensips提供多种类型的变量,用于路由脚本。变量类型之间的差异来自1)变量的可访问性(何处可访问)2)变量隶属于哪里(变量所在的位置)3)变量的读写状态(有些变量仅可读)4)多个值(for the same variable handled)
opensips变量可以在脚本中轻松识别,因为他们的所有名称均以’$'符号开头。
语法:
伪变量的完整语法是:

$(<context>name(subname)[index]{transformation})

name之外的部分是可选的,字段含义如下:

  • name(必选)- 伪变量名(类型)
    例如 pvar,avp,ru,DLG_status等等
  • subname - 给定类型的某个pv的标识符
    例如 hdr(From), avp(name)。
  • index - 一个pv可用存储多个值-可以一个值列表。如果指定index可以获取到List中的特定值。你也可以指定负数,-1标识最后一个插入的,-2标识标识倒数第二个插入的。
  • transformation - 可以作用于伪变量的一系列操作。你可以在Script-Tran发现所有可用的transformations.transformation可以级联,使用一个transformation的输出作为另一个transformation的输入。
  • context - the context in which the pseudo0variable will be evaluated. Now there are 2 pv contexts: reply and request. The reply context can be used in the failure route to request for the pseudo-variable to be evaluated in the context of the reply message. The request context can be used if in a reply route is desired for the pv to be evaluated in the context of the corresponding request.
    用法示例:
  • Only name: $ru
  • Name and 'subname: $hdr(Contact)
  • Name and index: $(ct[0])
  • Name, subname and index: $(avp(i:10)[2])
  • Context - :
    $(<request>ru) from a reply route will get the Request-URI from the request
    $(<reply>hdr(Contact)) context can be used from failure route to access information from the reply
    变量类型:
  • script variables - 顾名思义,这些变量严格的绑定到脚本语言。变量尽在路由块中可见-他们不与消息和事务相关,但是他们和进程相关(脚本变量将由同一opensips进程执行的脚本继承)。
  • AVP- Attribute Value Pair - AVPs是可以创建的动态变量-avps和单个消息或者事务(如果使用有状态代理)相关联。最初(当接收或者创建)消息或者事务具有附加的AVPs的空白列表。在路由脚本中,脚本或者从脚本调用的函数可以创建新的AVPS:和事件或者事务关联。AVPS对于事务处理的所有消息(回复或者请求)都是可访问的- branche_route, failure_route,noreply_route(对于noreplay_route,需要开启TM参数noreply_vap_mode)。AVPs是可读写的并且现有的AVP可以删除(移除)。一个AVP可以包含多个值-新的赋值(或写操作)将向AVP添加新值;这个值保存在’最后添加最先使用’序列中。
  • paeudo variables - 伪变量(或者PV)提供已处理的SIP消息的信息(headers,RURI,transprot level info,a.s.o)或者opensips内部(time values, process PID,return code of a function)。根据他们提供的消息,PVs即可以绑定到消息,也可以绑定到任何内容(全局)。大部分PVs是只读的并且仅仅少数允许写操作。一个PV可以返回几个值或仅返回一个值,具体取决于所引用的信息(可以有多个值)。标准PV是只读的并且仅仅返回一个值(如果没有另外说明).
  • escape sequences - escape sequences可以用于格式化字符串;他们实际上不是变量,而是格式化程序。

Script variables

Naming: $var(name)
Hints:

  • 如果你想在一个路由中初始化一个脚本变量,最好使用相同的值初始化它(或重置它),否则你可能从同一进程执行的先前路由继承数据。
  • 脚本变量比AVP更快,因为他们直接引用内存位置。
  • 脚本变量的值在opensips进程中持续存在。
  • 脚本变量只能有一个值。
    使用用例
$var(a) = 2;  # sets the value of variable 'a' to integer '2'
$var(a) = "2";  # sets the value of variable 'a' to string '2'
$var(a) = 3 + (7&(~2)); # arithmetic and bitwise operation
$var(a) = "sip:" + $au + "@" + $fd; # compose a value from authentication username and From URI domain

# using a script variable for tests
if( [ $var(a) & 4 ] ) {
  xlog("var a has third bit set\n");
}

设置变量为NULL实际上是初始化为整数’0’。脚本变量没有NULL值。

AVP variables

Naming: $avp(name) or $(avp(name)[N])
使用索引"N"时,可以强制AVP返回某个值(第N个值)。如果没有指定index,则返回第一个值。
Hints:

  • List item

猜你喜欢

转载自blog.csdn.net/long_longago/article/details/89396785