2005-12-13 20:52:33 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_event.c -- Event System
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch_event.h>
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
static switch_event *EVENT_QUEUE_HEAD;
|
|
|
|
static switch_event *EVENT_QUEUE_WORK;
|
2005-12-14 22:46:09 +00:00
|
|
|
static switch_thread_cond_t *COND;
|
2005-12-14 01:40:51 +00:00
|
|
|
static switch_event_node *EVENT_NODES[SWITCH_EVENT_ALL+1] = {NULL};
|
2005-12-14 21:29:46 +00:00
|
|
|
static switch_mutex_t *BLOCK = NULL;
|
|
|
|
static switch_mutex_t *QLOCK = NULL;
|
2005-12-13 20:52:33 +00:00
|
|
|
static switch_memory_pool *EPOOL = NULL;
|
2005-12-14 22:46:09 +00:00
|
|
|
static switch_hash *CUSTOM_HASH = NULL;
|
2005-12-14 20:22:19 +00:00
|
|
|
static int THREAD_RUNNING = 0;
|
2005-12-13 20:52:33 +00:00
|
|
|
|
|
|
|
/* make sure this is synced with the switch_event_t enum in switch_types.h
|
|
|
|
also never put any new ones before EVENT_ALL
|
|
|
|
*/
|
|
|
|
static char *EVENT_NAMES[] = {
|
|
|
|
"CUSTOM",
|
|
|
|
"INBOUND_CHAN",
|
|
|
|
"OUTBOUND_CHAN",
|
|
|
|
"ANSWER_CHAN",
|
|
|
|
"HANGUP_CHAN",
|
|
|
|
"STARTUP",
|
2005-12-14 21:29:46 +00:00
|
|
|
"EVENT_SHUTDOWN",
|
|
|
|
"SHUTDOWN",
|
2005-12-13 20:52:33 +00:00
|
|
|
"ALL"
|
|
|
|
};
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
static struct switch_event_subclass default_subclass = {__FILE__, "NONE"};
|
|
|
|
|
|
|
|
static int switch_events_match(switch_event *event, switch_event_node *node)
|
|
|
|
{
|
|
|
|
int match = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (node->event == SWITCH_EVENT_ALL) {
|
2005-12-15 20:20:57 +00:00
|
|
|
match++;
|
|
|
|
|
|
|
|
if (!node->subclass) {
|
|
|
|
return match;
|
|
|
|
}
|
2005-12-15 19:10:43 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 20:20:57 +00:00
|
|
|
if (match || event->event == node->event) {
|
2005-12-15 19:10:43 +00:00
|
|
|
|
2005-12-15 20:20:57 +00:00
|
|
|
if (event->subclass && node->subclass) {
|
|
|
|
match = strstr(event->subclass->name, node->subclass->name) ? 1 : 0;
|
|
|
|
} else if (event->subclass && !node->subclass) {
|
|
|
|
match = 1;
|
|
|
|
} else {
|
2005-12-15 19:10:43 +00:00
|
|
|
match = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
static void * SWITCH_THREAD_FUNC switch_event_thread(switch_thread *thread, void *obj)
|
|
|
|
{
|
2005-12-15 19:10:43 +00:00
|
|
|
switch_event_node *node;
|
2005-12-14 20:22:19 +00:00
|
|
|
switch_event *event = NULL, *out_event = NULL;
|
|
|
|
switch_event_t e;
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_t *mutex = NULL;
|
2005-12-14 20:22:19 +00:00
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_init(&mutex, SWITCH_MUTEX_NESTED, EPOOL);
|
|
|
|
switch_thread_cond_create(&COND, EPOOL);
|
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
|
|
|
|
assert(QLOCK != NULL);
|
2005-12-14 20:22:19 +00:00
|
|
|
assert(EPOOL != NULL);
|
|
|
|
|
|
|
|
THREAD_RUNNING = 1;
|
|
|
|
while(THREAD_RUNNING == 1) {
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_thread_cond_wait(COND, mutex);
|
|
|
|
switch_mutex_lock(QLOCK);
|
|
|
|
/* <LOCKED> -----------------------------------------------*/
|
|
|
|
EVENT_QUEUE_WORK = EVENT_QUEUE_HEAD;
|
|
|
|
EVENT_QUEUE_HEAD = NULL;
|
|
|
|
switch_mutex_unlock(QLOCK);
|
|
|
|
/* </LOCKED> -----------------------------------------------*/
|
2005-12-14 20:22:19 +00:00
|
|
|
|
|
|
|
for(event = EVENT_QUEUE_WORK; event;) {
|
|
|
|
out_event = event;
|
|
|
|
event = event->next;
|
|
|
|
out_event->next = NULL;
|
2005-12-14 21:29:46 +00:00
|
|
|
for(e = out_event->event;; e = SWITCH_EVENT_ALL) {
|
2005-12-15 19:10:43 +00:00
|
|
|
for(node = EVENT_NODES[e]; node; node = node->next) {
|
|
|
|
if (switch_events_match(out_event, node)) {
|
|
|
|
out_event->user_data = node->user_data;
|
|
|
|
if (!out_event->subclass) {
|
|
|
|
out_event->subclass = &default_subclass;
|
|
|
|
}
|
|
|
|
node->callback(out_event);
|
2005-12-14 20:22:19 +00:00
|
|
|
}
|
2005-12-14 21:29:46 +00:00
|
|
|
}
|
2005-12-14 20:22:19 +00:00
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
if (e == SWITCH_EVENT_ALL) {
|
|
|
|
break;
|
2005-12-14 20:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-14 21:29:46 +00:00
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
free(out_event->data);
|
|
|
|
free(out_event);
|
|
|
|
}
|
2005-12-14 21:29:46 +00:00
|
|
|
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
}
|
|
|
|
THREAD_RUNNING = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
SWITCH_DECLARE(char *) switch_event_name(switch_event_t event)
|
|
|
|
{
|
2005-12-14 21:29:46 +00:00
|
|
|
assert(BLOCK != NULL);
|
2005-12-14 20:22:19 +00:00
|
|
|
assert(EPOOL != NULL);
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
return EVENT_NAMES[event];
|
|
|
|
}
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_event_reserve_subclass_detailed(char *owner, char *subclass_name)
|
2005-12-14 22:46:09 +00:00
|
|
|
{
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
switch_event_subclass *subclass;
|
2005-12-14 22:46:09 +00:00
|
|
|
|
|
|
|
assert(EPOOL != NULL);
|
|
|
|
assert(CUSTOM_HASH != NULL);
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
if (switch_core_hash_find(CUSTOM_HASH, subclass_name)) {
|
2005-12-14 22:46:09 +00:00
|
|
|
return SWITCH_STATUS_INUSE;
|
|
|
|
}
|
2005-12-15 19:10:43 +00:00
|
|
|
|
|
|
|
if (!(subclass = switch_core_alloc(EPOOL, sizeof(*subclass)))) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
subclass->owner = switch_core_strdup(EPOOL, owner);
|
|
|
|
subclass->name = switch_core_strdup(EPOOL, subclass_name);
|
2005-12-14 22:46:09 +00:00
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
switch_core_hash_insert_dup(CUSTOM_HASH, subclass->name, subclass);
|
2005-12-14 22:46:09 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_event_shutdown(void)
|
|
|
|
{
|
|
|
|
THREAD_RUNNING = -1;
|
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_event_fire(SWITCH_EVENT_EVENT_SHUTDOWN, "Event System Shutting Down");
|
2005-12-14 20:22:19 +00:00
|
|
|
while(THREAD_RUNNING) {
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_yield(1000);
|
2005-12-14 20:22:19 +00:00
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool)
|
|
|
|
{
|
2005-12-14 20:22:19 +00:00
|
|
|
switch_thread *thread;
|
|
|
|
switch_threadattr_t *thd_attr;;
|
|
|
|
switch_threadattr_create(&thd_attr, pool);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
assert(pool != NULL);
|
|
|
|
EPOOL = pool;
|
|
|
|
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Eventing Engine.\n");
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_init(&BLOCK, SWITCH_MUTEX_NESTED, EPOOL);
|
|
|
|
switch_mutex_init(&QLOCK, SWITCH_MUTEX_NESTED, EPOOL);
|
2005-12-14 22:46:09 +00:00
|
|
|
switch_core_hash_init(&CUSTOM_HASH, EPOOL);
|
2005-12-14 20:22:19 +00:00
|
|
|
switch_thread_create(&thread,
|
|
|
|
thd_attr,
|
|
|
|
switch_event_thread,
|
|
|
|
NULL,
|
2005-12-14 21:29:46 +00:00
|
|
|
EPOOL
|
2005-12-14 20:22:19 +00:00
|
|
|
);
|
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
while(!THREAD_RUNNING) {
|
|
|
|
switch_yield(1000);
|
|
|
|
}
|
2005-12-14 20:22:19 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_event_fire_subclass(switch_event_t event, char *subclass_name, char *data)
|
2005-12-13 20:52:33 +00:00
|
|
|
{
|
2005-12-14 20:22:19 +00:00
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_event *new_event, *ep;
|
2005-12-15 19:10:43 +00:00
|
|
|
switch_event_subclass *subclass = NULL;
|
2005-12-13 20:52:33 +00:00
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
assert(BLOCK != NULL);
|
2005-12-13 20:52:33 +00:00
|
|
|
assert(EPOOL != NULL);
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
if (!(new_event = malloc(sizeof(*new_event)))) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
2005-12-13 20:52:33 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
if (subclass_name) {
|
|
|
|
subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name);
|
|
|
|
}
|
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
memset(new_event, 0, sizeof(*new_event));
|
|
|
|
new_event->event = event;
|
|
|
|
new_event->subclass = subclass;
|
|
|
|
new_event->data = strdup(data ? data : "");
|
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_lock(QLOCK);
|
2005-12-14 20:22:19 +00:00
|
|
|
/* <LOCKED> -----------------------------------------------*/
|
2005-12-14 21:29:46 +00:00
|
|
|
for(ep = EVENT_QUEUE_HEAD; ep && ep->next; ep = ep->next);
|
|
|
|
|
|
|
|
if (ep) {
|
|
|
|
ep->next = new_event;
|
|
|
|
} else {
|
|
|
|
EVENT_QUEUE_HEAD = new_event;
|
2005-12-14 20:22:19 +00:00
|
|
|
}
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_unlock(QLOCK);
|
2005-12-14 20:22:19 +00:00
|
|
|
/* </LOCKED> -----------------------------------------------*/
|
2005-12-15 19:10:43 +00:00
|
|
|
|
2005-12-14 20:22:19 +00:00
|
|
|
switch_thread_cond_signal(COND);
|
2005-12-13 20:52:33 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_event_bind(char *id, switch_event_t event, char *subclass_name, switch_event_callback_t callback, void *user_data)
|
2005-12-13 20:52:33 +00:00
|
|
|
{
|
|
|
|
switch_event_node *event_node;
|
2005-12-15 19:10:43 +00:00
|
|
|
switch_event_subclass *subclass = NULL;
|
2005-12-13 20:52:33 +00:00
|
|
|
|
2005-12-14 21:29:46 +00:00
|
|
|
assert(BLOCK != NULL);
|
2005-12-13 20:52:33 +00:00
|
|
|
assert(EPOOL != NULL);
|
|
|
|
|
2005-12-15 19:10:43 +00:00
|
|
|
if (subclass_name) {
|
|
|
|
if (!(subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name))) {
|
2005-12-15 23:56:21 +00:00
|
|
|
if (!(subclass = switch_core_alloc(EPOOL, sizeof(*subclass)))) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
} else {
|
|
|
|
subclass->owner = switch_core_strdup(EPOOL, id);
|
|
|
|
subclass->name = switch_core_strdup(EPOOL, subclass_name);
|
|
|
|
}
|
2005-12-15 19:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
if (event <= SWITCH_EVENT_ALL && (event_node = switch_core_alloc(EPOOL, sizeof(switch_event_node)))) {
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_lock(BLOCK);
|
2005-12-14 20:22:19 +00:00
|
|
|
/* <LOCKED> -----------------------------------------------*/
|
2005-12-13 20:52:33 +00:00
|
|
|
event_node->id = switch_core_strdup(EPOOL, id);
|
|
|
|
event_node->event = event;
|
|
|
|
event_node->subclass = subclass;
|
|
|
|
event_node->callback = callback;
|
2005-12-15 19:10:43 +00:00
|
|
|
event_node->user_data = user_data;
|
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
if (EVENT_NODES[event]) {
|
|
|
|
event_node->next = EVENT_NODES[event];
|
|
|
|
}
|
2005-12-15 19:10:43 +00:00
|
|
|
|
2005-12-13 20:52:33 +00:00
|
|
|
EVENT_NODES[event] = event_node;
|
2005-12-14 21:29:46 +00:00
|
|
|
switch_mutex_unlock(BLOCK);
|
2005-12-14 20:22:19 +00:00
|
|
|
/* </LOCKED> -----------------------------------------------*/
|
2005-12-13 20:52:33 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|