Merged revisions 34159-34160 via svnmerge from
[asterisk/asterisk.git] / apps / app_queue.c
index 5115de2..bb2c511 100644 (file)
@@ -506,8 +506,7 @@ static void *changethread(void *data)
        technology = ast_strdupa(sc->dev);
        loc = strchr(technology, '/');
        if (loc) {
-               *loc = '\0';
-               loc++;
+               *loc++ = '\0';
        } else {
                free(sc);
                return NULL;
@@ -520,43 +519,44 @@ static void *changethread(void *data)
        }
        AST_LIST_UNLOCK(&interfaces);
 
-       if (curint) {
-
+       if (!curint) {
                if (option_debug)
-                       ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s)\n", technology, loc, sc->state, devstate2str(sc->state));
-               AST_LIST_LOCK(&queues);
-               AST_LIST_TRAVERSE(&queues, q, list) {
-                       ast_mutex_lock(&q->lock);
-                       cur = q->members;
-                       while(cur) {
-                               if (!strcasecmp(sc->dev, cur->interface)) {
-                                       if (cur->status != sc->state) {
-                                               cur->status = sc->state;
-                                               if (!q->maskmemberstatus) {
-                                                       manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
-                                                               "Queue: %s\r\n"
-                                                               "Location: %s\r\n"
-                                                               "Membership: %s\r\n"
-                                                               "Penalty: %d\r\n"
-                                                               "CallsTaken: %d\r\n"
-                                                               "LastCall: %d\r\n"
-                                                               "Status: %d\r\n"
-                                                               "Paused: %d\r\n",
-                                                       q->name, cur->interface, cur->dynamic ? "dynamic" : "static",
-                                                       cur->penalty, cur->calls, (int)cur->lastcall, cur->status, cur->paused);
-                                               }
-                                       }
-                               }
-                               cur = cur->next;
+                       ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", technology, loc, sc->state, devstate2str(sc->state));
+               free(sc);
+               return NULL;
+        }
+
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s)\n", technology, loc, sc->state, devstate2str(sc->state));
+       AST_LIST_LOCK(&queues);
+       AST_LIST_TRAVERSE(&queues, q, list) {
+               ast_mutex_lock(&q->lock);
+               for (cur = q->members; cur; cur = cur->next) {
+                       if (strcasecmp(sc->dev, cur->interface))
+                               continue;
+
+                       if (cur->status != sc->state) {
+                               cur->status = sc->state;
+                               if (q->maskmemberstatus)
+                                       continue;
+
+                               manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
+                                             "Queue: %s\r\n"
+                                             "Location: %s\r\n"
+                                             "Membership: %s\r\n"
+                                             "Penalty: %d\r\n"
+                                             "CallsTaken: %d\r\n"
+                                             "LastCall: %d\r\n"
+                                             "Status: %d\r\n"
+                                             "Paused: %d\r\n",
+                                             q->name, cur->interface, cur->dynamic ? "dynamic" : "static",
+                                             cur->penalty, cur->calls, (int)cur->lastcall, cur->status, cur->paused);
                        }
-                       ast_mutex_unlock(&q->lock);
                }
-               AST_LIST_UNLOCK(&queues);
-       } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", technology, loc, sc->state, devstate2str(sc->state));
+               ast_mutex_unlock(&q->lock);
        }
-       free(sc);
+       AST_LIST_UNLOCK(&queues);
+
        return NULL;
 }
 
@@ -567,17 +567,19 @@ static int statechange_queue(const char *dev, int state, void *ign)
        struct statechange *sc;
        pthread_t t;
        pthread_attr_t attr;
-       
-       if ((sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1))) {
-               sc->state = state;
-               strcpy(sc->dev, dev);
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-               if (ast_pthread_create(&t, &attr, changethread, sc)) {
-                       ast_log(LOG_WARNING, "Failed to create update thread!\n");
-                       free(sc);
-               }
+
+       if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1)))
+               return 0;
+
+       sc->state = state;
+       strcpy(sc->dev, dev);
+       pthread_attr_init(&attr);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+       if (ast_pthread_create(&t, &attr, changethread, sc)) {
+               ast_log(LOG_WARNING, "Failed to create update thread!\n");
+               free(sc);
        }
