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);
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
SWITCH_STATUS_NOTIMPL,
SWITCH_STATUS_MEMERR,
SWITCH_STATUS_NOOP,
- SWITCH_STATUS_GENERR
+ SWITCH_STATUS_GENERR,
+ SWITCH_STATUS_INUSE
} switch_status;
typedef enum {
*/
#include <switch.h>
+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 = {
/* 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);
}
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;
}
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);
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
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;
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,
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) {
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;