星号是一级方程式赛车,而不是普通巴士

星号-FI,这是不好的举止



尊敬的读者,您好!按照传统,我是habr的长期读者,但直到现在我才决定发表一篇文章。事实上,什么促使您写?老实说,我不认识自己。所写的有关FreeSWITCH / Yate / 3CX / etc与Asterisk的性能的文章,或者后者的体系结构的实际问题,或者也许是想做一些独特的事情。



令人惊讶的是,在第一种情况下,它们通常比较柔软和温暖,可以说是FreeSWITCH / Yate / etc和FreePBX。是的,FreePBX。这不是错字。有趣的是,在所有比较中,默认配置中通常都带有一个星号。好吧,您知道,此配置已加载了所有可用的模块,Dialplan曲线(FreePBX有所贡献)以及许多其他偏差。至于星号的一般疮-是的,客观上讲,它们是马车和小推车。



这一切怎么办?打破陈规定型观念并修复出生创伤。这就是我们要做的。



我们用蛇穿过刺猬



许多新手在使用Asterisk描述拨号计划的语法时都会感到不舒服,有些人正因为需要以默认格式编写拨号计划而正当理由选择另一个电话服务器。就像铲铲多行XML一样,是您的最佳选择。是的,可以使用LUA / AEL,这很好。但是我个人认为这是不利的,尤其是在pbx_lua方面。



如前所述,能够使用完整的编程语言描述拨号方案是很好的。问题在于脚本及其环境的生存期等于通道的生存期。对于每个新频道,都会启动其自己的脚本实例,因此,再见,频道之间共享的变量,一次下载的第三方模块,与数据库的一个共享连接等,等等。严格来说,从嵌入式脚本描述语言开始这不是必需的,但我确实希望这样做。如果需要,则必须这样做。



因此,我们将采用经典Asterisk中的pbx_lua原理,将采用Yate的路由模型,并且由于不需要“开销”,因此不会从FreeSWITCH中采用任何方法。好的,我们已经决定要生育什么。我们将使用什么进行基因实验:



  • Asterisk, . ARI , , 12- . , - 1.8/1.6, 1.4, .
  • Lua — , . , , .
  • Lunapark — github', voip-.


Lunapark . , AMI- FastAGI, . , ARI AGI AMI .



: ? Asterisk REST Interface, ! . , ARI : , , , "" , WebSockets , , , XML/JSON — . , , , . — . — - , .



? FastAGI-, , pbx_lua . Asterisk’ , FastAGI- AMI-. , FastAGI-, , , NewChannel. ARI, , stasis' ARI .



Lunapark , . "shared data". , . — , , - .



?



— ? , , . , . .



:



[test]
exten => _XXX/102,1,Hangup()
exten => _XXX,1,Dial(SIP/${EXTEN})


, 102. , , extended CallerID . , , CallerIDName , , regexp, . , , , :



[test]
exten => _XXX/102,1,Hangup()

;  CallerIDName
exten => _XXX,1,ExecIf($[ "${CALLERID(name)}" == "Vasya" ]?Hangup())

;   
exten => _XXX,n,ExecIf($[ "${CHANNEL(state)}" != "Ring" ]?Hangup())

;   
exten => _XXX,n,ExecIf($[ "${CUT(CUT(CHANNEL,-,1),/,2)}" == "333" ]?Hangup())

exten => _XXX,n,Dial(SIP/${EXTEN})


, , Hangup', extensions.conf Goto, GoSub, Macro , , Local.



— .



:



${Exten}:match('%d%d%d')
           and 
(
  ${CallerIDNum}:match('201') or 
  ${CallerIDName}:match('Vasya') or 
  ${State}:lower() ~= 'ring' or 
  ${Channel}:match('^[^/]+/([^%-]+)') == '333'
) => Hangup();

${Exten}:match('%d%d%d') => Dial {callee = ('SIP/%s'):format(${Exten})};


, , . , regexp' , , , .



, .



Lunapark pbx_lua. . ${...} , ('...'). .

, :



-- Exten = 123
-- Sate = Ring
-- CallerIDNum = 100
-- CallerIDName = Test
-- Channel = SIP/100-00000012c

if ('123'):match('%d%d%d') and
(
  ('100'):match('201') or
  ('Test'):match('Vasya') or
  ('Ring'):lower() ~= 'ring' or
  ('SIP/100-00000012c'):match('^[^/]+/([^%-]+)') == '333'
) then
  Hangup()
end

if ('123'):match('%d%d%d') then
  Dial {callee = ('SIP/%s'):format(('123'))}
end


fmt syntax :



local fmt = function(str, tab)
 return (str:gsub('(%${[^}{]+})', function(w)
  local mark = w:sub(3, -2) 

  return (mark:gsub('(.+)',function(v)
   local out = tab[v] or v

   return ("('%s')"):format(out)
  end))
 end))
end

local syntax = function(str)
 return (str:gsub('([^;]+)=>([^;]+)',function(p,r)
  return ([[ 
   if %s then
    %s
   end
  ]]):format(p,r)
 end))
end


, . — , . routes.



local routes = function(...)
  local conf, content = ...

  local f, err = io.open(conf, "r")

  if io.type(f) ~= 'file' then
   log.warn(err)  --  LOG    Lunapark'
   return ""
  else
   content = f:read('*all')
  end

  f:close() return content
end


: Lunapark . — Lunapark handler'. , FastAGI- AMI .



, AMI — , AMI-, AMI . , extensions.conf.



[default]
exten => _[hit],1,NoOp()
exten => _.,n,Wait(5)

exten => _.,1,AGI(agi://127.0.0.1/${EXTEN}${IF($[ "X${PRMS}" != "X" ]?"?${PRMS}")})


Wait(5) FastAGI-, , Redirect default ${EXTEN}.



, Lunapark', FastAGI-.



--      rules
local rules = routes('routes.conf')
--   ,       
--          HUP/QUIT
ami.removeEvents('*')
--     
ami.addEvents {
 ['newchannel'] = function(e)
  -- ,         users
  if (e['Context'] and e['Context']:match('users')) and e['Exten'] then
   -- ,   ,  FastAGI   
   local step
   --    FatsAGI    
   local count = 0
   --      
   local code, err = loadstring(syntax(fmt(rules,e))) 
   --     ,  
   if type(code) == 'function' then
    --   FastAGI  
    setfenv(code,setmetatable({indexes = {}},{__index = function(t,k)
     --    
     return coroutine.wrap(
      function(...)
       local prms = {} --   FastAGI 
       local owner = t --  
       local event = e --   event
       local thread = coroutine.running() -- ID   
       --       URI
       for p,v in pairs({...}) do
        if type(v) == 'table' then
         for key, val in pairs(v) do
          table.insert(prms,("%s=%s"):format(key,val))
         end
        else
         table.insert(prms,("%s=%s"):format(p,v))
        end
       end
       --     FastAGI   
       if step then
        --    
        local last = ("%s"):format(step)
        --     UserEvent  . 
        --     indexes( )   
        --      
        table.insert(owner['indexes'],ami.addEvent('UserEvent',function(evt)
         --   AGIStatus    
         --     ,  
         if (evt['Channel'] and evt['Channel'] == event['Channel'])
               and
          (evt['UserEvent'] and evt['UserEvent']:match('AGIStatus'))
               and
          (evt['Script'] and evt['Script'] == last)
         then
          --     
          --         
          --        
          if owner['indexes'][count] == thread then
           if coroutine.status(thread) ~= 'dead' then
            coroutine.resume(thread)
           end
          end
         end
        end,thread))
        --    FastAGI 
        step = k
        --  
        coroutine.yield()
       else --    FastAGI   
        local index --    Hangup 
        --    FastAGI 
        step = k
        --     Hangup  
        --      
        index = ami.addEvent('Hangup',function(evt)
         if evt['Channel'] and evt['Channel'] == event['Channel'] then
          --    Hangup    
          ami.removeEvent('Hangup',index)
          --       
          for _,v in pairs(owner['indexes']) do
           ami.removeEvent('UserEvent',v)
          end
          --    
          owner = nil
         end
        end,thread)
       end
       --   AMI        
       ami.setvar{
        Value = table.concat(prms,'&'),
        Channel = event['Channel'],
        Variable = 'PRMS'
       }
       --    AGI-   default
       ami.redirect{
        Exten = k,
        Priority = 1,
        Channel = event['Channel'],
        Context = 'default'
       }
       --   
       count = count + 1
      end)
    end}))()
   else
    --  -   
    log.warn(err)
   end
  end
 end
}


, , , . , . , . , , , ..



, . — , redirect . , , FastAGI-. Lunapark UserEvent FastAGI- — . default , , PRMS.



, redirect' handler, AGI . Hangup() Dial(). .



function Hangup(...)
  local app, channel = ... --     pbx_lua

  app.verbose(('The Channel %s does not match by routing rules'):format(channel.get('CHANNEL')))
  app.hangup()
end

function Dial(...)
  local app, channel = ...
  local leg = app.agi.params['callee'] or ''

  app.verbose(('Trying to make a call from %s to %s'):format(
   channel.get('CALLERID(num)'),
   leg:match('^[^/]+/([^%-]+)'))
  )
  app.dial(leg)
end


, —



, . ?



  • , ;
  • VoIP-. Queue, , asterisk';
  • , VoIP-, asterisk' Mediahub, VoIP- ;
  • 使用相当简单,可扩展且非常灵活的脚本语言来创建VoIP应用程序的能力;
  • 从VoIP应用程序扩展了与外部系统集成的可能性。


作为任何人,但我仍然喜欢一切。



完全处理
local fmt = function(str, tab)
 return (str:gsub('(%${[^}{]+})', function(w)
  local mark = w:sub(3, -2) 

  return (mark:gsub('(.+)',function(v)
   local out = tab[v] or v

   return ("('%s')"):format(out)
  end))
 end))
end

local syntax = function(str)
 return (str:gsub('([^;]+)=>([^;]+)',function(p,r)
  return ([[ 
   if %s then
    %s
   end
  ]]):format(p,r)
 end))
end

local routes = function(...)
  local conf, content = ...

  local f, err = io.open(conf, "r")

  if io.type(f) ~= 'file' then
   log.warn(err)  --  LOG    Lunapark'
   return ""
  else
   content = f:read('*all')
  end

  f:close() return content
end

--      rules
local rules = routes('routes.conf')
--   ,        
--          HUP/QUIT
ami.removeEvents('*')
--     
ami.addEvents {
 ['newchannel'] = function(e)
  -- ,         users
  if (e['Context'] and e['Context']:match('users')) and e['Exten'] then
   local step -- ,   ,  FastAGI   
   local count = 0 --    FatsAGI    
   --      
   local code, err = loadstring(syntax(fmt(rules,e))) 
   --     ,  
   if type(code) == 'function' then
    --   FastAGI  
    setfenv(code,setmetatable({indexes = {}},{__index = function(t,k)
     --    
     return coroutine.wrap(
      function(...)
       local prms = {} --   FastAGI 
       local owner = t --  
       local event = e --   event
       local thread = coroutine.running() -- ID   
       --       URI
       for p,v in pairs({...}) do
        if type(v) == 'table' then
         for key, val in pairs(v) do
          table.insert(prms,("%s=%s"):format(key,val))
         end
        else
         table.insert(prms,("%s=%s"):format(p,v))
        end
       end
       --     FastAGI   
       if step then
        --    
        local last = ("%s"):format(step)
        --     UserEvent  . 
        --     indexes( )   
        --      
        table.insert(owner['indexes'],ami.addEvent('UserEvent',function(evt)
         --   AGIStatus    
         --     ,  
         if (evt['Channel'] and evt['Channel'] == event['Channel'])
               and
          (evt['UserEvent'] and evt['UserEvent']:match('AGIStatus'))
               and
          (evt['Script'] and evt['Script'] == last)
         then
          --     
          --         
          --        
          if owner['indexes'][count] == thread then
           if coroutine.status(thread) ~= 'dead' then
            coroutine.resume(thread)
           end
          end
         end
        end,thread))
        --    FastAGI 
        step = k
        --  
        coroutine.yield()
       else --    FastAGI   
        local index --    Hangup 
        --    FastAGI 
        step = k
        --     Hangup  
        --      
        index = ami.addEvent('Hangup',function(evt)
         if evt['Channel'] and evt['Channel'] == event['Channel'] then
          --    Hangup    
          ami.removeEvent('Hangup',index)
          --       
          for _,v in pairs(owner['indexes']) do
           ami.removeEvent('UserEvent',v)
          end
          --    
          owner = nil
         end
        end,thread)
       end
       --   AMI        
       ami.setvar{
        Value = table.concat(prms,'&'),
        Channel = event['Channel'],
        Variable = 'PRMS'
       }
       --    AGI-   default
       ami.redirect{
        Exten = k,
        Priority = 1,
        Channel = event['Channel'],
        Context = 'default'
       }
       --   
       count = count + 1
      end)
    end}))()
   else
    --  -   
    log.warn(err)
   end
  end
 end
}

function Hangup(...)
  local app, channel = ... --     pbx_lua

  app.verbose(('The Channel %s does not match by routing rules'):format(channel.get('CHANNEL')))
  app.hangup()
 end

function Dial(...)
  local app, channel = ...
  local leg = app.agi.params['callee'] or ''

  app.verbose(('Trying to make a call from %s to %s'):format(
   channel.get('CALLERID(num)'),
   leg:match('^[^/]+/([^%-]+)'))
  )
  app.dial(leg)
 end



All Articles