Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:11 2009

## Asterisk developer's documentation

# app_random.c File Reference

Random application. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`
`#include <string.h>`
`#include "asterisk.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/options.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`

Include dependency graph for app_random.c:

Go to the source code of this file.

## Functions

char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static int | random_exec (struct ast_channel *chan, void *data)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app_random = "Random"
| LOCAL_USER_DECL
static char * | random_descrip
static char | random_state [256]
static char * | random_synopsis = "Conditionally branches, based upon a probability"
| STANDARD_LOCAL_USER
static char * | tdesc = "Random goto"

## Detailed Description

Random application.

Author:
  Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>

Definition in file app_random.c.

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 112 of file app_random.c.

    00113 {
    00114    return tdesc;
    00115 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 123 of file app_random.c.

References ASTERISK_GPL_KEY.

    00124 {
    00125    return ASTERISK_GPL_KEY;
    00126 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 106 of file app_random.c.

References ast_register_application(), and random_exec().

    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 }

static int random_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 58 of file app_random.c.

References ast_log(), ast_parseable_goto(), ast_strdupa, ast_strlen_zero(), ast_verbose(), ast_channel::context, ast_channel::exten, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, option_verbose, ast_channel::priority, s, strsep(), and VERBOSE_PREFIX_3.

Referenced by load_module().

    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 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 95 of file app_random.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00096 {
    00097    int res;
    00098
    00099    res = ast_unregister_application(app_random);
    00100
    00101    STANDARD_HANGUP_LOCALUSERS;
    00102
    00103    return res;
    00104 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 117 of file app_random.c.

    00118 {
    00119    /* Don't allow unload, since rand(3) depends upon this module being here. */
    00120    return 1;
    00121 }

## Variable Documentation

char* app_random = "Random"`[static]`

Definition at line 44 of file app_random.c.

LOCAL_USER_DECL

Definition at line 54 of file app_random.c.

char* random_descrip`[static]`

Initial value:

    "Random([probability]:[[context|]extension|]priority)\n"
    "  probability := INTEGER in the range 1 to 100\n"

Definition at line 48 of file app_random.c.

char random_state[256]`[static]`

Definition at line 56 of file app_random.c.

char* random_synopsis = "Conditionally branches, based upon a probability"`[static]`

Definition at line 46 of file app_random.c.

STANDARD_LOCAL_USER

Definition at line 52 of file app_random.c.

char* tdesc = "Random goto"`[static]`

Definition at line 42 of file app_random.c.