]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add cleanup method to event consumer
authorAnthony Minessale <anthm@freeswitch.org>
Tue, 29 Jan 2013 15:34:23 +0000 (09:34 -0600)
committerAnthony Minessale <anthm@freeswitch.org>
Tue, 29 Jan 2013 15:34:23 +0000 (09:34 -0600)
src/include/switch_cpp.h
src/switch_cpp.cpp

index 8f50b70fe3de3ffb945426da879264fdb6f858de..d38824b333a32555b99352a1a9735d3812d9c03b 100644 (file)
@@ -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
index f1ebe3f4e67b191de95c341e9531eeeca982c06d..f94901e196f9cb1f51ad051a1a780bfd0c68f7aa 100644 (file)
@@ -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,