[asterisk-commits] twilson: branch twilson/sqlite3_playground r341868 - in /team/twilson/sqlite3...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Oct 22 01:15:04 CDT 2011


Author: twilson
Date: Sat Oct 22 01:15:01 2011
New Revision: 341868

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=341868
Log:
Make everything built-in.

I didn't do this at first because I was worried about handling
reloads on a non-module. Silly of me as we do that all over the
place.

Still need to add in forcing an "astdb" realtime handle if one isn't
configured in extconfig.conf.

Added:
    team/twilson/sqlite3_playground/main/realtime_sqlite3.c
      - copied, changed from r341867, team/twilson/sqlite3_playground/res/res_config_sqlite3.c
Removed:
    team/twilson/sqlite3_playground/res/res_astdb.c
    team/twilson/sqlite3_playground/res/res_astdb.exports.in
    team/twilson/sqlite3_playground/res/res_config_sqlite3.c
Modified:
    team/twilson/sqlite3_playground/include/asterisk/_private.h
    team/twilson/sqlite3_playground/main/asterisk.c
    team/twilson/sqlite3_playground/main/db.c
    team/twilson/sqlite3_playground/main/loader.c

Modified: team/twilson/sqlite3_playground/include/asterisk/_private.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/include/asterisk/_private.h?view=diff&rev=341868&r1=341867&r2=341868
==============================================================================
--- team/twilson/sqlite3_playground/include/asterisk/_private.h (original)
+++ team/twilson/sqlite3_playground/include/asterisk/_private.h Sat Oct 22 01:15:01 2011
@@ -21,6 +21,7 @@
 void close_logger(void);		/*!< Provided by logger.c */
 int init_framer(void);			/*!< Provided by frame.c */
 int ast_term_init(void);		/*!< Provided by term.c */
+int astdb_init(void);			/*!< Provided by db.c */
 void ast_channels_init(void);		/*!< Provided by channel.c */
 void ast_builtins_init(void);		/*!< Provided by cli.c */
 int ast_cli_perms_init(int reload);	/*!< Provided by cli.c */
@@ -47,6 +48,8 @@
 int ast_ssl_init(void);                 /*!< Provided by ssl.c */
 int ast_test_init(void);            /*!< Provided by test.c */
 int ast_msg_init(void);             /*!< Provided by message.c */
+int realtime_sqlite3_init(void);
+int realtime_sqlite3_reload(void); /*!< Provided by realtime_sqlite3.c */
 
 /*!
  * \brief Reload asterisk modules.

Modified: team/twilson/sqlite3_playground/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/asterisk.c?view=diff&rev=341868&r1=341867&r2=341868
==============================================================================
--- team/twilson/sqlite3_playground/main/asterisk.c (original)
+++ team/twilson/sqlite3_playground/main/asterisk.c Sat Oct 22 01:15:01 2011
@@ -3770,6 +3770,16 @@
 	ast_xmldoc_load_documentation();
 #endif
 
+	if (realtime_sqlite3_init()) {
+		printf("%s", term_quit());
+		exit(1);
+	}
+
+	if (astdb_init()) {
+		printf("%s", term_quit());
+		exit(1);
+	}
+
 	if (ast_msg_init()) {
 		printf("%s", term_quit());
 		exit(1);

Modified: team/twilson/sqlite3_playground/main/db.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/db.c?view=diff&rev=341868&r1=341867&r2=341868
==============================================================================
--- team/twilson/sqlite3_playground/main/db.c (original)
+++ team/twilson/sqlite3_playground/main/db.c Sat Oct 22 01:15:01 2011
@@ -27,9 +27,68 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/_private.h"
 #include "asterisk/astdb.h"
 #include "asterisk/config.h"
 #include "asterisk/strings.h"
+#include "asterisk/module.h"
+#include "asterisk/cli.h"
+#include "asterisk/utils.h"
+#include "asterisk/manager.h"
+
+/*** DOCUMENTATION
+	<manager name="DBGet" language="en_US">
+		<synopsis>
+			Get DB Entry.
+		</synopsis>
+		<syntax>
+			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+			<parameter name="Family" required="true" />
+			<parameter name="Key" required="true" />
+		</syntax>
+		<description>
+		</description>
+	</manager>
+	<manager name="DBPut" language="en_US">
+		<synopsis>
+			Put DB entry.
+		</synopsis>
+		<syntax>
+			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+			<parameter name="Family" required="true" />
+			<parameter name="Key" required="true" />
+			<parameter name="Val" />
+		</syntax>
+		<description>
+		</description>
+	</manager>
+	<manager name="DBDel" language="en_US">
+		<synopsis>
+			Delete DB entry.
+		</synopsis>
+		<syntax>
+			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+			<parameter name="Family" required="true" />
+			<parameter name="Key" required="true" />
+		</syntax>
+		<description>
+		</description>
+	</manager>
+	<manager name="DBDelTree" language="en_US">
+		<synopsis>
+			Delete DB Tree.
+		</synopsis>
+		<syntax>
+			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+			<parameter name="Family" required="true" />
+			<parameter name="Key" />
+		</syntax>
+		<description>
+		</description>
+	</manager>
+ ***/
+
+#define MAX_DB_FIELD 256
 
 static struct ast_str *_fullkey(const char *family, const char *key)
 {
@@ -218,3 +277,360 @@
 		ast_free(last);
 	}
 }
