<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [

<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../../doc/docbook/entities.xml">
%docentities;

]>

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

<chapter>

    <title>&adminguide;</title>

    <section>
	<title>Overview</title>
	<para>
		The mqueue module offers a generic message queue system in shared memory for
		inter-process communication using the config file. One example of usage is
		to send time consuming operations to one or several timer processes that consumes
		items in the queue, without affecting SIP message handling in the socket-listening
		process.
	</para>
	<para>
		There can be many defined queues. Access to queued values is done via
		pseudo variables.
	</para>
    </section>
    <section>
	<title>Dependencies</title>
	<section>
	    <title>&kamailio; 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
		&kamailio; with this module loaded:
	    	<itemizedlist>
		    <listitem>
			<para>
			    <emphasis>None</emphasis>.
			</para>
		    </listitem>
	    	</itemizedlist>
	    </para>
	</section>
    </section>
    <section>
	<title>Parameters</title>

	<section id="mqueue.p.db_url">
		<title><varname>db_url</varname> (str)</title>
		<para>
			The <acronym>URL</acronym> to connect to database for loading values in mqueue table at start up and/or saving values at shutdown.
		</para>
		<para>
		<emphasis>
			Default value is NULL (do not connect).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>db_url</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("mqueue", "db_url", "&defaultdb;")

# Example of table in sqlite, you have the set the fields to support the length according to the data that will be present in the mqueue
CREATE TABLE mqueue_name (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key character varying(64) DEFAULT "" NOT NULL,
val character varying(4096) DEFAULT "" NOT NULL
);
...
</programlisting>
		</example>
	</section>
	<section id="mqueue.p.mqueue">
	    <title><varname>mqueue</varname> (string)</title>
	    <para>
		Definition of a memory queue
	    </para>
	    <para>
		<emphasis>
		    Default value is <quote>none</quote>.
		</emphasis>
	    </para>
		<para>
		Value must be a list of parameters: attr=value;...
		<itemizedlist>
		<listitem>
			<para>Mandatory attributes:</para>
			<itemizedlist>
			<listitem>
				<para>
				<emphasis>name</emphasis>: name of the queue.
				</para>
			</listitem>
			</itemizedlist>
		</listitem>
		<listitem>
			<para>Optional attributes:</para>
			<itemizedlist>
			<listitem>
				<para>
				<emphasis>size</emphasis>: size of the queue.
				Specifies the maximum number of items in queue.
				If exceeded the oldest one is removed.
				If not set the queue will be limitless.
				</para>
			</listitem>
			<listitem>
				<para>
				<emphasis>dbmode</emphasis>: If set to 1, the content of the queue
				is read from database at startup and
				is written to database table when the SIP server is stopped
				(i.e., ensure persistency over restarts).
				If set to 2, it is read at startup but not written at shutdown.
				If set to 3, it is written at shutdown but not read at startup.
				Default value is 0 (no db table interaction).
				</para>
			</listitem>
			<listitem>
				<para>
				<emphasis>addmode</emphasis>: how to add new (key,value) pairs.
					<itemizedlist>
					<listitem>
						<para>
						<emphasis>0</emphasis>:
							Will push all new (key,value) pairs at the end of the queue. (default)
						</para>
					</listitem>
					<listitem>
						<para>
						<emphasis>1</emphasis>:
							Will keep oldest (key,value) pair in the queue, based on the key.
						</para>
					</listitem>
					<listitem>
						<para>
						<emphasis>2</emphasis>:
							Will keep newest (key,value) pair in the queue, based on the key.
						</para>
					</listitem>
					</itemizedlist>
				</para>
			</listitem>
			</itemizedlist>
		</listitem>
		</itemizedlist>
	    </para>
		<para>
		The parameter can be set many times, each holding the definition of one
		queue.
	    </para>
	    <example>
		<title>Set <varname>mqueue</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("mqueue", "mqueue", "name=myq;size=20;")
modparam("mqueue", "mqueue", "name=myq;size=10000;addmode=2")
modparam("mqueue", "mqueue", "name=qaz")
modparam("mqueue", "mqueue", "name=qaz;addmode=1")
...
</programlisting>
	    </example>
	</section>


	<section id="mqueue.p.mqueue_name">
	    <title><varname>mqueue_name</varname> (string)</title>
	    <para>
		Definition of a memory queue, just by name.
	    </para>
	    <para>
		<emphasis>
		    Default value is <quote>none</quote>.
		</emphasis>
	    </para>
	    <para>
		Value must be a string.
	    </para>
	    <para>
		The parameter can be set many times, each holding the definition of one queue.

		The max size of each queue defined this way will be equal to mqueue_size(if mqueue_size configured),
		or limitless (if mqueue_size not configured).
	    </para>
	    <example>
		<title>Set <varname>mqueue_name</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("mqueue", "mqueue_name", "my_own_queue")
...
		</programlisting>
	    </example>
	</section>

	<section id="mqueue.p.mqueue_size">
	    <title><varname>mqueue_size</varname> (int)</title>
	    <para>
		Definition of the size of all memory queues defined via "mqueue_name" parameter.
	    </para>
	    <para>
		<emphasis>
		    Default value is <quote>0</quote>.
		</emphasis>
	    </para>
	    <para>
		Value must be an int.
	    </para>
	    <para>
		The parameter should be set before defining any "mqueue_name".
		If not set, the queues defined via "mqueue_name" will be limitless.
	    </para>
	    <example>
		<title>Set <varname>mqueue_size</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("mqueue", "mqueue_size", 1024)