+
        return 0;
 }
 
@@ -656,7 +658,7 @@ static void clear_queue(struct ast_call_queue *q)
 
 static int add_to_interfaces(char *interface) 
 {
-       struct ast_member_interfaces *curint, *newint;
+       struct ast_member_interfaces *curint;
 
        AST_LIST_LOCK(&interfaces);
        AST_LIST_TRAVERSE(&interfaces, curint, list) {
@@ -664,18 +666,21 @@ static int add_to_interfaces(char *interface)
                        break; 
        }
 
-       if (!curint) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Adding %s to the list of interfaces that make up all of our queue members.\n", interface);
+       if (curint) {
+               AST_LIST_UNLOCK(&interfaces);
+               return 0;
+       }
 
-               if ((newint = ast_calloc(1, sizeof(*newint)))) {
-                       ast_copy_string(newint->interface, interface, sizeof(newint->interface));
-                       AST_LIST_INSERT_HEAD(&interfaces, newint, list);
-               }
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Adding %s to the list of interfaces that make up all of our queue members.\n", interface);
+       
+       if ((curint = ast_calloc(1, sizeof(*curint)))) {
+               ast_copy_string(curint->interface, interface, sizeof(curint->interface));
+               AST_LIST_INSERT_HEAD(&interfaces, curint, list);
        }
        AST_LIST_UNLOCK(&interfaces);
 
- return 0;
+       return 0;
 }
 
 static int interface_exists_global(char *interface)
@@ -687,31 +692,33 @@ static int interface_exists_global(char *interface)
        AST_LIST_LOCK(&queues);
        AST_LIST_TRAVERSE(&queues, q, list) {
                ast_mutex_lock(&q->lock);
-               for (mem = q->members; mem; mem = mem->next)
-                       if (!strcasecmp(interface, mem->interface)) {
-                               ast_mutex_unlock(&q->lock);
+               for (mem = q->members; mem && !ret; mem = mem->next) {
+                       if (!strcasecmp(interface, mem->interface))
                                ret = 1;
-                               break;
-                       }
+               }
                ast_mutex_unlock(&q->lock);
+               if (ret)
+                       break;
        }
        AST_LIST_UNLOCK(&queues);
 
        return ret;
 }
 
-
 static int remove_from_interfaces(char *interface)
 {
        struct ast_member_interfaces *curint;
 
        AST_LIST_LOCK(&interfaces);
        AST_LIST_TRAVERSE_SAFE_BEGIN(&interfaces, curint, list) {
-               if (!strcasecmp(curint->interface, interface) && !interface_exists_global(interface)) {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "Removing %s from the list of interfaces that make up all of our queue members.\n", interface);
-                       AST_LIST_REMOVE_CURRENT(&interfaces, list);
-                       free(curint);
+               if (!strcasecmp(curint->interface, interface)) {
+                       if (!interface_exists_global(interface)) {
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "Removing %s from the list of interfaces that make up all of our queue members.\n", interface);
+                               AST_LIST_REMOVE_CURRENT(&interfaces, list);
+                               free(curint);
+                       }
+                       break;
                }
        }
        AST_LIST_TRAVERSE_SAFE_END;
@@ -725,14 +732,9 @@ static void clear_and_free_interfaces(void)
        struct ast_member_interfaces *curint;
 
        AST_LIST_LOCK(&interfaces);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&interfaces, curint, list) {
-               AST_LIST_REMOVE_CURRENT(&interfaces, list);
+       while ((curint = AST_LIST_REMOVE_HEAD(&interfaces, list)))
                free(curint);
-       }
-       AST_LIST_TRAVERSE_SAFE_END;
        AST_LIST_UNLOCK(&interfaces);
-
-       return;
 }
 
 /*! \brief Configure a queue parameter.