From 1e22ba2ba5db92285b67a83e3e104e8a51c425d3 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 29 Jan 2013 09:34:23 -0600 Subject: [PATCH] add cleanup method to event consumer --- src/include/switch_cpp.h | 2 ++ src/switch_cpp.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/include/switch_cpp.h b/src/include/switch_cpp.h index 8f50b70fe3..d38824b333 100644 --- a/src/include/switch_cpp.h +++ b/src/include/switch_cpp.h @@ -178,6 +178,7 @@ SWITCH_DECLARE(bool) email(char *to, char *from, char *headers = NULL, char *bod class EventConsumer { protected: switch_memory_pool_t *pool; + int ready; public: switch_queue_t *events; 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(int) bind(const char *event_name, const char *subclass_name = ""); SWITCH_DECLARE(Event *) pop(int block = 0, int timeout = 0); + SWITCH_DECLARE(void) cleanup(); }; #ifdef SWIG diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp index f1ebe3f4e6..f94901e196 100644 --- a/src/switch_cpp.cpp +++ b/src/switch_cpp.cpp @@ -46,6 +46,7 @@ static void event_handler(switch_event_t *event) 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_event_destroy(&dup); } } @@ -61,6 +62,7 @@ SWITCH_DECLARE_CONSTRUCTOR EventConsumer::EventConsumer(const char *event_name, bind(event_name, subclass_name); } + ready = 1; } 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_name_event(event_name, &event_id); + if (!ready) { + return 0; + } + if (zstr(subclass_name)) { subclass_name = NULL; @@ -90,6 +96,10 @@ SWITCH_DECLARE(Event *) EventConsumer::pop(int block, int timeout) void *pop = NULL; Event *ret = NULL; switch_event_t *event; + + if (!ready) { + return NULL; + } if (block) { if (timeout > 0) { @@ -108,19 +118,42 @@ SWITCH_DECLARE(Event *) EventConsumer::pop(int block, int timeout) return ret; } -SWITCH_DECLARE_CONSTRUCTOR EventConsumer::~EventConsumer() +SWITCH_DECLARE(void) EventConsumer::cleanup() { + uint32_t i; + void *pop; + + if (!ready) { + return; + } + + ready = 0; for (i = 0; i < node_index; i++) { switch_event_unbind(&enodes[i]); } + node_index = 0; + if (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_DECLARE_CONSTRUCTOR EventConsumer::~EventConsumer() +{ + cleanup(); } SWITCH_DECLARE_CONSTRUCTOR IVRMenu::IVRMenu(IVRMenu *main,