[Freeswitch-svn] [commit] r11214 - freeswitch/trunk/scripts/contrib/dschreiber/mod_nibblebill
FreeSWITCH SVN
dschreiber at freeswitch.org
Wed Jan 14 19:58:46 PST 2009
Author: dschreiber
Date: Wed Jan 14 21:58:46 2009
New Revision: 11214
Log:
Formatting fixes, per Brian
Also changed comments to be C compat
Modified:
freeswitch/trunk/scripts/contrib/dschreiber/mod_nibblebill/mod_nibblebill.c
Modified: freeswitch/trunk/scripts/contrib/dschreiber/mod_nibblebill/mod_nibblebill.c
==============================================================================
--- freeswitch/trunk/scripts/contrib/dschreiber/mod_nibblebill/mod_nibblebill.c (original)
+++ freeswitch/trunk/scripts/contrib/dschreiber/mod_nibblebill/mod_nibblebill.c Wed Jan 14 21:58:46 2009
@@ -51,41 +51,44 @@
#include <switch_odbc.h>
#endif
-// Defaults
-//static char SQL_LOOKUP[] = "SELECT cash FROM accounts WHERE id=\"%s\"";
+/* Defaults */
+/* static char SQL_LOOKUP[] = "SELECT cash FROM accounts WHERE id=\"%s\""; */
static char SQL_SAVE[] = "UPDATE accounts SET cash=cash-%f WHERE id=\"%s\"";
-typedef struct {
- switch_time_t lastts; // Last time we did any billing
- float total; // Total amount billed so far
+typedef struct
+{
+ switch_time_t lastts; /* Last time we did any billing */
+ float total; /* Total amount billed so far */
} nibble_data_t;
-typedef struct nibblebill_results{
+typedef struct nibblebill_results
+{
float funds;
- float percall_max; // Overrides global on a per-user level
- float lowbal_amt; // ditto
+ float percall_max; /* Overrides global on a per-user level */
+ float lowbal_amt; /* ditto */
} nibblebill_results_t;
/* Keep track of our config, event hooks and database connection variables, for this module only */
-static struct {
- // Event hooks
+static struct
+{
+ /* Event hooks */
switch_event_node_t *node;
- // Global mutex (don't touch a session when it's already being touched)
+ /* Global mutex (don't touch a session when it's already being touched) */
switch_mutex_t *mutex;
- // Global billing config options
- float percall_max_amt; // Per-call billing limit (safety check, for fraud)
- char *percall_action; // Exceeded length of per-call action
- float lowbal_amt; // When we warn them they are near depletion
- char *lowbal_action; // Low balance action
- float nobal_amt; // Minimum amount that must remain in the account
- char *nobal_action; // Drop action
+ /* Global billing config options */
+ float percall_max_amt; /* Per-call billing limit (safety check, for fraud) */
+ char *percall_action; /* Exceeded length of per-call action */
+ float lowbal_amt; /* When we warn them they are near depletion */
+ char *lowbal_action; /* Low balance action */
+ float nobal_amt; /* Minimum amount that must remain in the account */
+ char *nobal_action; /* Drop action */
- // Database settings
+ /* Database settings */
char *db_username;
char *db_password;
char *db_dsn;
@@ -94,7 +97,7 @@
#ifdef SWITCH_HAVE_ODBC
switch_odbc_handle_t *master_odbc;
#else
- void *padding1; // Keep structures same size
+ void *padding1; /* Keep structures same size */
#endif
} globals;
@@ -120,8 +123,6 @@
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_lowbal_action, globals.lowbal_action);
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_nobal_action, globals.nobal_action);
-
-
static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnNames)
{
nibblebill_results_t *cbt = (nibblebill_results_t *) pArg;
@@ -131,8 +132,6 @@
return 0;
}
-
-
static switch_status_t load_config(void)
{
char *cf = "nibblebill.conf";
@@ -141,7 +140,7 @@
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
- status = SWITCH_STATUS_SUCCESS; // We don't fail because we can still write to a text file or buffer
+ status = SWITCH_STATUS_SUCCESS; /* We don't fail because we can still write to a text file or buffer */
goto done;
}
@@ -149,6 +148,7 @@
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, "db-username")) {
set_global_db_username(val);
} else if (!strcasecmp(var, "db-password")) {
@@ -188,7 +188,7 @@
if (switch_strlen_zero(globals.nobal_action)) {
set_global_nobal_action("hangup");
}
- // Need to add defaults for the integers!
+ /* Need to add defaults for the integers! */
#ifdef SWITCH_HAVE_ODBC
@@ -200,6 +200,7 @@
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database handle!\n");
}
+
if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot connect to ODBC driver/database %s (user: %s / pass %s)!\n", globals.db_dsn, globals.db_username, globals.db_password);
status = SWITCH_STATUS_FALSE;
@@ -210,7 +211,7 @@
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.db_dsn);
} else {
#endif
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "ODBC does not appear to be installed in the core\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "ODBC does not appear to be installed in the core. You need to run ./configure --enable-core-odbc-support\n");
#ifdef SWITCH_HAVE_ODBC
}
#endif
@@ -226,15 +227,15 @@
return status;
}
-
-void debug_event_handler(switch_event_t *event) {
+void debug_event_handler(switch_event_t *event)
+{
switch_event_header_t *event_header = NULL;
if (!event) {
return;
}
- // Print out all event headers, for fun
+ /* Print out all event headers, for fun */
if (event->headers) {
for (event_header = event->headers; event_header; event_header = event_header->next) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Header info: %s => %s\n", event_header->name, event_header->value);
@@ -243,7 +244,8 @@
}
-static switch_status_t sched_billing(switch_core_session_t *session) {
+static switch_status_t sched_billing(switch_core_session_t *session)
+{
switch_core_session_enable_heartbeat(session, 60);
@@ -251,15 +253,15 @@
}
-/* This is where we actually charge the guy */
-/* This can be called anytime a call is in progress or at the end of a call before the session is destroyed */
-static switch_status_t do_billing(switch_core_session_t *session) {
-
- // FS vars we will use
+/* This is where we actually charge the guy
+ This can be called anytime a call is in progress or at the end of a call before the session is destroyed */
+static switch_status_t do_billing(switch_core_session_t *session)
+{
+ /* FS vars we will use */
switch_channel_t *channel;
switch_caller_profile_t *profile;
- // Local vars
+ /* Local vars */
nibble_data_t *nibble_data;
switch_time_t ts = switch_timestamp_now();
float billamount;
@@ -269,34 +271,34 @@
switch_time_exp_t tm;
if (!session) {
- // wtf? Why are we here?
+ /* wtf? Why are we here? */
return SWITCH_STATUS_GENERR;
}
uuid = switch_core_session_get_uuid(session);
- // Get channel var
+ /* Get channel var */
channel = switch_core_session_get_channel(session);
if (!channel) {
return SWITCH_STATUS_GENERR;
}
- // Variables kept in FS but relevant only to this module
+ /* Variables kept in FS but relevant only to this module */
const char *billrate = switch_channel_get_variable(channel, "nibble_rate");
const char *billaccount = switch_channel_get_variable(channel, "nibble_account");
- // Return if there's no billing information on this session
+ /* Return if there's no billing information on this session */
if (!billrate || !billaccount) {
return SWITCH_STATUS_GENERR;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Attempting to bill at $%s per minute to account %s\n", billrate, billaccount);
- // Get caller profile info from channel
+ /* Get caller profile info from channel */
profile = switch_channel_get_caller_profile(channel);
if (!profile) {
- // No caller profile (why would this happen?)
+ /* No caller profile (why would this happen?) */
return SWITCH_STATUS_GENERR;
}
@@ -305,15 +307,15 @@
return SWITCH_STATUS_SUCCESS;
}
- // Lock this session's data for this module while we tinker with it
+ /* Lock this session's data for this module while we tinker with it */
if (globals.mutex) {
switch_mutex_lock(globals.mutex);
}
- // Get our nibble data var. This will be NULL if it's our first call here for this session
+ /* Get our nibble data var. This will be NULL if it's our first call here for this session */
nibble_data = (nibble_data_t *) switch_channel_get_private(channel, "_nibble_data_");
- // Have we done any billing on this channel yet? If no, set up vars for doing so
+ /* Have we done any billing on this channel yet? If no, set up vars for doing so */
if (!nibble_data) {
nibble_data = switch_core_session_alloc(session, sizeof(*nibble_data));
if (!nibble_data) {
@@ -321,8 +323,8 @@
}
memset(nibble_data, 0, sizeof(*nibble_data));
- // Setup new billing data (based on call answer time, in case this module started late with active calls)
- nibble_data->lastts = profile->times->answered; // Set the initial answer time to match when the call was really answered
+ /* Setup new billing data (based on call answer time, in case this module started late with active calls) */
+ nibble_data->lastts = profile->times->answered; /* Set the initial answer time to match when the call was really answered */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Beginning new billing on %s\n", uuid);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Last successful billing time was %s\n", date);
@@ -335,13 +337,13 @@
if ((ts - nibble_data->lastts) > 0) {
- // Convert billrate into microseconds and multiply by # of microseconds that have passed
+ /* Convert billrate into microseconds and multiply by # of microseconds that have passed */
billamount = (atof(billrate) / 1000000 / 60) * ((ts - nibble_data->lastts));
- // if ODBC call fails, we should return BEFORE updating the timestamp of last success!
+ /* if ODBC call fails, we should return BEFORE updating the timestamp of last success! */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Billing $%f to %s (Call: %s / %f so far)\n", billamount, billaccount, uuid, nibble_data->total);
- // DO ODBC BILLING HERE!
+ /* DO ODBC BILLING HERE! */
#ifdef SWITCH_HAVE_ODBC
char sql[1024] = "";
nibblebill_results_t pdata;
@@ -357,66 +359,65 @@
if (!(switch_odbc_handle_callback_exec(globals.master_odbc, sql, nibblebill_callback, &pdata) == SWITCH_ODBC_SUCCESS)){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DB Error while updating cash!\n");
- // TODO: If this is a hangup event, we should store this billing in a text file while the DB is unavailable
+ /* TODO: If this is a hangup event, we should store this billing in a text file while the DB is unavailable */
}
#endif
- // Increment total cost
+ /* Increment total cost */
nibble_data->total += billamount;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Just tried to bill %s negative minutes! wtf?\n", uuid);
}
- // Update the last time we billed
+ /* Update the last time we billed */
nibble_data->lastts = ts;
- // Save everything back
+ /* Save everything back */
switch_channel_set_private(channel, "_nibble_data_", nibble_data);
- // Release the session lock
+ /* Release the session lock */
switch_core_session_rwunlock(session);
- // Done changing - release lock
+ /* Done changing - release lock */
if (globals.mutex) {
switch_mutex_unlock(globals.mutex);
}
-
return SWITCH_STATUS_SUCCESS;
}
-
/* You can turn on session heartbeat on a channel to have us check billing more often */
-static void event_handler(switch_event_t *event) {
+static void event_handler(switch_event_t *event)
+{
if (!event){
- // WTF? We should never get here - it means an event came in without the event info
+ /* WTF? We should never get here - it means an event came in without the event info */
return;
}
- // FS vars we'll need
+ /* FS vars we'll need */
switch_core_session_t *session;
char *uuid = switch_event_get_header(event, "Unique-ID");
- // Make sure everything is sane
+ /* Make sure everything is sane */
if (!uuid){
- // Donde esta channel?
+ /* Donde esta channel? */
return;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Received request via %s!\n", switch_event_name (event->event_id));
- // Display debugging info
+ /* Display debugging info */
if (switch_event_get_header(event, "nibble_debug")) {
debug_event_handler(event);
}
- // Get session var
+ /* Get session var */
session = switch_core_session_locate(uuid);
if (!session) {
return;
}
- // Go bill
+ /* Go bill */
do_billing(session);
switch_core_session_rwunlock(session);
@@ -447,28 +448,28 @@
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "We made it!\n");
-// Check if session has variable "billrate" set. If it does, activate the heartbeat variable
-// switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds)
-// switch_core_session_sched_heartbeat(switch_core_session_t *session, uint32_t seconds)
+/* Check if session has variable "billrate" set. If it does, activate the heartbeat variable
+ switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds)
+ switch_core_session_sched_heartbeat(switch_core_session_t *session, uint32_t seconds)*/
return SWITCH_STATUS_SUCCESS;
}
-switch_state_handler_table_t nibble_state_handler = {
+switch_state_handler_table_t nibble_state_handler =
+{
/* on_init */ NULL,
- /* on_routing */ NULL, // Need to add a check here for anything in their account before routing
- /* on_execute */ sched_billing, // Turn on heartbeat for this session
- /* on_hangup */ do_billing, // On hangup - most important place to go bill
- /* on_exch_media */ NULL, // Run billing upon a call being established
+ /* on_routing */ NULL, /* Need to add a check here for anything in their account before routing */
+ /* on_execute */ sched_billing, /* Turn on heartbeat for this session */
+ /* on_hangup */ do_billing, /* On hangup - most important place to go bill */
+ /* on_exch_media */ NULL,
/* on_soft_exec */ NULL,
/* on_consume_med */ NULL,
/* on_hibernate */ NULL,
- /* on_reset */ do_billing // In case the switch is reset, we still want to log billing
+ /* on_reset */ NULL
};
-
SWITCH_MODULE_LOAD_FUNCTION(mod_nibblebill_load)
{
switch_api_interface_t *api_interface;
More information about the Freeswitch-svn
mailing list