Thu Sep 7 01:03:04 2017

Asterisk developer's documentation


res_realtime.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Anthony Minessale <anthmct@yahoo.com>
00007  * Mark Spencer <markster@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 RealTime CLI
00023  *
00024  * \author Anthony Minessale <anthmct@yahoo.com>
00025  * \author Mark Spencer <markster@digium.com>
00026  * 
00027  * \ingroup applications
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 342869 $")
00037 
00038 #include "asterisk/file.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/cli.h"
00045 
00046 
00047 static char *cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 
00048 {
00049 #define CRL_HEADER_FORMAT "%30s  %-30s\n"
00050    struct ast_variable *var = NULL, *orig_var = NULL;
00051 
00052    switch (cmd) {
00053    case CLI_INIT:
00054       e->command = "realtime load";
00055       e->usage =
00056          "Usage: realtime load <family> <colmatch> <value>\n"
00057          "       Prints out a list of variables using the RealTime driver.\n"
00058          "       You must supply a family name, a column to match on, and a value to match to.\n";
00059       return NULL;
00060    case CLI_GENERATE:
00061       return NULL;
00062    }
00063 
00064 
00065    if (a->argc < 5) 
00066       return CLI_SHOWUSAGE;
00067 
00068    var = ast_load_realtime_all(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00069 
00070    if (var) {
00071       ast_cli(a->fd, CRL_HEADER_FORMAT, "Column Name", "Column Value");
00072       ast_cli(a->fd, CRL_HEADER_FORMAT, "--------------------", "--------------------");
00073       orig_var = var;
00074       while (var) {
00075          ast_cli(a->fd, CRL_HEADER_FORMAT, var->name, var->value);
00076          var = var->next;
00077       }
00078    } else {
00079       ast_cli(a->fd, "No rows found matching search criteria.\n");
00080    }
00081    ast_variables_destroy(orig_var);
00082    return CLI_SUCCESS;
00083 }
00084 
00085 static char *cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00086 {
00087    int res = 0;
00088 
00089    switch (cmd) {
00090    case CLI_INIT:
00091       e->command = "realtime update";
00092       e->usage =
00093          "Usage: realtime update <family> <colmatch> <valuematch> <colupdate> <newvalue>\n"
00094          "       Update a single variable using the RealTime driver.\n"
00095          "       You must supply a family name, a column to update on, a new value, column to match, and value to match.\n"
00096          "       Ex: realtime update sippeers name bobsphone port 4343\n"
00097          "       will execute SQL as UPDATE sippeers SET port = 4343 WHERE name = bobsphone\n";
00098       return NULL;
00099    case CLI_GENERATE:
00100       return NULL;
00101    }
00102 
00103    if (a->argc < 7) 
00104       return CLI_SHOWUSAGE;
00105 
00106    res = ast_update_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00107 
00108    if (res < 0) {
00109       ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00110       return CLI_FAILURE;
00111    }
00112 
00113    ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
00114 
00115    return CLI_SUCCESS;
00116 }
00117 
00118 static char *cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00119 {
00120    int res = -1;
00121 
00122    switch (cmd) {
00123    case CLI_INIT:
00124       e->command = "realtime update2";
00125       e->usage =
00126          "Usage: realtime update2 <family> <colmatch> <valuematch> [... <colmatch5> <valuematch5>] NULL <colupdate> <newvalue>\n"
00127          "   Update a single variable, requiring one or more fields to match using the\n"
00128          "   RealTime driver.  You must supply a family name, a column to update, a new\n"
00129          "   value, and at least one column and value to match.\n"
00130          "   Ex: realtime update sippeers name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
00131          "   will execute SQL as\n"
00132          "   UPDATE sippeers SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
00133       return NULL;
00134    case CLI_GENERATE:
00135       return NULL;
00136    }
00137 
00138    if (a->argc < 7) 
00139       return CLI_SHOWUSAGE;
00140 
00141    if (a->argc == 7) {
00142       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL, a->argv[5], a->argv[6], SENTINEL);
00143    } else if (a->argc == 9) {
00144       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL, a->argv[7], a->argv[8], SENTINEL);
00145    } else if (a->argc == 11) {
00146       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL, a->argv[9], a->argv[10], SENTINEL);
00147    } else if (a->argc == 13) {
00148       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL, a->argv[11], a->argv[12], SENTINEL);
00149    } else if (a->argc == 15) {
00150       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL, a->argv[13], a->argv[14], SENTINEL);
00151    } else {
00152       return CLI_SHOWUSAGE;
00153    }
00154 
00155    if (res < 0) {
00156       ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00157       return CLI_FAILURE;
00158    }
00159 
00160    ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
00161 
00162    return CLI_SUCCESS;
00163 }
00164 
00165 static char *cli_realtime_store(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00166 {
00167    int res = -1;
00168 
00169    switch (cmd) {
00170    case CLI_INIT:
00171       e->command = "realtime store";
00172       e->usage =
00173          "Usage: realtime store <family> <colname1> <value1> [<colname2> <value2> [... <colname5> <value5>]]\n"
00174          "       Create a stored row using the RealTime driver.\n"
00175          "       You must supply a family name and name/value pairs (up to 5).  If\n"
00176          "       you need to store more than 5 key/value pairs, start with the first\n"
00177          "       five, then use 'realtime update' or 'realtime update2' to add\n"
00178          "       additional columns.\n";
00179       return NULL;
00180    case CLI_GENERATE:
00181       return NULL;
00182    }
00183 
00184    if (a->argc < 5) {
00185       return CLI_SHOWUSAGE;
00186    } else if (a->argc == 5) {
00187       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00188    } else if (a->argc == 7) {
00189       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00190    } else if (a->argc == 9) {
00191       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
00192    } else if (a->argc == 11) {
00193       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
00194    } else if (a->argc == 13) {
00195       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
00196    } else {
00197       return CLI_SHOWUSAGE;
00198    }
00199 
00200    if (res < 0) {
00201       ast_cli(a->fd, "Failed to store record. Check the debug log for possible SQL related entries.\n");
00202       return CLI_FAILURE;
00203    }
00204 
00205    ast_cli(a->fd, "Stored RealTime record.\n");
00206 
00207    return CLI_SUCCESS;
00208 }
00209 
00210 static char *cli_realtime_destroy(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00211 {
00212    int res = -1;
00213 
00214    switch (cmd) {
00215    case CLI_INIT:
00216       e->command = "realtime destroy";
00217       e->usage =
00218          "Usage: realtime destroy <family> <colmatch1> <valuematch1> [<colmatch2> <valuematch2> [... <colmatch5> <valuematch5>]]\n"
00219          "       Remove a stored row using the RealTime driver.\n"
00220          "       You must supply a family name and name/value pairs (up to 5).\n";
00221       return NULL;
00222    case CLI_GENERATE:
00223       return NULL;
00224    }
00225 
00226    if (a->argc < 5) {
00227       return CLI_SHOWUSAGE;
00228    } else if (a->argc == 5) {
00229       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00230    } else if (a->argc == 7) {
00231       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00232    } else if (a->argc == 9) {
00233       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
00234    } else if (a->argc == 11) {
00235       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
00236    } else if (a->argc == 13) {
00237       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
00238    } else {
00239       return CLI_SHOWUSAGE;
00240    }
00241 
00242    if (res < 0) {
00243       ast_cli(a->fd, "Failed to remove record. Check the debug log for possible SQL related entries.\n");
00244       return CLI_FAILURE;
00245    }
00246 
00247    ast_cli(a->fd, "Removed %d RealTime record%s.\n", res, ESS(res));
00248 
00249    return CLI_SUCCESS;
00250 }
00251 
00252 static struct ast_cli_entry cli_realtime[] = {
00253    AST_CLI_DEFINE(cli_realtime_load, "Used to print out RealTime variables."),
00254    AST_CLI_DEFINE(cli_realtime_update, "Used to update RealTime variables."),
00255    AST_CLI_DEFINE(cli_realtime_update2, "Used to test the RealTime update2 method"),
00256    AST_CLI_DEFINE(cli_realtime_store, "Store a new row into a RealTime database"),
00257    AST_CLI_DEFINE(cli_realtime_destroy, "Delete a row from a RealTime database"),
00258 };
00259 
00260 static int unload_module(void)
00261 {
00262    ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
00263    return 0;
00264 }
00265 
00266 static int load_module(void)
00267 {
00268    ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
00269    return AST_MODULE_LOAD_SUCCESS;
00270 }
00271 
00272 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");

Generated on 7 Sep 2017 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1