Author: voctel Date: Sat Sep 23 16:50:30 2006 New Revision: 2808 Modified: freeswitch/branches/voctel/conf/freeswitch.xml freeswitch/branches/voctel/src/mod/applications/mod_phonecard/mod_phonecard.c Log: laying the ground work Modified: freeswitch/branches/voctel/conf/freeswitch.xml ============================================================================== --- freeswitch/branches/voctel/conf/freeswitch.xml (original) +++ freeswitch/branches/voctel/conf/freeswitch.xml Sat Sep 23 16:50:30 2006 @@ -378,7 +378,20 @@ </profile> </profiles> </configuration> + + <configuration name="phonecard.conf" description="Phonecard Configuration"> + <settings> + <param name="debug" value="0"/> + <param name="db_hostname" value="localhost"> --> + <param name="db_port" value="4569"/> + <param name="db_name" value="freeswitch_phonecard"/> + <param name="db_username" value=""/> + <param name="db_password" value=""/> + <param name="sound_directory" value=""/> + </settings> + </configuration> </section> + <section name="dialplan" description="Regex/XML Dialplan"> <!-- Valid fields in conditions: "dialplan, caller_id_name, ani, ani2, caller_id_number, Modified: freeswitch/branches/voctel/src/mod/applications/mod_phonecard/mod_phonecard.c ============================================================================== --- freeswitch/branches/voctel/src/mod/applications/mod_phonecard/mod_phonecard.c (original) +++ freeswitch/branches/voctel/src/mod/applications/mod_phonecard/mod_phonecard.c Sat Sep 23 16:50:30 2006 @@ -1,160 +1,196 @@ -/* - * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@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@yahoo.com> - * Portions created by the Initial Developer are Copyright (C) - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Richard Cook <richard@voctel.com> - * Anthony Minessale II <anthmct@yahoo.com> - * - * - * mod_phonecard.c -- Calling Card Module - * - */ -#include <switch.h> - -//-- PostgreSQL -#include <libpq-fe.h> - -static const char modname[] = "mod_phonecard"; -static const char global_app_name[] = "phonecard"; -static char *global_cf_name = "phonecard.conf"; - -static char *db_hostname = NULL; -static char *db_name = NULL; -static char *db_username = NULL; -static char *db_password = NULL; -static char *db_language = NULL; -static char *db_table = NULL; -static char *db_port = NULL; - -static int db_connected = 0; - -//-- PostgreSQL declarations -PGconn *DBConn; - -switch_mutex_t *db_lock; - -//-- function used to connect/verify database connection -static int phonecard_dbconnected(void) { - PGresult *result; - - //-- check to see if we are already connected - if (db_connected) { - result = PQexec(DBConn,"SELECT 1"); - - PQclear(result); - - //-- check for connectivity - if (PQstatus(DBConn)!= CONNECTION_OK) { - db_connected = 0; - - PQreset(DBConn); - - //-- log the database error - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "FATAL - Database connection lost '%s'\n", db_hostname); - } - else { - db_connected = 1; - } - } - else { - //-- we are not connected - so make the initial connection - DBConn = PQsetdbLogin(db_hostname, db_port, NULL, NULL, db_name, db_username, db_password); - if (PQstatus(DBConn) != CONNECTION_OK) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "FATAL - Couldn't connect to database server '%s'. \n", db_hostname); - db_connected = 0; - } - else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Successfully connected to database '%s:%s'.\n", db_hostname, db_name); - db_connected = 1; - } - } - - return db_connected; -} - -//-- main application routine -static void phonecard_main(switch_core_session_t *session, char *data) -{ - char *mydata = switch_core_session_strdup(session, data); - - //-- +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@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@yahoo.com> + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Richard Cook <richard@voctel.com> + * Anthony Minessale II <anthmct@yahoo.com> + * + * + * mod_phonecard.c -- Calling Card Module + * + */ +#include <switch.h> + +//-- PostgreSQL +#include <libpq-fe.h> + +static const char modname[] = "mod_phonecard"; +static const char global_app_name[] = "phonecard"; +static char *global_cf_name = "phonecard.conf"; + +static switch_memory_pool_t *module_pool = NULL; + +static struct { + int debug; + char *db_hostname; + char *db_port; + char *db_name; + char *db_username; + char *db_password; + int db_connected; + char *sound_folder; + switch_mutex_t *mutex; +} globals; + +//-- PostgreSQL declarations +PGconn *DBConn; + +switch_mutex_t *db_lock; + +//-- Function used to load the configuration file +static switch_status_t load_config(void) +{ + switch_xml_t cfg, xml, settings, param; + + memset(&globals, 0, sizeof(globals)); + + switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool); + + if (!(xml = switch_xml_open_cfg(global_cf_name, &cfg, NULL))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", global_cf_name); + 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 (!strcmp(var, "debug")) { + globals.debug = atoi(val); + } else if (!strcmp(var, "db_hostname")) { + globals.db_hostname = val; + } else if (!strcmp(var, "db_port")) { + globals.db_port = val; + } else if (!strcmp(var, "db_name")) { + globals.db_name = val; + } else if (!strcmp(var, "db_username")) { + globals.db_username = val; + } else if (!strcmp(var, "db_password")) { + globals.db_password = val; + } else if (!strcmp(var, "sound_folder")) { + globals.sound_folder = val; + } + } + } + + //-- set the defaults + globals.db_connected = 0; + + if (!globals.db_hostname) + globals.db_hostname = "localhost"; + + if (!globals.db_port) + globals.db_port = "5432"; + + + //-- clean up + switch_xml_free(xml); + + return SWITCH_STATUS_SUCCESS; +} + + +//-- main application routine +static void phonecard_main(switch_core_session_t *session, char *data) +{ + switch_channel_t *channel = NULL; + char *mydata = switch_core_session_strdup(session, data); + + channel = switch_core_session_get_channel(session); + assert(channel != NULL); + + //-- if (!mydata) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Pool Failure\n"); return; } - - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n"); -} - - -//-- application interface -static const switch_application_interface_t phonecard_application_interface = { - /*.interface_name */ global_app_name, - /*.application_function */ phonecard_main, NULL, NULL, NULL, - /*.next*/ NULL -}; - -//-- module interface -static switch_loadable_module_interface_t phonecard_module_interface = { - /*.module_name */ modname, - /*.endpoint_interface */ NULL, - /*.timer_interface */ NULL, - /*.dialplan_interface */ NULL, - /*.codec_interface */ NULL, - /*.application_interface */ &phonecard_application_interface, - /*.api_interface */ NULL, - /*.file_interface */ NULL, - /*.speech_interface */ NULL, - /*.directory_interface */ NULL -}; - -//-- called when the module is loaded -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 = &phonecard_module_interface; - - - - - /* indicate that the module should continue to be loaded */ - return SWITCH_STATUS_SUCCESS; -} - -/* - Called when the system shuts down - SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void) - { - return SWITCH_STATUS_SUCCESS; - } -*/ - -/* - If it exists, this is called in it's own thread when the module-load completes - SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void) - { - return SWITCH_STATUS_SUCCESS; - } -*/ + + //-- say hello + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n"); + +} + +//-- application interface +static const switch_application_interface_t phonecard_application_interface = { + /*.interface_name */ global_app_name, + /*.application_function */ phonecard_main, NULL, NULL, NULL, + /*.next*/ NULL +}; + +//-- module interface +static switch_loadable_module_interface_t phonecard_module_interface = { + /*.module_name */ modname, + /*.endpoint_interface */ NULL, + /*.timer_interface */ NULL, + /*.dialplan_interface */ NULL, + /*.codec_interface */ NULL, + /*.application_interface */ &phonecard_application_interface, + /*.api_interface */ NULL, + /*.file_interface */ NULL, + /*.speech_interface */ NULL, + /*.directory_interface */ NULL +}; + +//-- called when the module is loaded +SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename) +{ + //-- create the module pool + if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n"); + return SWITCH_STATUS_TERM; + } + + //-- load the configuration + switch_status_t status = SWITCH_STATUS_SUCCESS; + + if ((status =load_config()) != SWITCH_STATUS_SUCCESS) { + return status; + } + + //-- connect my internal structure to the blank pointer passed to me + *module_interface = &phonecard_module_interface; + + + //-- indicate that the module should continue to be loaded + return SWITCH_STATUS_SUCCESS; +} + + + +/* + Called when the system shuts down + SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void) + { + return SWITCH_STATUS_SUCCESS; + } +*/ + +/* + If it exists, this is called in it's own thread when the module-load completes + SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void) + { + return SWITCH_STATUS_SUCCESS; + } +*/