]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
refactor timeout tracker for valet
authorAnthony Minessale <anthm@freeswitch.org>
Fri, 16 Sep 2011 16:51:44 +0000 (11:51 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Fri, 16 Sep 2011 16:51:44 +0000 (11:51 -0500)
src/mod/applications/mod_valet_parking/mod_valet_parking.c

index 0b25a47e94f83c73076b19c3bf352e701e4b4bd6..bd40512a7ee48aace7f47dd28e4a2e24c5891013 100644 (file)
@@ -47,6 +47,7 @@ typedef struct {
        switch_hash_t *hash;
        switch_mutex_t *mutex;
        switch_memory_pool_t *pool;
+       time_t last_timeout_check;
 } valet_lot_t;
 
 static valet_lot_t globals = { 0 };
@@ -92,40 +93,55 @@ static void check_timeouts(void)
        void *val;
        time_t now;
        valet_lot_t *lot;
+       switch_console_callback_match_t *matches = NULL;
+       switch_console_callback_match_node_t *m;
+       switch_hash_index_t *i_hi;
+       const void *i_var;
+       void *i_val;
+       char *i_ext;
+       valet_token_t *token;
 
        now = switch_epoch_time_now(NULL);
 
        switch_mutex_lock(globals.mutex);
+       if (now - globals.last_timeout_check > 30) {
+               switch_mutex_unlock(globals.mutex);
+               return;
+       }
 
-
+       globals.last_timeout_check = now;
        for (hi = switch_hash_first(NULL, globals.hash); hi; hi = switch_hash_next(hi)) {
-               switch_hash_index_t *i_hi;
-               const void *i_var;
-               void *i_val;
-               char *i_ext;
-               valet_token_t *token;
-
                switch_hash_this(hi, &var, NULL, &val);
-               lot = (valet_lot_t *) val;
+               switch_console_push_match(&matches, (const char *) var);
+       }
+       switch_mutex_unlock(globals.mutex);
 
-               switch_mutex_lock(lot->mutex);
 
-       top:
+       if (matches) {
+               for (m = matches->head; m; m = m->next) {
+
+                       lot = valet_find_lot(m->val);
+                       switch_mutex_lock(lot->mutex);
+
+               top:
                
-               for (i_hi = switch_hash_first(NULL, lot->hash); i_hi; i_hi = switch_hash_next(i_hi)) {
-                       switch_hash_this(i_hi, &i_var, NULL, &i_val);
-                       i_ext = (char *) i_var;
-                       token = (valet_token_t *) i_val;
-                       if (token->timeout > 0 && (token->timeout < now || token->timeout == 1)) {
-                               switch_core_hash_delete(lot->hash, i_ext);
-                               switch_safe_free(token);
-                               goto top;
+                       for (i_hi = switch_hash_first(NULL, lot->hash); i_hi; i_hi = switch_hash_next(i_hi)) {
+                               switch_hash_this(i_hi, &i_var, NULL, &i_val);
+                               i_ext = (char *) i_var;
+                               token = (valet_token_t *) i_val;
+                               if (token->timeout > 0 && (token->timeout < now || token->timeout == 1)) {
+                                       switch_core_hash_delete(lot->hash, i_ext);
+                                       switch_safe_free(token);
+                                       goto top;
+                               }
                        }
+
+                       switch_mutex_unlock(lot->mutex);
                }
 
-               switch_mutex_unlock(lot->mutex);
+               switch_console_free_matches(&matches);
        }
-       switch_mutex_unlock(globals.mutex);
+
 }
 
 static int next_id(valet_lot_t *lot, int min, int max, int in)
@@ -170,8 +186,6 @@ SWITCH_STANDARD_APP(valet_parking_function)
                play_announce = switch_true(var);
        }
 
-       check_timeouts();
-
        if (!zstr(data) && (lbuf = switch_core_session_strdup(session, data))
                && (argc = switch_separate_string(lbuf, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) {
                char *lot_name = argv[0];
@@ -391,6 +405,8 @@ SWITCH_STANDARD_APP(valet_parking_function)
 
  end:
 
+       check_timeouts();
+
        if (token) {
                token->timeout = 1;
        }
@@ -452,6 +468,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_valet_parking_load)
                return SWITCH_STATUS_TERM;
        }
 
+       memset(&globals, 0, sizeof(globals));
+
        globals.pool = pool;
        switch_core_hash_init(&globals.hash, NULL);
        switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);