]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
more event code
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 14 Dec 2005 22:46:09 +0000 (22:46 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 14 Dec 2005 22:46:09 +0000 (22:46 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@153 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_core.h
src/include/switch_event.h
src/include/switch_types.h
src/mod/mod_event_test/mod_event_test.c
src/switch_channel.c
src/switch_core.c
src/switch_event.c
src/switch_loadable_module.c

index 4146cfc4c02a0e71768886ad5301b8949aa04424..197d743380f4193be251724ef48aeba74a824ae9 100644 (file)
@@ -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);
index 8603ca044e5b874b47dbad82597a72e3396c2c63..0cb83fb397ebecd988cd49094ebc6e689b6faf7b 100644 (file)
@@ -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
index 81df789e3a1fd62e8526b7698c29c8c3544ece4d..575362f5f3e4b41374d78f2dda01ed09996793ec 100644 (file)
@@ -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 {
index da4c94d51b27a023088e119c082d7d3d0de26160..c84f3ff1ef0d831ad6a3197204dde32544759785 100644 (file)
  */
 #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 = {
@@ -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);
        }
index b25aa9ac4656ce28e59cd84b3306c4af7d28e07a..931a9d0d0911288562ba851ca1513253b8a1e0c9 100644 (file)
@@ -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;
 }
index 8097835b0e8892472e30037ac6e21106f8699865..b20ce306452fcca13fde1cb0ccdb8ee42aedaacf 100644 (file)
@@ -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);
index 0592d670786524052004cc200dcd8cead2aa630f..d0d3091f4b44a4924c46584b91e561e9b4e8887b 100644 (file)
 
 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,
index c1dfb9b3c3363d9197fba24c21d0b953a39c83fc..4569cb73a9320e24354d4ef38592b4d199a9ed54 100644 (file)
@@ -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;