xjab_presence.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 #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
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
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
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
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
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
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
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
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
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 {
00173
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
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
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
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
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