public void Run(params string[] args) { PhoneSystem ps = PhoneSystem.Root; Tenant t = ps.GetTenants()[0]; OutboundRule r = t.CreateOutboundRule(); r.OutboundRoutes[0].Gateway = ps.GetGateways()[0]; try { //this line should throw an exception r.OutboundRoutes[5].Gateway = ps.GetGateways()[0]; } catch (IndexOutOfRangeException) { Console.WriteLine("Number of available routes is " + r.OutboundRoutes.Length + " by default"); } r.Save(); Console.WriteLine(r); }
public void Run(params string[] args) { PhoneSystem ps = PhoneSystem.Root; Tenant t = ps.GetTenants()[0]; OutboundRule r = t.CreateOutboundRule(); r.Name = "OMSamples Test Rule"; r.NumberOfRoutes = 5; //must be set to specify 5 routes. otherwise it is only 3 routes. r.Priority = t.GetOutboundRules().Max(x => x.Priority) + 1; //lowest priority of rule. must be unique. r.OutboundRoutes[0].Gateway = ps.GetGateways()[0]; //make first route to any gateway object available in configuration. try { //should fail. r.OutboundRoutes[5].Gateway = ps.GetGateways()[0]; } catch (IndexOutOfRangeException) { Console.WriteLine("Number of available routes is " + r.OutboundRoutes.Length); r.OutboundRoutes[4].Gateway = ps.GetGateways()[0]; //it is fifth route of outbound rule. } r.Save(); Console.WriteLine(r); }
private static void CreateAndSaveOutboundRule(PhoneSystem ps, Tenant tenant, OutBoundRuleDetails outbound_rule_to_create) { OutboundRule[] rules = tenant.GetOutboundRules(); int last_maximum_priority = 0; /* Determine the highest priority outbound rule in the system at the moment. * While we're here, might as well see if this new rule exists or not...there are allowed to be duplicates though */ foreach (OutboundRule rule in rules) { if (rule.Priority > last_maximum_priority) { last_maximum_priority = rule.Priority; } if (rule.Name.Equals(outbound_rule_to_create.Name) == true) { if (isDebug) { LogMessage("Duplicate rule found, " + rule.Name, Severity.Warn); } } if (print_rules == true) { LogMessage(rule.Name, Severity.Info); } } if (tenant != null) { OutboundRule outbound_rule = null; outbound_rule = tenant.CreateOutboundRule(); if (outbound_rule != null) { /* Populate the outbound rule, using defaults where non is explicity set */ outbound_rule.Name = String.IsNullOrEmpty(outbound_rule_to_create.Name) ? outbound_rule.Name : outbound_rule_to_create.Name; outbound_rule.Prefix = String.IsNullOrEmpty(outbound_rule_to_create.Prefix) ? outbound_rule.Prefix : outbound_rule_to_create.Prefix; outbound_rule.NumberLengthRanges = String.IsNullOrEmpty(outbound_rule_to_create.Calls_to_numbers_with_a_length_of) ? outbound_rule.NumberLengthRanges : outbound_rule_to_create.Calls_to_numbers_with_a_length_of; /* Get a list of gateways the system is aware of */ Gateway[] gateways = ps.GetGateways(); bool found_user_specified_gateway_in_system = false; if (gateways.Length > 0) { foreach (Gateway gw in gateways) { if (print_gateways) { LogMessage(String.Format("GATEWAY {0}", gw.Name), Severity.Info); } /* GetAppPath the gateways that the user wants to add to this outbound_rule_to_create rule */ if (gw.Name.Equals(outbound_rule_to_create.Outbound_route[0].name) == true) { outbound_rule.OutboundRoutes[0].Gateway = gw; found_user_specified_gateway_in_system = true; } } outbound_rule.OutboundRoutes[0].Prepend = outbound_rule_to_create.Outbound_route[0].prepend; outbound_rule.OutboundRoutes[0].StripDigits = outbound_rule_to_create.Outbound_route[0].strip_count; } else { LogMessage("No gateways are defined in the system, no specifying one in the outbound rule", Severity.Warn); } /*Use default gateway if one is available and we cant find the use specified gateway name*/ if (!found_user_specified_gateway_in_system && gateways.Length > 0) { outbound_rule.OutboundRoutes[0].Gateway = gateways[0]; LogMessage("Using default gateway as route for outbound rule - route not found or not provided.", Severity.Warn); } if (outbound_rule_to_create.External_lines != null) { if (outbound_rule_to_create.External_lines.Length > 0) { outbound_rule.ExternalLines = outbound_rule_to_create.External_lines; } } if (String.IsNullOrEmpty(outbound_rule_to_create.ExtensionRange) == false) { string[] ranges = outbound_rule_to_create.ExtensionRange.Split(','); //allocate space for extensio nranges DNRange[] made_ranges = new DNRange[ranges.Length]; for (int i = 0; i < ranges.Length; i++) { made_ranges[i] = outbound_rule.CreateDNRange(); if (ranges[i].Contains("-") == true) { string[] toAndFrom = ranges[i].Split('-'); made_ranges[i].From = toAndFrom[0]; made_ranges[i].To = toAndFrom[1]; } else { // no range just single extension made_ranges[i].From = ranges[i]; made_ranges[i].To = ranges[i]; } } outbound_rule.DNRanges = made_ranges; } if (outbound_rule_to_create.DNGroups != null) { if (outbound_rule_to_create.DNGroups.Length > 0) { outbound_rule.DNGroups = outbound_rule_to_create.DNGroups; } } /* NB - ensure that the priority of the successive Outbound rules are at consecutive multiples of 5 larger than the highest priority in the system */ outbound_rule.Priority = last_maximum_priority + 5; //this is a static variable because we cannot determine which order the last outbound rule was saved in when calling GetOutBoundRules outbound_rule.Save(); // Save it to the system tenant.Refresh(); // This is so that the next call to the Tenant Object's GetOutboundRules() contains reference to the rule we just created. LogMessage("Saved outbound rule, " + outbound_rule.Name, Severity.Info); } else { LogMessage("Could not create/allocate an Outbound rule template to use to create a new rule.", Severity.Critical); } } else { LogMessage("Tenant " + tenant.Name + " is not found but is required.", Severity.Critical); } }