Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- groups = [ // Массив
- { // Объект войс сервера
- name : "group1", // Группа маршрутов, объедененных одним правилом роутинга
- rule : ".*5555.*", // Правило роутинга
- lines : [ // Массив, содержащий настройки маршрутов. Приоретет маршрутов сверху вниз.
- // маршрут с наивысшим приоритетом
- { name : "fast_line1", // имя маршрута, может быть любым
- direction : "@192.168.100.50:4588", // адрес маршрута
- count : 0, // текущая емкость, ДОЛЖНА БЫТЬ 0
- max : 10, // максимальная емкость
- },
- // маршрут с наинизшим приоритетом
- { name: "slow_line1",
- 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;
- }
- }/* Скрипт для роутинга звонков в соответствии с емкостью каналов.
- *
- * Ппуть: /opt/yate/share/yate/scripts/
- * Необходимые настройки:
- *
- * ## Файл /opt/yate/etc/yate/javascript.conf
- *
- * в раздел [scripts] добавить строку:
- * capacity_router=capacity_router.js
- *
- *
- * */
- if (group == null) {
- return null;
- }
- context = {
- id : msg.id,
- state : "routing",
- called : msg.called,
- caller : msg.caller,
- line_index : -1,
- group : group,
- };
- // Engine.print_r(context);
- 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;
- }
- return null;
- }
- function onDisconnect(msg) {
- Engine.debug(Engine.DebugWarn,"Call disconnected: " + msg);
- if (msg.reason != "congestion") {
- Engine.print_r(msg);
- return true;
- }
- context = active_calls[msg.id];
- var m = new Message("chan.masquerade");
- m.message = "call.route";
- m.id = context.id;
- m.caller = context.caller;
- m.called = context.called;
- m.dispatch(true);
- return false;
- }
- 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) {
- // Engine.print_r(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");
- Message.install(onDisconnect,"chan.disconnected");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement