[Freeswitch-branches] [commit] r6829 - freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr

Freeswitch SVN mishehu at freeswitch.org
Sun Dec 16 13:36:25 EST 2007


Author: mishehu
Date: Sun Dec 16 13:36:24 2007
New Revision: 6829

Added:
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.cpp
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.h
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.cpp
   freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.h

Log:
Added in the base mysql pool files.

Added: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.cpp
==============================================================================
--- (empty file)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.cpp	Sun Dec 16 13:36:24 2007
@@ -0,0 +1,107 @@
+#include "mysqlconfclass.h"
+
+Switch_Mysql_ConfClass::Switch_Mysql_ConfClass()
+{
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR,"Why did we make a Switch_Mysql_ConfClass object without a memory pool?\n");
+}
+
+Switch_Mysql_ConfClass::Switch_Mysql_ConfClass(switch_memory_pool_t *mem) : SwitchToMempool(mem)
+{
+	mysql_activated = 0;
+	mysql_threads = 1;
+	
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Switch_Mysql_ConfClass object with a memory pool initialized and ready.\n");
+}
+
+void Switch_Mysql_ConfClass::load_config()
+{
+	switch_mysql_db_config_t *tmp_dbconfig = (switch_mysql_db_config_t*) switch_core_alloc(memorypool,sizeof(switch_mysql_db_config_t));
+	tmp_dbconfig->port = 3306;
+	
+	char configfile[] = "mod_cdr_mysql.conf";
+	switch_xml_t cfg, xml, settings, param;
+		
+	const char *err;
+	switch_xml_t xml_root;
+	
+	if (!(xml = switch_xml_open_cfg(configfile, &cfg, NULL)))
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "open of %s failed\n", configfile);
+	else
+	{
+		int count_config_params = 0;  // Need to make sure all params are set before we load
+		if ((settings = switch_xml_child(cfg, "mod_cdr_mysql"))) 
+		{
+			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 (!strcmp(var, "db_hostname"))
+				{
+					if(val != 0)
+					{
+						strncpy(tmp_dbconfig->hostname,val,strlen(val));
+						count_config_params++;
+						switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Hostname: %s\n",val);
+					}
+				}
+				else if (!strcmp(var, "db_username")) 
+				{
+					if(val != 0)
+					{
+						strncpy(tmp_dbconfig->username,val,strlen(val));
+						count_config_params++;
+						switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Username: %s\n",val);
+					}
+				}
+				else if (!strcmp(var,"db_password"))
+				{
+					if(val != 0)
+					{
+						strncpy(tmp_dbconfig->password,val,strlen(val));
+						switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Password: %s\n",val);
+					}
+				}
+				else if(!strcmp(var,"db_dbname"))
+				{
+					if(val != 0)
+					{
+						strncpy(tmp_dbconfig->dbname,val,strlen(val));
+						count_config_params++;
+						switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Database: %s\n",val);
+					}
+				}
+				else if (!strcmp(var, "db_socket"))
+				{
+					if(val != 0)
+					{
+						tmp_dbconfig->socket_length = strlen(val);
+						tmp_dbconfig->socket = new char[tmp_dbconfig->socket_length+1];
+						strncpy(tmp_dbconfig->hostname,val,tmp_dbconfig->socket_length);
+						count_config_params++;
+					}
+				}
+				else if (!strcmp(var, "db_port"))
+				{
+					if(val != 0)
+						tmp_dbconfig->port = atoi(val);
+				}
+				else if (!strcmp(var, "mysql_threads"))
+				{
+					if(val != 0)
+					{
+						mysql_threads = atoi(val);
+						switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Number of threads: %s\n",val);
+					}
+				}
+			}
+			
+			if(count_config_params>=3 && mysql_threads)
+			{
+				mysql_activated = 1;
+				dbconfig = tmp_dbconfig;
+			}
+		}
+	}
+	switch_xml_free(xml);
+}

Added: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.h
==============================================================================
--- (empty file)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlconfclass.h	Sun Dec 16 13:36:24 2007
@@ -0,0 +1,46 @@
+//
+// C++ Interface: confclass
+//
+// Description: FreeSWITCH MySQL 4 or higher configuration header
+//
+//
+// Author: Yossi Neiman <freeswitch at cartissolutions.com>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#include <iostream>
+#include <switch_cpp.h>
+
+#ifndef SWITCHMYSQLCONFCLASS
+#define SWITCHMYSQLCONFCLASS
+
+struct switch_mysql_db_config {
+	char hostname[255];
+	char username[255];
+	char password[255];
+	char dbname[255];
+	unsigned int port;
+	char *socket;
+	switch_size_t socket_length;
+};
+
+typedef struct switch_mysql_db_config switch_mysql_db_config_t;
+
+class Switch_Mysql_ConfClass : public SwitchToMempool {
+	public:
+		ConfClass();
+		ConfClass(switch_memory_pool_t *);
+		~ConfClass();
+		void load_config();
+		switch_mysql_db_config_t* get_db_config() { return dbconfig; }
+		uint32_t get_num_mysql_threads() { return mysql_threads; }
+		bool get_mysql_activated() { return mysql_activated; }
+		
+	private:
+		uint32_t mysql_threads;
+		switch_mysql_db_config_t *dbconfig;
+		bool mysql_activated;
+};
+
+#endif

Added: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.cpp
==============================================================================
--- (empty file)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.cpp	Sun Dec 16 13:36:24 2007
@@ -0,0 +1,160 @@
+//
+// C++ Implementation: mysqlpool
+//
+// Description: This file implements the MySQL C++ routines, including the threadpool functionality.
+//
+//
+// Author: Yossi Neiman <freeswitch at cartissolutions.com>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include "mysqlpool.h"
+
+uint32_t Switch_MySQLpool::number_of_threads = 0;
+switch_mysql_db_config_t* Switch_MySQLpool::internal_db_configuration = 0;
+bool Switch_MySQLpool::activated = 0;
+
+Switch_MySQLpool::Switch_MySQLpool()
+{
+	connected = 0;
+}
+
+Switch_MySQLpool::Switch_MySQLpool( switch_memory_pool_t *mem) : SwitchToMempool::SwitchToMempool( mem )
+{
+	connected = 0;
+	conn = mysql_init(NULL);
+	switch_mutex_init(&lock,SWITCH_MUTEX_UNNESTED,memorypool);
+}
+
+Switch_MySQLpool::~ Switch_MySQLpool()
+{
+	disconnect();
+	switch_mutex_destroy(lock);
+}
+
+void Switch_MySQLpool::set_db_config(switch_mysql_db_config_t *db_config)
+{
+	switch_mutex_lock(lock);
+	
+	internal_db_configuration = db_config;
+	
+	if(connected)
+		mysql_close(conn);
+	
+	connected = 0;
+	
+	switch_mutex_unlock(lock);
+}
+
+bool Switch_MySQLpool::disconnect()
+{
+	if(conn)
+		mysql_close(conn);
+		
+	return 1;
+}
+
+bool Switch_MySQLpool::connect()
+{
+	switch_mutex_lock(lock);
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Connecting to the MySQL database.\n");
+	// mysql_options(conn, MYSQL_READ_DEFAULT_FILE, "");
+	//switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Hostname:\t%s\nUsername:\t%s\nPassword:\t%s\nPort:\t%s\nDatabase:\t%s\n",internal_db_configuration->hostname,internal_db_configuration->username,internal_db_configuration->password,internal_db_configuration->port,internal_db_configuration->dbname);
+	//if(internal_db_configuration->socket)
+	//	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Socket:\t%s\n",internal_db_configuration->socket);
+	
+	if(mysql_real_connect(conn,internal_db_configuration->hostname,internal_db_configuration->username,internal_db_configuration->password,internal_db_configuration->dbname,internal_db_configuration->port,internal_db_configuration->socket,CLIENT_MULTI_STATEMENTS  ) == NULL)
+	{
+		const char *error1 = mysql_error(conn);
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot connect to MySQL Server.  The error was: %s\n", error1);
+		errorstate = 1;
+	}
+	else
+		conn->reconnect = false;
+	
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Connection to the MySQL database complete.\n");
+	//mysql_autocommit(conn,0);
+	switch_mutex_unlock(lock);
+	return 1;
+}
+
+int Switch_MySQLpool::flush_results(MYSQL_RES *tempres)
+{
+	int count = 0, next_result_result = 0;
+	
+	if(!errorstate)
+	{
+		mysql_free_result(tempres);
+		for( next_result_result = mysql_next_result(conn); next_result_result != 0; count++)
+		{
+			tempres = mysql_store_result(conn);
+			mysql_free_result(tempres);
+		}
+	}
+	
+	return count;
+}
+
+void Switch_MySQLpool::myerror(std::string query_name)
+{
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR,"There was an error processing %s.\n",query_name.c_str());
+	unsigned int mysql_errorno = mysql_errno(conn);
+	const char *mysql_choked = mysql_error(conn);
+	switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR,"The error was #%d %s\n",mysql_errorno,mysql_choked);
+}
+
+MYSQL_RES* Switch_MySQLpool::process_sp_query(std::string &query, std::string query_name, bool returns_results = 0)
+{
+	MYSQL_RES *return_me = 0;
+	
+	if(!errorstate)
+	{
+		ping_the_db();
+		
+		if(mysql_real_query(conn,query.c_str(),query.size()))
+			myerror(query_name);
+		else
+		{
+			return_me = mysql_store_result(conn);
+			if(returns_results)
+			{
+				while(return_me != NULL && mysql_field_count(conn) < 1)
+				{
+					switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_INFO,"Cycling thru res_%s.\n",query_name.c_str());
+					mysql_free_result(return_me);
+					mysql_next_result(conn);
+					return_me = mysql_store_result(conn);
+				}
+			}
+		}
+	}
+	return return_me;
+}
+
+void Switch_MySQLpool::ping_the_db()
+{
+	for(int mysql_ping_result = -1, count = 0; mysql_ping_result != 0 && count < 5; count++)
+	{
+		mysql_ping_result = mysql_ping(conn);
+		if(mysql_ping_result)
+		{
+			switch(mysql_ping_result)
+			{
+				case CR_SERVER_GONE_ERROR:
+				case CR_SERVER_LOST:
+				{
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We lost connection to the MySQL server.  Trying to reconnect.\n");
+					connect();
+					break;
+				}
+				default:
+				{
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We have encountered an unknown error when pinging the MySQL server.  Attempting to reconnect anyways.\n");
+					connect();
+				}
+			}
+		}
+	}
+}

