diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 4146cfc4c0..197d743380 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -70,6 +70,7 @@ SWITCH_DECLARE(void *) switch_core_alloc(switch_memory_pool *pool, size_t memory SWITCH_DECLARE(switch_status) switch_core_hash_init(switch_hash **hash, switch_memory_pool *pool); SWITCH_DECLARE(switch_status) switch_core_hash_destroy(switch_hash *hash); SWITCH_DECLARE(switch_status) switch_core_hash_insert(switch_hash *hash, char *key, void *data); +SWITCH_DECLARE(switch_status) switch_core_hash_insert_dup(switch_hash *hash, char *key, void *data); SWITCH_DECLARE(switch_status) switch_core_hash_delete(switch_hash *hash, char *key); SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash *hash, char *key); SWITCH_DECLARE(void) switch_core_launch_module_thread(void *(*func)(switch_thread *, void*), void *obj); diff --git a/src/include/switch_event.h b/src/include/switch_event.h index 8603ca044e..0cb83fb397 100644 --- a/src/include/switch_event.h +++ b/src/include/switch_event.h @@ -58,6 +58,8 @@ SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool); SWITCH_DECLARE(switch_status) switch_event_fire_subclass(switch_event_t event, int subclass, char *data); SWITCH_DECLARE(switch_status) switch_event_bind(char *id, switch_event_t event, int subclass, switch_event_callback_t callback); SWITCH_DECLARE(char *) switch_event_name(switch_event_t event); +SWITCH_DECLARE(char *) switch_event_subclass_name(int subclass); +SWITCH_DECLARE(switch_status) switch_event_reserve_subclass(int subclass, char *name); #define switch_event_fire(event, data) switch_event_fire_subclass(event, 0, data); #endif diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 81df789e3a..575362f5f3 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -56,7 +56,8 @@ typedef enum { SWITCH_STATUS_NOTIMPL, SWITCH_STATUS_MEMERR, SWITCH_STATUS_NOOP, - SWITCH_STATUS_GENERR + SWITCH_STATUS_GENERR, + SWITCH_STATUS_INUSE } switch_status; typedef enum { diff --git a/src/mod/mod_event_test/mod_event_test.c b/src/mod/mod_event_test/mod_event_test.c index da4c94d51b..c84f3ff1ef 100644 --- a/src/mod/mod_event_test/mod_event_test.c +++ b/src/mod/mod_event_test/mod_event_test.c @@ -31,11 +31,20 @@ */ #include +typedef enum { + MY_EVENT_UNDEF, + MY_EVENT_COOL +} my_event_t; + static const char modname[] = "mod_event_test"; static void event_handler (switch_event *event) { - switch_console_printf(SWITCH_CHANNEL_CONSOLE,"*** OK *** I got event [%s] subclass [%d] data [%s]\n", switch_event_name(event->event), event->subclass, event->data); + switch_console_printf(SWITCH_CHANNEL_CONSOLE,"*** OK *** I got event [%s] subclass [%d(%s)] data [%s]\n", + switch_event_name(event->event), + event->subclass, + switch_event_subclass_name(event->subclass), + event->data); } static switch_loadable_module_interface event_test_module_interface = { @@ -51,19 +60,25 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(switch_loadable_module_inte /* connect my internal structure to the blank pointer passed to me */ *interface = &event_test_module_interface; + if (switch_event_reserve_subclass(MY_EVENT_COOL, "my cool event") != SWITCH_STATUS_SUCCESS) { + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Couldn't register subclass!"); + return SWITCH_STATUS_GENERR; + } switch_event_bind((char *)modname, SWITCH_EVENT_ALL, -1, event_handler); /* indicate that the module should continue to be loaded */ return SWITCH_STATUS_SUCCESS; } +//#define TORTURE_ME + #ifdef TORTURE_ME SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) { for(;;) { int x; for(x = 0; x < 100; x++) { - switch_event_fire(SWITCH_EVENT_CUSTOM, "hello world"); + switch_event_fire_subclass(SWITCH_EVENT_CUSTOM, MY_EVENT_COOL, "hello world"); } switch_yield(100000); } diff --git a/src/switch_channel.c b/src/switch_channel.c index b25aa9ac46..931a9d0d09 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -207,9 +207,7 @@ SWITCH_DECLARE(switch_status) switch_channel_set_variable(switch_channel *channe assert(channel != NULL); switch_core_hash_delete(channel->variables, varname); - if (value) { - switch_core_hash_insert(channel->variables, varname, switch_core_session_strdup(channel->session, value)); - } + switch_core_hash_insert_dup(channel->variables, varname, switch_core_session_strdup(channel->session, value)); return SWITCH_STATUS_SUCCESS; } diff --git a/src/switch_core.c b/src/switch_core.c index 8097835b0e..b20ce30645 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -1316,6 +1316,12 @@ SWITCH_DECLARE(switch_status) switch_core_hash_destroy(switch_hash *hash) return SWITCH_STATUS_SUCCESS; } +SWITCH_DECLARE(switch_status) switch_core_hash_insert_dup(switch_hash *hash, char *key, void *data) +{ + apr_hash_set(hash, switch_core_strdup(apr_hash_pool_get(hash), key), APR_HASH_KEY_STRING, data); + return SWITCH_STATUS_SUCCESS; +} + SWITCH_DECLARE(switch_status) switch_core_hash_insert(switch_hash *hash, char *key, void *data) { apr_hash_set(hash, key, APR_HASH_KEY_STRING, data); diff --git a/src/switch_event.c b/src/switch_event.c index 0592d67078..d0d3091f4b 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -33,12 +33,12 @@ static switch_event *EVENT_QUEUE_HEAD; static switch_event *EVENT_QUEUE_WORK; +static switch_thread_cond_t *COND; static switch_event_node *EVENT_NODES[SWITCH_EVENT_ALL+1] = {NULL}; static switch_mutex_t *BLOCK = NULL; static switch_mutex_t *QLOCK = NULL; static switch_memory_pool *EPOOL = NULL; -switch_thread_cond_t *COND; - +static switch_hash *CUSTOM_HASH = NULL; static int THREAD_RUNNING = 0; /* make sure this is synced with the switch_event_t enum in switch_types.h @@ -115,6 +115,46 @@ SWITCH_DECLARE(char *) switch_event_name(switch_event_t event) return EVENT_NAMES[event]; } +SWITCH_DECLARE(char *) switch_event_subclass_name(int subclass) +{ + char *name; + char val[50] = ""; + + assert(EPOOL != NULL); + assert(CUSTOM_HASH != NULL); + + if (subclass <= 0) { + return "NONE"; + } + + snprintf(val, sizeof(val), "%d", subclass); + name = switch_core_hash_find(CUSTOM_HASH, val); + return name ? name : "UNRESERVED"; +} + + +SWITCH_DECLARE(switch_status) switch_event_reserve_subclass(int subclass, char *name) +{ + char val[50] = ""; + + assert(EPOOL != NULL); + assert(CUSTOM_HASH != NULL); + + if (subclass <= 0) { + return SWITCH_STATUS_NOTIMPL; + } + + snprintf(val, sizeof(val), "%d", subclass); + if (switch_core_hash_find(CUSTOM_HASH, val)) { + return SWITCH_STATUS_INUSE; + } + + switch_core_hash_insert_dup(CUSTOM_HASH, val, switch_core_strdup(EPOOL, name)); + + return SWITCH_STATUS_SUCCESS; + +} + SWITCH_DECLARE(switch_status) switch_event_shutdown(void) { THREAD_RUNNING = -1; @@ -139,7 +179,7 @@ SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool) switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Eventing Engine.\n"); switch_mutex_init(&BLOCK, SWITCH_MUTEX_NESTED, EPOOL); switch_mutex_init(&QLOCK, SWITCH_MUTEX_NESTED, EPOOL); - + switch_core_hash_init(&CUSTOM_HASH, EPOOL); switch_thread_create(&thread, thd_attr, switch_event_thread, diff --git a/src/switch_loadable_module.c b/src/switch_loadable_module.c index c1dfb9b3c3..4569cb73a9 100644 --- a/src/switch_loadable_module.c +++ b/src/switch_loadable_module.c @@ -320,12 +320,12 @@ SWITCH_DECLARE(switch_status) switch_loadable_module_init() SWITCH_DECLARE(void) loadable_module_shutdown(void) { - apr_hash_index_t* hi; + switch_hash_index_t* hi; void *val; switch_loadable_module *module; - for (hi = apr_hash_first(loadable_modules.pool, loadable_modules.module_hash); hi; hi = apr_hash_next(hi)) { - apr_hash_this(hi, NULL, NULL, &val); + for (hi = switch_hash_first(loadable_modules.pool, loadable_modules.module_hash); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, NULL, NULL, &val); module = (switch_loadable_module *) val; switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Checking %s\t", module->interface->module_name); if (module->switch_module_shutdown) { @@ -370,12 +370,12 @@ SWITCH_DECLARE(switch_api_interface *) loadable_module_get_api_interface(char *n SWITCH_DECLARE(int) loadable_module_get_codecs(switch_memory_pool *pool, switch_codec_interface **array, int arraylen) { - apr_hash_index_t* hi; + switch_hash_index_t* hi; void *val; int i = 0; - for (hi = apr_hash_first(pool, loadable_modules.codec_hash); hi; hi = apr_hash_next(hi)) { - apr_hash_this(hi, NULL, NULL, &val); + for (hi = switch_hash_first(pool, loadable_modules.codec_hash); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, NULL, NULL, &val); array[i++] = val; if (i > arraylen) { break;