Wed Oct 14 15:02:04 2009

Asterisk developer's documentation


autoservice.c File Reference

Automatic channel service routines. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/sched.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/file.h"
#include "asterisk/translate.h"
#include "asterisk/manager.h"
#include "asterisk/chanvars.h"
#include "asterisk/linkedlists.h"
#include "asterisk/indications.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Data Structures

struct  asent
struct  aslist

Defines

#define MAX_AUTOMONS   1500

Functions

void ast_autoservice_init (void)
int ast_autoservice_start (struct ast_channel *chan)
 Automatically service a channel for us...
int ast_autoservice_stop (struct ast_channel *chan)
 Stop servicing a channel for us...
static void * autoservice_run (void *ign)

Variables

static int as_chan_list_state
static ast_cond_t as_cond
static pthread_t asthread = AST_PTHREADT_NULL


Detailed Description

Automatic channel service routines.

Author:
Mark Spencer <markster@digium.com>

Russell Bryant <russell@digium.com>

Definition in file autoservice.c.


Define Documentation

#define MAX_AUTOMONS   1500

Definition at line 55 of file autoservice.c.

Referenced by autoservice_run().


Function Documentation

void ast_autoservice_init ( void   ) 

Provided by astobj2.c Provided by autoservice.c

Definition at line 323 of file autoservice.c.

References as_cond, and ast_cond_init().

Referenced by main().

00324 {
00325    ast_cond_init(&as_cond, NULL);
00326 }

int ast_autoservice_start ( struct ast_channel chan  ) 

Automatically service a channel for us...

Return values:
0 success
-1 failure, or the channel is already being autoserviced

Definition at line 196 of file autoservice.c.

References as_cond, ast_calloc, ast_channel_lock, ast_channel_unlock, ast_cond_signal(), AST_FLAG_END_DTMF_ONLY, AST_LIST_EMPTY, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_pthread_create_background, AST_PTHREADT_NULL, ast_set_flag, ast_test_flag, asthread, autoservice_run(), asent::chan, free, asent::list, LOG_WARNING, and asent::use_count.

Referenced by _macro_exec(), acf_curl_exec(), acf_odbc_read(), acf_odbc_write(), ast_dtmf_stream(), ast_get_enum(), ast_get_srv(), ast_get_txt(), bridge_playfile(), builtin_automonitor(), builtin_blindtransfer(), conf_play(), do_atxfer(), feature_exec_app(), function_realtime_read(), function_realtime_write(), osplookup_exec(), sla_station_exec(), smdi_msg_retrieve_read(), and system_exec_helper().

00197 {
00198    int res = 0;
00199    struct asent *as;
00200 
00201    /* Check if the channel already has autoservice */
00202    AST_LIST_LOCK(&aslist);
00203    AST_LIST_TRAVERSE(&aslist, as, list) {
00204       if (as->chan == chan) {
00205          as->use_count++;
00206          break;
00207       }
00208    }
00209    AST_LIST_UNLOCK(&aslist);
00210 
00211    if (as) {
00212       /* Entry exists, autoservice is already handling this channel */
00213       return 0;
00214    }
00215 
00216    if (!(as = ast_calloc(1, sizeof(*as))))
00217       return -1;
00218    
00219    /* New entry created */
00220    as->chan = chan;
00221    as->use_count = 1;
00222 
00223    ast_channel_lock(chan);
00224    as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
00225    if (!as->orig_end_dtmf_flag)
00226       ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
00227    ast_channel_unlock(chan);
00228 
00229    AST_LIST_LOCK(&aslist);
00230 
00231    if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
00232       ast_cond_signal(&as_cond);
00233    }
00234 
00235    AST_LIST_INSERT_HEAD(&aslist, as, list);
00236 
00237    if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
00238       if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
00239          ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
00240          /* There will only be a single member in the list at this point,
00241             the one we just added. */
00242          AST_LIST_REMOVE(&aslist, as, list);
00243          free(as);
00244          asthread = AST_PTHREADT_NULL;
00245          res = -1;
00246       } else {
00247          pthread_kill(asthread, SIGURG);
00248       }
00249    }
00250 
00251    AST_LIST_UNLOCK(&aslist);
00252 
00253    return res;
00254 }