+
+static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database put";
+		e->usage =
+			"Usage: database put <family> <key> <value>\n"
+			"       Adds or updates an entry in the Asterisk database for\n"
+			"       a given family, key, and value.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 5)
+		return CLI_SHOWUSAGE;
+	res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
+	if (res)  {
+		ast_cli(a->fd, "Failed to update entry\n");
+	} else {
+		ast_cli(a->fd, "Updated database successfully\n");
+	}
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+	char tmp[MAX_DB_FIELD];
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database get";
+		e->usage =
+			"Usage: database get <family> <key>\n"
+			"       Retrieves an entry in the Asterisk database for a given\n"
+			"       family and key.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 4)
+		return CLI_SHOWUSAGE;
+	res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
+	if (res) {
+		ast_cli(a->fd, "Database entry not found.\n");
+	} else {
+		ast_cli(a->fd, "Value: %s\n", tmp);
+	}
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database del";
+		e->usage =
+			"Usage: database del <family> <key>\n"
+			"       Deletes an entry in the Asterisk database for a given\n"
+			"       family and key.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 4)
+		return CLI_SHOWUSAGE;
+	res = ast_db_del(a->argv[2], a->argv[3]);
+	if (res) {
+		ast_cli(a->fd, "Database entry does not exist.\n");
+	} else {
+		ast_cli(a->fd, "Database entry removed.\n");
+	}
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database deltree";
+		e->usage =
+			"Usage: database deltree <family> [subfamily]\n"
+			"       Deletes a family or specific subfamily within a family\n"
+			"       in the Asterisk database.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if ((a->argc < 3) || (a->argc > 4))
+		return CLI_SHOWUSAGE;
+	if (a->argc == 4) {
+		res = ast_db_deltree(a->argv[2], a->argv[3]);
+	} else {
+		res = ast_db_deltree(a->argv[2], NULL);
+	}
+	if (res < 0) {
+		ast_cli(a->fd, "Database entries do not exist.\n");
+	} else {
+		ast_cli(a->fd, "%d database entries removed.\n",res);
+	}
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int counter = 0;
+	struct ast_db_entry *results = NULL, *iter;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database show";
+		e->usage =
+			"Usage: database show [family [subfamily]]\n"
+			"       Shows Asterisk database contents, optionally restricted\n"
+			"       to a given family, or family and subfamily.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc == 4) {
+		/* Family and key tree */
+		results = ast_db_gettree(a->argv[2], a->argv[3]);
+	} else if (a->argc == 3) {
+		/* Family only */
+		results = ast_db_gettree(a->argv[2], NULL);
+	} else if (a->argc == 2) {
+		results = ast_db_gettree(NULL, NULL);
+	} else {
+		return CLI_SHOWUSAGE;
+	}
+
+	for (iter = results; iter; iter = iter->next) {
+		const char *key_s = iter->key, *value_s = iter->data;
+
+		if (!(key_s && value_s)) {
+			ast_log(LOG_WARNING, "Invalid key or value. Skipping.\n");
+			continue;
+		}
+
+		++counter;
+		ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
+	}
+
+	ast_db_freetree(results);
+
+	ast_cli(a->fd, "%d results found.\n", counter);
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int counter = 0;
+	struct ast_str *lookup;
+	struct ast_config *results;
+	const char *cat;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database showkey";
+		e->usage =
+			"Usage: database showkey <subfamily>\n"
+			"       Shows Asterisk database contents, restricted to a given key.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 3) {
+		return CLI_SHOWUSAGE;
+	}
+
+	if (ast_strlen_zero(a->argv[2])) {
+		goto done;
+	}
+
+	if (!(lookup = ast_str_create(256))) {
+		goto done;
+	}
+
+	ast_str_set(&lookup, 0, "%%/%s", a->argv[2]);
+
+	if (!(results = ast_load_realtime_multientry(ast_db_family, "key LIKE", ast_str_buffer(lookup), SENTINEL))) {
+		goto done;
+	}
+
+	for (cat = ast_category_browse(results, NULL); cat; cat = ast_category_browse(results, cat)) {
+		struct ast_variable *var;
+		const char *key_s = NULL, *value_s = NULL;
+		for(var = ast_variable_browse(results, cat); var; var = var->next) {
+			if (!strcasecmp(var->name, ast_db_key)) {
+				key_s = var->value;
+			} else if (!strcasecmp(var->name, ast_db_value)) {
+				value_s = var->value;
+			}
+		}
+		if (!(key_s && value_s)) {
+			continue;
+		}
+
+		++counter;
+		ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
+	}
+
+done:
+	ast_cli(a->fd, "%d results found.\n", counter);
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_database[] = {
+	AST_CLI_DEFINE(handle_cli_database_show,    "Shows database contents"),
+	AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
+	AST_CLI_DEFINE(handle_cli_database_get,     "Gets database value"),
+	AST_CLI_DEFINE(handle_cli_database_put,     "Adds/updates database value"),
+	AST_CLI_DEFINE(handle_cli_database_del,     "Removes database key/value"),
+	AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database subfamily/values"),
+};
+
+static int manager_dbput(struct mansession *s, const struct message *m)
+{
+	const char *family = astman_get_header(m, "Family");
+	const char *key = astman_get_header(m, "Key");
+	const char *val = astman_get_header(m, "Val");
+	int res;
+
+	if (ast_strlen_zero(family)) {
+		astman_send_error(s, m, "No family specified");
+		return 0;
+	}
+	if (ast_strlen_zero(key)) {
+		astman_send_error(s, m, "No key specified");
+		return 0;
+	}
+
+	res = ast_db_put(family, key, S_OR(val, ""));
+	if (res) {
+		astman_send_error(s, m, "Failed to update entry");
+	} else {
+		astman_send_ack(s, m, "Updated database successfully");
+	}
+	return 0;
+}
+
+static int manager_dbget(struct mansession *s, const struct message *m)
+{
+	const char *id = astman_get_header(m,"ActionID");
+	char idText[256] = "";
+	const char *family = astman_get_header(m, "Family");
+	const char *key = astman_get_header(m, "Key");
+	char tmp[MAX_DB_FIELD];
+	int res;
+
+	if (ast_strlen_zero(family)) {
+		astman_send_error(s, m, "No family specified.");
+		return 0;
+	}
+	if (ast_strlen_zero(key)) {
+		astman_send_error(s, m, "No key specified.");
+		return 0;
+	}
+
+	if (!ast_strlen_zero(id))
+		snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
+
+	res = ast_db_get(family, key, tmp, sizeof(tmp));
+	if (res) {
+		astman_send_error(s, m, "Database entry not found");
+	} else {
+		astman_send_ack(s, m, "Result will follow");
+		astman_append(s, "Event: DBGetResponse\r\n"
+				"Family: %s\r\n"
+				"Key: %s\r\n"
+				"Val: %s\r\n"
+				"%s"
+				"\r\n",
+				family, key, tmp, idText);
+		astman_append(s, "Event: DBGetComplete\r\n"
+				"%s"
+				"\r\n",
+				idText);
+	}
+	return 0;
+}
+
+static int manager_dbdel(struct mansession *s, const struct message *m)
+{
+	const char *family = astman_get_header(m, "Family");
+	const char *key = astman_get_header(m, "Key");
+	int res;
+
+	if (ast_strlen_zero(family)) {
+		astman_send_error(s, m, "No family specified.");
+		return 0;
+	}
+
+	if (ast_strlen_zero(key)) {
+		astman_send_error(s, m, "No key specified.");
+		return 0;
+	}
+
+	res = ast_db_del(family, key);
+	if (res)
+		astman_send_error(s, m, "Database entry not found");
+	else
+		astman_send_ack(s, m, "Key deleted successfully");
+
+	return 0;
+}
+
+static int manager_dbdeltree(struct mansession *s, const struct message *m)
+{
+	const char *family = astman_get_header(m, "Family");
+	const char *key = astman_get_header(m, "Key");
+	int res;
+
+	if (ast_strlen_zero(family)) {
+		astman_send_error(s, m, "No family specified.");
+		return 0;
+	}
+
+	if (!ast_strlen_zero(key))
+		res = ast_db_deltree(family, key);
+	else
+		res = ast_db_deltree(family, NULL);
+
+	if (res <= 0)
+		astman_send_error(s, m, "Database entry not found");
+	else
+		astman_send_ack(s, m, "Key tree deleted successfully");
+
+	return 0;
+}
+
+int astdb_init(void)
+{
+	if (ast_realtime_require_field(ast_db_family, ast_db_key, RQ_CHAR, 256, ast_db_value, RQ_CHAR, 256, SENTINEL)) {
+		ast_log(LOG_ERROR, "Could not find proper internal database backend.\n");
+		return -1;
+	}
+	ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
+	ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
+	ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
+	ast_manager_register_xml("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
+	ast_manager_register_xml("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
+	return 0;
+}

Modified: team/twilson/sqlite3_playground/main/loader.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/loader.c?view=diff&rev=341868&r1=341867&r2=341868
==============================================================================
--- team/twilson/sqlite3_playground/main/loader.c (original)
+++ team/twilson/sqlite3_playground/main/loader.c Sat Oct 22 01:15:01 2011
@@ -270,6 +270,7 @@
 	{ "indications", ast_indications_reload },
 	{ "cel",        ast_cel_engine_reload },
 	{ "plc",        ast_plc_reload },
+	{ "sqlite3",    realtime_sqlite3_reload },
 	{ NULL, 	NULL }
 };
 

Copied: team/twilson/sqlite3_playground/main/realtime_sqlite3.c (from r341867, team/twilson/sqlite3_playground/res/res_config_sqlite3.c)
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/realtime_sqlite3.c?view=diff&rev=341868&p1=team/twilson/sqlite3_playground/res/res_config_sqlite3.c&r1=341867&p2=team/twilson/sqlite3_playground/main/realtime_sqlite3.c&r2=341868
==============================================================================
--- team/twilson/sqlite3_playground/res/res_config_sqlite3.c (original)
+++ team/twilson/sqlite3_playground/main/realtime_sqlite3.c Sat Oct 22 01:15:01 2011
@@ -40,9 +40,8 @@
 
 #include <sqlite3.h>
 
-#include "asterisk/module.h"
+#include "asterisk/_private.h"
 #include "asterisk/config.h"
-#include "asterisk/paths.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/lock.h"
 #include "asterisk/utils.h"
@@ -1141,13 +1140,13 @@
 	return 0;
 }
 
