<!-- Module User's Guide -->

<chapter>
	<chapterinfo>
	<revhistory>
		<revision>
		<revnumber>$Revision: 1827 $</revnumber>
		<date>$Date: 2007-03-12 17:22:53 +0200 (Mon, 12 Mar 2007) $</date>
		</revision>
	</revhistory>
	</chapterinfo>
	<title>User's Guide</title>

	<section>
	<title>Overview</title>
	<para>
		This modules implements a dispatcher for destination addresses. It 
		computes hashes over parts of the request and selects an address from
		a destination set. The selected address is used then as outbound
		proxy.
	</para>
	<para>
		The module can be used as a stateless load balancer, having no
		guarantee of fair distribution.
	</para>
	</section>
	<section>
	<title>Dependencies</title>
	<section>
		<title>&ser; modules</title>
		<para>
		The following modules must be loaded before this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>none</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>
	<section>
		<title>External libraries or applications</title>
		<para>
		The following libraries or applications must be installed before
		running &ser; with this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>none</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>
	</section>

	<section>
	<title>Exported Parameters</title>
	<section>
		<title><varname>list_file</varname> (string)</title>
		<para>
		Path to the file with destination sets.
		</para>
		<para>
		<emphasis>
			Default value is <quote>/etc/openser/dispatcher.list</quote> or
			<quote>/usr/local/etc/openser/dispatcher.list</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set the <quote>list_file</quote> parameter</title>
		<programlisting format="linespecific">
...
modparam("dispatcher", "list_file", "/var/run/openser/dispatcher.list")
...
</programlisting>
		</example>
	</section>
	<section>
		<title><varname>force_dst</varname> (int)</title>
		<para>
		If set to 1, force overwriting of destination address when that is
		already set.
		</para>
		<para>
		<emphasis>
			Default value is <quote>0</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set the <quote>force_dst</quote> parameter</title>
