xjab_presence.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xjab_presence.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * XJAB module
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 #include <string.h>
00026 #include <unistd.h>
00027 #include <stdio.h>
00028 #include <sys/types.h>
00029 
00030 #include "../../dprint.h"
00031 #include "../../mem/mem.h"
00032 #include "../../mem/shm_mem.h"
00033 
00034 #include "xjab_presence.h"
00035 
00036 /**
00037  * create a presence cell
00038  */
00039 xj_pres_cell xj_pres_cell_new(void)
00040 {
00041    xj_pres_cell prc = NULL;
00042    prc = (xj_pres_cell)pkg_malloc(sizeof(t_xj_pres_cell));
00043    if(prc == NULL)
00044       return NULL;
00045    prc->key = 0;
00046    prc->userid.s = NULL;
00047    prc->userid.len = 0;
00048    prc->state = XJ_PS_OFFLINE;
00049    prc->status = XJ_PRES_STATUS_NULL;
00050    prc->cbf = NULL;
00051    prc->cbp = NULL;
00052    prc->prev = NULL;
00053    prc->next = NULL;
00054 
00055    return prc;
00056 }
00057 
00058 /**
00059  * free the presence cell
00060  */
00061 void xj_pres_cell_free(xj_pres_cell prc)
00062 {
00063    if(!prc)
00064       return;
00065    if(prc->userid.s)
00066       pkg_free(prc->userid.s);
00067    pkg_free(prc);
00068    prc = NULL;
00069 }
00070 
00071 /**
00072  * free all presence cell linked to
00073  */
00074 void xj_pres_cell_free_all(xj_pres_cell prc)
00075 {
00076    xj_pres_cell p, p0;
00077    if(!prc)
00078       return;
00079    p = prc;
00080    while(p)
00081    {
00082       p0 = p->next;
00083       xj_pres_cell_free(p);
00084       p = p0;
00085    }
00086 }
00087 
00088 /**
00089  * init a presence cell
00090  */
00091 int xj_pres_cell_init(xj_pres_cell prc, str* uid, pa_callback_f f, void* p)
00092 {
00093    if(!prc || !uid || !uid->s || uid->len<=0)
00094       return -1;
00095    prc->userid.s = (char*)pkg_malloc(uid->len*sizeof(char));
00096    if(prc->userid.s == NULL)
00097       return -1;
00098    strncpy(prc->userid.s, uid->s, uid->len);
00099    prc->userid.len = uid->len;
00100    prc->key = xj_get_hash(uid, NULL);
00101    prc->cbf = f;
00102    prc->cbp = p;
00103    return 0;
00104 }
00105 
00106 /**
00107  * update attributes for a presence cell
00108  */
00109 int xj_pres_cell_update(xj_pres_cell prc, pa_callback_f f, void *p)
00110 {
00111    if(!prc)
00112       return -1;
00113    prc->cbf = f;
00114    prc->cbp = p;
00115    return 0;
00116 }
00117 
00118 /**
00119  * init a presence list
00120  */
00121 xj_pres_list xj_pres_list_init(void)
00122 {
00123    xj_pres_list prl = NULL;
00124    
00125    prl = (xj_pres_list)pkg_malloc(sizeof(t_xj_pres_list));
00126    if(!prl)
00127       return NULL;
00128    prl->nr = 0;
00129    prl->clist = NULL;
00130 
00131    return prl;
00132 }
00133 
00134 /**
00135  * free the presence list
00136  */
00137 void xj_pres_list_free(xj_pres_list prl)
00138 {
00139    if(!prl)
00140       return;
00141    xj_pres_cell_free_all(prl->clist);
00142    pkg_free(prl);
00143    prl = NULL;
00144 }
00145 
00146 /**
00147  * add, if does not exist, an user in present list
00148  */
00149 xj_pres_cell xj_pres_list_add(xj_pres_list prl, xj_pres_cell prc)
00150 {
00151    xj_pres_cell p, p0;
00152    if(!prc)
00153       return NULL;
00154    if(!prl)
00155    {
00156       xj_pres_cell_free(prc);
00157       return NULL;
00158    }
00159    // presence list empty
00160    if(prl->clist==NULL)
00161    {
00162       prl->nr++;
00163       prl->clist = prc;
00164       return prc;
00165    }
00166 
00167    p0 = p = prl->clist;
00168    while(p && p->key <= prc->key)
00169    {
00170       if(p->key == prc->key && p->userid.len == prc->userid.len
00171          && !strncasecmp(p->userid.s, prc->userid.s, p->userid.len))
00172       { // cell already exist
00173          // update cbf and cbp
00174          p->cbf = prc->cbf;
00175          p->cbp = prc->cbp;
00176          xj_pres_cell_free(prc);
00177          return p;
00178       }
00179       p0 = p;
00180       p = p->next;
00181    }
00182 
00183    // add a the cell in list
00184    prc->next = p0->next;
00185    prc->prev = p0;
00186    if(p0->next)
00187       p0->next->prev = prc;
00188    p0->next = prc;
00189    prl->nr++;
00190 
00191    return prc;
00192 }
00193 
00194 /**
00195  * delete a user from presence list
00196  */
00197 int xj_pres_list_del(xj_pres_list prl, str *uid)
00198 {
00199    xj_pres_cell p;
00200    int lkey;
00201    if(!prl || !uid || !uid->s || uid->len<=0)
00202       return -1;
00203    if(prl->nr<=0 || prl->clist==NULL)
00204       return 0;
00205    
00206    lkey = xj_get_hash(uid, NULL);
00207 
00208    p = prl->clist;
00209    while(p && p->key <= lkey)
00210    {
00211       if(p->key == lkey && p->userid.len == uid->len
00212          && !strncasecmp(p->userid.s, uid->s, uid->len))
00213       {
00214          prl->nr--;
00215          if(p->next)
00216             p->next->prev = p->prev;
00217          if(p->prev == NULL)
00218             prl->clist = p->next;
00219          else
00220             p->prev->next = p->next;
00221          xj_pres_cell_free(p);
00222          return 0;
00223       }
00224       p = p->next;
00225    }
00226 
00227    return 0;
00228 }
00229 
00230 /**
00231  * Check if a user is already in presence list
00232  */
00233 xj_pres_cell xj_pres_list_check(xj_pres_list prl, str* uid)
00234 {
00235    xj_pres_cell p;
00236    int lkey;
00237 
00238    if(!prl || !uid || !uid->s || uid->len<=0 || prl->nr<=0 || prl->clist==NULL)
00239       return NULL;
00240    lkey = xj_get_hash(uid, NULL);
00241 
00242    p = prl->clist;
00243    while(p && p->key <= lkey)
00244    {
00245       if(p->key == lkey && p->userid.len == uid->len
00246             && !strncasecmp(p->userid.s, uid->s, uid->len))
00247          return p;
00248       p = p->next;
00249    }
00250    return NULL;
00251 }
00252 
00253 /**
00254  * Notify all users from list
00255  */
00256 void xj_pres_list_notifyall(xj_pres_list prl, int s)
00257 {
00258    xj_pres_cell p;
00259    if(!prl || prl->nr<=0 || prl->clist==NULL)
00260       return;
00261    p = prl->clist;
00262    while(p)
00263    {
00264       if(p->cbf)
00265          (*(p->cbf))(&(p->userid),&(p->userid), (s==XJ_PS_CHECK)?p->state:s,
00266                p->cbp);
00267       p = p->next;
00268    }
00269 }
00270 

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