Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- groups = [ // Массив
- {
- name : "group1", // Группа маршрутов, объедененных одним правилом роутинга
- rule : "5555.*", // Правило роутинга
- lines : [ // Массив, содержащий настройки маршрутов. Приоретет маршрутов сверху вниз.
- // маршрут с наивысшим приоритетом
- { name : "line1", // имя маршрута, может быть любым
- direction : "@192.168.100.50:4588", // адрес маршрута
- count : 0, // текущая емкость, ДОЛЖНА БЫТЬ 0
- max : 10, // максимальная емкость
- },
- // маршрут с наинизшим приоритетом
- { name: "line2",
- direction : "@192.168.100.50:4589",
- count : 0,
- max : 4,
- }
- ]
- },
- {
- name : "group2",
- rule : "666.*",
- lines : [
- { name : "line1",
- direction : "@192.168.100.50:4588",
- count : 0,
- max : 10,
- },
- { name: "line2",
- direction : "@192.168.100.50:4589",
- count : 0,
- max : 4,
- }
- ]
- }
- ];
- /////////////////// END OF USER CONFIGURATIONS ////////////////////////
- Engine.debugName("CapacityRouter");
- Message.trackName(Engine.debugName());
- active_calls = {};
- function createCallContext(msg) {
- group = null;
- for (var group_id = 0; group_id < groups.length; group_id++) {
- group = groups[group_id];
- rule = new RegExp(group.rule, "i");
- if (rule.test(msg.called)) {
- break;
- }
- }
- if (group == null) {
- return null;
- }
- context = {
- id : msg.id,
- state : "routing",
- called : msg.called,
- caller : msg.caller,
- formats : msg.formats,
- line_index : -1,
- group : group,
- };
- return context;
- }
- function routeInContext(context) {
- while (context.line_index < context.group.lines.length) {
- context.line_index++;
- if (context.group.lines[context.line_index].count >= context.group.lines[context.line_index].max) {
- continue;
- }
- route = context.group.lines[context.line_index].direction;
- context.group.lines[context.line_index].count += 1;
- active_calls[context.id] = context;
- return route;
- }
- Engine.debug(Engine.DebugWarn,"Failed to route call from " + context.caller + " to " + context.called + ": no channels avaliable");
- return null;
- }
- function makeCall(id, caller, called, route, formats) {
- var m = new Message("chan.masquerade");
- m.message = "call.execute"; // Real message
- m.id = id; // ID of originator channel
- m.userData = null;
- m.driver = null;
- m.callername = caller;
- m.called = called;
- m.callto = route;
- m.caller = caller;
- m.formats = formats;
- result = m.dispatch();
- return result;
- }
- function onDisconnect(msg) {
- Engine.debug(Engine.DebugInfo,"Call disconnected: " + msg.id);
- if (msg.reason == "congestion") { // We'll reroute call only if there were no ready channels
- context = active_calls[msg.id];
- route = routeInContext(context);
- if (route == null) {
- msg.retValue("true");
- return true; // No mo routes to try
- }
- route = "iax/" + context.called + route;
- Engine.debug(Engine.DebugWarn,"Rerouting call to " + route);
- makeCall(context.id, context.caller, context.called, route, context.formats);
- return true;
- }
- return true;
- }
- function onHangup(msg) {
- context = active_calls[msg.id];
- if (typeof context != 'undefined') {
- context.group.lines[context.line_index].count -= 1;
- delete active_calls[msg.id];
- Engine.debug(Engine.DebugWarn,"Call from " + context.caller + " to " + context.called + " cleared");
- }
- }
- function onRoute(msg) {
- if (!msg.called) {
- return false;
- }
- context = createCallContext(msg);
- if (context == null) {
- return false;
- }
- direction = routeInContext(context);
- if (direction == null) {
- msg.retValue("-");
- msg.error = "congestion";
- return true;
- }
- route = "iax/" + msg.called + direction;
- msg.line = route;
- msg.forward_sdp = true;
- msg.callername = msg.caller;
- msg.retValue(route);
- Engine.debug(Engine.DebugWarn,"Call routed: " + route);
- active_calls[context.id] = context;
- return true;
- }
- Message.install(onRoute, "call.route", 2);
- Message.install(onHangup,"chan.hangup", 2);
- Message.install(onDisconnect,"chan.disconnected", 2);
Advertisement
Add Comment
Please, Sign In to add comment