int ast_autoservice_stop ( struct ast_channel chan  ) 

Stop servicing a channel for us...

Note:
if chan is locked prior to calling ast_autoservice_stop, it is likely that there will be a deadlock between the thread that calls ast_autoservice_stop and the autoservice thread. It is important that chan is not locked prior to this call
Return values:
0 success
-1 error, or the channel has been hungup

Definition at line 256 of file autoservice.c.

References ast_channel::_softhangup, as_chan_list_state, ast_channel_lock, ast_channel_unlock, ast_clear_flag, AST_FLAG_END_DTMF_ONLY, ast_frfree, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, AST_PTHREADT_NULL, ast_queue_frame_head(), asthread, asent::chan, asent::deferred_frames, f, ast_frame::frame_list, free, asent::list, asent::orig_end_dtmf_flag, and asent::use_count.

Referenced by _macro_exec(), acf_curl_exec(), acf_odbc_read(), acf_odbc_write(), ast_dtmf_stream(), ast_get_srv(), ast_get_txt(), ast_hangup(), bridge_playfile(), builtin_automonitor(), conf_play(), do_atxfer(), feature_exec_app(), finishup(), function_realtime_read(), function_realtime_write(), osplookup_exec(), sla_station_exec(), smdi_msg_retrieve_read(), and system_exec_helper().

00257 {
00258    int res = -1;
00259    struct asent *as, *removed = NULL;
00260    struct ast_frame *f;
00261    int chan_list_state;
00262 
00263    AST_LIST_LOCK(&aslist);
00264 
00265    /* Save the autoservice channel list state.  We _must_ verify that the channel
00266     * list has been rebuilt before we return.  Because, after we return, the channel
00267     * could get destroyed and we don't want our poor autoservice thread to step on
00268     * it after its gone! */
00269    chan_list_state = as_chan_list_state;
00270 
00271    /* Find the entry, but do not free it because it still can be in the
00272       autoservice thread array */
00273    AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {  
00274       if (as->chan == chan) {
00275          as->use_count--;
00276          if (as->use_count < 1) {
00277             AST_LIST_REMOVE_CURRENT(&aslist, list);
00278             removed = as;
00279          }
00280          break;
00281       }
00282    }
00283    AST_LIST_TRAVERSE_SAFE_END
00284 
00285    if (removed && asthread != AST_PTHREADT_NULL) {
00286       pthread_kill(asthread, SIGURG);
00287    }
00288 
00289    AST_LIST_UNLOCK(&aslist);
00290 
00291    if (!removed) {
00292       return 0;
00293    }
00294 
00295    /* Wait while autoservice thread rebuilds its list. */
00296    while (chan_list_state == as_chan_list_state) {
00297       usleep(1000);
00298    }
00299 
00300    /* Now autoservice thread should have no references to our entry
00301       and we can safely destroy it */
00302 
00303    if (!chan->_softhangup) {
00304       res = 0;
00305    }
00306 
00307    if (!as->orig_end_dtmf_flag) {
00308       ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
00309    }
00310 
00311    ast_channel_lock(chan);
00312    while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
00313       ast_queue_frame_head(chan, f);
00314       ast_frfree(f);
00315    }
00316    ast_channel_unlock(chan);
00317 
00318    free(as);
00319 
00320    return res;
00321 }

static void* autoservice_run ( void *  ign  )  [static]

Definition at line 78 of file autoservice.c.

References ast_channel::_softhangup, ast_cond_wait(), AST_CONTROL_HANGUP, AST_FRAME_CNG, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_HTML, AST_FRAME_IAX, AST_FRAME_IMAGE, AST_FRAME_MODEM, AST_FRAME_NULL, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frdup(), ast_frfree, ast_frisolate(), AST_LIST_EMPTY, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_read(), ast_waitfor_n(), asthread, asent::chan, asent::deferred_frames, f, ast_frame::frame_list, ast_frame::frametype, LOG_WARNING, MAX_AUTOMONS, and ast_frame::subclass.

Referenced by ast_autoservice_start().

