| 1 |
/* |
| 2 |
* ccmlib_eventapi.c: OCF event API. |
| 3 |
* |
| 4 |
* Copyright (c) International Business Machines Corp., 2002 |
| 5 |
* Author: Ram Pai ([email protected]) |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This software is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
/* |
| 24 |
* implements 0.2 version of proposed event api |
| 25 |
*/ |
| 26 |
#define __CCM_LIBRARY__ |
| 27 |
#include <ccmlib.h> |
| 28 |
|
| 29 |
void *cookie_construct(void (*f)(void *), void (*free_f)(void *), void *); |
| 30 |
void * cookie_get_data(void *ck); |
| 31 |
void * cookie_get_func(void *ck); |
| 32 |
void cookie_ref(void *ck); |
| 33 |
void cookie_unref(void *ck); |
| 34 |
|
| 35 |
static GHashTable *tokenhash = NULL; |
| 36 |
|
| 37 |
typedef struct oc_ev_cookie_s { |
| 38 |
void (*func) (void *); |
| 39 |
void (*freefunc) (void *); |
| 40 |
void *data; |
| 41 |
int refcount; |
| 42 |
} oc_ev_cookie_t; |
| 43 |
|
| 44 |
static guint token_counter=0; |
| 45 |
|
| 46 |
typedef struct oc_ev_s { |
| 47 |
int oc_flag; |
| 48 |
GHashTable *oc_eventclass; |
| 49 |
} __oc_ev_t; |
| 50 |
|
| 51 |
#define EVENT_INIT 1 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
/* |
| 56 |
* BEGIN OF FUNCTIONS DEALING WITH COOKIES |
| 57 |
*/ |
| 58 |
|
| 59 |
void * |
| 60 |
cookie_construct(void (*f)(void *), void (*free_f)(void *), void *data) |
| 61 |
{ |
| 62 |
oc_ev_cookie_t *cookie = g_malloc(sizeof(oc_ev_cookie_t)); |
| 63 |
cookie->func = f; |
| 64 |
cookie->data = data; |
| 65 |
cookie->freefunc = free_f; |
| 66 |
cookie->refcount = 1; |
| 67 |
return (void *)cookie; |
| 68 |
} |
| 69 |
|
| 70 |
void * |
| 71 |
cookie_get_data(void *ck) |
| 72 |
{ |
| 73 |
oc_ev_cookie_t *cookie = (oc_ev_cookie_t *)ck; |
| 74 |
if (!cookie) { |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
return cookie->data; |
| 79 |
} |
| 80 |
void * |
| 81 |
cookie_get_func(void *ck) |
| 82 |
{ |
| 83 |
oc_ev_cookie_t *cookie = (oc_ev_cookie_t *)ck; |
| 84 |
if(!cookie) { |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
return cookie->func; |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
cookie_unref(void *ck) |
| 93 |
{ |
| 94 |
oc_ev_cookie_t *cookie = (oc_ev_cookie_t *)ck; |
| 95 |
if(!cookie) { |
| 96 |
return; |
| 97 |
} |
| 98 |
if(--cookie->refcount == 0) { |
| 99 |
if(cookie->freefunc){ |
| 100 |
cookie->freefunc(cookie->data); |
| 101 |
} |
| 102 |
g_free(cookie); |
| 103 |
} |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
cookie_ref(void *ck) |
| 109 |
{ |
| 110 |
oc_ev_cookie_t *cookie = (oc_ev_cookie_t *)ck; |
| 111 |
if(!cookie) { |
| 112 |
return; |
| 113 |
} |
| 114 |
++cookie->refcount; |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* END OF FUNCTIONS DEALING WITH COOKIES |
| 120 |
*/ |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static class_t * |
| 126 |
class_construct(oc_ev_class_t class_type, oc_ev_callback_t *fn) |
| 127 |
{ |
| 128 |
class_t *t_class = NULL; |
| 129 |
|
| 130 |
switch(class_type) { |
| 131 |
case OC_EV_MEMB_CLASS: |
| 132 |
t_class = oc_ev_memb_class(fn); |
| 133 |
break; |
| 134 |
|
| 135 |
case OC_EV_CONN_CLASS: |
| 136 |
case OC_EV_GROUP_CLASS: |
| 137 |
default : |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
return t_class; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
static void |
| 146 |
oc_ev_init(void) |
| 147 |
{ |
| 148 |
static gboolean ocinit_flag = FALSE; |
| 149 |
|
| 150 |
if(ocinit_flag==FALSE) { |
| 151 |
tokenhash = g_hash_table_new(g_direct_hash, |
| 152 |
g_direct_equal); |
| 153 |
ocinit_flag = TRUE; |
| 154 |
} |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
static gboolean |
| 159 |
eventclass_remove_func(gpointer key, |
| 160 |
gpointer value, |
| 161 |
gpointer user_data) |
| 162 |
{ |
| 163 |
class_t *class = (class_t *)value; |
| 164 |
class->unregister(class); |
| 165 |
g_free(class); |
| 166 |
|
| 167 |
return TRUE; |
| 168 |
} |
| 169 |
|
| 170 |
static gboolean |
| 171 |
token_invalid(const __oc_ev_t *token) |
| 172 |
{ |
| 173 |
if(!token){ |
| 174 |
return TRUE; |
| 175 |
} |
| 176 |
|
| 177 |
if(token->oc_flag!= EVENT_INIT) { |
| 178 |
return TRUE; |
| 179 |
} |
| 180 |
|
| 181 |
return FALSE; |
| 182 |
} |
| 183 |
|
| 184 |
static void |
| 185 |
activate_func(gpointer key, |
| 186 |
gpointer value, |
| 187 |
gpointer user_data) |
| 188 |
{ |
| 189 |
class_t *class = (class_t *)value; |
| 190 |
oc_ev_class_t class_type = (oc_ev_class_t) GPOINTER_TO_SIZE(key); |
| 191 |
int *fd = (int *) user_data; |
| 192 |
int tmp; |
| 193 |
|
| 194 |
tmp = class->activate(class); |
| 195 |
/* NOTE: the event API 0.2 is broken. |
| 196 |
* since membership class is the only event |
| 197 |
* class that is supported with this API, we |
| 198 |
* just return its file descriptor |
| 199 |
*/ |
| 200 |
if(class_type == OC_EV_MEMB_CLASS) { |
| 201 |
*fd = tmp; |
| 202 |
} |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
static gboolean |
| 207 |
handle_func(gpointer key, |
| 208 |
gpointer value, |
| 209 |
gpointer user_data) |
| 210 |
{ |
| 211 |
class_t *class = (class_t *)value; |
| 212 |
|
| 213 |
/* if handle event fails, remove this class */ |
| 214 |
if(!class->handle_event((void *)class)){ |
| 215 |
/* before we remove this class, |
| 216 |
we should unregister and free it first */ |
| 217 |
class->unregister(class); |
| 218 |
g_free(class); |
| 219 |
return TRUE; |
| 220 |
} |
| 221 |
/*do not remove this class*/ |
| 222 |
return FALSE; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
int |
| 227 |
oc_ev_register(oc_ev_t **token) |
| 228 |
{ |
| 229 |
__oc_ev_t *rettoken; |
| 230 |
|
| 231 |
oc_ev_init(); |
| 232 |
|
| 233 |
|
| 234 |
rettoken = (__oc_ev_t *)g_malloc(sizeof(__oc_ev_t)); |
| 235 |
*token = (oc_ev_t *)GUINT_TO_POINTER(token_counter++); |
| 236 |
|
| 237 |
if(!rettoken) { |
| 238 |
return ENOMEM; |
| 239 |
} |
| 240 |
|
| 241 |
rettoken->oc_flag = EVENT_INIT; |
| 242 |
rettoken->oc_eventclass = g_hash_table_new(g_direct_hash, |
| 243 |
g_direct_equal); |
| 244 |
|
| 245 |
g_hash_table_insert(tokenhash, *token, rettoken); |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
int |
| 253 |
oc_ev_unregister(oc_ev_t *tok) |
| 254 |
{ |
| 255 |
__oc_ev_t *token = NULL; |
| 256 |
|
| 257 |
if(tokenhash) { |
| 258 |
token = g_hash_table_lookup(tokenhash, tok); |
| 259 |
} |
| 260 |
|
| 261 |
if(token == NULL || token_invalid(token)){ |
| 262 |
return EINVAL; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
/* |
| 267 |
* delete all the event classes associated within |
| 268 |
* this handle |
| 269 |
*/ |
| 270 |
g_hash_table_foreach_remove(token->oc_eventclass, |
| 271 |
eventclass_remove_func, NULL); |
| 272 |
|
| 273 |
g_hash_table_remove(tokenhash, tok); |
| 274 |
g_free(token); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/* a to configure any special parameters for |
| 280 |
* any of the classes. This function is not |
| 281 |
* part of the 0.2 event API. Is been added |
| 282 |
* to support setup of any special behaviour. |
| 283 |
*/ |
| 284 |
void |
| 285 |
oc_ev_special(const oc_ev_t *tok, |
| 286 |
oc_ev_class_t class_type, |
| 287 |
int type) |
| 288 |
{ |
| 289 |
class_t *class; |
| 290 |
const __oc_ev_t *token = (__oc_ev_t *) |
| 291 |
g_hash_table_lookup(tokenhash, tok); |
| 292 |
|
| 293 |
if(token == NULL || token_invalid(token)){ |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
/* if structure for the class already exists |
| 298 |
* just update the callback. Else allocate |
| 299 |
* a structure and update the callback |
| 300 |
*/ |
| 301 |
if((class = g_hash_table_lookup(token->oc_eventclass, |
| 302 |
(void *)class_type)) == NULL){ |
| 303 |
return; |
| 304 |
} |
| 305 |
class->special(class, type); |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
int |
| 310 |
oc_ev_set_callback(const oc_ev_t *tok, |
| 311 |
oc_ev_class_t class_type, |
| 312 |
oc_ev_callback_t *fn, |
| 313 |
oc_ev_callback_t **prev_fn) |
| 314 |
{ |
| 315 |
class_t *class; |
| 316 |
oc_ev_callback_t *pre_callback; |
| 317 |
|
| 318 |
const __oc_ev_t *token = (__oc_ev_t *) |
| 319 |
g_hash_table_lookup(tokenhash, tok); |
| 320 |
|
| 321 |
if(token == NULL || token_invalid(token)){ |
| 322 |
return EINVAL; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
/* if structure for the class already exists |
| 327 |
* just update the callback. Else allocate |
| 328 |
* a structure and update the callback |
| 329 |
*/ |
| 330 |
if((class = g_hash_table_lookup(token->oc_eventclass, |
| 331 |
(void *)class_type)) == NULL){ |
| 332 |
class = class_construct(class_type, NULL); |
| 333 |
g_hash_table_insert(token->oc_eventclass, |
| 334 |
(void *)GINT_TO_POINTER(class_type), |
| 335 |
class); |
| 336 |
} |
| 337 |
|
| 338 |
assert(class && class->set_callback); |
| 339 |
|
| 340 |
pre_callback = class->set_callback(class, fn); |
| 341 |
if(prev_fn) |
| 342 |
*prev_fn = pre_callback; |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
int |
| 349 |
oc_ev_activate(const oc_ev_t *tok, int *fd) |
| 350 |
{ |
| 351 |
const __oc_ev_t *token = (__oc_ev_t *) |
| 352 |
g_hash_table_lookup(tokenhash, tok); |
| 353 |
|
| 354 |
*fd = -1; |
| 355 |
|
| 356 |
if(token == NULL || token_invalid(token)){ |
| 357 |
return EINVAL; |
| 358 |
} |
| 359 |
|
| 360 |
if(!g_hash_table_size(token->oc_eventclass)){ |
| 361 |
return EMFILE; |
| 362 |
} |
| 363 |
|
| 364 |
g_hash_table_foreach(token->oc_eventclass, |
| 365 |
activate_func, fd); |
| 366 |
if(*fd == -1){ |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
int |
| 375 |
oc_ev_handle_event(const oc_ev_t *tok) |
| 376 |
{ |
| 377 |
const __oc_ev_t *token = (__oc_ev_t *) |
| 378 |
g_hash_table_lookup(tokenhash, tok); |
| 379 |
|
| 380 |
if(token == NULL || token_invalid(token)){ |
| 381 |
return EINVAL; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
if(!g_hash_table_size(token->oc_eventclass)){ |
| 386 |
return -1; |
| 387 |
} |
| 388 |
|
| 389 |
if(g_hash_table_size(token->oc_eventclass)) { |
| 390 |
g_hash_table_foreach_remove(token->oc_eventclass, |
| 391 |
handle_func, |
| 392 |
NULL); |
| 393 |
} |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
int |
| 400 |
oc_ev_callback_done(void *ck) |
| 401 |
{ |
| 402 |
void (*f)(void *); |
| 403 |
oc_ev_cookie_t *cookie = (oc_ev_cookie_t *)ck; |
| 404 |
f = cookie_get_func(cookie); |
| 405 |
f(ck); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
int |
| 411 |
oc_ev_is_my_nodeid(const oc_ev_t *tok, const oc_node_t *node) |
| 412 |
{ |
| 413 |
class_t *class; |
| 414 |
const __oc_ev_t *token = (__oc_ev_t *) |
| 415 |
g_hash_table_lookup(tokenhash, tok); |
| 416 |
|
| 417 |
if(token == NULL || token_invalid(token) || !node){ |
| 418 |
return EINVAL; |
| 419 |
} |
| 420 |
|
| 421 |
class = g_hash_table_lookup(token->oc_eventclass, |
| 422 |
(void *)OC_EV_MEMB_CLASS); |
| 423 |
|
| 424 |
return class->is_my_nodeid(class, node); |
| 425 |
} |