Mon Nov 24 15:34:23 2008

Asterisk developer's documentation


app_realtime.c File Reference

RealTime App. More...

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

Go to the source code of this file.

Defines

#define crop_data(str)   { *(str) = '\0' ; (str)++; }
#define next_one(var)   var = var->next

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Realtime Data Lookup/Rewrite")
static int cli_realtime_load (int fd, int argc, char **argv)
static int cli_realtime_update (int fd, int argc, char **argv)
static int load_module (void)
static int realtime_exec (struct ast_channel *chan, void *data)
static int realtime_update_exec (struct ast_channel *chan, void *data)
static int unload_module (void)

Variables

static char * app = "RealTime"
static struct ast_cli_entry cli_realtime []
static char cli_realtime_load_usage []
static char cli_realtime_update_usage []
static char * desc
static char * synopsis = "Realtime Data Lookup"
static char * uapp = "RealTimeUpdate"
static char * udesc
static char * USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])"
static char * usynopsis = "Realtime Data Rewrite"
static char * UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)"


Detailed Description

RealTime App.

Author:
Anthony Minessale <anthmct@yahoo.com>

Mark Spencer <markster@digium.com>

Definition in file app_realtime.c.


Define Documentation

#define crop_data ( str   )     { *(str) = '\0' ; (str)++; }

Definition at line 50 of file app_realtime.c.

Referenced by realtime_exec(), and realtime_update_exec().

#define next_one ( var   )     var = var->next

Definition at line 49 of file app_realtime.c.


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Realtime Data Lookup/Rewrite"   
)

static int cli_realtime_load ( int  fd,
int  argc,
char **  argv 
) [static]

Definition at line 72 of file app_realtime.c.

References ast_cli(), ast_load_realtime(), RESULT_FAILURE, RESULT_SUCCESS, and var.

00073 {
00074    char *header_format = "%30s  %-30s\n";
00075    struct ast_variable *var=NULL;
00076 
00077    if(argc<5) {
00078       ast_cli(fd, "You must supply a family name, a column to match on, and a value to match to.\n");
00079       return RESULT_FAILURE;
00080    }
00081 
00082    var = ast_load_realtime(argv[2], argv[3], argv[4], NULL);
00083 
00084    if(var) {
00085       ast_cli(fd, header_format, "Column Name", "Column Value");
00086       ast_cli(fd, header_format, "--------------------", "--------------------");
00087       while(var) {
00088          ast_cli(fd, header_format, var->name, var->value);
00089          var = var->next;
00090       }
00091    } else {
00092       ast_cli(fd, "No rows found matching search criteria.\n");
00093    }
00094    return RESULT_SUCCESS;
00095 }

static int cli_realtime_update ( int  fd,
int  argc,
char **  argv 
) [static]

Definition at line 97 of file app_realtime.c.

References ast_cli(), ast_update_realtime(), RESULT_FAILURE, and RESULT_SUCCESS.

