Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:48 2009

## Asterisk developer's documentation

# app_random.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (c) 2003 - 2005 Tilghman Lesher.  All rights reserved.
    00005  *
    00006  * Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>
    00007  *
    00008  * This code is released by the author with no restrictions on usage or distribution.
    00009  *
    00010  * See http://www.asterisk.org for more information about
    00011  * the Asterisk project. Please do not directly contact
    00012  * any of the maintainers of this project for assistance;
    00013  * the project provides a web site, mailing lists and IRC
    00014  * channels for your use.
    00015  *
    00016  */
    00017
    00018 /*! \file
    00019  *
    00020  * \brief Random application
    00021  *
    00022  * \author Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>
    00023  * \ingroup applications
    00024  */
    00025
    00026 #include <stdio.h>
    00027 #include <stdlib.h>
    00028 #include <unistd.h>
    00029 #include <string.h>
    00030
    00031 #include "asterisk.h"
    00032
    00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211526 $")
    00034
    00035 #include "asterisk/file.h"
    00036 #include "asterisk/logger.h"
    00037 #include "asterisk/options.h"
    00038 #include "asterisk/channel.h"
    00039 #include "asterisk/pbx.h"
    00040 #include "asterisk/module.h"
    00041
    00042 static char *tdesc = "Random goto";
    00043
    00044 static char *app_random = "Random";
    00045
    00046 static char *random_synopsis = "Conditionally branches, based upon a probability";
    00047
    00048 static char *random_descrip =
    00049 "Random([probability]:[[context|]extension|]priority)\n"
    00050 "  probability := INTEGER in the range 1 to 100\n";
    00051
    00052 STANDARD_LOCAL_USER;
    00053
    00054 LOCAL_USER_DECL;
    00055
    00056 static char random_state[256];
    00057
    00058 static int random_exec(struct ast_channel *chan, void *data)
    00059 {
    00060    int res=0;
    00061    struct localuser *u;
    00062
    00063    char *s;
    00064    char *prob;
    00065    int probint;
    00066
    00067    if (ast_strlen_zero(data)) {
    00068       ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
    00069       return -1;
    00070    }
    00071
    00072    LOCAL_USER_ADD(u);
    00073
    00074    s = ast_strdupa(data);
    00075    if (!s) {
    00076       ast_log(LOG_ERROR, "Out of memory!\n");
    00077       LOCAL_USER_REMOVE(u);
    00078       return -1;
    00079    }
    00080
    00081    prob = strsep(&s,":");
    00082    if ((!prob) || (sscanf(prob, "%3d", &probint) != 1))
    00083       probint = 0;
    00084
    00085    if ((random() % 100) + probint >= 100) {
    00086       res = ast_parseable_goto(chan, s);
    00087       if (option_verbose > 2)
    00088          ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
    00089             chan->context,chan->exten, chan->priority+1);
    00090    }
    00091    LOCAL_USER_REMOVE(u);
    00092    return res;
    00093 }
    00094
    00095 int unload_module(void)
    00096 {
    00097    int res;
    00098
    00099    res = ast_unregister_application(app_random);
    00100
    00101    STANDARD_HANGUP_LOCALUSERS;
    00102
    00103    return res;
    00104 }
    00105
    00106 int load_module(void)
    00107 {
    00108    initstate((getppid() * 65535 + getpid()) % RAND_MAX, random_state, 256);
    00109    return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
    00110 }
    00111
    00112 char *description(void)
    00113 {
    00114    return tdesc;
    00115 }
    00116
    00117 int usecount(void)
    00118 {
    00119    /* Don't allow unload, since rand(3) depends upon this module being here. */
    00120    return 1;
    00121 }
    00122
    00123 char *key()
    00124 {
    00125    return ASTERISK_GPL_KEY;
    00126 }