From ab0b99eb00ee9e8e45a7d20cabf90891fd7dd9bc Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Mon, 19 Dec 2005 19:57:32 +0000 Subject: [PATCH] more event stuff you should now be able to bind an event handler to a paticiular file or function not just a paticular event when using the custom event like "file:somefile.c" or "func:somefunc" also events now have headers which can be added with varargs and should be created and delivered with api calls switch_event *event; regular event: if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) { switch_event_add_header(event, "event_info", "System Ready"); switch_event_fire(&event); } custom event: if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "some_subclass_string") == SWITCH_STATUS_SUCCESS) { switch_event_add_header(event, "event_info", "hello world %d", 42); switch_event_fire(&event); } switch_event_add_header(event, "test %d", 42); also you can serialize and event into a buffer in a printable/transferrable format with optional body char buf[1024]; with body: switch_event_serialize(event, buf, sizeof(buf), "This is a body my favorite number is %d", 42); no body: switch_event_serialize(event, buf, sizeof(buf), NULL); git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@173 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_event.h | 5 ++--- src/switch_event.c | 45 ++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/include/switch_event.h b/src/include/switch_event.h index 93927a0b2b..daf4d1ab95 100644 --- a/src/include/switch_event.h +++ b/src/include/switch_event.h @@ -71,7 +71,7 @@ struct switch_event_node { SWITCH_DECLARE(switch_status) switch_event_shutdown(void); SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool); -SWITCH_DECLARE(switch_status) switch_event_create_detailed(switch_event **event, switch_event_t event_id, char *subclass_name); +SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event, switch_event_t event_id, char *subclass_name); SWITCH_DECLARE(char *) switch_event_get_header(switch_event *event, char *header_name); SWITCH_DECLARE(switch_status) switch_event_add_header(switch_event *event, char *header_name, char *fmt, ...); SWITCH_DECLARE(void) switch_event_destroy(switch_event **event); @@ -83,8 +83,7 @@ SWITCH_DECLARE(switch_status) switch_event_reserve_subclass_detailed(char *owner SWITCH_DECLARE(switch_status) switch_event_serialize(switch_event *event, char *buf, size_t buflen, char *fmt, ...); #define switch_event_reserve_subclass(subclass_name) switch_event_reserve_subclass_detailed(__FILE__, subclass_name) -#define switch_event_create(event, id) switch_event_create_detailed(event, id, SWITCH_EVENT_SUBCLASS_ANY) -#define switch_event_create_subclass(event, id, subclass) switch_event_create_detailed(event, id, subclass) +#define switch_event_create(event, id) switch_event_create_subclass(event, id, SWITCH_EVENT_SUBCLASS_ANY) #define switch_event_fire(event) switch_event_fire_detailed(__FILE__, __FUNCTION__, __LINE__, event) #endif diff --git a/src/switch_event.c b/src/switch_event.c index 0b8b25c838..5ebf65c12d 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -57,6 +57,21 @@ static char *EVENT_NAMES[] = { }; +#if 0 +static void debug_hash(void) { + switch_hash_index_t* hi; + void *val; + const void *var; + for (hi = switch_hash_first(EPOOL, CUSTOM_HASH); hi; hi = switch_hash_next(hi)) { + switch_event_subclass *subclass; + switch_hash_this(hi, &var, NULL, &val); + subclass = val; + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "***WTF %s=%s\n", (char *) var, subclass->name); + } +} +#endif + + static int switch_events_match(switch_event *event, switch_event_node *node) { @@ -72,9 +87,20 @@ static int switch_events_match(switch_event *event, switch_event_node *node) } if (match || event->event_id == node->event_id) { - if (event->subclass && node->subclass) { - match = strstr(event->subclass->name, node->subclass->name) ? 1 : 0; + if (!strncasecmp(node->subclass->name, "file:", 5)) { + char *file_header; + if ((file_header = switch_event_get_header(event, "file"))) { + match = strstr(node->subclass->name + 5, file_header) ? 1 : 0; + } + } else if (!strncasecmp(node->subclass->name, "func:", 5)) { + char *func_header; + if ((func_header = switch_event_get_header(event, "function"))) { + match = strstr(node->subclass->name + 5, func_header) ? 1 : 0; + } + } else { + match = strstr(event->subclass->name, node->subclass->name) ? 1 : 0; + } } else if (event->subclass && !node->subclass) { match = 1; } else { @@ -164,7 +190,7 @@ SWITCH_DECLARE(switch_status) switch_event_reserve_subclass_detailed(char *owner subclass->owner = switch_core_strdup(EPOOL, owner); subclass->name = switch_core_strdup(EPOOL, subclass_name); - switch_core_hash_insert_dup(CUSTOM_HASH, subclass->name, subclass); + switch_core_hash_insert(CUSTOM_HASH, subclass->name, subclass); return SWITCH_STATUS_SUCCESS; @@ -212,13 +238,8 @@ SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool) } -SWITCH_DECLARE(switch_status) switch_event_create_detailed(switch_event **event, switch_event_t event_id, char *subclass_name) +SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event, switch_event_t event_id, char *subclass_name) { - switch_event_subclass *subclass = NULL; - - if (subclass_name) { - subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name); - } if (event_id != SWITCH_EVENT_CUSTOM && subclass_name) { return SWITCH_STATUS_GENERR; @@ -229,7 +250,13 @@ SWITCH_DECLARE(switch_status) switch_event_create_detailed(switch_event **event, } memset(*event, 0, sizeof(switch_event)); + (*event)->event_id = event_id; + + if (subclass_name) { + (*event)->subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name); + } + return SWITCH_STATUS_SUCCESS; }