00097                                                               {
00098    int res = 0;
00099 
00100    if(argc<7) {
00101       ast_cli(fd, "You must supply a family name, a column to update on, a new value, column to match, and value to to match.\n");
00102       ast_cli(fd, "Ex: realtime update sipfriends name bobsphone port 4343\n will execute SQL as UPDATE sipfriends SET port = 4343 WHERE name = bobsphone\n");
00103       return RESULT_FAILURE;
00104    }
00105 
00106    res = ast_update_realtime(argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
00107 
00108    if(res < 0) {
00109       ast_cli(fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00110       return RESULT_SUCCESS;
00111    }
00112 
00113        ast_cli(fd, "Updated %d RealTime record%s.\n", res, (res != 1) ? "s" : "");
00114 
00115    return RESULT_SUCCESS;
00116 }

static int load_module ( void   )  [static]

Definition at line 250 of file app_realtime.c.

References ast_cli_register_multiple(), ast_register_application(), cli_realtime, realtime_exec(), and realtime_update_exec().

00251 {
00252    int res;
00253 
00254    ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00255    res = ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
00256    res |= ast_register_application(app, realtime_exec, synopsis, desc);
00257 
00258    return res;
00259 }

static int realtime_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 180 of file app_realtime.c.

References ast_load_realtime(), ast_log(), ast_module_user_add, ast_module_user_remove, ast_strdupa, ast_strlen_zero(), ast_variables_destroy(), ast_verbose(), crop_data, len, LOG_ERROR, LOG_WARNING, ast_variable::name, ast_variable::next, option_verbose, pbx_builtin_setvar_helper(), prefix, ast_variable::value, var, and VERBOSE_PREFIX_4.

Referenced by load_module().

00181 {
00182    int res=0, count=0;
00183    struct ast_module_user *u;
00184    struct ast_variable *var, *itt;
00185    char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
00186    char countc[13];
00187    size_t len;
00188       
00189         ast_log(LOG_WARNING, "The RealTime application has been deprecated in favor of the REALTIME dialplan function.\n");
00190 
00191    if (ast_strlen_zero(data)) {
00192       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00193       return -1;
00194    }
00195    
00196    u = ast_module_user_add(chan);
00197 
00198    family = ast_strdupa(data);
00199    if ((colmatch = strchr(family,'|'))) {
00200       crop_data(colmatch);
00201       if ((value = strchr(colmatch,'|'))) {
00202          crop_data(value);
00203          if ((prefix = strchr(value,'|')))
00204             crop_data(prefix);
00205       }
00206    }
00207    if (! (family && value && colmatch) ) {
00208       ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
00209       res = -1;
00210    } else {
00211       if (option_verbose > 3)
00212          ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
00213       if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
00214          for (itt = var; itt; itt = itt->next) {
00215             if(prefix) {
00216                len = strlen(prefix) + strlen(itt->name) + 2;
00217                vname = alloca(len);
00218                snprintf(vname,len,"%s%s",prefix,itt->name);
00219                
00220             } else 
00221                vname = itt->name;
00222 
00223             pbx_builtin_setvar_helper(chan, vname, itt->value);
00224             count++;
00225          }
00226          ast_variables_destroy(var);
00227       } else if (option_verbose > 3)
00228          ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
00229    }
00230    snprintf(countc, sizeof(countc), "%d", count);
00231    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00232    
00233    ast_module_user_remove(u);
00234    return res;
00235 }

static int realtime_update_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 136 of file app_realtime.c.

References ast_log(), ast_module_user_add, ast_module_user_remove, ast_strdupa, ast_strlen_zero(), ast_update_realtime(), ast_module_user::chan, crop_data, LOG_ERROR, LOG_WARNING, and pbx_builtin_setvar_helper().

Referenced by load_module().

00137 {
00138    char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
00139    struct ast_module_user *u;
00140    int res = 0, count = 0;
00141    char countc[13];
00142 
00143         ast_log(LOG_WARNING, "The RealTimeUpdate application has been deprecated in favor of the REALTIME dialplan function.\n");
00144 
00145    if (ast_strlen_zero(data)) {
00146       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00147       return -1;
00148    }
00149    
00150    u = ast_module_user_add(chan);
00151 
00152    family = ast_strdupa(data);
00153    if ((colmatch = strchr(family,'|'))) {
00154       crop_data(colmatch);
00155       if ((value = strchr(colmatch,'|'))) {
00156          crop_data(value);
00157          if ((newcol = strchr(value,'|'))) {
00158             crop_data(newcol);
00159             if ((newval = strchr(newcol,'|'))) 
00160                crop_data(newval);
00161          }
00162       }
00163    }
00164    if (! (family && value && colmatch && newcol && newval) ) {
00165       ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
00166       res = -1;
00167    } else {
00168       count = ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
00169    }
00170 
00171    snprintf(countc, sizeof(countc), "%d", count);
00172    pbx_builtin_setvar_helper(chan, "REALTIMECOUNT", countc);
00173 
00174    ast_module_user_remove(u);
00175    
00176    return res;
00177 }

static int unload_module ( void   )  [static]

Definition at line 237 of file app_realtime.c.

References ast_cli_unregister_multiple(), ast_module_user_hangup_all, ast_unregister_application(), and cli_realtime.

00238 {
00239    int res;
00240 
00241    ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
00242    res = ast_unregister_application(uapp);
00243    res |= ast_unregister_application(app);
00244 
00245    ast_module_user_hangup_all();
00246 
00247    return res;
00248 }


Variable Documentation

char* app = "RealTime" [static]

Definition at line 52 of file app_realtime.c.

struct ast_cli_entry cli_realtime[] [static]

Definition at line 126 of file app_realtime.c.

Referenced by load_module(), and unload_module().

char cli_realtime_load_usage[] [static]

Initial value:

"Usage: realtime load <family> <colmatch> <value>\n"
"       Prints out a list of variables using the RealTime driver.\n"

Definition at line 118 of file app_realtime.c.

char cli_realtime_update_usage[] [static]

Initial value:

"Usage: realtime update <family> <colmatch> <value>\n"
"       Update a single variable using the RealTime driver.\n"

Definition at line 122 of file app_realtime.c.

char* desc [static]

Definition at line 58 of file app_realtime.c.

char* synopsis = "Realtime Data Lookup" [static]

Definition at line 54 of file app_realtime.c.

char* uapp = "RealTimeUpdate" [static]

Definition at line 53 of file app_realtime.c.

char* udesc [static]

Initial value:

 "Use the RealTime config handler system to update a value\n"
"RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
"The column <newcol> in 'family' matching column <colmatch>=<value> will be\n"
"updated to <newval>.  REALTIMECOUNT will be set with the number of rows\n"
"updated or -1 if an error occurs.\n"

Definition at line 65 of file app_realtime.c.

char* USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])" [static]

Definition at line 56 of file app_realtime.c.

char* usynopsis = "Realtime Data Rewrite" [static]

Definition at line 55 of file app_realtime.c.

char* UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)" [static]

Definition at line 57 of file app_realtime.c.


Generated on Mon Nov 24 15:34:23 2008 for Asterisk - the Open Source PBX by  doxygen 1.4.7