-static int reload(void)
+int realtime_sqlite3_reload(void)
 {
 	parse_config(1);
 	return 0;
 }
 
-static int unload_module(void)
+static void realtime_sqlite3_atexit(void)
 {
 	ast_mutex_lock(&config_lock);
 	ao2_callback(databases, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, stop_batch_cb, NULL);
@@ -1155,33 +1154,25 @@
 	databases = NULL;
 	ast_config_engine_deregister(&sqlite3_config_engine);
 	ast_mutex_unlock(&config_lock);
-
-	return 0;
-}
-
-static int load_module(void)
+}
+
+int realtime_sqlite3_init(void)
 {
 	if (!((databases = ao2_container_alloc(DB_BUCKETS, db_hash_fn, db_cmp_fn)))) {
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
+		return -1;
+	}
+
+	ast_register_atexit(realtime_sqlite3_atexit);
 	if (parse_config(0)) {
 		ao2_ref(databases, -1);
-		return AST_MODULE_LOAD_FAILURE;
+		return -1;
 	}
 
 	if (!(ast_config_engine_register(&sqlite3_config_engine))) {
 		ast_log(LOG_ERROR, "The config API must have changed, this shouldn't happen.\n");
 		ao2_ref(databases, -1);
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
-	return AST_MODULE_LOAD_SUCCESS;
-}
-
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite 3 realtime config engine",
-	.load = load_module,
-	.unload = unload_module,
-	.reload = reload,
-	.load_pri = AST_MODPRI_REALTIME_DRIVER,
-);
+		return -1;
+	}
+
+	return 0;
+}




More information about the asterisk-commits mailing list