[Freeswitch-trunk] [commit] r14023 - freeswitch/trunk/contrib/ledr/mod_xml_odbc

FreeSWITCH SVN ledr at freeswitch.org
Sun Jun 28 12:19:08 PDT 2009


Author: ledr
Date: Sun Jun 28 14:19:08 2009
New Revision: 14023

Log:
cleaned up a bit, added comments, moved odbc-dsn to settings tag instead of per binding - perhaps re-add this later when memory leaks are fixed


Modified:
   freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c
   freeswitch/trunk/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml

Modified: freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c
==============================================================================
--- freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c	(original)
+++ freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c	Sun Jun 28 14:19:08 2009
@@ -35,10 +35,10 @@
 #include <switch.h>
 
 typedef enum {
-	XML_ODBC_CONFIG = 0,
+	XML_ODBC_CONFIGURATION = 0,
 	XML_ODBC_DIRECTORY = 0,
 	XML_ODBC_DIALPLAN = 0,
-	XML_ODBC_PHRASE = 0
+	XML_ODBC_PHRASES = 0
 } xml_odbc_query_type_t;
 
 SWITCH_MODULE_LOAD_FUNCTION(mod_xml_odbc_load);
@@ -52,16 +52,12 @@
 
 #define XML_ODBC_SYNTAX "[debug_on|debug_off|render_template]"
 
-static int logi = 0;
-static void logger(char *str)
-{
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG[%i] [%s]\n", ++logi, str);
-}
-
 static struct {
-	char *dbname;
 	char *odbc_dsn;
-	char *template;
+	char *configuration_template_name;
+	char *directory_template_name;
+	char *dialplan_template_name;
+	char *phrases_template_name;
 	switch_xml_t templates_tag;
 	switch_mutex_t *mutex;
 	switch_memory_pool_t *pool;
@@ -150,6 +146,7 @@
 
 	xml_odbc_query_helper_t query_helper;
 
+	/* special case xml-odbc-do - this tag is not copied, but action is done */
 	if (!strcasecmp(xml_in->name, "xml-odbc-do")) {
 		char *name = NULL;
 		char *value = NULL, *new_value = NULL;
@@ -200,6 +197,9 @@
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring unknown xml-odbc-do name=[%s]\n", name);
 		}
 
+		switch_safe_free(new_value);
+
+	/* just copy current tag xml_in to xml_out and recurse for all children */
 	} else {
 
 		/* set name if current root node */
@@ -273,22 +273,22 @@
 		//switch_xml_set_attr_d(xml_out, "type", "freeswitch/xml");
 	}
 
-	xml_odbc_query_type_t query_type;
-
+	char *template_name;
 	int off = 0, ret = 1;
 
 	if (!binding) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No bindings... sorry bud returning now\n");
 		return NULL;
 	}
+
 	if (!strcmp(section, "configuration")) {
-		query_type = XML_ODBC_CONFIG;
+		template_name = strdup(globals.configuration_template_name);
 	} else if (!strcmp(section, "directory")) {
-		query_type = XML_ODBC_DIRECTORY;
+		template_name = strdup(globals.directory_template_name);
 	} else if (!strcmp(section, "dialplan")) {
-		query_type = XML_ODBC_DIALPLAN;
+		template_name = strdup(globals.dialplan_template_name);
 	} else if (!strcmp(section, "phrases")) {
-		query_type = XML_ODBC_PHRASE;
+		template_name = strdup(globals.phrases_template_name);
 	} else {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid section\n");
 		return NULL;
@@ -301,7 +301,7 @@
 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG in xml_odbc_search, header [%s]=[%s]\n", hi->name, hi->value);
 				}
 			}
-			xml_odbc_render_template(globals.template, params, xml_out, &off); // TODO globals.template should be replace with something specific for this section
+			xml_odbc_render_template(template_name, params, xml_out, &off);
 		}
 	} else {
 		goto cleanup;
@@ -327,22 +327,25 @@
 static switch_status_t do_config()
 {
 	char *cf = "xml_odbc.conf";
-	switch_xml_t cfg, xml, bindings_tag, binding_tag, templates_tag, param;
+	switch_xml_t cfg, xml, settings_tag, bindings_tag, binding_tag, templates_tag, param;
 	xml_binding_t *binding = NULL;
 
-	switch_status_t status = SWITCH_STATUS_SUCCESS;
+	switch_status_t status = SWITCH_STATUS_FALSE;
 
 	char *odbc_user = NULL;
 	char *odbc_pass = NULL;
-	char *sql = NULL;
-
-logger("X");
+	int binding_count = 0;
 
 	if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
 		return SWITCH_STATUS_TERM;
 	}
 
+	if (!(settings_tag = switch_xml_child(cfg, "settings"))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing <settings> tag!\n");
+		goto done;
+	}
+
 	if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing <bindings> tag!\n");
 		goto done;
@@ -353,8 +356,36 @@
 		goto done;
 	}
 
+// is this allowed ? or should I copy the entire tree ?
 	globals.templates_tag = templates_tag;
 