Added: freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.h
==============================================================================
--- (empty file)
+++ freeswitch/branches/mishehu/src/mod/event_handlers/mod_cdr/mysqlpool.h	Sun Dec 16 13:36:24 2007
@@ -0,0 +1,57 @@
+//
+// C++ Interface: mysqlpool
+//
+// Description: This file describes some MySQL related functions, including a threadpool of connections
+// to a MySQL database.
+//
+//
+// Author: Yossi Neiman <freeswitch at cartissolutions.com>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include <iostream>
+#include <switch.h>
+#include <errmsg.h>
+#include <my_global.h>
+#include <my_dbug.h>
+#include <my_pthread.h>
+#include <mysql.h>
+#include <switch_cpp.h>
+#include "mysqlconfclass.h"
+
+#ifndef SWITCH_MYSQL_DB_MUTEX
+#define SWITCH_MYSQL_DB_MUTEX
+
+
+class Switch_MySQLpool:public SwitchToMempool {
+	public:
+		Switch_MySQLpool();
+		Switch_MySQLpool(switch_memory_pool_t *);
+		~Switch_MySQLpool();
+		bool connect();
+		bool disconnect();
+		int flush_results(MYSQL_RES*);
+		MYSQL_RES* process_sp_query(std::string&, std::string, bool);
+		void myerror(std::string query_name);
+		// void connect_to_database();
+		MYSQL* get_conn() { return conn; }
+		static uint32_t get_number_of_threads() { return number_of_threads; }
+		bool is_active() { return activated; }
+		void set_db_config(switch_mysql_db_config_t *db_config);
+	
+	private:
+		MYSQL *conn;
+		MYSQL_STMT *stmt;
+		MYSQL_STMT *stmt_chanvars;
+		switch_mutex_t *lock;
+		static uint32_t number_of_threads;
+		static bool activated;
+		bool connected;
+		bool errorstate;
+		static switch_mysql_db_config_t *internal_db_configuration;
+		void ping_the_db();
+};
+
+#endif



More information about the Freeswitch-branches mailing list