Confucij

JS call reroute

May 5th, 2016
229
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 : "line1",                         // имя маршрута, может быть любым
  8.                 direction : "@192.168.100.50:4588",     // адрес маршрута
  9.                 count : 0,                              // текущая емкость, ДОЛЖНА БЫТЬ 0
  10.                 max : 10,                               // максимальная емкость
  11.             },
  12.                                                         // маршрут с наинизшим приоритетом
  13.             {   name: "line2",
  14.                 direction : "@192.168.100.50:4589",
  15.                 count : 0,
  16.                 max : 4,
  17.             }
  18.         ]
  19.     },
  20.     {
  21.         name : "group2",
  22.         rule : "666.*",
  23.         lines : [
  24.             {   name : "line1",
  25.                 direction : "@192.168.100.50:4588",
  26.                 count : 0,
  27.                 max : 10,
  28.             },
  29.             {   name: "line2",
  30.                 direction : "@192.168.100.50:4589",
  31.                 count : 0,
  32.                 max : 4,
  33.             }
  34.         ]
  35.     }
  36. ];
  37.  
  38.  
  39. /////////////////// END OF USER CONFIGURATIONS ////////////////////////
  40.  
  41. Engine.debugName("CapacityRouter");
  42. Message.trackName(Engine.debugName());
  43. active_calls = {};
  44.  
  45. function createCallContext(msg) {
  46.  
  47.     group = null;
  48.     for (var group_id = 0; group_id < groups.length; group_id++) {
  49.         group = groups[group_id];
  50.         rule = new RegExp(group.rule, "i");
  51.         if (rule.test(msg.called)) {
  52.             break;
  53.         }
  54.     }
  55.  
  56.     if (group == null) {
  57.         return null;
  58.     }
  59.     context = {
  60.         id : msg.id,
  61.         state : "routing",
  62.         called : msg.called,
  63.         caller : msg.caller,
  64.         formats : msg.formats,
  65.         line_index : -1,
  66.         group : group,
  67.     };
  68.     return context;
  69. }
  70.  
  71.  
  72. function routeInContext(context) {
  73.  
  74.     while (context.line_index < context.group.lines.length) {
  75.         context.line_index++;
  76.  
  77.         if (context.group.lines[context.line_index].count >= context.group.lines[context.line_index].max) {
  78.             continue;
  79.         }
  80.         route = context.group.lines[context.line_index].direction;
  81.         context.group.lines[context.line_index].count += 1;
  82.         active_calls[context.id] = context;
  83.         return route;
  84.     }
  85.     Engine.debug(Engine.DebugWarn,"Failed to route call from " + context.caller + " to " + context.called + ": no channels avaliable");
  86.     return null;
  87.  
  88. }
  89.  
  90. function makeCall(id, caller, called, route, formats) {
  91.     var m = new Message("chan.masquerade");
  92.     m.message = "call.execute";                 // Real message
  93.     m.id = id;                                  // ID of originator channel
  94.     m.userData = null;
  95.     m.driver = null;
  96.     m.callername = caller;
  97.     m.called = called;
  98.     m.callto = route;
  99.     m.caller = caller;
  100.     m.formats = formats;
  101.     result = m.dispatch();
  102.  
  103.     return result;
  104.  
  105. }
  106.  
  107. function onDisconnect(msg) {
  108.     Engine.debug(Engine.DebugInfo,"Call disconnected: " + msg.id);
  109.  
  110.     if (msg.reason == "congestion") {           // We'll reroute call only if there were no ready channels
  111.         context = active_calls[msg.id];
  112.         route = routeInContext(context);
  113.         if (route == null) {
  114.             msg.retValue("true");
  115.             return true;                            // No mo routes to try
  116.         }
  117.         route = "iax/" + context.called + route;
  118.         Engine.debug(Engine.DebugWarn,"Rerouting call to " + route);
  119.         makeCall(context.id, context.caller, context.called, route, context.formats);
  120.         return true;
  121.     }
  122.     return true;
  123. }
  124.  
  125. function onHangup(msg) {
  126.     context = active_calls[msg.id];
  127.     if (typeof context != 'undefined') {
  128.         context.group.lines[context.line_index].count -= 1;
  129.         delete active_calls[msg.id];
  130.         Engine.debug(Engine.DebugWarn,"Call from " + context.caller + " to " + context.called + " cleared");
  131.     }
  132. }
  133.  
  134. function onRoute(msg) {
  135.     if (!msg.called) {
  136.         return false;
  137.     }
  138.  
  139.     context = createCallContext(msg);
  140.     if (context == null) {
  141.         return false;
  142.     }
  143.  
  144.     direction = routeInContext(context);
  145.     if (direction == null) {
  146.         msg.retValue("-");
  147.         msg.error = "congestion";
  148.         return true;
  149.     }
  150.  
  151.     route = "iax/" + msg.called + direction;
  152.     msg.line = route;
  153.     msg.forward_sdp = true;
  154.     msg.callername = msg.caller;
  155.     msg.retValue(route);
  156.     Engine.debug(Engine.DebugWarn,"Call routed: " + route);
  157.     active_calls[context.id] = context;
  158.     return true;
  159.  
  160. }
  161.  
  162. Message.install(onRoute, "call.route", 2);
  163. Message.install(onHangup,"chan.hangup", 2);
  164. Message.install(onDisconnect,"chan.disconnected", 2);
Advertisement
Add Comment
Please, Sign In to add comment