Advertisement
Guest User

js route

a guest
May 3rd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. groups = [                                              // Массив
  2.     {                                                   // Объект войс сервера
  3.         name : "group1",                                // Группа маршрутов, объедененных одним правилом роутинга
  4.         rule : ".*5555.*",                          // Правило роутинга
  5.         lines : [                                       // Массив, содержащий настройки маршрутов. Приоретет маршрутов сверху вниз.
  6.                                                         // маршрут с наивысшим приоритетом
  7.             {   name : "fast_line1",                    // имя маршрута, может быть любым
  8.                 direction : "@192.168.100.50:4588",     // адрес маршрута
  9.                 count : 0,                              // текущая емкость, ДОЛЖНА БЫТЬ 0
  10.                 max : 10,                               // максимальная емкость
  11.             },
  12.                                                         // маршрут с наинизшим приоритетом
  13.             {   name: "slow_line1",
  14.                 direction : "@192.168.100.50:4589",
  15.                 count : 0,
  16.                 max : 4,
  17.             }
  18.         ]
  19.     }
  20. ];
  21.  
  22.  
  23. /////////////////// END OF USER CONFIGURATIONS ////////////////////////
  24.  
  25. Engine.debugName("CapacityRouter");
  26. Message.trackName(Engine.debugName());
  27. active_calls = {};
  28.  
  29. function createCallContext(msg) {
  30.    
  31.     group = null;
  32.     for (var group_id = 0; group_id < groups.length; group_id++) {
  33.         group = groups[group_id];
  34.         rule = new RegExp(group.rule, "i");
  35.         if (rule.test(msg.called)) {
  36.             break;
  37.         }
  38.     }/* Скрипт для роутинга звонков в соответствии с емкостью каналов.
  39.  *
  40.  * Ппуть: /opt/yate/share/yate/scripts/
  41.  * Необходимые настройки:
  42.  *
  43.  * ## Файл /opt/yate/etc/yate/javascript.conf
  44.  *
  45.  *  в раздел [scripts] добавить строку:
  46.  *  capacity_router=capacity_router.js
  47.  *
  48.  *
  49.  * */
  50.    
  51.     if (group == null) {
  52.         return null;
  53.     }
  54.     context = {
  55.         id : msg.id,
  56.         state : "routing",
  57.         called : msg.called,
  58.         caller : msg.caller,
  59.         line_index : -1,
  60.         group : group,
  61.     };
  62. //  Engine.print_r(context);
  63.     return context;
  64. }
  65.  
  66.  
  67. function routeInContext(context) {
  68.  
  69.     while (context.line_index < context.group.lines.length) {
  70.         context.line_index++;
  71.  
  72.         if (context.group.lines[context.line_index].count >= context.group.lines[context.line_index].max) {
  73.             continue;
  74.         }
  75.         route = context.group.lines[context.line_index].direction;
  76.         context.group.lines[context.line_index].count += 1;
  77.         active_calls[context.id] = context;
  78.         return route;
  79.     }
  80.     return null;
  81.  
  82. }
  83.  
  84. function onDisconnect(msg) {
  85.     Engine.debug(Engine.DebugWarn,"Call disconnected: " + msg);
  86.  
  87.     if (msg.reason != "congestion") {
  88.         Engine.print_r(msg);
  89.         return true;
  90.     }
  91.    
  92.     context = active_calls[msg.id];
  93.  
  94.     var m = new Message("chan.masquerade");
  95.     m.message = "call.route";
  96.     m.id = context.id;
  97.     m.caller = context.caller;
  98.     m.called = context.called;
  99.     m.dispatch(true);
  100.     return false;
  101. }  
  102.  
  103.  
  104. function onHangup(msg) {
  105.    
  106.     context = active_calls[msg.id];
  107.  
  108.     if (typeof context != 'undefined') {
  109.         context.group.lines[context.line_index].count -= 1;
  110.         delete active_calls[msg.id];
  111.         Engine.debug(Engine.DebugWarn,"Call from " + context.caller + " to " + context.called + " cleared");
  112.     }
  113. }
  114.  
  115. function onRoute(msg) {
  116.     //   Engine.print_r(msg);
  117.  
  118.     if (!msg.called) {
  119.         return false;
  120.     }
  121.    
  122.     context = createCallContext(msg);
  123.     if (context == null) {
  124.         return false;
  125.     }
  126.     direction = routeInContext(context);
  127.     if (direction == null) {
  128.         msg.retValue("-");
  129.         msg.error = "congestion";
  130.         return true;
  131.     }
  132.    
  133.     route = "iax/" + msg.called + direction;
  134.     msg.line = route;
  135.     msg.forward_sdp = true;
  136.     msg.callername = msg.caller;
  137.     msg.retValue(route);
  138.     Engine.debug(Engine.DebugWarn,"Call routed: " + route);
  139.     active_calls[context.id] = context;
  140.     return true;
  141.  
  142. }
  143.  
  144. Message.install(onRoute, "call.route", 2);
  145. Message.install(onHangup,"chan.hangup");
  146. Message.install(onDisconnect,"chan.disconnected");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement