Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2006, Digium, Inc.
* Mark Spencer <markster@digium.com>
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
* \brief True call queues with optional send URL on answer
* \author Mark Spencer <markster@digium.com>
*
* \note 2004-11-25: Persistent Dynamic Members added by:
* NetNation Communications (www.netnation.com)
* Kevin Lindsay <kevinl@netnation.com>
* Each dynamic agent in each queue is now stored in the astdb.
* When asterisk is restarted, each agent will be automatically
* readded into their recorded queues. This feature can be
Mark Spencer
committed
* configured with the 'persistent_members=<1|0>' setting in the
* '[general]' category in queues.conf. The default is on.
* \note 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
* \note These features added by David C. Troy <dave@toad.net>:
* - Per-queue holdtime calculation
* - Estimated holdtime announcement
* - Position announcement
* - Abandoned/completed call counters
* - Failout timer passed as optional app parameter
* - Optional monitoring of calls, started when call is answered
*
* Patch Version 1.07 2003-12-24 01
*
* Added servicelevel statistic by Michiel Betel <michiel@betel.nl>
James Golovich
committed
* Added Priority jumping code for adding and removing queue members by Jonathan Stanton <asterisk@doilooklikeicare.com>
* Fixed to work with CVS as of 2004-02-25 and released as 1.07a
* by Matthew Enger <m.enger@xi.com.au>
*
/*** MODULEINFO
<depend>res_monitor</depend>
***/
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <sys/time.h>
#include <sys/signal.h>
#include <netinet/in.h>
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/linkedlists.h"
Kevin P. Fleming
committed
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/say.h"
#include "asterisk/features.h"
#include "asterisk/musiconhold.h"
#include "asterisk/cli.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
#include "asterisk/monitor.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/astdb.h"
Kevin P. Fleming
committed
#include "asterisk/devicestate.h"
#include "asterisk/stringfields.h"
Russell Bryant
committed
#include "asterisk/event.h"
#include "asterisk/strings.h"
#include "asterisk/global_datastores.h"
Russell Bryant
committed
enum {
QUEUE_STRATEGY_RINGALL = 0,
QUEUE_STRATEGY_LEASTRECENT,
QUEUE_STRATEGY_FEWESTCALLS,
QUEUE_STRATEGY_RANDOM,
Mark Michelson
committed
QUEUE_STRATEGY_RRMEMORY,
QUEUE_STRATEGY_LINEAR,
QUEUE_STRATEGY_WRANDOM
Russell Bryant
committed
};
static struct strategy {
int strategy;
char *name;
} strategies[] = {
{ QUEUE_STRATEGY_RINGALL, "ringall" },
{ QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
{ QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
{ QUEUE_STRATEGY_RANDOM, "random" },
{ QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
Mark Michelson
committed
{ QUEUE_STRATEGY_LINEAR, "linear" },
{ QUEUE_STRATEGY_WRANDOM, "wrandom"},
};
#define DEFAULT_RETRY 5
#define DEFAULT_TIMEOUT 15
#define RECHECK 1 /* Recheck every second to see we we're at the top yet */
#define MAX_PERIODIC_ANNOUNCEMENTS 10 /* The maximum periodic announcements we can have */
Russell Bryant
committed
#define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15 /* The minimum number of seconds between position announcements
The default value of 15 provides backwards compatibility */
Mark Michelson
committed
#define MAX_QUEUE_BUCKETS 53
#define RES_OKAY 0 /* Action completed */
#define RES_EXISTS (-1) /* Entry already exists */
#define RES_OUTOFMEMORY (-2) /* Out of memory */
#define RES_NOSUCHQUEUE (-3) /* No such queue */
#define RES_NOT_DYNAMIC (-4) /* Member is not dynamic */
static char *app = "Queue";
static char *synopsis = "Queue a call for a call queue";
static char *descrip =
Tilghman Lesher
committed
" Queue(queuename[,options[,URL][,announceoverride][,timeout][,AGI][,macro][,gosub]):\n"
"Queues an incoming call in a particular call queue as defined in queues.conf.\n"
"This application will return to the dialplan if the queue does not exist, or\n"
"any of the join options cause the caller to not enter the queue.\n"
"The option string may contain zero or more of the following characters:\n"
Jason Parker
committed
" 'c' -- continue in the dialplan if the callee hangs up.\n"
" 'd' -- data-quality (modem) call (minimum delay).\n"
Joshua Colp
committed
" 'h' -- allow callee to hang up by pressing *.\n"
" 'H' -- allow caller to hang up by pressing *.\n"
" 'n' -- no retries on the timeout; will exit this application and \n"
" 'i' -- ignore call forward requests from queue members and do nothing\n"
" when they are requested.\n"
Steve Murphy
committed
" 'r' -- ring instead of playing MOH. Periodic Announcements are still made, if applicable.\n"
Joshua Colp
committed
" 't' -- allow the called user to transfer the calling user.\n"
" 'T' -- allow the calling user to transfer the call.\n"
" 'w' -- allow the called user to write the conversation to disk via Monitor.\n"
" 'W' -- allow the calling user to write the conversation to disk via Monitor.\n"
" 'k' -- Allow the called party to enable parking of the call by sending\n"
" the DTMF sequence defined for call parking in features.conf.\n"
" 'K' -- Allow the calling party to enable parking of the call by sending\n"
" the DTMF sequence defined for call parking in features.conf.\n"
" 'x' -- allow the called user to write the conversation to disk via MixMonitor\n"
" 'X' -- allow the calling user to write the conversation to disk via MixMonitor\n"
" In addition to transferring the call, a call may be parked and then picked\n"
" The optional URL will be sent to the called party if the channel supports\n"
" The optional AGI parameter will setup an AGI script to be executed on the \n"
"calling party's channel once they are connected to a queue member.\n"
" The optional macro parameter will run a macro on the \n"
"calling party's channel once they are connected to a queue member.\n"
Steve Murphy
committed
" The optional gosub parameter will run a gosub on the \n"
"calling party's channel once they are connected to a queue member.\n"
" The timeout will cause the queue to fail out after a specified number of\n"
Mark Spencer
committed
"seconds, checked between each queues.conf 'timeout' and 'retry' cycle.\n"
" This application sets the following channel variable upon completion:\n"
" QUEUESTATUS The status of the call as a text string, one of\n"
Jason Parker
committed
" TIMEOUT | FULL | JOINEMPTY | LEAVEEMPTY | JOINUNAVAIL | LEAVEUNAVAIL | CONTINUE\n";
static char *app_aqm = "AddQueueMember" ;
static char *app_aqm_synopsis = "Dynamically adds queue members" ;
static char *app_aqm_descrip =
Tilghman Lesher
committed
" AddQueueMember(queuename[,interface[,penalty[,options[,membername]]]]):\n"
James Golovich
committed
"Dynamically adds interface to an existing queue.\n"
Russell Bryant
committed
"If the interface is already in the queue it will return an error.\n"
" This application sets the following channel variable upon completion:\n"
" AQMSTATUS The status of the attempt to add a queue member as a \n"
" text string, one of\n"
" ADDED | MEMBERALREADY | NOSUCHQUEUE \n"
Tilghman Lesher
committed
"Example: AddQueueMember(techsupport,SIP/3000)\n"
"";
static char *app_rqm = "RemoveQueueMember" ;
static char *app_rqm_synopsis = "Dynamically removes queue members" ;
static char *app_rqm_descrip =
Tilghman Lesher
committed
" RemoveQueueMember(queuename[,interface[,options]]):\n"
"Dynamically removes interface to an existing queue\n"
Russell Bryant
committed
"If the interface is NOT in the queue it will return an error.\n"
" This application sets the following channel variable upon completion:\n"
" RQMSTATUS The status of the attempt to remove a queue member as a\n"
" text string, one of\n"
" REMOVED | NOTINQUEUE | NOSUCHQUEUE \n"
Tilghman Lesher
committed
"Example: RemoveQueueMember(techsupport,SIP/3000)\n"
static char *app_pqm = "PauseQueueMember" ;
static char *app_pqm_synopsis = "Pauses a queue member" ;
static char *app_pqm_descrip =
" PauseQueueMember([queuename],interface[,options[,reason]]):\n"
"Pauses (blocks calls for) a queue member.\n"
"The given interface will be paused in the given queue. This prevents\n"
"any calls from being sent from the queue to the interface until it is\n"
"unpaused with UnpauseQueueMember or the manager interface. If no\n"
"queuename is given, the interface is paused in every queue it is a\n"
Russell Bryant
committed
"member of. The application will fail if the interface is not found.\n"
"The reason string is entirely optional and is used to add extra information\n"
"to the appropriate queue_log entries and manager events.\n"
" This application sets the following channel variable upon completion:\n"
" PQMSTATUS The status of the attempt to pause a queue member as a\n"
" text string, one of\n"
" PAUSED | NOTFOUND\n"
Tilghman Lesher
committed
"Example: PauseQueueMember(,SIP/3000)\n";
static char *app_upqm = "UnpauseQueueMember" ;
static char *app_upqm_synopsis = "Unpauses a queue member" ;
static char *app_upqm_descrip =
" UnpauseQueueMember([queuename],interface[,options[,reason]]):\n"
"Unpauses (resumes calls to) a queue member.\n"
"This is the counterpart to PauseQueueMember and operates exactly the\n"
"same way, except it unpauses instead of pausing the given interface.\n"
"The reason string is entirely optional and is used to add extra information\n"
"to the appropriate queue_log entries and manager events.\n"
" This application sets the following channel variable upon completion:\n"
" UPQMSTATUS The status of the attempt to unpause a queue \n"
" member as a text string, one of\n"
" UNPAUSED | NOTFOUND\n"
Tilghman Lesher
committed
"Example: UnpauseQueueMember(,SIP/3000)\n";
static char *app_ql = "QueueLog" ;
static char *app_ql_synopsis = "Writes to the queue_log" ;
static char *app_ql_descrip =
Tilghman Lesher
committed
" QueueLog(queuename,uniqueid,agent,event[,additionalinfo]):\n"
"Allows you to write your own events into the queue log\n"
Tilghman Lesher
committed
"Example: QueueLog(101,${UNIQUEID},${AGENT},WENTONBREAK,600)\n";
static const char *pm_family = "Queue/PersistentMembers";
/* The maximum length of each persistent member queue database entry */
BJ Weschke
committed
#define PM_MAX_LEN 8192
Mark Spencer
committed
/*! \brief queues.conf [general] option */
static int queue_keep_stats = 0;
static int queue_persistent_members = 0;
Mark Spencer
committed
/*! \brief queues.conf [general] option */
static int autofill_default = 0;
/*! \brief queues.conf [general] option */
static int montype_default = 0;
Mark Michelson
committed
/*! \brief queues.conf [general] option */
static int shared_lastcall = 0;
Russell Bryant
committed
/*! \brief Subscription to device state change events */
static struct ast_event_sub *device_state_sub;
/*! \brief queues.conf [general] option */
static int update_cdr = 0;
Mark Spencer
committed
enum queue_result {
QUEUE_UNKNOWN = 0,
QUEUE_TIMEOUT = 1,
QUEUE_JOINEMPTY = 2,
QUEUE_LEAVEEMPTY = 3,
QUEUE_JOINUNAVAIL = 4,
QUEUE_LEAVEUNAVAIL = 5,
QUEUE_FULL = 6,
Jason Parker
committed
QUEUE_CONTINUE = 7,
Mark Spencer
committed
};
Mark Spencer
committed
enum queue_result id;
char *text;
} queue_results[] = {
{ QUEUE_UNKNOWN, "UNKNOWN" },
{ QUEUE_TIMEOUT, "TIMEOUT" },
{ QUEUE_JOINEMPTY,"JOINEMPTY" },
{ QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
{ QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
{ QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
{ QUEUE_FULL, "FULL" },
Jason Parker
committed
{ QUEUE_CONTINUE, "CONTINUE" },
Mark Spencer
committed
};
/*! \brief We define a custom "local user" structure because we
use it not only for keeping track of what is in use but
also for keeping track of who we're dialing. */
struct callattempt {
struct callattempt *q_next;
char interface[256];
Mark Spencer
committed
int oldstatus;
Mark Michelson
committed
struct call_queue *lastqueue;
struct member *member;
struct call_queue *parent; /*!< What queue is our parent */
char moh[80]; /*!< Name of musiconhold to be used */
char announce[80]; /*!< Announcement to play for member when call is answered */
char context[AST_MAX_CONTEXT]; /*!< Context when user exits queue */
char digits[AST_MAX_EXTENSION]; /*!< Digits entered while in queue */
int valid_digits; /*!< Digits entered correspond to valid extension. Exited */
int pos; /*!< Where we are in the queue */
int prio; /*!< Our priority */
int last_pos_said; /*!< Last position we told the user */
time_t last_periodic_announce_time; /*!< The last time we played a periodic announcement */
int last_periodic_announce_sound; /*!< The last periodic announcement we made */
time_t last_pos; /*!< Last time we told the user their position */
int opos; /*!< Where we started in the queue */
int handled; /*!< Whether our call was handled */
int pending; /*!< Non-zero if we are attempting to call a member */
int max_penalty; /*!< Limit the members that can take this call to this penalty or lower */
Mark Michelson
committed
int linpos; /*!< If using linear strategy, what position are we at? */
int linwrapped; /*!< Is the linpos wrapped? */
time_t start; /*!< When we started holding */
time_t expire; /*!< When this entry should expire (time out of queue) */
struct ast_channel *chan; /*!< Our channel */
struct queue_ent *next; /*!< The next queue entry */
char membername[80]; /*!< Member name to use in queue logs */
int penalty; /*!< Are we a last resort? */
int calls; /*!< Number of calls serviced by this member */
int dynamic; /*!< Are we dynamically added? */
int realtime; /*!< Is this member realtime? */
int status; /*!< Status of queue member */
int paused; /*!< Are we paused (not accepting calls)? */
time_t lastcall; /*!< When last successful call was hungup */
Mark Michelson
committed
struct call_queue *lastqueue; /*!< Last queue we received a call */
unsigned int dead:1; /*!< Used to detect members deleted in realtime */
unsigned int delme:1; /*!< Flag to delete entry on reload */
BJ Weschke
committed
char interface[80];
AST_LIST_ENTRY(member_interface) list; /*!< Next call queue */
BJ Weschke
committed
};
static AST_LIST_HEAD_STATIC(interfaces, member_interface);
BJ Weschke
committed
/* values used in multi-bit flags in call_queue */
Mark Spencer
committed
#define QUEUE_EMPTY_NORMAL 1
#define QUEUE_EMPTY_STRICT 2
#define QUEUE_EMPTY_LOOSE 3
Mark Spencer
committed
#define ANNOUNCEHOLDTIME_ALWAYS 1
#define ANNOUNCEHOLDTIME_ONCE 2
#define QUEUE_EVENT_VARIABLES 3
Mark Spencer
committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
AST_DECLARE_STRING_FIELDS(
/*! Queue name */
AST_STRING_FIELD(name);
/*! Music on Hold class */
AST_STRING_FIELD(moh);
/*! Announcement to play when call is answered */
AST_STRING_FIELD(announce);
/*! Exit context */
AST_STRING_FIELD(context);
/*! Macro to run upon member connection */
AST_STRING_FIELD(membermacro);
/*! Gosub to run upon member connection */
AST_STRING_FIELD(membergosub);
/*! Sound file: "Your call is now first in line" (def. queue-youarenext) */
AST_STRING_FIELD(sound_next);
/*! Sound file: "There are currently" (def. queue-thereare) */
AST_STRING_FIELD(sound_thereare);
/*! Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting) */
AST_STRING_FIELD(sound_calls);
/*! Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
AST_STRING_FIELD(sound_holdtime);
/*! Sound file: "minutes." (def. queue-minutes) */
AST_STRING_FIELD(sound_minutes);
/*! Sound file: "less-than" (def. queue-lessthan) */
AST_STRING_FIELD(sound_lessthan);
/*! Sound file: "seconds." (def. queue-seconds) */
AST_STRING_FIELD(sound_seconds);
/*! Sound file: "Thank you for your patience." (def. queue-thankyou) */
AST_STRING_FIELD(sound_thanks);
/*! Sound file: Custom announce for caller, no default */
AST_STRING_FIELD(sound_callerannounce);
/*! Sound file: "Hold time" (def. queue-reporthold) */
AST_STRING_FIELD(sound_reporthold);
);
/*! Sound files: Custom announce, no default */
struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
unsigned int dead:1;
unsigned int joinempty:2;
unsigned int eventwhencalled:2;
unsigned int leavewhenempty:2;
Kevin P. Fleming
committed
unsigned int ringinuse:1;
unsigned int setinterfacevar:1;
unsigned int setqueuevar:1;
unsigned int setqueueentryvar:1;
unsigned int reportholdtime:1;
unsigned int wrapped:1;
unsigned int timeoutrestart:1;
unsigned int announceholdtime:2;
unsigned int announceposition:1;
unsigned int maskmemberstatus:1;
unsigned int realtime:1;
int announcefrequency; /*!< How often to announce their position */
Russell Bryant
committed
int minannouncefrequency; /*!< The minimum number of seconds between position announcements (def. 15) */
int periodicannouncefrequency; /*!< How often to play periodic announcement */
int roundingseconds; /*!< How many seconds do we round to? */
int holdtime; /*!< Current avg holdtime, based on recursive boxcar filter */
int callscompleted; /*!< Number of queue calls completed */
int callsabandoned; /*!< Number of queue calls abandoned */
int servicelevel; /*!< seconds setting for servicelevel*/
int callscompletedinsl; /*!< Number of calls answered with servicelevel*/
char monfmt[8]; /*!< Format to use when recording calls */
int montype; /*!< Monitor type Monitor vs. MixMonitor */
int count; /*!< How many entries */
int maxlen; /*!< Max number of entries */
int wrapuptime; /*!< Wrapup Time */
int retry; /*!< Retry calling everyone after this amount of time */
int timeout; /*!< How long to wait for an answer */
int weight; /*!< Respective weight */
int autopause; /*!< Auto pause queue members if they fail to answer */
/* Queue strategy things */
int rrpos; /*!< Round Robin - position */
int memberdelay; /*!< Seconds to delay connecting member to caller */
int autofill; /*!< Ignore the head call status and ring an available agent */
struct ao2_container *members; /*!< Head of the list of members */
/*!
* \brief Number of members _logged in_
* \note There will be members in the members container that are not logged
* in, so this can not simply be replaced with ao2_container_count().
*/
int membercount;
struct queue_ent *head; /*!< Head of the list of callers */
AST_LIST_ENTRY(call_queue) list; /*!< Next call queue */
Mark Michelson
committed
static struct ao2_container *queues;
static void update_realtime_members(struct call_queue *q);
static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
Mark Spencer
committed
static void set_queue_result(struct ast_channel *chan, enum queue_result res)
{
int i;
for (i = 0; i < sizeof(queue_results) / sizeof(queue_results[0]); i++) {
if (queue_results[i].id == res) {
pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
return;
}
}
}
static char *int2strat(int strategy)
{
int x;
for (x = 0; x < sizeof(strategies) / sizeof(strategies[0]); x++) {
if (strategy == strategies[x].strategy)
return strategies[x].name;
}
return "<unknown>";
}
static int strat2int(const char *strategy)
{
int x;
for (x = 0; x < sizeof(strategies) / sizeof(strategies[0]); x++) {
if (!strcasecmp(strategy, strategies[x].name))
return strategies[x].strategy;
}
return -1;
}
Mark Michelson
committed
static int queue_hash_cb(const void *obj, const int flags)
{
const struct call_queue *q = obj;
return ast_str_hash(q->name);
}
static int queue_cmp_cb(void *obj, void *arg, int flags)
{
struct call_queue *q = obj, *q2 = arg;
return !strcasecmp(q->name, q2->name) ? CMP_MATCH : 0;
}
static inline struct call_queue *queue_ref(struct call_queue *q)
{
ao2_ref(q, 1);
return q;
}
static inline struct call_queue *queue_unref(struct call_queue *q)
{
ao2_ref(q, -1);
return q;
}
static void set_queue_variables(struct queue_ent *qe)
{
char interfacevar[256]="";
float sl = 0;
if (qe->parent->setqueuevar) {
sl = 0;
if (qe->parent->callscompleted > 0)
sl = 100 * ((float) qe->parent->callscompletedinsl / (float) qe->parent->callscompleted);
snprintf(interfacevar,sizeof(interfacevar),
"QUEUEMAX=%d|QUEUESTRATEGY=%s|QUEUECALLS=%d|QUEUEHOLDTIME=%d|QUEUECOMPLETED=%d|QUEUEABANDONED=%d|QUEUESRVLEVEL=%d|QUEUESRVLEVELPERF=%2.1f",
qe->parent->maxlen, int2strat(qe->parent->strategy), qe->parent->count, qe->parent->holdtime, qe->parent->callscompleted,
qe->parent->callsabandoned, qe->parent->servicelevel, sl);
pbx_builtin_setvar(qe->chan, interfacevar);
}
}
/*! \brief Insert the 'new' entry after the 'prev' entry of queue 'q' */
static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
{
struct queue_ent *cur;
if (!q || !new)
return;
if (prev) {
cur = prev->next;
prev->next = new;
} else {
cur = q->head;
q->head = new;
}
new->next = cur;
new->parent = q;
new->pos = ++(*pos);
new->opos = *pos;
}
Mark Spencer
committed
enum queue_member_status {
QUEUE_NO_MEMBERS,
QUEUE_NO_REACHABLE_MEMBERS,
QUEUE_NO_UNPAUSED_REACHABLE_MEMBERS,
Mark Spencer
committed
QUEUE_NORMAL
};
static enum queue_member_status get_member_status(struct call_queue *q, int max_penalty)
Mark Spencer
committed
{
struct member *member;
Mark Spencer
committed
enum queue_member_status result = QUEUE_NO_MEMBERS;
Mark Michelson
committed
ao2_lock(q);
mem_iter = ao2_iterator_init(q->members, 0);
Russell Bryant
committed
for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
if (max_penalty && (member->penalty > max_penalty))
Kevin P. Fleming
committed
continue;
Mark Spencer
committed
switch (member->status) {
case AST_DEVICE_INVALID:
Mark Spencer
committed
/* nothing to do */
break;
case AST_DEVICE_UNAVAILABLE:
if (result != QUEUE_NO_UNPAUSED_REACHABLE_MEMBERS)
result = QUEUE_NO_REACHABLE_MEMBERS;
Mark Spencer
committed
break;
default:
if (member->paused) {
result = QUEUE_NO_UNPAUSED_REACHABLE_MEMBERS;
} else {
Mark Michelson
committed
ao2_unlock(q);
return QUEUE_NORMAL;
}
break;
Mark Spencer
committed
}
}
Mark Michelson
committed
ao2_unlock(q);
Mark Spencer
committed
return result;
Mark Spencer
committed
}
Russell Bryant
committed
AST_LIST_ENTRY(statechange) entry;
Russell Bryant
committed
static void *handle_statechange(struct statechange *sc)
struct member_interface *curint;
Mark Michelson
committed
struct ao2_iterator queue_iter;
technology = ast_strdupa(sc->dev);
loc = strchr(technology, '/');
BJ Weschke
committed
AST_LIST_LOCK(&interfaces);
AST_LIST_TRAVERSE(&interfaces, curint, list) {
char *interface;
char *slash_pos;
interface = ast_strdupa(curint->interface);
if ((slash_pos = strchr(interface, '/')))
if ((slash_pos = strchr(slash_pos + 1, '/')))
*slash_pos = '\0';
if (!strcasecmp(interface, sc->dev))
BJ Weschke
committed
}
AST_LIST_UNLOCK(&interfaces);
ast_debug(3, "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_debug(1, "Device '%s/%s' changed to state '%d' (%s)\n", technology, loc, sc->state, devstate2str(sc->state));
Mark Michelson
committed
queue_iter = ao2_iterator_init(queues, 0);
while ((q = ao2_iterator_next(&queue_iter))) {
ao2_lock(q);
mem_iter = ao2_iterator_init(q->members, 0);
while ((cur = ao2_iterator_next(&mem_iter))) {
char *interface;
char *slash_pos;
interface = ast_strdupa(cur->interface);
if ((slash_pos = strchr(interface, '/')))
if ((slash_pos = strchr(slash_pos + 1, '/')))
*slash_pos = '\0';
if (strcasecmp(sc->dev, interface)) {
ao2_ref(cur, -1);
if (cur->status != sc->state) {
cur->status = sc->state;
if (q->maskmemberstatus) {
ao2_ref(cur, -1);
manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
"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->membername, cur->dynamic ? "dynamic" : cur->realtime ? "realtime" : "static",
cur->penalty, cur->calls, (int)cur->lastcall, cur->status, cur->paused);
Mark Michelson
committed
queue_unref(q);
ao2_unlock(q);
Russell Bryant
committed
/*!
* \brief Data used by the device state thread
*/
static struct {
/*! Set to 1 to stop the thread */
unsigned int stop:1;
/*! The device state monitoring thread */
pthread_t thread;
/*! Lock for the state change queue */
ast_mutex_t lock;
/*! Condition for the state change queue */
ast_cond_t cond;
/*! Queue of state changes */
AST_LIST_HEAD_NOLOCK(, statechange) state_change_q;
} device_state = {
.thread = AST_PTHREADT_NULL,
};
static void *device_state_thread(void *data)
{
struct statechange *sc = NULL;
Russell Bryant
committed
while (!device_state.stop) {
ast_mutex_lock(&device_state.lock);
if (!(sc = AST_LIST_REMOVE_HEAD(&device_state.state_change_q, entry))) {
ast_cond_wait(&device_state.cond, &device_state.lock);
sc = AST_LIST_REMOVE_HEAD(&device_state.state_change_q, entry);
}
Russell Bryant
committed
ast_mutex_unlock(&device_state.lock);
/* Check to see if we were woken up to see the request to stop */
if (device_state.stop)
Russell Bryant
committed
if (!sc)
continue;
Russell Bryant
committed
handle_statechange(sc);
Russell Bryant
committed
}
while ((sc = AST_LIST_REMOVE_HEAD(&device_state.state_change_q, entry)))
Russell Bryant
committed
return NULL;
}
Russell Bryant
committed
static int statechange_queue(const char *dev, enum ast_device_state state)
if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1)))
return 0;
sc->state = state;
strcpy(sc->dev, dev);
Russell Bryant
committed
ast_mutex_lock(&device_state.lock);
AST_LIST_INSERT_TAIL(&device_state.state_change_q, sc, entry);
ast_cond_signal(&device_state.cond);
ast_mutex_unlock(&device_state.lock);
Russell Bryant
committed
static void device_state_cb(const struct ast_event *event, void *unused)
{
enum ast_device_state state;
const char *device;
state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
if (ast_strlen_zero(device)) {
ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
return;
}
statechange_queue(device, state);
}
static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused)
{
struct member *cur;
if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
cur->penalty = penalty;
cur->paused = paused;
ast_copy_string(cur->interface, interface, sizeof(cur->interface));
if(!ast_strlen_zero(membername))
ast_copy_string(cur->membername, membername, sizeof(cur->membername));
else
ast_copy_string(cur->membername, interface, sizeof(cur->membername));
if (!strchr(cur->interface, '/'))
ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
cur->status = ast_device_state(interface);
}
return cur;
}
static int compress_char(const char c)
{
if (c < 32)
return 0;
else if (c > 96)
return c - 64;
else
return c - 32;
}
static int member_hash_fn(const void *obj, const int flags)
{
const struct member *mem = obj;
const char *chname = strchr(mem->interface, '/');
int ret = 0, i;
if (!chname)
chname = mem->interface;
for (i = 0; i < 5 && chname[i]; i++)
ret += compress_char(chname[i]) << (i * 6);
return ret;
}
static int member_cmp_fn(void *obj1, void *obj2, int flags)
{
struct member *mem1 = obj1, *mem2 = obj2;
Mark Michelson
committed
return strcasecmp(mem1->interface, mem2->interface) ? 0 : CMP_MATCH;
static void init_queue(struct call_queue *q)
{
q->dead = 0;
q->retry = DEFAULT_RETRY;
q->timeout = -1;
q->maxlen = 0;
q->announcefrequency = 0;
Russell Bryant
committed
q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
q->announceholdtime = 0;
q->announceholdtime = 1;
q->roundingseconds = 0; /* Default - don't announce seconds */
q->servicelevel = 0;
Kevin P. Fleming
committed
q->ringinuse = 1;
q->setinterfacevar = 0;
q->setqueuevar = 0;
q->setqueueentryvar = 0;
q->autofill = autofill_default;
q->montype = montype_default;
q->monfmt[0] = '\0';
q->periodicannouncefrequency = 0;
Mark Michelson
committed
if(!q->members) {
if(q->strategy == QUEUE_STRATEGY_LINEAR)
/* linear strategy depends on order, so we have to place all members in a single bucket */
q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
else
q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
}
ast_string_field_set(q, sound_next, "queue-youarenext");
ast_string_field_set(q, sound_thereare, "queue-thereare");
ast_string_field_set(q, sound_calls, "queue-callswaiting");
ast_string_field_set(q, sound_holdtime, "queue-holdtime");
ast_string_field_set(q, sound_minutes, "queue-minutes");
ast_string_field_set(q, sound_seconds, "queue-seconds");
ast_string_field_set(q, sound_thanks, "queue-thankyou");
ast_string_field_set(q, sound_lessthan, "queue-less-than");
ast_string_field_set(q, sound_reporthold, "queue-reporthold");
if ((q->sound_periodicannounce[0] = ast_str_create(32)))
ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
if (q->sound_periodicannounce[i])
ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
}
}
static void clear_queue(struct call_queue *q)
{
q->holdtime = 0;
q->callscompleted = 0;
q->callsabandoned = 0;
q->callscompletedinsl = 0;
q->wrapuptime = 0;
}
static int add_to_interfaces(const char *interface)
BJ Weschke
committed
{
struct member_interface *curint;
BJ Weschke
committed
AST_LIST_LOCK(&interfaces);
AST_LIST_TRAVERSE(&interfaces, curint, list) {
if (!strcasecmp(curint->interface, interface))
BJ Weschke
committed
}
if (curint) {
AST_LIST_UNLOCK(&interfaces);
return 0;
}
BJ Weschke
committed
ast_debug(1, "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);
BJ Weschke
committed
}
AST_LIST_UNLOCK(&interfaces);
BJ Weschke
committed
}
static int interface_exists_global(const char *interface)
BJ Weschke
committed
{
Mark Michelson
committed
struct ao2_iterator queue_iter;
BJ Weschke
committed
int ret = 0;
ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
Mark Michelson
committed
queue_iter = ao2_iterator_init(queues, 0);
while ((q = ao2_iterator_next(&queue_iter))) {
Mark Michelson
committed
ao2_lock(q);
if ((mem = ao2_find(q->members, &tmpmem, OBJ_POINTER))) {
Mark Michelson
committed
ao2_unlock(q);
queue_unref(q);
Mark Michelson
committed
ret = 1;
Mark Michelson
committed
}
ao2_unlock(q);
queue_unref(q);
BJ Weschke
committed
}
return ret;
}
static int remove_from_interfaces(const char *interface)
BJ Weschke
committed
{
struct member_interface *curint;
BJ Weschke
committed
AST_LIST_LOCK(&interfaces);
AST_LIST_TRAVERSE_SAFE_BEGIN(&interfaces, curint, list) {
if (!strcasecmp(curint->interface, interface)) {
if (!interface_exists_global(interface)) {
ast_debug(1, "Removing %s from the list of interfaces that make up all of our queue members.\n", interface);
AST_LIST_REMOVE_CURRENT(list);
Tilghman Lesher
committed
ast_free(curint);
BJ Weschke
committed
}
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&interfaces);
BJ Weschke
committed
}
static void clear_and_free_interfaces(void)
{
struct member_interface *curint;
BJ Weschke
committed
AST_LIST_LOCK(&interfaces);
while ((curint = AST_LIST_REMOVE_HEAD(&interfaces, list)))
Tilghman Lesher
committed
ast_free(curint);
BJ Weschke
committed
AST_LIST_UNLOCK(&interfaces);
}
For error reporting, line number is passed for .conf static configuration.
For Realtime queues, linenum is -1.
The failunknown flag is set for config files (and static realtime) to show
errors for unknown parameters. It is cleared for dynamic realtime to allow
extra fields in the tables. */
static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
{
Kevin P. Fleming
committed
if (!strcasecmp(param, "musicclass") ||
!strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
ast_string_field_set(q, moh, val);
} else if (!strcasecmp(param, "announce")) {
ast_string_field_set(q, announce, val);
} else if (!strcasecmp(param, "context")) {
ast_string_field_set(q, context, val);
} else if (!strcasecmp(param, "timeout")) {
q->timeout = atoi(val);
if (q->timeout < 0)
q->timeout = DEFAULT_TIMEOUT;
Kevin P. Fleming
committed
} else if (!strcasecmp(param, "ringinuse")) {
q->ringinuse = ast_true(val);
} else if (!strcasecmp(param, "setinterfacevar")) {
q->setinterfacevar = ast_true(val);