xjab_util.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00044
00045
00046
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
00096
00097
00098
00099
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
00128
00129
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
00146
00147
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
00169
00170
00171
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
00191 return _ojc;
00192 }
00193 i++;
00194 }
00195
00196 return NULL;
00197 }
00198
00199
00200
00201
00202
00203
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
00231
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