<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15421] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15421">15421</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-11-10 20:19:44 -0600 (Tue, 10 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>add mod_distributor</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkconfautoload_configsdistributorconfxml">freeswitch/trunk/conf/autoload_configs/distributor.conf.xml</a></li>
<li>freeswitch/trunk/src/mod/applications/mod_distributor/</li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_distributormod_distributor2008vcproj">freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.2008.vcproj</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_distributormod_distributorc">freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkconfautoload_configsdistributorconfxml"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/conf/autoload_configs/distributor.conf.xml (0 => 15421)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/conf/autoload_configs/distributor.conf.xml                                (rev 0)
+++ freeswitch/trunk/conf/autoload_configs/distributor.conf.xml        2009-11-11 02:19:44 UTC (rev 15421)
</span><span class="lines">@@ -0,0 +1,10 @@
</span><ins>+&lt;configuration name=&quot;distributor.conf&quot; description=&quot;Distributor Configuration&quot;&gt;
+  &lt;lists&gt;
+    &lt;!-- every 10 calls to test you will get foo1 once and foo2 9 times...yes NINE TIMES! --&gt;
+    &lt;!-- this is not the same as 100 with 10 and 90 that would do foo1 10 times in a row then foo2 90 times in a row --&gt;
+    &lt;list name=&quot;test&quot; target-weight=&quot;10&quot;&gt;
+      &lt;node name=&quot;foo1&quot; weight=&quot;1&quot;/&gt;
+      &lt;node name=&quot;foo2&quot; weight=&quot;9&quot;/&gt;
+    &lt;/list&gt;
+  &lt;/lists&gt;
+&lt;/configuration&gt;
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_distributormod_distributor2008vcproj"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.2008.vcproj (0 => 15421)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.2008.vcproj                                (rev 0)
+++ freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.2008.vcproj        2009-11-11 02:19:44 UTC (rev 15421)
</span><span class="lines">@@ -0,0 +1,287 @@
</span><ins>+&lt;?xml version=&quot;1.0&quot; encoding=&quot;Windows-1252&quot;?&gt;
+&lt;VisualStudioProject
+        ProjectType=&quot;Visual C++&quot;
+        Version=&quot;9.00&quot;
+        Name=&quot;mod_distributor&quot;
+        ProjectGUID=&quot;{11C9BC3D-45E9-46E3-BE84-B8CEE4685E39}&quot;
+        RootNamespace=&quot;mod_distributor&quot;
+        Keyword=&quot;Win32Proj&quot;
+        TargetFrameworkVersion=&quot;131072&quot;
+        &gt;
+        &lt;Platforms&gt;
+                &lt;Platform
+                        Name=&quot;Win32&quot;
+                /&gt;
+                &lt;Platform
+                        Name=&quot;x64&quot;
+                /&gt;
+        &lt;/Platforms&gt;
+        &lt;ToolFiles&gt;
+        &lt;/ToolFiles&gt;
+        &lt;Configurations&gt;
+                &lt;Configuration
+                        Name=&quot;Debug|Win32&quot;
+                        ConfigurationType=&quot;2&quot;
+                        InheritedPropertySheets=&quot;..\..\..\..\w32\module_debug.vsprops&quot;
+                        CharacterSet=&quot;2&quot;
+                        &gt;
+                        &lt;Tool
+                                Name=&quot;VCPreBuildEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCustomBuildTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXMLDataGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCWebServiceProxyGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCMIDLTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCLCompilerTool&quot;
+                                UsePrecompiledHeader=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManagedResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPreLinkEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCLinkerTool&quot;
+                                RandomizedBaseAddress=&quot;1&quot;
+                                DataExecutionPrevention=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCALinkTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManifestTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXDCMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCBscMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCFxCopTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCAppVerifierTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPostBuildEventTool&quot;
+                        /&gt;
+                &lt;/Configuration&gt;
+                &lt;Configuration
+                        Name=&quot;Debug|x64&quot;
+                        OutputDirectory=&quot;$(PlatformName)\$(ConfigurationName)&quot;
+                        IntermediateDirectory=&quot;$(PlatformName)\$(ConfigurationName)&quot;
+                        ConfigurationType=&quot;2&quot;
+                        InheritedPropertySheets=&quot;..\..\..\..\w32\module_debug.vsprops&quot;
+                        CharacterSet=&quot;2&quot;
+                        &gt;
+                        &lt;Tool
+                                Name=&quot;VCPreBuildEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCustomBuildTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXMLDataGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCWebServiceProxyGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCMIDLTool&quot;
+                                TargetEnvironment=&quot;3&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCLCompilerTool&quot;
+                                UsePrecompiledHeader=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManagedResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPreLinkEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCLinkerTool&quot;
+                                OutputFile=&quot;$(SolutionDir)$(PlatformName)\$(ConfigurationName)/mod/$(ProjectName).dll&quot;
+                                RandomizedBaseAddress=&quot;1&quot;
+                                DataExecutionPrevention=&quot;0&quot;
+                                TargetMachine=&quot;17&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCALinkTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManifestTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXDCMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCBscMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCFxCopTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCAppVerifierTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPostBuildEventTool&quot;
+                        /&gt;
+                &lt;/Configuration&gt;
+                &lt;Configuration
+                        Name=&quot;Release|Win32&quot;
+                        ConfigurationType=&quot;2&quot;
+                        InheritedPropertySheets=&quot;..\..\..\..\w32\module_release.vsprops&quot;
+                        CharacterSet=&quot;2&quot;
+                        &gt;
+                        &lt;Tool
+                                Name=&quot;VCPreBuildEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCustomBuildTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXMLDataGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCWebServiceProxyGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCMIDLTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCLCompilerTool&quot;
+                                UsePrecompiledHeader=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManagedResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPreLinkEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCLinkerTool&quot;
+                                RandomizedBaseAddress=&quot;1&quot;
+                                DataExecutionPrevention=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCALinkTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManifestTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXDCMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCBscMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCFxCopTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCAppVerifierTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPostBuildEventTool&quot;
+                        /&gt;
+                &lt;/Configuration&gt;
+                &lt;Configuration
+                        Name=&quot;Release|x64&quot;
+                        OutputDirectory=&quot;$(PlatformName)\$(ConfigurationName)&quot;
+                        IntermediateDirectory=&quot;$(PlatformName)\$(ConfigurationName)&quot;
+                        ConfigurationType=&quot;2&quot;
+                        InheritedPropertySheets=&quot;..\..\..\..\w32\module_release.vsprops&quot;
+                        CharacterSet=&quot;2&quot;
+                        &gt;
+                        &lt;Tool
+                                Name=&quot;VCPreBuildEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCustomBuildTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXMLDataGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCWebServiceProxyGeneratorTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCMIDLTool&quot;
+                                TargetEnvironment=&quot;3&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCCLCompilerTool&quot;
+                                UsePrecompiledHeader=&quot;0&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManagedResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCResourceCompilerTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPreLinkEventTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCLinkerTool&quot;
+                                OutputFile=&quot;$(SolutionDir)$(PlatformName)\$(ConfigurationName)/mod/$(ProjectName).dll&quot;
+                                RandomizedBaseAddress=&quot;1&quot;
+                                DataExecutionPrevention=&quot;0&quot;
+                                TargetMachine=&quot;17&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCALinkTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCManifestTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCXDCMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCBscMakeTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCFxCopTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCAppVerifierTool&quot;
+                        /&gt;
+                        &lt;Tool
+                                Name=&quot;VCPostBuildEventTool&quot;
+                        /&gt;
+                &lt;/Configuration&gt;
+        &lt;/Configurations&gt;
+        &lt;References&gt;
+        &lt;/References&gt;
+        &lt;Files&gt;
+                &lt;File
+                        RelativePath=&quot;.\mod_distributor.c&quot;
+                        &gt;
+                &lt;/File&gt;
+        &lt;/Files&gt;
+        &lt;Globals&gt;
+        &lt;/Globals&gt;
+&lt;/VisualStudioProject&gt;
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_distributormod_distributorc"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.c (0 => 15421)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.c                                (rev 0)
+++ freeswitch/trunk/src/mod/applications/mod_distributor/mod_distributor.c        2009-11-11 02:19:44 UTC (rev 15421)
</span><span class="lines">@@ -0,0 +1,404 @@
</span><ins>+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2009, Anthony Minessale II &lt;anthm@freeswitch.org&gt;
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the &quot;License&quot;); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an &quot;AS IS&quot; basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II &lt;anthm@freeswitch.org&gt;
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Anthony Minessale II &lt;anthm@freeswitch.org&gt;
+ * Neal Horman &lt;neal at wanlink dot com&gt;
+ *
+ *
+ * mod_distributor.c -- Framework Demo Module
+ *
+ */
+#include &lt;switch.h&gt;
+
+/* Prototypes */
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_distributor_shutdown);
+SWITCH_MODULE_RUNTIME_FUNCTION(mod_distributor_runtime);
+SWITCH_MODULE_LOAD_FUNCTION(mod_distributor_load);
+
+/* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime) 
+ * Defines a switch_loadable_module_function_table_t and a static const char[] modname
+ */
+SWITCH_MODULE_DEFINITION(mod_distributor, mod_distributor_load, mod_distributor_shutdown, NULL);
+
+
+struct dist_node {
+        char *name;
+        int weight;
+        int cur_weight;
+        struct dist_node *next;
+};
+
+struct dist_list {
+        char *name;
+        int target_weight;
+        int last;
+        int node_count;
+        struct dist_node *lastnode;
+        struct dist_node *nodes;
+        struct dist_list *next;
+};
+
+
+static void destroy_node(struct dist_node *node)
+{
+        struct dist_node *old;
+        while(node) {
+                old = node;
+                node = node-&gt;next;
+                if (old-&gt;name) {
+                        free(old-&gt;name);
+                }
+                free(old);
+        }
+}
+
+static void destroy_list(struct dist_list *list)
+{
+        struct dist_list *old;
+
+        while(list) {
+                old = list;
+                list = list-&gt;next;
+                destroy_node(old-&gt;nodes);
+                if (old-&gt;name) {
+                        free(old-&gt;name);
+                }
+                free(old);
+        }
+}
+
+static struct {
+        switch_mutex_t *mod_lock;
+        switch_memory_pool_t *pool;
+        struct dist_list *list;
+} globals;
+
+
+
+static int load_config(int reloading) 
+{
+        struct dist_list *main_list=NULL, *new_list, *old_list=NULL, *lp=NULL;
+        switch_status_t status = SWITCH_STATUS_FALSE;
+        char *cf = &quot;distributor.conf&quot;;
+        switch_xml_t cfg, xml, lists, list, param;
+        
+        
+        if (!(xml = switch_xml_open_cfg(cf, &amp;cfg, NULL))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Open of %s failed\n&quot;, cf);
+                return SWITCH_STATUS_TERM;
+        }
+
+        if (!(lists = switch_xml_child(cfg, &quot;lists&quot;))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Can't find any lists!\n&quot;);
+                return status;
+        }
+
+        switch_mutex_lock(globals.mod_lock);
+
+        for (list = switch_xml_child(lists, &quot;list&quot;); list; list = list-&gt;next) {
+                const char *name = switch_xml_attr(list, &quot;name&quot;);
+                const char *tweight = switch_xml_attr(list, &quot;total-weight&quot;);
+                struct dist_node *node, *np=NULL;
+                int target_weight = 10;
+                int accum = 0;
+
+                if (zstr(name)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Missing NAME!\n&quot;);
+                        continue;
+                }
+                
+                if (!zstr(tweight)) {
+                        target_weight = atoi(tweight);
+                }
+
+                switch_zmalloc(new_list, sizeof(*new_list));
+                
+                new_list-&gt;name = strdup(name);
+                new_list-&gt;last = -1;
+                new_list-&gt;target_weight = target_weight;
+                
+                if (lp) {
+                        lp-&gt;next = new_list;
+                } else {
+                        main_list = new_list;
+                }
+                
+                lp = new_list;
+                
+                for (param = switch_xml_child(list, &quot;node&quot;); param; param = param-&gt;next) {
+                        char *name = (char *) switch_xml_attr_soft(param, &quot;name&quot;);
+                        char *weight_val = (char *) switch_xml_attr_soft(param, &quot;weight&quot;);
+                        int weight = 0, tmp;
+                        
+                        if ((tmp = atoi(weight_val)) &lt; 1) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Weight %d value incorrect, must be &gt; 0\n&quot;, tmp);
+                                continue;
+                        }
+
+                        if (tmp &gt;= lp-&gt;target_weight) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Weight %d value incorrect, must be less than %d\n&quot;, tmp, lp-&gt;target_weight);
+                                continue;
+                        }
+
+                        if (accum + tmp &gt; lp-&gt;target_weight) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Target Weight %d already met, ignoring subsequent entries.\n&quot;, lp-&gt;target_weight);
+                                continue;
+                        }
+
+                        accum += tmp;
+
+                        weight = lp-&gt;target_weight - tmp;
+                                
+                        if (weight &lt; 0 || weight &gt; lp-&gt;target_weight) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Weight %d value incorrect, must be between 1 and %d\n&quot;, weight, lp-&gt;target_weight);
+                                continue;
+                        }
+                        
+                        switch_zmalloc(node, sizeof(*node));
+                        node-&gt;name = strdup(name);
+                        node-&gt;weight = node-&gt;cur_weight = weight;
+                        
+
+                        if (np) {
+                                np-&gt;next = node;
+                        } else {
+                                lp-&gt;nodes = node;
+                        }
+
+                        np = node;
+                        lp-&gt;node_count++;
+                }
+
+                if (accum &lt; lp-&gt;target_weight) {
+                        struct dist_node *np1;
+                        int remain = lp-&gt;target_weight - accum;
+                        int ea = remain / lp-&gt;node_count ? lp-&gt;node_count : 1;
+
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Total weight does not add up to total weight %d\n&quot;, lp-&gt;target_weight);
+
+                        for(np1 = lp-&gt;nodes; np1; np1 = np1-&gt;next) {
+                                np1-&gt;weight += lp-&gt;target_weight - ea;
+                        }
+
+                }
+
+        }
+
+        if (main_list) {
+                old_list = globals.list;
+                globals.list = main_list;
+                status = SWITCH_STATUS_SUCCESS;
+        }
+
+        switch_mutex_unlock(globals.mod_lock);
+
+        if (old_list) {
+                destroy_list(old_list);
+        }
+
+
+        if (xml) {
+                switch_xml_free(xml);
+        }
+
+        return status;
+}
+
+static int reset_list(struct dist_list *list)
+{
+        struct dist_node *np;
+
+        for(np = list-&gt;nodes; np; np = np-&gt;next) {
+                np-&gt;cur_weight = np-&gt;weight;
+        }
+        list-&gt;last = -1;
+        list-&gt;lastnode = NULL;
+        return 0;
+}
+
+static struct dist_node *find_next(struct dist_list *list)
+{
+        struct dist_node *np, *match = NULL;
+        int x = 0, mx = 0;
+        int matches = 0, loops = 0;
+        
+        for(;;) {
+
+                if (++loops &gt; 1000) {
+                        break;
+                }
+
+                x = 0;
+                if (list-&gt;last &gt;= list-&gt;node_count) {
+                        list-&gt;last = -1;
+                }
+                match = NULL;
+                for(np = list-&gt;nodes; np; np = np-&gt;next) {
+                        if (np-&gt;cur_weight &lt; list-&gt;target_weight) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;%s %d/%d\n&quot;, np-&gt;name, np-&gt;cur_weight, list-&gt;target_weight);
+                                matches++;
+                                if (!match &amp;&amp; x &gt; list-&gt;last) {
+                                        match = np;
+                                        mx = x;
+                                }
+                        }
+                        x++;
+                }
+
+                if (match) {
+                        match-&gt;cur_weight++;
+                        list-&gt;lastnode = match;
+                        list-&gt;last = mx;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Choose %s\n&quot;, match-&gt;name);
+                        return match;
+                }
+                
+                if (matches) {
+                        list-&gt;last = -1;
+                } else {
+                        reset_list(list);
+                }
+
+        }
+
+        return NULL;
+}
+
+
+static char *dist_engine(const char *name)
+{
+        struct dist_node *np = NULL;
+        struct dist_list *lp;
+        char *str = NULL;
+
+        switch_mutex_lock(globals.mod_lock);
+        for(lp = globals.list; lp; lp = lp-&gt;next) {
+                if (!strcasecmp(name, lp-&gt;name)) {
+                        np = find_next(lp);
+                        break;
+                }
+        }
+
+        if (np) {
+                str = strdup(np-&gt;name);
+        }
+        switch_mutex_unlock(globals.mod_lock);
+
+        return str;
+        
+}
+
+SWITCH_STANDARD_APP(distributor_exec)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        char *ret = NULL;
+        
+        if (zstr(data)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;distributor requires an argument\n&quot;);
+                return;
+        }
+
+        if ((ret = dist_engine(data))) {
+                switch_channel_set_variable(channel, &quot;DISTRIBUTOR&quot;, ret);
+                free(ret);
+        }
+}
+
+SWITCH_STANDARD_API(distributor_function)
+{
+        char *ret = NULL;
+
+        if (!zstr(cmd) &amp;&amp; (ret = dist_engine(cmd))) {
+                stream-&gt;write_function(stream, &quot;%s&quot;, ret);
+                free(ret);
+        } else {
+                stream-&gt;write_function(stream, &quot;-err&quot;);
+        }
+
+        return SWITCH_STATUS_SUCCESS;
+
+}
+
+
+SWITCH_STANDARD_API(distributor_ctl_function)
+{
+
+        if (!zstr(cmd) &amp;&amp; !strcasecmp(cmd, &quot;reload&quot;)) {
+                if (load_config(SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
+                        stream-&gt;write_function(stream, &quot;+ok reloaded.\n&quot;);
+                        return SWITCH_STATUS_SUCCESS;
+                }
+        }
+
+        stream-&gt;write_function(stream, &quot;-error!\n&quot;);
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+/* Macro expands to: switch_status_t mod_distributor_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
+SWITCH_MODULE_LOAD_FUNCTION(mod_distributor_load)
+{
+        switch_api_interface_t *api_interface;
+        switch_application_interface_t *app_interface;
+
+        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+
+        memset(&amp;globals, 0, sizeof(globals));
+        switch_mutex_init(&amp;globals.mod_lock, SWITCH_MUTEX_NESTED, pool);
+        globals.pool = pool;
+
+        load_config(SWITCH_FALSE);
+        
+
+        SWITCH_ADD_API(api_interface, &quot;distributor&quot;, &quot;Distributor API&quot;, distributor_function, &quot;&lt;list name&gt;&quot;);
+        SWITCH_ADD_API(api_interface, &quot;distributor_ctl&quot;, &quot;Distributor API&quot;, distributor_ctl_function, &quot;[reload]&quot;);
+        SWITCH_ADD_APP(app_interface, &quot;distributor&quot;, &quot;Distributor APP&quot;, &quot;Distributor APP&quot;, distributor_exec, &quot;&lt;list name&gt;&quot;, SAF_SUPPORT_NOMEDIA|SAF_ROUTING_EXEC);
+
+
+        /* indicate that the module should continue to be loaded */
+        return SWITCH_STATUS_SUCCESS;
+}
+
+/*
+  Called when the system shuts down
+  Macro expands to: switch_status_t mod_distributor_shutdown() */
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_distributor_shutdown)
+{
+        switch_mutex_lock(globals.mod_lock);
+        destroy_list(globals.list);
+        switch_mutex_unlock(globals.mod_lock);
+        return SWITCH_STATUS_SUCCESS;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4
+ */
</ins></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>