<programlisting format="linespecific">
...
modparam("dispatcher", "force_dst", 1)
...
</programlisting>
		</example>
	</section>
 	<section>
 		<title><varname>flags</varname> (int)</title>
 		<para>
 		Various flags that affect dispatcher's behaviour. The flags are defined
		as a bitmask on an integer value.
 		If flag 1 is set only the username
 		part of the uri will be used when computing an uri based hash.
 		If no flags are set the username, hostname and port will be used
 		The port is used only if different from 5060 (normal sip uri) or 5061
 		(in the sips case).
 		</para>
 		<para>
		If flag 2 is set, then the failover support is enabled. The functions
		exported by the module will store the rest of addresses from the
		destination set in AVP, and use these AVPs to contact next address when
		the current-tried fails.
 		</para>
 		<para>
 		<emphasis>
 			Default value is <quote>0</quote>.
 		</emphasis>
 		</para>
 		<example>
 		<title>Set the <quote>flags</quote> parameter</title>
 <programlisting format="linespecific">
 ...
 modparam("dispatcher", "flags", 3)
 ...
 </programlisting>
 		</example>
 	</section>
 	<section>
 		<title><varname>use_default</varname> (int)</title>
 		<para>
 		If the parameter is set to 1, the last address in destination set
		is used as last option to send the message. For example, it is good
		when wanting to send the call to an anouncement server saying:
		"the gateways are full, try later".
 		</para>
 		<para>
 		<emphasis>
 			Default value is <quote>0</quote>.
 		</emphasis>
 		</para>
 		<example>
 		<title>Set the <quote>use_default</quote> parameter</title>
 <programlisting format="linespecific">
 ...
 modparam("dispatcher", "use_default", 1)
 ...
 </programlisting>
 		</example>
	</section>
 	<section>
 		<title><varname>dst_avp</varname> (str)</title>
 		<para>
 		The name of the avp which will hold the list with addresses, in the
		order
		they have been selected by the chosen algorithm. If use_default is 1,
		the value of last dst_avp_id is the last address in destination set. The
		first dst_avp_id is the selected destinations. All the other addresses
		from the destination set will be added in the avp list to be able to
		implement serial forking.
 		</para>
		<note>
 		<para>
		You must set this parameter if you want do do load balancing fail over.
 		</para>
		</note>
 		<para>
 		<emphasis>
 			Default value is <quote>null</quote> - don't add AVPs.
 		</emphasis>
 		</para>
 		<example>
 		<title>Set the <quote>dst_avp</quote> parameter</title>
 <programlisting format="linespecific">
 ...
 modparam("dispatcher", "dst_avp", "$avp(i:271)")
 ...
 </programlisting>
 		</example>
 	</section>
 	<section>
 		<title><varname>grp_avp</varname> (str)</title>
 		<para>
 		The name of the avp storing the group id of the destination set. Good
		to have it for later usage or checks.
 		</para>
		<note>
 		<para>
		You must set this parameter if you want do do load balancing fail over.
 		</para>
		</note>
 		<para>
 		<emphasis>
 			Default value is <quote>null</quote> - don't add AVP.
 		</emphasis>
 		</para>
 		<example>
 		<title>Set the <quote>grp_avp</quote> parameter</title>
 <programlisting format="linespecific">
 ...
 modparam("dispatcher", "grp_avp", "$avp(i:272)")
 ...
 </programlisting>
 		</example>
 	</section>
 	<section>
 		<title><varname>cnt_avp</varname> (str)</title>
 		<para>
 		The name of the avp storing the number of destination addresses kept in
		dst_avp avps.
 		</para>
		<note>
 		<para>
		You must set this parameter if you want do do load balancing fail over.
 		</para>
		</note>
 		<para>
 		<emphasis>
 			Default value is <quote>null</quote> - don't add AVP.
 		</emphasis>
 		</para>
 		<example>
 		<title>Set the <quote>cnt_avp</quote> parameter</title>
 <programlisting format="linespecific">
 ...
 modparam("dispatcher", "cnt_avp", "$avp(i:273)")
 ...
 </programlisting>
 		</example>
 	</section>
	</section>

	<section>
	<title>Exported Functions</title>
	<section>
		<title>
		<function moreinfo="none">ds_select_dst(set, alg)</function>
		</title>
		<para>
		The method selects a destination from addresses set.
		<para>Meaning of the parameters is as follows:</para>
		<itemizedlist>
		<listitem>
			<para>
			<emphasis>set</emphasis> - the id of the set from where to pick
			up destination address. It is the first column in destination
			list file.
			</para>
		</listitem>
		<listitem>
			<para>
			<emphasis>alg</emphasis> - the algorithm used to select the
			destination address.
			</para>
			<itemizedlist>
			<listitem>
				<para>
				<quote>0</quote> - hash over callid
				</para>
			</listitem>
			<listitem>
				<para>
				<quote>1</quote> - hash over from uri.
				</para>
			</listitem>
			<listitem>
				<para>
				<quote>2</quote> - hash over to uri.
				</para>
			</listitem>
			<listitem>
				<para>
				<quote>3</quote> - hash over request-uri.
				</para>
			</listitem>
			<listitem>
				<para>
				<quote>4</quote> - round-robin (next destination).
				</para>
			</listitem>
			<listitem>
				<para>
				<quote>X</quote> - if the algorithm is not implemented, the
				first entry in set is chosen.
				</para>
			</listitem>
			</itemizedlist>
		</listitem>
		</itemizedlist>
		<para>
		If the bit 2 in 'flags' is set, the rest of the addresses from the
		destination set is stored in AVP list. You can use 'ds_next_dst()' to
		use next address to achieve serial forking to all possible
		destinations.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE.
		</para>
		<example>
		<title><function>ds_select_dst</function> usage</title>
		<programlisting format="linespecific">