00079 {
00080    for (;;) {
00081       struct ast_channel *mons[MAX_AUTOMONS];
00082       struct asent *ents[MAX_AUTOMONS];
00083       struct ast_channel *chan;
00084       struct asent *as;
00085       int i, x = 0, ms = 50;
00086       struct ast_frame *f = NULL;
00087       struct ast_frame *defer_frame = NULL;
00088 
00089       AST_LIST_LOCK(&aslist);
00090 
00091       /* At this point, we know that no channels that have been removed are going
00092        * to get used again. */
00093       as_chan_list_state++;
00094 
00095       if (AST_LIST_EMPTY(&aslist)) {
00096          ast_cond_wait(&as_cond, &aslist.lock);
00097       }
00098 
00099       AST_LIST_TRAVERSE(&aslist, as, list) {
00100          if (!as->chan->_softhangup) {
00101             if (x < MAX_AUTOMONS) {
00102                ents[x] = as;
00103                mons[x++] = as->chan;
00104             } else {
00105                ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
00106             }
00107          }
00108       }
00109 
00110       AST_LIST_UNLOCK(&aslist);
00111 
00112       if (!x) {
00113          continue;
00114       }
00115 
00116       chan = ast_waitfor_n(mons, x, &ms);
00117       if (!chan) {
00118          continue;
00119       }
00120 
00121       f = ast_read(chan);
00122    
00123       if (!f) {
00124          struct ast_frame hangup_frame = { 0, };
00125          /* No frame means the channel has been hung up.
00126           * A hangup frame needs to be queued here as ast_waitfor() may
00127           * never return again for the condition to be detected outside
00128           * of autoservice.  So, we'll leave a HANGUP queued up so the
00129           * thread in charge of this channel will know. */
00130 
00131          hangup_frame.frametype = AST_FRAME_CONTROL;
00132          hangup_frame.subclass = AST_CONTROL_HANGUP;
00133 
00134          defer_frame = &hangup_frame;
00135       } else {
00136 
00137          /* Do not add a default entry in this switch statement.  Each new
00138           * frame type should be addressed directly as to whether it should
00139           * be queued up or not. */
00140 
00141          switch (f->frametype) {
00142          /* Save these frames */
00143          case AST_FRAME_DTMF_END:
00144          case AST_FRAME_CONTROL:
00145          case AST_FRAME_TEXT:
00146          case AST_FRAME_IMAGE:
00147          case AST_FRAME_HTML:
00148             defer_frame = f;
00149             break;
00150 
00151          /* Throw these frames away */
00152          case AST_FRAME_DTMF_BEGIN:
00153          case AST_FRAME_VOICE:
00154          case AST_FRAME_VIDEO:
00155          case AST_FRAME_NULL:
00156          case AST_FRAME_IAX:
00157          case AST_FRAME_CNG:
00158          case AST_FRAME_MODEM:
00159             break;
00160          }
00161       }
00162 
00163       if (defer_frame) {
00164          for (i = 0; i < x; i++) {
00165             struct ast_frame *dup_f;
00166             
00167             if (mons[i] != chan) {
00168                continue;
00169             }
00170             
00171             if (defer_frame != f) {
00172                if ((dup_f = ast_frdup(defer_frame))) {
00173                   AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00174                }
00175             } else {
00176                if ((dup_f = ast_frisolate(defer_frame))) {
00177                   if (dup_f != defer_frame) {
00178                      ast_frfree(defer_frame);
00179                   }
00180                   AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00181                }
00182             }
00183             
00184             break;
00185          }
00186       } else if (f) {
00187          ast_frfree(f);
00188       }
00189    }
00190 
00191    asthread = AST_PTHREADT_NULL;
00192 
00193    return NULL;
00194 }


Variable Documentation

int as_chan_list_state [static]

Definition at line 76 of file autoservice.c.

Referenced by ast_autoservice_stop().

ast_cond_t as_cond [static]

Definition at line 72 of file autoservice.c.

Referenced by ast_autoservice_init(), and ast_autoservice_start().

pthread_t asthread = AST_PTHREADT_NULL [static]

Definition at line 74 of file autoservice.c.

Referenced by ast_autoservice_start(), ast_autoservice_stop(), and autoservice_run().


Generated on Wed Oct 14 15:02:04 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7