add cleanup method to event consumer

This commit is contained in:
Anthony Minessale 2013-01-29 09:34:23 -06:00
parent 4319bc8bd9
commit 1e22ba2ba5
2 changed files with 36 additions and 1 deletions

View File

@ -178,6 +178,7 @@ SWITCH_DECLARE(bool) email(char *to, char *from, char *headers = NULL, char *bod
class EventConsumer { class EventConsumer {
protected: protected:
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
int ready;
public: public:
switch_queue_t *events; switch_queue_t *events;
switch_event_types_t e_event_id; switch_event_types_t e_event_id;
@ -191,6 +192,7 @@ SWITCH_DECLARE(bool) email(char *to, char *from, char *headers = NULL, char *bod
SWITCH_DECLARE_CONSTRUCTOR ~ EventConsumer(); SWITCH_DECLARE_CONSTRUCTOR ~ EventConsumer();
SWITCH_DECLARE(int) bind(const char *event_name, const char *subclass_name = ""); SWITCH_DECLARE(int) bind(const char *event_name, const char *subclass_name = "");
SWITCH_DECLARE(Event *) pop(int block = 0, int timeout = 0); SWITCH_DECLARE(Event *) pop(int block = 0, int timeout = 0);
SWITCH_DECLARE(void) cleanup();
}; };
#ifdef SWIG #ifdef SWIG

View File

@ -46,6 +46,7 @@ static void event_handler(switch_event_t *event)
if (switch_queue_trypush(E->events, dup) != SWITCH_STATUS_SUCCESS) { if (switch_queue_trypush(E->events, dup) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot queue any more events.....\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot queue any more events.....\n");
switch_event_destroy(&dup);
} }
} }
@ -61,6 +62,7 @@ SWITCH_DECLARE_CONSTRUCTOR EventConsumer::EventConsumer(const char *event_name,
bind(event_name, subclass_name); bind(event_name, subclass_name);
} }
ready = 1;
} }
SWITCH_DECLARE(int) EventConsumer::bind(const char *event_name, const char *subclass_name) SWITCH_DECLARE(int) EventConsumer::bind(const char *event_name, const char *subclass_name)
@ -68,6 +70,10 @@ SWITCH_DECLARE(int) EventConsumer::bind(const char *event_name, const char *subc
switch_event_types_t event_id = SWITCH_EVENT_CUSTOM; switch_event_types_t event_id = SWITCH_EVENT_CUSTOM;
switch_name_event(event_name, &event_id); switch_name_event(event_name, &event_id);
if (!ready) {
return 0;
}
if (zstr(subclass_name)) { if (zstr(subclass_name)) {
subclass_name = NULL; subclass_name = NULL;
@ -90,6 +96,10 @@ SWITCH_DECLARE(Event *) EventConsumer::pop(int block, int timeout)
void *pop = NULL; void *pop = NULL;
Event *ret = NULL; Event *ret = NULL;
switch_event_t *event; switch_event_t *event;
if (!ready) {
return NULL;
}
if (block) { if (block) {
if (timeout > 0) { if (timeout > 0) {
@ -108,19 +118,42 @@ SWITCH_DECLARE(Event *) EventConsumer::pop(int block, int timeout)
return ret; return ret;
} }
SWITCH_DECLARE_CONSTRUCTOR EventConsumer::~EventConsumer() SWITCH_DECLARE(void) EventConsumer::cleanup()
{ {
uint32_t i; uint32_t i;
void *pop;
if (!ready) {
return;
}
ready = 0;
for (i = 0; i < node_index; i++) { for (i = 0; i < node_index; i++) {
switch_event_unbind(&enodes[i]); switch_event_unbind(&enodes[i]);
} }
node_index = 0;
if (events) { if (events) {
switch_queue_interrupt_all(events); switch_queue_interrupt_all(events);
} }
while(switch_queue_trypop(events, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *event = (switch_event_t *) pop;
switch_event_destroy(&event);
}
switch_core_destroy_memory_pool(&pool); switch_core_destroy_memory_pool(&pool);
}
SWITCH_DECLARE_CONSTRUCTOR EventConsumer::~EventConsumer()
{
cleanup();
} }
SWITCH_DECLARE_CONSTRUCTOR IVRMenu::IVRMenu(IVRMenu *main, SWITCH_DECLARE_CONSTRUCTOR IVRMenu::IVRMenu(IVRMenu *main,