...
ds_select_dst("1", "0");
...
</programlisting>
		</example>
	</section>
  	<section>
 		<title>
 		<function moreinfo="none">ds_select_domain(set, alg)</function>
 		</title>
 		<para>
 		The method selects a destination from addresses set  and rewrites the
 		host and port from R-URI. The parameters have same meaning as for
 		ds_select_dst().
 		<para>
		<para>
		If the bit 2 in 'flags' is set, the rest of the addresses from the
		destination set is stored in AVP list. You can use 'ds_next_domain()'
		to use next address to achieve serial forking to all possible
		destinations.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE.
		</para>
 	</section>
  	<section>
 		<title>
 		<function moreinfo="none">ds_next_dst()</function>
 		</title>
 		<para>
 		Takes the next destination address from the AVPs with id 'dst_avp_id'
		and sets the dst_uri (outbound proxy address).
 		</para>
		<para>
		This function can be used from FAILURE_ROUTE.
		</para>
 	</section>
  	<section>
 		<title>
 		<function moreinfo="none">ds_next_domain()</function>
 		</title>
 		<para>
 		Takes the next destination address from the AVPs with id 'dst_avp_id'
		and sets the domain part of the request uri.
 		</para>
		<para>
		This function can be used from FAILURE_ROUTE.
		</para>
 	</section>
  	<section>
 		<title>
 		<function moreinfo="none">ds_mark_dst()</function>
 		</title>
 		<para>
 		Mark the last used address from destination set as inactive, in order
		to be ingnored in the future. In this way it can be implemented an
		automatic detection of failed gateways. When an address is marked as
		inactive, it will be ignored by 'ds_select_dst' and 'ds_select_domain'.
 		</para>
		<para>
		This function can be used from FAILURE_ROUTE.
		</para>
 	</section>
	</section>

	<section>
	<title>Exported MI Functions</title>
	<section>
		<title>
		<function moreinfo="none">ds_set_state</function>
		</title>
		<para>
        Sets the status for a destination address (can be use to mark the destination 
		as active or inactive).
		</para>
		<para>
		Name: <emphasis>ds_set_state</emphasis>
		</para>
		<para>Parameters:</para>
		<itemizedlist>
			<listitem><para>_state_ : state of the destination address</para>
			      <itemizedlist>
	                 <listitem><para> <quote>a</quote>: active </para></listitem> 
			         <listitem><para> <quote>i</quote>: inactive </para></listitem>	
				  </itemizedlist>
			</listitem>	  

			<listitem><para>_group_: destination group id</para></listitem>
			
			<listitem><para>_address_: address of the destination in the _group_</para></listitem>
		</itemizedlist>
		<para>
		MI FIFO Command Format:
		</para>
        <programlisting  format="linespecific">
		:ds_set_state:_reply_fifo_file_
		_state_
		_group_
		_address_
		_empty_line_
		</programlisting>
    </section>
	<section>
		<title>
		<function moreinfo="none">ds_list</function>
		</title>
		<para>
		It lists the groups and included destinations.
		</para>
		<para>
		Name: <emphasis>ds_list</emphasis>
		</para>
		<para>Parameters: <emphasis>none</emphasis></para>
 		<para>
		MI FIFO Command Format:
		</para>
        <programlisting  format="linespecific">
		:ds_list:_reply_fifo_file_
		_empty_line_
		</programlisting>
    </section>
    </section>
	
	<section>
	<title>Installation & Running</title>
	<section>
		<title>Destination List File</title>
		<para>
		Each destination point must be on one line. First token is the set
		id and next is destination address. The set id must be an integer
		value. Destination address must be a valid SIP URI. Empty lines or
		lines starting with <quote>#</quote> are ignored.
		</para>
		<example>
		<title>dispatcher list file</title>
		<programlisting format="linespecific">
...
&dispatcherlist;
...
</programlisting>
		</example>
		</section>
	
		<section>
		<title>&ser; config file</title>
		<para>
		Next picture displays a sample usage of dispatcher.
		</para>
		<example>
		<title>&ser; config script - sample dispatcher usage</title>
		<programlisting format="linespecific">
...
&dispatchercfg;
...
		</programlisting>
		</example>
	</section>
	</section>
</chapter>

<!-- Keep this element at the end of the file
Local Variables:
sgml-parent-document: ("dispatcher.sgml" "Book" "chapter")
End:
-->
