Thu Dec 17 23:51:04 2009

Asterisk developer's documentation


autoservice.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Russell Bryant <russell@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Automatic channel service routines
00023  *
00024  * \author Mark Spencer <markster@digium.com> 
00025  * \author Russell Bryant <russell@digium.com>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 223486 $")
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <sys/time.h>
00036 #include <signal.h>
00037 #include <errno.h>
00038 #include <unistd.h>
00039 
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/frame.h"
00042 #include "asterisk/sched.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/file.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/manager.h"
00049 #include "asterisk/chanvars.h"
00050 #include "asterisk/linkedlists.h"
00051 #include "asterisk/indications.h"
00052 #include "asterisk/lock.h"
00053 #include "asterisk/utils.h"
00054 
00055 #define MAX_AUTOMONS 1500
00056 
00057 struct asent {
00058    struct ast_channel *chan;
00059    /*! This gets incremented each time autoservice gets started on the same
00060     *  channel.  It will ensure that it doesn't actually get stopped until 
00061     *  it gets stopped for the last time. */
00062    unsigned int use_count;
00063    unsigned int orig_end_dtmf_flag:1;
00064    /*! Frames go on at the head of deferred_frames, so we have the frames
00065     *  from newest to oldest.  As we put them at the head of the readq, we'll
00066     *  end up with them in the right order for the channel's readq. */
00067    AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
00068    AST_LIST_ENTRY(asent) list;
00069 };
00070 
00071 static AST_LIST_HEAD_STATIC(aslist, asent);
00072 static ast_cond_t as_cond;
00073 
00074 static pthread_t asthread = AST_PTHREADT_NULL;
00075 
00076 static int as_chan_list_state;
00077 
00078 static void *autoservice_run(void *ign)
00079 {
00080    struct ast_frame hangup_frame = {
00081       .frametype = AST_FRAME_CONTROL,
00082       .subclass = AST_CONTROL_HANGUP,
00083    };
00084 
00085    for (;;) {
00086       struct ast_channel *mons[MAX_AUTOMONS];
00087       struct asent *ents[MAX_AUTOMONS];
00088       struct ast_channel *chan;
00089       struct asent *as;
00090       int i, x = 0, ms = 50;
00091       struct ast_frame *f = NULL;
00092       struct ast_frame *defer_frame = NULL;
00093 
00094       AST_LIST_LOCK(&aslist);
00095 
00096       /* At this point, we know that no channels that have been removed are going
00097        * to get used again. */
00098       as_chan_list_state++;
00099 
00100       if (AST_LIST_EMPTY(&aslist)) {
00101          ast_cond_wait(&as_cond, &aslist.lock);
00102       }
00103 
00104       AST_LIST_TRAVERSE(&aslist, as, list) {
00105          if (!as->chan->_softhangup) {
00106             if (x < MAX_AUTOMONS) {
00107                ents[x] = as;
00108                mons[x++] = as->chan;
00109             } else {
00110                ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
00111             }
00112          }
00113       }
00114 
00115       AST_LIST_UNLOCK(&aslist);
00116 
00117       if (!x) {
00118          continue;
00119       }
00120 
00121       chan = ast_waitfor_n(mons, x, &ms);
00122       if (!chan) {
00123          continue;
00124       }
00125 
00126       f = ast_read(chan);
00127 
00128       if (!f) {
00129          /* No frame means the channel has been hung up.
00130           * A hangup frame needs to be queued here as ast_waitfor() may
00131           * never return again for the condition to be detected outside
00132           * of autoservice.  So, we'll leave a HANGUP queued up so the
00133           * thread in charge of this channel will know. */
00134 
00135          defer_frame = &hangup_frame;
00136       } else {
00137 
00138          /* Do not add a default entry in this switch statement.  Each new
00139           * frame type should be addressed directly as to whether it should
00140           * be queued up or not. */
00141 
00142          switch (f->frametype) {
00143          /* Save these frames */
00144          case AST_FRAME_DTMF_END:
00145          case AST_FRAME_CONTROL:
00146          case AST_FRAME_TEXT:
00147          case AST_FRAME_IMAGE:
00148          case AST_FRAME_HTML:
00149             defer_frame = f;
00150             break;
00151 
00152          /* Throw these frames away */
00153          case AST_FRAME_DTMF_BEGIN:
00154          case AST_FRAME_VOICE:
00155          case AST_FRAME_VIDEO:
00156          case AST_FRAME_NULL:
00157          case AST_FRAME_IAX:
00158          case AST_FRAME_CNG:
00159          case AST_FRAME_MODEM:
00160             break;
00161          }
00162       }
00163 
00164       if (defer_frame) {
00165          for (i = 0; i < x; i++) {
00166             struct ast_frame *dup_f;
00167             
00168             if (mons[i] != chan) {
00169                continue;
00170             }
00171             
00172             if (defer_frame != f) {
00173                if ((dup_f = ast_frdup(defer_frame))) {
00174                   AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00175                }
00176             } else {
00177                if ((dup_f = ast_frisolate(defer_frame))) {
00178                   if (dup_f != defer_frame) {
00179                      ast_frfree(defer_frame);
00180                   }
00181                   AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00182                }
00183             }
00184             
00185             break;
00186          }
00187       } else if (f) {
00188          ast_frfree(f);
00189       }
00190    }
00191 
00192    asthread = AST_PTHREADT_NULL;
00193 
00194    return NULL;
00195 }
00196 
00197 int ast_autoservice_start(struct ast_channel *chan)
00198 {
00199    int res = 0;
00200    struct asent *as;
00201 
00202    /* Check if the channel already has autoservice */
00203    AST_LIST_LOCK(&aslist);
00204    AST_LIST_TRAVERSE(&aslist, as, list) {
00205       if (as->chan == chan) {
00206          as->use_count++;
00207          break;
00208       }
00209    }
00210    AST_LIST_UNLOCK(&aslist);
00211 
00212    if (as) {
00213       /* Entry exists, autoservice is already handling this channel */
00214       return 0;
00215    }
00216 
00217    if (!(as = ast_calloc(1, sizeof(*as))))
00218       return -1;
00219    
00220    /* New entry created */
00221    as->chan = chan;
00222    as->use_count = 1;
00223 
00224    ast_channel_lock(chan);
00225    as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
00226    if (!as->orig_end_dtmf_flag)
00227       ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
00228    ast_channel_unlock(chan);
00229 
00230    AST_LIST_LOCK(&aslist);
00231 
00232    if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
00233       ast_cond_signal(&as_cond);
00234    }
00235 
00236    AST_LIST_INSERT_HEAD(&aslist, as, list);
00237 
00238    if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
00239       if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
00240          ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
00241          /* There will only be a single member in the list at this point,
00242             the one we just added. */
00243          AST_LIST_REMOVE(&aslist, as, list);
00244          free(as);
00245          asthread = AST_PTHREADT_NULL;
00246          res = -1;
00247       } else {
00248          pthread_kill(asthread, SIGURG);
00249       }
00250    }
00251 
00252    AST_LIST_UNLOCK(&aslist);
00253 
00254    return res;
00255 }
00256 
00257 int ast_autoservice_stop(struct ast_channel *chan)
00258 {
00259    int res = -1;
00260    struct asent *as, *removed = NULL;
00261    struct ast_frame *f;
00262    int chan_list_state;
00263 
00264    AST_LIST_LOCK(&aslist);
00265 
00266    /* Save the autoservice channel list state.  We _must_ verify that the channel
00267     * list has been rebuilt before we return.  Because, after we return, the channel
00268     * could get destroyed and we don't want our poor autoservice thread to step on
00269     * it after its gone! */
00270    chan_list_state = as_chan_list_state;
00271 
00272    /* Find the entry, but do not free it because it still can be in the
00273       autoservice thread array */
00274    AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {  
00275       if (as->chan == chan) {
00276          as->use_count--;
00277          if (as->use_count < 1) {
00278             AST_LIST_REMOVE_CURRENT(&aslist, list);
00279             removed = as;
00280          }
00281          break;
00282       }
00283    }
00284    AST_LIST_TRAVERSE_SAFE_END
00285 
00286    if (removed && asthread != AST_PTHREADT_NULL) {
00287       pthread_kill(asthread, SIGURG);
00288    }
00289 
00290    AST_LIST_UNLOCK(&aslist);
00291 
00292    if (!removed) {
00293       return 0;
00294    }
00295 
00296    /* Wait while autoservice thread rebuilds its list. */
00297    while (chan_list_state == as_chan_list_state) {
00298       usleep(1000);
00299    }
00300 
00301    /* Now autoservice thread should have no references to our entry
00302       and we can safely destroy it */
00303 
00304    if (!chan->_softhangup) {
00305       res = 0;
00306    }
00307 
00308    if (!as->orig_end_dtmf_flag) {
00309       ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
00310    }
00311 
00312    ast_channel_lock(chan);
00313    while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
00314       ast_queue_frame_head(chan, f);
00315       ast_frfree(f);
00316    }
00317    ast_channel_unlock(chan);
00318 
00319    free(as);
00320 
00321    return res;
00322 }
00323 
00324 void ast_autoservice_init(void)
00325 {
00326    ast_cond_init(&as_cond, NULL);
00327 }

Generated on Thu Dec 17 23:51:04 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7