Routing script tutorial:route order Tutorial
(→:route_field_name script example) |
|||
Line 55: | Line 55: | ||
route.each do |key, value| | route.each do |key, value| | ||
case key.to_s | case key.to_s | ||
− | when " | + | when "remapped_nap" |
@local_name = value | @local_name = value | ||
when "priority" | when "priority" |
Latest revision as of 11:28, 6 December 2010
Contents |
route_order tutorial
'route_order' is a method implemented in the base_routing and is use to order routes. This tutorial will explain how to use the parameter for the 'route_order' method.
route_order examples
Things to know about these examples:
- The :show_route_order method print in the output window the order of the naps.
- A new field called 'priority' needs to be add to the routes.
- This script will not find any route match, therefore dropping the call.
route_order setup
Routes to order:
Calling Called NAP Remapped NAP Priority 5550000 2220000 sip_p1 isdn 4 5550000 1110000 sip_p3 sip 3 5550000 3330000 sip_p2 isdn_1 2
Nap list:
Name sip_p1 sip_p3 sip_p2
The call and naps input in the test script window:
@call_params = {:calling => '5550000', :called => '5550002', :nap => 'sip_p3'} @nap_list = [ {:name => 'sip_p3'}, {:name => 'sip_p2'}, {:name => 'sip_p1'} ]
:route_field_name parameter
:route_field_name script example
Here how it can be order using the ':route_field_name' argument. The example script will order the routes by priority.
require 'base_routing' class SimpleOrdering < BaseRouting route_order :route_field_name => :priority route_match :method => :show_route_order def show_route_order( route, call, nap_list ) @local_name = "N/A" @local_priority = "N/A" route.each do |key, value| case key.to_s when "remapped_nap" @local_name = value when "priority" @local_priority = value else #puts "key = #{key}, value = #{value}" end end puts "Nap name:#{@local_name}, priority:#{@local_priority}" return true end end @@routing = SimpleOrdering.new def init_routes( routes ) @@routing.init routes end def route( call, nap_list ) puts "This script should put all the route priority order starting with the lowest." @@routing.route call, nap_list end
:route_field_name script output
This script will not find any route match, therefore dropping the call, but it will print in the output the route ordered according to their priority. The output should look like this (if tried in the test script window).
OUT: This script should put all the route priority order starting with the lowest. OUT: OUT: Nap name:sip_p2, priority:2 OUT: OUT: Nap name:sip_p3, priority:3 OUT: OUT: Nap name:sip_p1, priority:4 OUT:
:method parameter
:method script example
Here how it can be order using the ':method' argument. The example script will order the routes alphabetically by nap. The example script will order the routes alphabetically by nap just like the 'proc' example.
require 'base_routing' class SimpleMethodOrdering < BaseRouting route_order :method => :nap_alphabetical_order route_match :method => :show_route_order def nap_alphabetical_order( routes, nap_list ) @tmp_routes = routes.sort {|x,y| x[:nap] <=> y[:nap] } end def show_route_order( route, call, nap_list ) @local_name = "N/A" @local_priority = "N/A" route.each do |key, value| case key.to_s when "nap" @local_name = value when "priority" @local_priority = value else #puts "key = #{key}, value = #{value}" end end puts "Nap name:#{@local_name}, priority:#{@local_priority}" return false end end @@routing = SimpleMethodOrdering .new def init_routes( routes ) @@routing.init routes end def route( call, nap_list ) puts "This script should put all the route priority order starting with the lowest." @@routing.route call, nap_list end
:method script output
This script will not find any route match, therefore dropping the call, but it will print in the output the route ordered alphabetically by nap. The output should look like this (if tried in the test script window).
OUT: This script should put all the route priority order starting with the lowest. OUT: OUT: Nap name:sip_p1, priority:4 OUT: OUT: Nap name:sip_p2, priority:2 OUT: OUT: Nap name:sip_p3, priority:3 OUT:
:proc parameter
:proc script example
Here how it can be order using the ':proc' argument. The example script will order the routes alphabetically by nap just like the 'method' example
require 'base_routing' class SimpleProcOrdering < BaseRouting route_order :proc => Proc.new { |routes, nap_list| @tmp_routes = routes.sort {|x,y| x[:nap] <=> y[:nap] } } route_match :method => :show_route_order def show_route_order( route, call, nap_list ) @local_name = "N/A" @local_priority = "N/A" route.each do |key, value| case key.to_s when "nap" @local_name = value when "priority" @local_priority = value else #puts "key = #{key}, value = #{value}" end end puts "Nap name:#{@local_name}, priority:#{@local_priority}" return false end end @@routing = SimpleProcOrdering .new def init_routes( routes ) @@routing.init routes end def route( call, nap_list ) puts "This script should put all the route priority order starting with the lowest." @@routing.route call, nap_list end
:proc script output
This script will not find any route match, therefore dropping the call, but it will print in the output the route ordered alphabetically by nap. The output should look like this (if tried in the test script window).
OUT: This script should put all the route priority order starting with the lowest. OUT: OUT: Nap name:sip_p1, priority:4 OUT: OUT: Nap name:sip_p2, priority:2 OUT: OUT: Nap name:sip_p3, priority:3 OUT:
Back to Routing Script Tutorial.