[Freeswitch-branches] [commit] r3881 - freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql

Freeswitch SVN outcast at freeswitch.org
Mon Jan 1 04:28:23 EST 2007


Author: outcast
Date: Mon Jan  1 04:28:22 2007
New Revision: 3881

Added:
   freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/
   freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/Makefile
   freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/README
   freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/mod_xml_mysql.c

Log:
new mod_xml_mysql module. Just testing some stuff. I will have better version soon.




Added: freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/Makefile
==============================================================================
--- (empty file)
+++ freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/Makefile	Mon Jan  1 04:28:22 2007
@@ -0,0 +1,20 @@
+FLAGS  += $(shell mysql_config --include)
+LDFLAGS += $(shell mysql_config --libs)
+
+all:	depends $(MODNAME).$(DYNAMIC_LIB_EXTEN)
+
+depends:
+		
+
+
+$(MODNAME).$(DYNAMIC_LIB_EXTEN): $(MODNAME).c
+	$(CC) $(CFLAGS) -fPIC -c $(MODNAME).c -o $(MODNAME).o 
+	$(CC) $(SOLINK) -o $(MODNAME).$(DYNAMIC_LIB_EXTEN)  $(MODNAME).o $(LDFLAGS)
+
+
+clean:
+	rm -fr *.$(DYNAMIC_LIB_EXTEN) *.o *~
+	rm -fr *.$(DYNAMIC_LIB_EXTEN) *.so *~
+
+install:
+	cp -f $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(PREFIX)/mod

Added: freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/README
==============================================================================
--- (empty file)
+++ freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/README	Mon Jan  1 04:28:22 2007
@@ -0,0 +1,10 @@
+mod_xml_mysql
+-----
+
+
+This is an experimental module that I have written to load Freeswitch's configuration from a MySQL database. It may seem a bit crude. This is my first module for Freeswitch and the first time I have used the MySQL API. I would highly recommend not to use this in production. The database schema currently is not included due to the fact it is lame. I will come up with a better database schema and version of the module hopfully by mid-Jan. If you have any quesitons please email me.
+
+
+Outcast aka James Jones <james at virtualrealmsoftware.com>
+
+

