xjab_util.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xjab_util.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * eXtended JABber module - Jabber connections pool
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 
00026 #include <string.h>
00027 #include <unistd.h>
00028 #include <stdio.h>
00029 #include <sys/time.h>
00030 #include <sys/types.h>
00031 
00032 #include "../../dprint.h"
00033 #include "../../mem/mem.h"
00034 #include "../../mem/shm_mem.h"
00035 #include "../../timer.h"
00036 
00037 #include "xjab_util.h"
00038 #include "xjab_jcon.h"
00039 
00040 #include "mdefines.h"
00041 
00042 /**
00043  * init a jc_pool structure
00044  * - size : maximum number of the open connection to Jabber
00045  * - jlen : maximum size of messages queue
00046  * return : pointer to the structure or NULL on error
00047  */
00048 xj_jcon_pool xj_jcon_pool_init(int size, int jlen, int ch)
00049 {
00050    xj_jcon_pool jcp = (xj_jcon_pool)_M_MALLOC(sizeof(t_xj_jcon_pool));
00051    if(jcp == NULL)
00052       return NULL;
00053    jcp->len = size;
00054    jcp->ojc = (xj_jcon*)_M_MALLOC(size*sizeof(xj_jcon));
00055    if(jcp->ojc == NULL)
00056    {
00057       _M_FREE(jcp);
00058       return NULL;
00059    }
00060    memset( jcp->ojc , 0, size*sizeof(xj_jcon) );
00061    jcp->jmqueue.len = jlen;
00062    jcp->jmqueue.size = 0;
00063    jcp->jmqueue.cache = (ch>0)?ch:90;
00064    jcp->jmqueue.expire = (int*)_M_MALLOC(jlen*sizeof(int));
00065    if(jcp->jmqueue.expire == NULL)
00066    {
00067       _M_FREE(jcp->ojc);
00068       _M_FREE(jcp);
00069       return NULL;
00070    }
00071    jcp->jmqueue.jsm=(xj_sipmsg*)_M_MALLOC(jlen*sizeof(xj_sipmsg));
00072    if(jcp->jmqueue.jsm == NULL)
00073    {
00074       _M_FREE(jcp->jmqueue.expire);
00075       _M_FREE(jcp->ojc);
00076       _M_FREE(jcp);
00077       return NULL;
00078    }
00079    jcp->jmqueue.ojc = (xj_jcon*)_M_MALLOC(jlen*sizeof(xj_jcon));
00080    if(jcp->jmqueue.ojc == NULL)
00081    {
00082       _M_FREE(jcp->jmqueue.expire);
00083       _M_FREE(jcp->jmqueue.jsm);
00084       _M_FREE(jcp->ojc);
00085       _M_FREE(jcp);
00086       return NULL;
00087    }
00088    memset( jcp->jmqueue.expire , 0, jlen*sizeof(int) );
00089    memset( jcp->jmqueue.jsm , 0, jlen*sizeof(xj_sipmsg) );
00090    memset( jcp->jmqueue.ojc , 0, jlen*sizeof(xj_jcon) );
00091    return jcp;
00092 }
00093 
00094 /**
00095  * add a new element in messages queue
00096  * - jcp : pointer to the Jabber connections pool structure
00097  * - _jsm : pointer to the message
00098  * - _ojc : pointer to the Jabber connection that will be used for this message
00099  * return : 0 on success or <0 on error
00100  */
00101 int xj_jcon_pool_add_jmsg(xj_jcon_pool jcp, xj_sipmsg _jsm, xj_jcon _ojc)
00102 {
00103    int i;
00104    
00105    if(jcp == NULL)
00106       return -1;
00107    if(jcp->jmqueue.size == jcp->jmqueue.len)
00108       return -2;
00109 #ifdef XJ_EXTRA_DEBUG
00110    LM_DBG("add msg into the pool\n");
00111 #endif
00112    for(i = 0; i<jcp->jmqueue.len; i++)
00113    {
00114       if(jcp->jmqueue.jsm[i] == NULL || jcp->jmqueue.ojc[i] == NULL)
00115       {
00116          jcp->jmqueue.size++;
00117          jcp->jmqueue.expire[i] = get_ticks() + jcp->jmqueue.cache;
00118          jcp->jmqueue.jsm[i] = _jsm;
00119          jcp->jmqueue.ojc[i] = _ojc;
00120          return 0;
00121       }
00122    }
00123    return -2;
00124 }
00125 
00126 /**
00127  * delete first element from messages queue
00128  * - jcp : pointer to the Jabber connections pool structure
00129  * return : 0 on success or <0 on error
00130  */
00131 int xj_jcon_pool_del_jmsg(xj_jcon_pool jcp, int idx)
00132 {
00133    if(jcp == NULL)
00134       return -1;
00135    if(jcp->jmqueue.size <= 0)
00136       return -2;
00137    jcp->jmqueue.size--;
00138    jcp->jmqueue.jsm[idx] = NULL;
00139    jcp->jmqueue.ojc[idx] = NULL;
00140    
00141    return 0;
00142 }
00143 
00144 /**
00145  * add a new connection in pool
00146  * - jcp : pointer to the Jabber connections pool structure
00147  * return : 0 on success or <0 on error
00148  */
00149 int xj_jcon_pool_add(xj_jcon_pool jcp, xj_jcon jc)
00150 {
00151    int i = 0;
00152    
00153    if(jcp == NULL)
00154       return -1;
00155 #ifdef XJ_EXTRA_DEBUG
00156    LM_DBG("add connection into the pool\n");
00157 #endif   
00158    while(i < jcp->len && jcp->ojc[i] != NULL)
00159       i++;
00160    if(i >= jcp->len)
00161       return -1;
00162    jcp->ojc[i] = jc;
00163    
00164    return 0;
00165 }
00166 
00167 /**
00168  * get the jabber connection associated with 'id'
00169  * - jcp : pointer to the Jabber connections pool structure
00170  * - id : id of the Jabber connection
00171  * return : pointer to the open connection to Jabber structure or NULL on error
00172  */
00173 xj_jcon xj_jcon_pool_get(xj_jcon_pool jcp, xj_jkey jkey)
00174 {
00175    int i = 0;
00176    xj_jcon _ojc;
00177    
00178    if(jcp==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
00179       return NULL;
00180 #ifdef XJ_EXTRA_DEBUG
00181    LM_DBG("looking for the connection of <%.*s>"
00182       " into the pool\n", jkey->id->len, jkey->id->s);
00183 #endif
00184    while(i < jcp->len)
00185    {
00186       if((jcp->ojc[i]!=NULL) && jcp->ojc[i]->jkey->hash==jkey->hash && 
00187          (!strncmp(jcp->ojc[i]->jkey->id->s, jkey->id->s, jkey->id->len)))
00188       {
00189          _ojc = jcp->ojc[i];
00190          //jcp->ojc[i] = NULL;
00191          return _ojc;
00192       }
00193       i++;
00194    }
00195 
00196    return NULL;
00197 }
00198 
00199 /**
00200  * remove the connection associated with 'id' from pool
00201  * - jcp : pointer to the Jabber connections pool structure
00202  * - id : id of the Jabber connection
00203  * return : 0 on success or <0 on error
00204  */
00205 int xj_jcon_pool_del(xj_jcon_pool jcp, xj_jkey jkey)
00206 {
00207    int i = 0;
00208    
00209    if(jcp==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
00210       return -1;
00211 #ifdef XJ_EXTRA_DEBUG
00212    LM_DBG("removing a connection from the pool\n");
00213 #endif
00214    while(i < jcp->len)
00215    {
00216       if((jcp->ojc[i]!=NULL) && jcp->ojc[i]->jkey->hash==jkey->hash && 
00217          (!strncmp(jcp->ojc[i]->jkey->id->s,jkey->id->s,jkey->id->len)))
00218       {
00219          xj_jcon_free(jcp->ojc[i]);
00220          jcp->ojc[i] = NULL;
00221          break;
00222       }
00223       i++;
00224    }
00225 
00226    return 0;
00227 }
00228 
00229 /**
00230  * free a Jabber connections pool structure
00231  * - jcp : pointer to the Jabber connections pool structure
00232  */
00233 void xj_jcon_pool_free(xj_jcon_pool jcp)
00234 {
00235    int i;
00236    if(jcp == NULL)
00237       return;
00238 #ifdef XJ_EXTRA_DEBUG
00239    LM_DBG("-----START-----\n");
00240 #endif
00241    if(jcp->ojc != NULL)
00242    {
00243       for(i=0; i<jcp->len; i++)
00244       {
00245          if(jcp->ojc[i] != NULL)
00246             xj_jcon_free(jcp->ojc[i]);
00247       }
00248       _M_FREE(jcp->ojc);
00249    }
00250    if(jcp->jmqueue.jsm != NULL)
00251       _M_FREE(jcp->jmqueue.jsm);
00252    if(jcp->jmqueue.ojc != NULL)
00253       _M_FREE(jcp->jmqueue.ojc);
00254    if(jcp->jmqueue.expire != NULL)
00255       _M_FREE(jcp->jmqueue.expire);
00256       
00257    _M_FREE(jcp);
00258 }
00259 

Generated on Fri May 25 00:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6