...
		</programlisting>
	    </example>
	</section>

	<section id="mqueue.p.mqueue_addmode">
	    <title><varname>mqueue_addmode</varname> (int)</title>
	    <para>
		Sets the mode to be used when adding a new (key,value) pair in the mqueue.
	    </para>
		<itemizedlist>
		    <listitem>
			<para>
			    <emphasis>0</emphasis> Add all new (key,value) at the end of mqueue
			</para>
		    </listitem>
		    <listitem>
			<para>
			    <emphasis>1</emphasis> Unique key, keep oldest (key,value)
			</para>
		    </listitem>
		    <listitem>
			<para>
			    <emphasis>2</emphasis> Unique key, keep newest(key,value)
			</para>
		    </listitem>
		</itemizedlist>
	    <para>
		<emphasis>
		    Default value is <quote>0</quote>.
		</emphasis>
	    </para>
	    <para>
		Value must be an int.
	    </para>
	    <para>
		The parameter should be set before defining any "mqueue_name".
		If not set, the queues defined via "mqueue_name" will have default addmode 0.
	    </para>
	    <example>
		<title>Set <varname>mqueue_addmode</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("mqueue", "mqueue_addmode", 2)
...
		</programlisting>
	    </example>
	</section>


	</section>

    <section>
	<title>Functions</title>
 	<section id="mqueue.f.mq_add">
	    <title>
		<function moreinfo="none">mq_add(queue, key, value)</function>
	    </title>
	    <para>
		Add a new item (key, value) in the queue. If max size of queue is
		exceeded, the oldest one is removed.
	    </para>
		<example>
		<title><function>mq_add</function> usage</title>
		<programlisting format="linespecific">
...
mq_add("myq", "$rU", "call from $fU");
...
</programlisting>
	    </example>
	</section>

 	<section id="mqueue.f.mq_fetch">
	    <title>
		<function moreinfo="none">mq_fetch(queue)</function>
	    </title>
	    <para>
		Take oldest item from queue and fill $mqk(queue) and
		$mqv(queue) pseudo variables.
	    </para>
	    <para>
		Return: true on success (1); false on failure (-1) or
		no item fetched (-2).
	    </para>
		<example>
		<title><function>mq_fetch</function> usage</title>
		<programlisting format="linespecific">
...
while(mq_fetch("myq"))
{
   xlog("$mqk(myq) - $mqv(myq)\n");
}
...
</programlisting>
	    </example>
	</section>

 	<section id="mqueue.f.mq_pv_free">
	    <title>
		<function moreinfo="none">mq_pv_free(queue)</function>
	    </title>
	    <para>
		Free the item fetched in pseudo-variables. It is optional, a new fetch
		frees the previous values.
	    </para>
		<example>
		<title><function>mq_pv_free</function> usage</title>
		<programlisting format="linespecific">
...
mq_pv_free("myq");
...
</programlisting>
	    </example>
	</section>

	<section id="mqueue.f.mq_size">
	    <title>
		<function moreinfo="none">mq_size(queue)</function>
	    </title>
	    <para>
		Returns the current number of elements in the mqueue.
	    </para>
	    <para>
	    If the mqueue is empty, the function returns -1. If the
	    mqueue is not found, the function returns -2.
	    </para>
		<example>
		<title><function>mq_size</function> usage</title>
		<programlisting format="linespecific">
...
$var(q_size) = mq_size("queue");
xlog("L_INFO", "Size of queue is: $var(q_size)\n");
...
</programlisting>
	    </example>
	</section>

    </section>

    <section>
	<title>Exported Variables</title>
		<itemizedlist>
			<listitem>
				<emphasis>$mqk(mqueue)</emphasis> - the most recent item key fetched from the specified mqueue
			</listitem>
			<listitem>
				<emphasis>$mqv(mqueue)</emphasis> - the most recent item value fetched from the specified mqueue
			</listitem>
			<listitem>
				<emphasis>$mq_size(mqueue)</emphasis> - the size of the specified mqueue
			</listitem>
		</itemizedlist>
		<para>
		Exported pseudo-variables are documented at &kamwikilink;.
		</para>
	</section>

    <section>
	<title>RPC Commands</title>
	<section id="mqueue.r.get_size">
		<title>mqueue.get_size</title>
		<para>
			Get the size of a memory queue.
		</para>
		<para>
			Parameters:
		</para>
		<itemizedlist>
			<listitem>
				<emphasis>name</emphasis> - the name of memory queue
			</listitem>
		</itemizedlist>
		<example>
		<title><function>mqueue.get_size</function> usage</title>
		<programlisting format="linespecific">
...
&kamcmd; mqueue.get_size xyz
...
</programlisting>
	    </example>
	</section>
	<section id="mqueue.r.fetch">
		<title>mqueue.fetch</title>
		<para>
			Fetch a key-value pair from a memory queue.
		</para>
		<para>
			Parameters:
		</para>
		<itemizedlist>
			<listitem>
				<emphasis>name</emphasis> - the name of memory queue
			</listitem>
		</itemizedlist>
		<example>
		<title><function>mqueue.fetch</function> usage</title>
		<programlisting format="linespecific">
...
&kamcmd; mqueue.fetch xyz
...
</programlisting>
	    </example>
	</section>

	<section id="mqueue.r.get_sizes">
		<title>mqueue.get_sizes</title>
		<para>
			Get the size for all memory queues.
		</para>
		<para>
			Parameters: none
		</para>
		<example>
		<title><function>mqueue.get_sizes</function> usage</title>
		<programlisting format="linespecific">
...
&kamcmd; mqueue.get_sizes
...
</programlisting>
	    </example>
	</section>
    </section>

</chapter>