Added: freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/mod_xml_mysql.c
==============================================================================
--- (empty file)
+++ freeswitch/branches/outcast/src/mod/xml_int/mod_xml_mysql/mod_xml_mysql.c	Mon Jan  1 04:28:22 2007
@@ -0,0 +1,219 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct at yahoo.com>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthmct at yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Outcast aka James Jones <james at virtualrealmsoftware.com>
+ * Michael Jerris <mike at jerris.com> 
+ *
+ * mod_xml_mysql.c -- CURL XML Gateway
+ *
+ * Based on mod_xml_curl by Michael Jerris <mike at jerris.com>
+ */
+
+
+#include <switch.h>
+#include <mysql/mysql.h>
+
+static const char modname[] = "mod_xml_mysql";
+
+
+static struct {
+	char *dbname;
+	char *bindings;
+    char *dbuser;
+    char *dbpass;
+    char *dbhost;
+    char *debug;
+    MYSQL mysql;
+} globals;
+
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dbname, globals.dbname);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_bindings, globals.bindings);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dbuser, globals.dbuser);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dbpass, globals.dbpass);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dbhost, globals.dbhost);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_debug, globals.debug);
+
+
+static switch_xml_t xml_mysql_fetch(char *section,
+								  char *tag_name,
+								  char *key_name,
+								  char *key_value,
+								  char *params)
+{
+    char *mysql_section = NULL;
+    char *mysql_tag_name = NULL;
+    char *mysql_key_name = NULL;
+    char *mysql_key_value = NULL;
+    char *mysql_params = NULL;
+    char *mysql_xml = NULL;
+    char mysql_query[1000];
+	switch_xml_t xml = NULL;
+    MYSQL_RES *result;
+    MYSQL_ROW row;
+
+    if (!((mysql_section = section) && (mysql_tag_name = tag_name) && (mysql_key_name = key_name) && (mysql_key_value = key_value))) {
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
+        return NULL;
+    }
+    
+    snprintf(mysql_query, sizeof(mysql_query), "SELECT data FROM xml_templates WHERE section = \"%s\" and name = \"%s\"", mysql_section, mysql_key_value);
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Built query: %s\nSize: %d\n", mysql_query, sizeof(mysql_query));
+    
+    if(mysql_real_query(&globals.mysql, mysql_query, strlen(mysql_query)) == 0){
+
+        result = mysql_store_result(&globals.mysql);
+        if(result){
+            
+            if(mysql_num_fields(result) == 1) {
+                if(!(row = mysql_fetch_row(result))) {
+
+                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database returned an empty result\n");
+                    return NULL;
+                } else {
+
+                    mysql_xml = strdup(row[0]);
+                }
+            } else {
+
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Incorrect amount of fields retrived please check MySQL query.\n");
+                return NULL; 
+           }
+        } else {
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No records found for %s in database\n", mysql_key_value);
+            return NULL;
+        }
+    } else {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Mysql Error: %s\n", mysql_error(&globals.mysql));
+        return NULL;
+    } 
+            
+    if(mysql_query == NULL) {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database returned a null value\n");
+        return NULL;
+    }
+
+	if (!(xml = switch_xml_parse_str(mysql_xml, strlen(mysql_xml)))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n");
+	}
+    mysql_free_result(result);
+	return xml;
+}
+
+
+static switch_loadable_module_interface_t xml_mysql_module_interface = {
+	/*.module_name */ modname,
+	/*.endpoint_interface */ NULL,
+	/*.timer_interface */ NULL,
+	/*.dialplan_interface */ NULL,
+	/*.codec_interface */ NULL,
+	/*.application_interface */ NULL,
+	/*.api_interface */ NULL,
+	/*.file_interface */ NULL,
+	/*.speech_interface */ NULL,
+	/*.directory_interface */ NULL
+};
+
+static switch_status_t do_config(void) 
+{
+	char *cf = "xml_mysql.conf";
+	switch_xml_t cfg, xml, settings, param;
+
+	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 = switch_xml_child(cfg, "settings"))) {
+		for (param = switch_xml_child(settings, "param"); param; param = param->next) {
+			char *var = (char *) switch_xml_attr_soft(param, "name");
+			char *val = (char *) switch_xml_attr_soft(param, "value");
+
+            if (!strcasecmp(var, "dbname")) {
+				char *bindings = (char *) switch_xml_attr_soft(param, "bindings");
+				set_global_bindings(bindings);
+				set_global_dbname(val);
+			} else if (!strcasecmp(var, "dbhost")) {
+                set_global_dbhost(val);
+            } else if (!strcasecmp(var, "dbuser")) {
+                set_global_dbuser(val);
+            } else if (!strcasecmp(var, "dbpass")) {
+                set_global_dbpass(val);
+            } else if (!strcasecmp(var, "dbdebug")) {
+                set_global_debug(val);
+            }
+		}
+	}
+
+	switch_xml_free(xml);
+
+	return globals.dbname ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+}
+
+
+SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
+{
+	/* connect my internal structure to the blank pointer passed to me */
+	*module_interface = &xml_mysql_module_interface;
+
+	if (do_config() == SWITCH_STATUS_SUCCESS) {
+        if(mysql_init(&globals.mysql)==NULL) {
+
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to iniate MySQL Connection\n");
+            return SWITCH_STATUS_FALSE;
+        }
+        
+        if (!mysql_real_connect(&globals.mysql,globals.dbhost, globals.dbuser, globals.dbpass, globals.dbname,0,NULL,0)) {
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to connect to Database: %s @ %s\n", globals.dbname, globals.dbhost);
+            return SWITCH_STATUS_FALSE;
+        }
+            
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding MySQL Fetch Function [%s] [%s]\n", 
+                          globals.dbname, globals.bindings ? globals.bindings : "all");
+		switch_xml_bind_search_function(xml_mysql_fetch, switch_xml_parse_section_string(globals.bindings));
+	} else {
+        return SWITCH_STATUS_FALSE;
+    }
+
+	/* indicate that the module should continue to be loaded */
+	return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
+{
+    mysql_close(&globals.mysql);
+	return SWITCH_STATUS_SUCCESS;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:nil
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */



More information about the Freeswitch-branches mailing list