add API for searching active channels by context/exten

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6619 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming
2005-09-15 16:34:39 +00:00
parent b8f23573fa
commit 29bdd1b424
2 changed files with 45 additions and 15 deletions

View File

@@ -660,8 +660,15 @@ void ast_channel_undefer_dtmf(struct ast_channel *chan)
} }
/* /*
* Helper function to return the channel after prev, or the one matching name, * Helper function to find channels. It supports these modes:
* with the channel's lock held. If getting the individual lock fails, *
* prev != NULL : get channel next in list after prev
* name != NULL : get channel with matching name
* name != NULL && namelen != 0 : get channel whose name starts with prefix
* exten != NULL : get channel whose exten or macroexten matches
* context != NULL && exten != NULL : get channel whose context or macrocontext
*
* It returns with the channel's lock held. If getting the individual lock fails,
* unlock and retry quickly up to 10 times, then give up. * unlock and retry quickly up to 10 times, then give up.
* *
* XXX Note that this code has cost O(N) because of the need to verify * XXX Note that this code has cost O(N) because of the need to verify
@@ -675,7 +682,8 @@ void ast_channel_undefer_dtmf(struct ast_channel *chan)
* We should definitely go for a better scheme that is deadlock-free. * We should definitely go for a better scheme that is deadlock-free.
*/ */
static struct ast_channel *channel_find_locked(const struct ast_channel *prev, static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
const char *name, const int namelen) const char *name, const int namelen,
const char *context, const char *exten)
{ {
const char *msg = prev ? "deadlock" : "initial deadlock"; const char *msg = prev ? "deadlock" : "initial deadlock";
int retries, done; int retries, done;
@@ -684,10 +692,11 @@ static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
for (retries = 0; retries < 10; retries++) { for (retries = 0; retries < 10; retries++) {
ast_mutex_lock(&chlock); ast_mutex_lock(&chlock);
for (c = channels; c; c = c->next) { for (c = channels; c; c = c->next) {
if (prev == NULL) { if (!prev) {
/* want head of list */ /* want head of list */
if (!name) if (!name && !exten)
break; break;
if (name) {
/* want match by full name */ /* want match by full name */
if (!namelen) { if (!namelen) {
if (!strcasecmp(c->name, name)) if (!strcasecmp(c->name, name))
@@ -698,6 +707,18 @@ static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
/* want match by name prefix */ /* want match by name prefix */
if (!strncasecmp(c->name, name, namelen)) if (!strncasecmp(c->name, name, namelen))
break; break;
} else if (exten) {
/* want match by context and exten */
if (context && (strcasecmp(c->context, context) &&
strcasecmp(c->macrocontext, context)))
continue;
/* match by exten */
if (strcasecmp(c->exten, exten) &&
strcasecmp(c->macroexten, exten))
continue;
else
break;
}
} else if (c == prev) { /* found, return c->next */ } else if (c == prev) { /* found, return c->next */
c = c->next; c = c->next;
break; break;
@@ -726,19 +747,25 @@ static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
/*--- ast_channel_walk_locked: Browse channels in use */ /*--- ast_channel_walk_locked: Browse channels in use */
struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev) struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev)
{ {
return channel_find_locked(prev, NULL, 0); return channel_find_locked(prev, NULL, 0, NULL, NULL);
} }
/*--- ast_get_channel_by_name_locked: Get channel by name and lock it */ /*--- ast_get_channel_by_name_locked: Get channel by name and lock it */
struct ast_channel *ast_get_channel_by_name_locked(const char *name) struct ast_channel *ast_get_channel_by_name_locked(const char *name)
{ {
return channel_find_locked(NULL, name, 0); return channel_find_locked(NULL, name, 0, NULL, NULL);
} }
/*--- ast_get_channel_by_name_prefix_locked: Get channel by name prefix and lock it */ /*--- ast_get_channel_by_name_prefix_locked: Get channel by name prefix and lock it */
struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen) struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen)
{ {
return channel_find_locked(NULL, name, namelen); return channel_find_locked(NULL, name, namelen, NULL, NULL);
}
/*--- ast_get_channel_by_exten_locked: Get channel by exten (and optionally context) and lock it */
struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context)
{
return channel_find_locked(NULL, NULL, 0, context, exten);
} }
/*--- ast_safe_sleep_conditional: Wait, look for hangups and condition arg */ /*--- ast_safe_sleep_conditional: Wait, look for hangups and condition arg */

View File

@@ -754,6 +754,9 @@ struct ast_channel *ast_get_channel_by_name_locked(const char *chan);
/*! Get channel by name prefix (locks channel) */ /*! Get channel by name prefix (locks channel) */
struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen); struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen);
/*--- ast_get_channel_by_exten_locked: Get channel by exten (and optionally context) and lock it */
struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context);
/*! Waits for a digit */ /*! Waits for a digit */
/*! /*!
* \param c channel to wait for a digit on * \param c channel to wait for a digit on