+	/* get all the settings */
+	for (param = switch_xml_child(settings_tag, "param"); param; param = param->next) {
+		char *var = NULL;
+		char *val = NULL;
+
+		var = (char *) switch_xml_attr_soft(param, "name");
+		val = (char *) switch_xml_attr_soft(param, "value");
+
+		/* set globals.odbc_dsn */
+		if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
+			globals.odbc_dsn = switch_core_strdup(globals.pool, val);
+			if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
+				*odbc_user++ = '\0';
+				if ((odbc_pass = strchr(odbc_user, ':'))) {
+					*odbc_pass++ = '\0';
+				}
+			}
+		}
+	}
+
+	/* check if odbc_dsn is set */
+	if (!globals.odbc_dsn) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No odbc-dsn setting is set!\n");
+		goto done;
+	}
+
+	/* get all bindings */
 	for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) {
 		char *bname = (char*) switch_xml_attr_soft(binding_tag, "name");
 
@@ -365,22 +396,23 @@
 			var = (char *) switch_xml_attr_soft(param, "name");
 			val = (char *) switch_xml_attr_soft(param, "value");
 
-			if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
-				globals.odbc_dsn = switch_core_strdup(globals.pool, val);
-				if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
-					*odbc_user++ = '\0';
-					if ((odbc_pass = strchr(odbc_user, ':'))) {
-						*odbc_pass++ = '\0';
-					}
+			if (!strcasecmp(var, "template") && !switch_strlen_zero(val)) {
+				if (!strcmp(bname, "configuration")) {
+					globals.configuration_template_name = strdup(val);
+				} else if (!strcmp(bname, "directory")) {
+					globals.directory_template_name = strdup(val);
+				} else if (!strcmp(bname, "dialplan")) {
+					globals.dialplan_template_name = strdup(val);
+				} else if (!strcmp(bname, "phrases")) {
+					globals.phrases_template_name = strdup(val);
+				} else {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid binding name [%s]\n", bname);
+					goto done;
 				}
-			} else if (!strcasecmp(var, "template") && !switch_strlen_zero(val)) {
-				globals.template = strdup(val);
 			}
 		}
 
-		if (switch_strlen_zero(globals.odbc_dsn) || switch_strlen_zero(odbc_user) || switch_strlen_zero(odbc_pass)) {
-			globals.dbname = "xml_odbc";
-		}
+		binding_count++;
 
 		if (!(binding = malloc(sizeof(*binding)))) {
 			goto done;
@@ -390,11 +422,19 @@
 		binding->bindings = strdup(bname);
 
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML ODBC Fetch Function [%s]\n",
-			switch_strlen_zero(bname) ? "N/A" : bname, binding->bindings ? binding->bindings : "all");
+			switch_strlen_zero(bname) ? "N/A" : bname, binding->bindings);
+
 		switch_xml_bind_search_function(xml_odbc_search, switch_xml_parse_section_string(bname), binding);
+
 		binding = NULL;
 	}
 
+	/* check if a binding is set */
+	if (binding_count == 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No binding is set, is this really what you want?\n");
+	}
+
+	/* make odbc connection */
 	if (switch_odbc_available() && globals.odbc_dsn) {
 
 		if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
@@ -412,10 +452,10 @@
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.odbc_dsn);
 	}
 
-  done:
-
-	switch_safe_free(sql);
+	/* all went fine */
+	status = SWITCH_STATUS_SUCCESS;
 
+  done:
 	return status;
 }
 
@@ -438,16 +478,16 @@
 
 	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
 
-	SWITCH_ADD_API(xml_odbc_api_interface, "xml_odbc", "XML ODBC", xml_odbc_function, XML_ODBC_SYNTAX);
-	switch_console_set_complete("add xml_odbc debug_on");
-	switch_console_set_complete("add xml_odbc debug_off");
-	switch_console_set_complete("add xml_odbc render_template");
-
 	if (do_config() != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load xml_odbc config file\n");
 		return SWITCH_STATUS_FALSE;
 	}
 
+	SWITCH_ADD_API(xml_odbc_api_interface, "xml_odbc", "XML ODBC", xml_odbc_function, XML_ODBC_SYNTAX);
+	switch_console_set_complete("add xml_odbc debug_on");
+	switch_console_set_complete("add xml_odbc debug_off");
+	switch_console_set_complete("add xml_odbc render_template");
+
 	/* indicate that the module should continue to be loaded */
 	return SWITCH_STATUS_SUCCESS;
 }
@@ -455,6 +495,9 @@
 
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_odbc_shutdown)
 {
+	switch_odbc_handle_disconnect(globals.master_odbc);
+	switch_odbc_handle_destroy(&globals.master_odbc);
+
 	return SWITCH_STATUS_SUCCESS;
 }
 

Modified: freeswitch/trunk/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml
==============================================================================
--- freeswitch/trunk/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml	(original)
+++ freeswitch/trunk/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml	Sun Jun 28 14:19:08 2009
@@ -1,25 +1,25 @@
 <configuration name="xml_odbc.conf" description="XML ODBC Configuration">
 
+  <settings>
+    <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
+  </settings>
+
   <bindings>
 <!--
     <binding name="configuration">
-      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
       <param name="template" value="configuration"/>
     </binding>
 -->
     <binding name="directory">
-      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
       <!-- <param name="template" value="directory_swk"/> -->
       <param name="template" value="directory"/>
     </binding>
 <!--
     <binding name="dialplan">
-      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
       <param name="template" value="dialplan"/>
     </binding>
 
     <binding name="phrases">
-      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
       <param name="template" value="phrases"/>
     </binding>
 -->



More information about the Freeswitch-trunk mailing list