[asterisk-commits] branch tilghman/res_odbc_rewrite r18131 - /team/tilghman/res_odbc_rewrite/res/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Apr 7 00:39:32 MST 2006


Author: tilghman
Date: Fri Apr  7 02:39:31 2006
New Revision: 18131

URL: http://svn.digium.com/view/asterisk?rev=18131&view=rev
Log:
Add missing function reload() since we've disabled the unloading of this module

Modified:
    team/tilghman/res_odbc_rewrite/res/res_odbc.c

Modified: team/tilghman/res_odbc_rewrite/res/res_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/res/res_odbc.c?rev=18131&r1=18130&r2=18131&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_odbc.c Fri Apr  7 02:39:31 2006
@@ -61,6 +61,7 @@
 	unsigned int haspool:1;         /* Boolean - TDS databases need this */
 	unsigned int limit:10;          /* Gives a limit of 1023 maximum */
 	unsigned int count:10;          /* Running count of pooled connections */
+	unsigned int delme:1;			/* Purge the class */
 	AST_LIST_HEAD(, odbc_obj) odbc_obj;
 };
 
@@ -511,6 +512,150 @@
 
 LOCAL_USER_DECL;
 
+int reload(void)
+{
+	static char *cfg = "res_odbc.conf";
+	struct ast_config *config;
+	struct ast_variable *v;
+	char *cat, *dsn, *username, *password;
+	int enabled, pooling, limit;
+	int connect = 0, res = 0;
+
+	struct odbc_class *new, *class;
+	struct odbc_obj *current;
+
+	/* First, mark all to be purged */
+	AST_LIST_LOCK(&odbc_list);
+	AST_LIST_TRAVERSE(&odbc_list, class, list) {
+		class->delme = 1;
+	}
+
+	config = ast_config_load(cfg);
+	if (config) {
+		for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
+			if (!strcasecmp(cat, "ENV")) {
+				for (v = ast_variable_browse(config, cat); v; v = v->next) {
+					setenv(v->name, v->value, 1);
+					ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
+				}
+			} else {
+				/* Reset all to defaults for each class of odbc connections */
+				dsn = username = password = NULL;
+				enabled = 1;
+				connect = 0;
+				pooling = 0;
+				limit = 0;
+				for (v = ast_variable_browse(config, cat); v; v = v->next) {
+					if (!strcasecmp(v->name, "pooling")) {
+						pooling = 1;
+					} else if (!strcasecmp(v->name, "limit")) {
+						sscanf(v->value, "%d", &limit);
+						if (ast_true(v->value) && !limit) {
+							ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
+							limit = 1023;
+						} else if (ast_false(v->value)) {
+							ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
+							enabled = 0;
+							break;
+						}
+					} else if (!strcasecmp(v->name, "enabled")) {
+						enabled = ast_true(v->value);
+					} else if (!strcasecmp(v->name, "pre-connect")) {
+						connect = ast_true(v->value);
+					} else if (!strcasecmp(v->name, "dsn")) {
+						dsn = v->value;
+					} else if (!strcasecmp(v->name, "username")) {
+						username = v->value;
+					} else if (!strcasecmp(v->name, "password")) {
+						password = v->value;
+					}
+				}
+
+				if (enabled && !ast_strlen_zero(dsn)) {
+					/* First, check the list to see if it already exists */
+					AST_LIST_TRAVERSE(&odbc_list, class, list) {
+						if (!strcmp(class->name, cat)) {
+							class->delme = 0;
+							break;
+						}
+					}
+
+					if (class) {
+						new = class;
+					} else {
+						new = ast_calloc(1, sizeof(*new));
+					}
+
+					if (!new) {
+						ast_log(LOG_ERROR, "Memory error while loading configuration.\n");
+						res = -1;
+						break;
+					}
+
+					if (cat)
+						ast_copy_string(new->name, cat, sizeof(new->name));
+					if (dsn)
+						ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
+					if (username)
+						ast_copy_string(new->username, username, sizeof(new->username));
+					if (password)
+						ast_copy_string(new->password, password, sizeof(new->password));
+
+					if (!class) {
+						SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
+						res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
+
+						if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+							ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
+							SQLFreeHandle(SQL_HANDLE_ENV, new->env);
+							AST_LIST_UNLOCK(&odbc_list);
+							return res;
+						}
+					}
+
+					if (pooling) {
+						new->haspool = pooling;
+						if (limit) {
+							new->limit = limit;
+						} else {
+							ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
+							new->limit = 5;
+						}
+					}
+
+					if (class) {
+						ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
+					} else {
+						odbc_register_class(new, connect);
+						ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
+					}
+				}
+			}
+		}
+		ast_config_destroy(config);
+	}
+
+	/* Purge classes that we know can go away (pooled with 0, only) */
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
+		if (class->delme && class->haspool && class->count == 0) {
+			AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
+				AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
+				odbc_obj_disconnect(current);
+				ast_mutex_destroy(&current->lock);
+				free(current);
+			}
+			AST_LIST_TRAVERSE_SAFE_END;
+
+			AST_LIST_REMOVE_CURRENT(&odbc_list, list);
+			free(class);
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&odbc_list);
+
+	return 0;
+}
+
 int unload_module(void)
 {
 	/* Prohibit unloading */



More information about the asterisk-commits mailing list