[asterisk-commits] file: branch 1.2 r61804 - in /branches/1.2: ./
apps/ funcs/ include/asterisk/
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Wed Apr 25 11:52:51 MST 2007
Author: file
Date: Wed Apr 25 13:52:50 2007
New Revision: 61804
URL: http://svn.digium.com/view/asterisk?view=rev&rev=61804
Log:
Merge rewritten group counting support. No more storing data on the variable list of the channels. That was bad, mmmk? (issue #7497 reported by sabbathbh)
Modified:
branches/1.2/app.c
branches/1.2/apps/app_groupcount.c
branches/1.2/channel.c
branches/1.2/funcs/func_groupcount.c
branches/1.2/include/asterisk/app.h
Modified: branches/1.2/app.c
URL: http://svn.digium.com/view/asterisk/branches/1.2/app.c?view=diff&rev=61804&r1=61803&r2=61804
==============================================================================
--- branches/1.2/app.c (original)
+++ branches/1.2/app.c Wed Apr 25 13:52:50 2007
@@ -48,9 +48,11 @@
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/indications.h"
+#include "asterisk/linkedlists.h"
#define MAX_OTHER_FORMATS 10
+static AST_LIST_HEAD_STATIC(groups, ast_group_info);
/* !
This function presents a dialtone and reads an extension into 'collect'
@@ -1023,61 +1025,74 @@
else
res = -1;
- if (cat)
- snprintf(category, category_max, "%s_%s", GROUP_CATEGORY_PREFIX, cat);
- else
- ast_copy_string(category, GROUP_CATEGORY_PREFIX, category_max);
+ if (!ast_strlen_zero(cat))
+ ast_copy_string(category, cat, category_max);
return res;
}
int ast_app_group_set_channel(struct ast_channel *chan, char *data)
{
- int res=0;
- char group[80] = "";
- char category[80] = "";
-
- if (!ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
- pbx_builtin_setvar_helper(chan, category, group);
- } else
+ int res = 0;
+ char group[80] = "", category[80] = "";
+ struct ast_group_info *gi = NULL;
+ size_t len = 0;
+
+ if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category)))
+ return -1;
+
+ /* Calculate memory we will need if this is new */
+ len = sizeof(*gi) + strlen(group) + 1;
+ if (!ast_strlen_zero(category))
+ len += strlen(category) + 1;
+
+ AST_LIST_LOCK(&groups);
+ AST_LIST_TRAVERSE(&groups, gi, list) {
+ if (gi->chan == chan && !strcasecmp(gi->group, group) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))))
+ break;
+ }
+
+ if (!gi && (gi = calloc(1, len))) {
+ gi->chan = chan;
+ gi->group = (char *) gi + sizeof(*gi);
+ strcpy(gi->group, group);
+ if (!ast_strlen_zero(category)) {
+ gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
+ strcpy(gi->category, category);
+ }
+ AST_LIST_INSERT_TAIL(&groups, gi, list);
+ } else {
res = -1;
+ }
+
+ AST_LIST_UNLOCK(&groups);
return res;
}
int ast_app_group_get_count(char *group, char *category)
{
- struct ast_channel *chan;
+ struct ast_group_info *gi = NULL;
int count = 0;
- char *test;
- char cat[80];
- char *s;
if (ast_strlen_zero(group))
return 0;
- s = (!ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
- ast_copy_string(cat, s, sizeof(cat));
-
- chan = NULL;
- while ((chan = ast_channel_walk_locked(chan)) != NULL) {
- test = pbx_builtin_getvar_helper(chan, cat);
- if (test && !strcasecmp(test, group))
- count++;
- ast_mutex_unlock(&chan->lock);
- }
+ AST_LIST_LOCK(&groups);
+ AST_LIST_TRAVERSE(&groups, gi, list) {
+ if (!strcasecmp(gi->group, group) && (ast_strlen_zero(category) || !strcasecmp(gi->category, category)))
+ count++;
+ }
+ AST_LIST_UNLOCK(&groups);
return count;
}
int ast_app_group_match_get_count(char *groupmatch, char *category)
{
+ struct ast_group_info *gi = NULL;
regex_t regexbuf;
- struct ast_channel *chan;
int count = 0;
- char *test;
- char cat[80];
- char *s;
if (ast_strlen_zero(groupmatch))
return 0;
@@ -1086,20 +1101,48 @@
if (regcomp(®exbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
return 0;
- s = (!ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
- ast_copy_string(cat, s, sizeof(cat));
-
- chan = NULL;
- while ((chan = ast_channel_walk_locked(chan)) != NULL) {
- test = pbx_builtin_getvar_helper(chan, cat);
- if (test && !regexec(®exbuf, test, 0, NULL, 0))
+ AST_LIST_LOCK(&groups);
+ AST_LIST_TRAVERSE(&groups, gi, list) {
+ if (!regexec(®exbuf, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || !strcasecmp(gi->category, category)))
count++;
- ast_mutex_unlock(&chan->lock);
- }
+ }
+ AST_LIST_UNLOCK(&groups);
regfree(®exbuf);
return count;
+}
+
+int ast_app_group_discard(struct ast_channel *chan)
+{
+ struct ast_group_info *gi = NULL;
+
+ AST_LIST_LOCK(&groups);
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&groups, gi, list) {
+ if (gi->chan == chan) {
+ AST_LIST_REMOVE_CURRENT(&groups, list);
+ free(gi);
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+ AST_LIST_UNLOCK(&groups);
+
+ return 0;
+}
+
+int ast_app_group_list_lock(void)
+{
+ return AST_LIST_LOCK(&groups);
+}
+
+struct ast_group_info *ast_app_group_list_head(void)
+{
+ return AST_LIST_FIRST(&groups);
+}
+
+int ast_app_group_list_unlock(void)
+{
+ return AST_LIST_UNLOCK(&groups);
}
unsigned int ast_app_separate_args(char *buf, char delim, char **array, int arraylen)
Modified: branches/1.2/apps/app_groupcount.c
URL: http://svn.digium.com/view/asterisk/branches/1.2/apps/app_groupcount.c?view=diff&rev=61804&r1=61803&r2=61804
==============================================================================
--- branches/1.2/apps/app_groupcount.c (original)
+++ branches/1.2/apps/app_groupcount.c Wed Apr 25 13:52:50 2007
@@ -201,10 +201,8 @@
{
#define FORMAT_STRING "%-25s %-20s %-20s\n"
- struct ast_channel *c = NULL;
int numchans = 0;
- struct ast_var_t *current;
- struct varshead *headp;
+ struct ast_group_info *gi = NULL;
regex_t regexbuf;
int havepattern = 0;
@@ -218,25 +216,19 @@
}
ast_cli(fd, FORMAT_STRING, "Channel", "Group", "Category");
- while ( (c = ast_channel_walk_locked(c)) != NULL) {
- headp=&c->varshead;
- AST_LIST_TRAVERSE(headp,current,entries) {
- if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
- if (!havepattern || !regexec(®exbuf, ast_var_value(current), 0, NULL, 0)) {
- ast_cli(fd, FORMAT_STRING, c->name, ast_var_value(current),
- (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
- numchans++;
- }
- } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
- if (!havepattern || !regexec(®exbuf, ast_var_value(current), 0, NULL, 0)) {
- ast_cli(fd, FORMAT_STRING, c->name, ast_var_value(current), "(default)");
- numchans++;
- }
- }
+
+ ast_app_group_list_lock();
+
+ gi = ast_app_group_list_head();
+ while (gi) {
+ if (!havepattern || !regexec(®exbuf, gi->group, 0, NULL, 0)) {
+ ast_cli(fd, FORMAT_STRING, gi->chan->name, gi->group, (ast_strlen_zero(gi->category) ? "(default)" : gi->category));
+ numchans++;
}
- numchans++;
- ast_mutex_unlock(&c->lock);
- }
+ gi = AST_LIST_NEXT(gi, list);
+ }
+
+ ast_app_group_list_unlock();
if (havepattern)
regfree(®exbuf);
Modified: branches/1.2/channel.c
URL: http://svn.digium.com/view/asterisk/branches/1.2/channel.c?view=diff&rev=61804&r1=61803&r2=61804
==============================================================================
--- branches/1.2/channel.c (original)
+++ branches/1.2/channel.c Wed Apr 25 13:52:50 2007
@@ -953,6 +953,9 @@
while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
ast_var_delete(vardata);
+
+ /* Drop out of the group counting radar */
+ ast_app_group_discard(chan);
free(chan);
ast_mutex_unlock(&chlock);
@@ -2920,22 +2923,6 @@
static void clone_variables(struct ast_channel *original, struct ast_channel *clone)
{
- struct ast_var_t *varptr;
-
- /* we need to remove all app_groupcount related variables from the original
- channel before merging in the clone's variables; any groups assigned to the
- original channel should be released, only those assigned to the clone
- should remain
- */
-
- AST_LIST_TRAVERSE_SAFE_BEGIN(&original->varshead, varptr, entries) {
- if (!strncmp(ast_var_name(varptr), GROUP_CATEGORY_PREFIX, strlen(GROUP_CATEGORY_PREFIX))) {
- AST_LIST_REMOVE_CURRENT(&original->varshead, entries);
- ast_var_delete(varptr);
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
-
/* Append variables from clone channel into original channel */
/* XXX Is this always correct? We have to in order to keep MACROS working XXX */
if (AST_LIST_FIRST(&clone->varshead))
@@ -3118,6 +3105,8 @@
for (x = 0; x < AST_MAX_FDS; x++) {
original->fds[x] = clone->fds[x];
}
+ /* Drop group from original */
+ ast_app_group_discard(original);
clone_variables(original, clone);
AST_LIST_HEAD_INIT_NOLOCK(&clone->varshead);
/* Presense of ADSI capable CPE follows clone */
Modified: branches/1.2/funcs/func_groupcount.c
URL: http://svn.digium.com/view/asterisk/branches/1.2/funcs/func_groupcount.c?view=diff&rev=61804&r1=61803&r2=61804
==============================================================================
--- branches/1.2/funcs/func_groupcount.c (original)
+++ branches/1.2/funcs/func_groupcount.c Wed Apr 25 13:52:50 2007
@@ -37,22 +37,15 @@
static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
- int count;
- char group[80] = "";
- char category[80] = "";
- char *grp;
+ int count = -1;
+ char group[80] = "", category[80] = "";
ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
- if (ast_strlen_zero(group)) {
- if ((grp = pbx_builtin_getvar_helper(chan, category)))
- ast_copy_string(group, grp, sizeof(group));
- else
- ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
- }
-
- count = ast_app_group_get_count(group, category);
- snprintf(buf, len, "%d", count);
+ if ((count = ast_app_group_get_count(group, category)) == -1)
+ ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
+ else
+ snprintf(buf, len, "%d", count);
return buf;
}
@@ -100,18 +93,25 @@
static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
- char varname[256];
- char *group;
+ struct ast_group_info *gi = NULL;
- if (!ast_strlen_zero(data)) {
- snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
- } else {
- ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
- }
+ ast_app_group_list_lock();
- group = pbx_builtin_getvar_helper(chan, varname);
- if (group)
- ast_copy_string(buf, group, len);
+ gi = ast_app_group_list_head();
+ while (gi) {
+ if (gi->chan != chan)
+ continue;
+ if (ast_strlen_zero(data))
+ break;
+ if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
+ break;
+ gi = AST_LIST_NEXT(gi, list);
+ }
+
+ if (gi)
+ ast_copy_string(buf, gi->group, len);
+
+ ast_app_group_list_unlock();
return buf;
}
@@ -144,33 +144,35 @@
static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
- struct ast_var_t *current;
- struct varshead *headp;
- char tmp1[1024] = "";
- char tmp2[1024] = "";
+ struct ast_group_info *gi = NULL;
+ char tmp1[1024] = "";
+ char tmp2[1024] = "";
- if (!chan)
- return "";
+ ast_app_group_list_lock();
- headp=&chan->varshead;
- AST_LIST_TRAVERSE(headp,current,entries) {
- if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
- if (!ast_strlen_zero(tmp1)) {
- ast_copy_string(tmp2, tmp1, sizeof(tmp2));
- snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
- } else {
- snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
- }
- } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
- if (!ast_strlen_zero(tmp1)) {
- ast_copy_string(tmp2, tmp1, sizeof(tmp2));
- snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
- } else {
- snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
- }
- }
- }
- ast_copy_string(buf, tmp1, len);
+ gi = ast_app_group_list_head();
+ while (gi) {
+ if (gi->chan != chan)
+ continue;
+ if (!ast_strlen_zero(tmp1)) {
+ ast_copy_string(tmp2, tmp1, sizeof(tmp2));
+ if (!ast_strlen_zero(gi->category))
+ snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
+ else
+ snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
+ } else {
+ if (!ast_strlen_zero(gi->category))
+ snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
+ else
+ snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
+ }
+ gi = AST_LIST_NEXT(gi, list);
+ }
+
+ ast_app_group_list_unlock();
+
+ ast_copy_string(buf, tmp1, len);
+
return buf;
}
Modified: branches/1.2/include/asterisk/app.h
URL: http://svn.digium.com/view/asterisk/branches/1.2/include/asterisk/app.h?view=diff&rev=61804&r1=61803&r2=61804
==============================================================================
--- branches/1.2/include/asterisk/app.h (original)
+++ branches/1.2/include/asterisk/app.h Wed Apr 25 13:52:50 2007
@@ -166,7 +166,12 @@
/*! Read a file into asterisk*/
char *ast_read_textfile(const char *file);
-#define GROUP_CATEGORY_PREFIX "GROUP"
+struct ast_group_info {
+ struct ast_channel *chan;
+ char *category;
+ char *group;
+ AST_LIST_ENTRY(ast_group_info) list;
+};
/*! Split a group string into group and category, returning a default category if none is provided. */
int ast_app_group_split_group(char *data, char *group, int group_max, char *category, int category_max);
@@ -179,6 +184,18 @@
/*! Get the current channel count of all groups that match the specified pattern and category. */
int ast_app_group_match_get_count(char *groupmatch, char *category);
+
+/*! Discard all group counting for a channel */
+int ast_app_group_discard(struct ast_channel *chan);
+
+/*! Lock the group count list */
+int ast_app_group_list_lock(void);
+
+/*! Get the head of the group count list */
+struct ast_group_info *ast_app_group_list_head(void);
+
+/*! Unlock the group count list */
+int ast_app_group_list_unlock(void);
/*!
\brief Define an application argument
More information about the asterisk-commits
mailing list