[asterisk-commits] juggie: branch juggie/NoLossCDR r78334 - in /team/juggie/NoLossCDR: cdr/ incl...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 6 21:24:28 CDT 2007


Author: juggie
Date: Mon Aug  6 21:24:28 2007
New Revision: 78334

URL: http://svn.digium.com/view/asterisk?view=rev&rev=78334
Log:
initial nolosscdr commit, again

Modified:
    team/juggie/NoLossCDR/cdr/cdr_adaptive_odbc.c
    team/juggie/NoLossCDR/cdr/cdr_csv.c
    team/juggie/NoLossCDR/cdr/cdr_custom.c
    team/juggie/NoLossCDR/cdr/cdr_manager.c
    team/juggie/NoLossCDR/cdr/cdr_odbc.c
    team/juggie/NoLossCDR/cdr/cdr_pgsql.c
    team/juggie/NoLossCDR/cdr/cdr_radius.c
    team/juggie/NoLossCDR/cdr/cdr_sqlite.c
    team/juggie/NoLossCDR/cdr/cdr_sqlite3_custom.c
    team/juggie/NoLossCDR/cdr/cdr_tds.c
    team/juggie/NoLossCDR/include/asterisk/cdr.h
    team/juggie/NoLossCDR/main/cdr.c

Modified: team/juggie/NoLossCDR/cdr/cdr_adaptive_odbc.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_adaptive_odbc.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_adaptive_odbc.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_adaptive_odbc.c Mon Aug  6 21:24:28 2007
@@ -25,7 +25,7 @@
  */
 
 /*** MODULEINFO
-	<depend>unixodbc</depend>
+	<depend>unixODBC</depend>
  ***/
 
 #include "asterisk.h"
@@ -301,7 +301,7 @@
 				}																\
 			} while (0)
 
-static int odbc_log(struct ast_cdr *cdr)
+static int odbc_log(struct ast_cdr *cdr, void *data)
 {
 	struct tables *tableptr;
 	struct columns *entry;
@@ -578,10 +578,10 @@
 
 static int unload_module(void)
 {
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	usleep(1);
 	if (AST_RWLIST_WRLOCK(&odbc_tables)) {
-		ast_cdr_register(name, ast_module_info->description, odbc_log);
+		ast_cdr_register(name, NULL, ast_module_info->description, odbc_log);
 		ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
 		return -1;
 	}
@@ -600,7 +600,7 @@
 
 	load_config();
 	AST_RWLIST_UNLOCK(&odbc_tables);
-	ast_cdr_register(name, ast_module_info->description, odbc_log);
+	ast_cdr_register(name, NULL, ast_module_info->description, odbc_log);
 	return 0;
 }
 

Modified: team/juggie/NoLossCDR/cdr/cdr_csv.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_csv.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_csv.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_csv.c Mon Aug  6 21:24:28 2007
@@ -271,7 +271,7 @@
 }
 
 
-static int csv_log(struct ast_cdr *cdr)
+static int csv_log(struct ast_cdr *cdr, void *data)
 {
 	/* Make sure we have a big enough buf */
 	char buf[1024];
@@ -282,6 +282,7 @@
 #endif
 	if (build_csv_record(buf, sizeof(buf), cdr)) {
 		ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes.  CDR not recorded!\n", (int)sizeof(buf));
+		return AST_CDR_POST_FATAL;
 	} else {
 		/* because of the absolutely unconditional need for the
 		   highest reliability possible in writing billing records,
@@ -289,6 +290,7 @@
 		mf = fopen(csvmaster, "a");
 		if (!mf) {
 			ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
+			return AST_CDR_POST_RETRY;
 		}
 		if (mf) {
 			fputs(buf, mf);
@@ -297,18 +299,20 @@
 			mf = NULL;
 		}
 		if (!ast_strlen_zero(cdr->accountcode)) {
-			if (writefile(buf, cdr->accountcode))
+			if (writefile(buf, cdr->accountcode)) {
 				ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
-		}
-	}
-	return 0;
+				return AST_CDR_POST_RETRY;
+			}
+		}
+	}
+	return AST_CDR_POST_OK;
 }
 
 static int unload_module(void)
 {
 	if (mf)
 		fclose(mf);
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	return 0;
 }
 
@@ -319,7 +323,7 @@
 	if(!load_config())
 		return AST_MODULE_LOAD_DECLINE;
 
-	res = ast_cdr_register(name, ast_module_info->description, csv_log);
+	res = ast_cdr_register(name, NULL, ast_module_info->description, csv_log);
 	if (res) {
 		ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
 		if (mf)
@@ -339,4 +343,4 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);

Modified: team/juggie/NoLossCDR/cdr/cdr_custom.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_custom.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_custom.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_custom.c Mon Aug  6 21:24:28 2007
@@ -105,7 +105,7 @@
 
 
 
-static int custom_log(struct ast_cdr *cdr)
+static int custom_log(struct ast_cdr *cdr, void *data)
 {
 	/* Make sure we have a big enough buf */
 	char buf[2048];
@@ -127,6 +127,7 @@
 	mf = fopen(master, "a");
 	if (!mf) {
 		ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
+		return AST_CDR_POST_RETRY;
 	}
 	if (mf) {
 		fputs(buf, mf);
@@ -134,14 +135,14 @@
 		fclose(mf);
 		mf = NULL;
 	}
-	return 0;
+	return AST_CDR_POST_OK;
 }
 
 static int unload_module(void)
 {
 	if (mf)
 		fclose(mf);
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	return 0;
 }
 
@@ -150,7 +151,7 @@
 	int res = 0;
 
 	if (!load_config(0)) {
-		res = ast_cdr_register(name, ast_module_info->description, custom_log);
+		res = ast_cdr_register(name, NULL, ast_module_info->description, custom_log);
 		if (res)
 			ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
 		if (mf)
@@ -169,5 +170,5 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);
 

Modified: team/juggie/NoLossCDR/cdr/cdr_manager.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_manager.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_manager.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_manager.c Mon Aug  6 21:24:28 2007
@@ -101,7 +101,7 @@
 	return 1;
 }
 
-static int manager_log(struct ast_cdr *cdr)
+static int manager_log(struct ast_cdr *cdr, void *data)
 {
 	struct ast_tm timeresult;
 	char strStartTime[80] = "";
@@ -111,7 +111,7 @@
 	struct ast_channel dummy;
 
 	if (!enablecdr)
-		return 0;
+		return AST_POST_CDR_DISABLED;
 
 	ast_localtime(&cdr->start, &timeresult, NULL);
 	ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
@@ -157,12 +157,12 @@
 	    cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
 	    ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
 
-	return 0;
+	return AST_CDR_POST_OK;
 }
 
 static int unload_module(void)
 {
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	if (customfields)
 		ast_free(customfields);
 
@@ -177,7 +177,7 @@
 	if (!load_config())
 		return AST_MODULE_LOAD_DECLINE;
 	
-	res = ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
+	res = ast_cdr_register(name, NULL, "Asterisk Manager Interface CDR Backend", manager_log);
 	if (res) {
 		ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
 	}
@@ -199,4 +199,4 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);

Modified: team/juggie/NoLossCDR/cdr/cdr_odbc.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_odbc.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_odbc.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_odbc.c Mon Aug  6 21:24:28 2007
@@ -90,7 +90,7 @@
 	connected = 0;
 }
 
-static int odbc_log(struct ast_cdr *cdr)
+static int odbc_log(struct ast_cdr *cdr, void *data)
 {
 	int ODBC_res;
 	char sqlcmd[2048] = "", timestr[128];
@@ -221,7 +221,7 @@
 		ast_free(table);
 	}
 
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	ast_mutex_unlock(&odbc_lock);
 	return 0;
 }
@@ -338,7 +338,7 @@
 		ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
 		ast_verb(3, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
 		}
-	res = ast_cdr_register(name, ast_module_info->description, odbc_log);
+	res = ast_cdr_register(name, NULL, ast_module_info->description, odbc_log);
 	if (res) {
 		ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
 	}
@@ -436,4 +436,4 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);

Modified: team/juggie/NoLossCDR/cdr/cdr_pgsql.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_pgsql.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_pgsql.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_pgsql.c Mon Aug  6 21:24:28 2007
@@ -69,7 +69,7 @@
 
 static PGconn	*conn = NULL;
 
-static int pgsql_log(struct ast_cdr *cdr)
+static int pgsql_log(struct ast_cdr *cdr, void *data)
 {
 	struct ast_tm tm;
 	char sqlcmd[2048] = "", timestr[128];
@@ -198,7 +198,7 @@
 		ast_free(pgdbport);
 	if (table)
 		ast_free(table);
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 	return 0;
 }
 
@@ -282,7 +282,7 @@
 		connected = 0;
 	}
 
-	return ast_cdr_register(name, ast_module_info->description, pgsql_log);
+	return ast_cdr_register(name, NULL, ast_module_info->description, pgsql_log);
 }
 
 static int my_load_module(void)
@@ -325,4 +325,4 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);

Modified: team/juggie/NoLossCDR/cdr/cdr_radius.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_radius.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_radius.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_radius.c Mon Aug  6 21:24:28 2007
@@ -206,21 +206,21 @@
 	return 0;
 }
 
-static int radius_log(struct ast_cdr *cdr)
-{
-	int result = ERROR_RC;
+static int radius_log(struct ast_cdr *cdr, void *data)
+{
 	VALUE_PAIR *send = NULL;
 
 	if (build_radius_record(&send, cdr)) {
 		ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
-		return result;
+		return AST_CDR_POST_FATAL;
 	}
 	
-	result = rc_acct(rh, 0, send);
-	if (result != OK_RC)
+	if (rc_acct(rh, 0, send) != OK_RC) {
 		ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
-
-	return result;
+		return AST_CDR_POST_FATAL;
+	}
+
+	return AST_CDR_POST_OK;
 }
 
 static int unload_module(void)

Modified: team/juggie/NoLossCDR/cdr/cdr_sqlite.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_sqlite.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_sqlite.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_sqlite.c Mon Aug  6 21:24:28 2007
@@ -92,7 +92,7 @@
 #endif
 ");";
 
-static int sqlite_log(struct ast_cdr *cdr)
+static int sqlite_log(struct ast_cdr *cdr, void *data)
 {
 	int res = 0;
 	char *zErr = 0;

Modified: team/juggie/NoLossCDR/cdr/cdr_sqlite3_custom.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_sqlite3_custom.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_sqlite3_custom.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_sqlite3_custom.c Mon Aug  6 21:24:28 2007
@@ -148,7 +148,7 @@
 	return 0;
 }
 
-static int sqlite3_log(struct ast_cdr *cdr)
+static int sqlite3_log(struct ast_cdr *cdr, void *data)
 {
 	int res = 0;
 	char *zErr = 0;
@@ -191,7 +191,7 @@
 	if (db)
 		sqlite3_close(db);
 
-	ast_cdr_unregister(name);
+	ast_cdr_unregister(name, NULL);
 
 	return 0;
 }
@@ -204,7 +204,7 @@
 	char *sql_cmd;
 
 	if (!load_config(0)) {
-		res = ast_cdr_register(name, desc, sqlite3_log);
+		res = ast_cdr_register(name, NULL, desc, sqlite3_log);
 		if (res) {
 			ast_log(LOG_ERROR, "%s: Unable to register custom SQLite3 CDR handling\n", name);
 			return AST_MODULE_LOAD_DECLINE;

Modified: team/juggie/NoLossCDR/cdr/cdr_tds.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/cdr/cdr_tds.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/cdr/cdr_tds.c (original)
+++ team/juggie/NoLossCDR/cdr/cdr_tds.c Mon Aug  6 21:24:28 2007
@@ -108,7 +108,7 @@
 static int mssql_connect(void);
 static int mssql_disconnect(void);
 
-static int tds_log(struct ast_cdr *cdr)
+static int tds_log(struct ast_cdr *cdr, void *data)
 {
 	char sqlcmd[2048], start[80], answer[80], end[80];
 	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
@@ -522,4 +522,4 @@
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,
-	       );
+		);

Modified: team/juggie/NoLossCDR/include/asterisk/cdr.h
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/include/asterisk/cdr.h?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/include/asterisk/cdr.h (original)
+++ team/juggie/NoLossCDR/include/asterisk/cdr.h Mon Aug  6 21:24:28 2007
@@ -44,6 +44,12 @@
 
 #define AST_MAX_USER_FIELD			256
 #define AST_MAX_ACCOUNT_CODE		20
+
+/* CDR Return Flags */
+#define AST_CDR_POST_OK 0
+#define AST_CDR_POST_RETRY -1
+#define AST_CDR_POST_FATAL -2
+#define AST_CDR_POST_DISABLED -3
 
 /* Include channel.h after relevant declarations it will need */
 #include "asterisk/channel.h"
@@ -95,7 +101,10 @@
 	/*! A linked list for variables */
 	struct varshead varshead;
 
+	/* count where i'm used in memory */
+	int usecount;
 	/* no more cdr stack struct ast_cdr *next; */
+	AST_RWLIST_ENTRY(ast_cdr) list;
 };
 
 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw);
@@ -104,7 +113,7 @@
 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur);
 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr);
 
-typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
+typedef int (*ast_cdrbe)(struct ast_cdr *cdr, void *data);
 
 /*! \brief Return TRUE if CDR subsystem is enabled */
 int check_cdr_enabled(void);
@@ -164,14 +173,14 @@
  * \retval 0 on success.
  * \retval -1 on error
  */
-int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be);
+int ast_cdr_register(const char *name, const char *name_detail, const char *desc, ast_cdrbe be);
 
 /*! 
  * \brief Unregister a CDR handling engine 
  * \param name name of CDR handler to unregister
  * Unregisters a CDR by it's name
  */
-void ast_cdr_unregister(const char *name);
+void ast_cdr_unregister(const char *name, const char *name_detail);
 
 /*! 
  * \brief Start a call 

Modified: team/juggie/NoLossCDR/main/cdr.c
URL: http://svn.digium.com/view/asterisk/team/juggie/NoLossCDR/main/cdr.c?view=diff&rev=78334&r1=78333&r2=78334
==============================================================================
--- team/juggie/NoLossCDR/main/cdr.c (original)
+++ team/juggie/NoLossCDR/main/cdr.c Mon Aug  6 21:24:28 2007
@@ -54,6 +54,9 @@
 #include "asterisk/config.h"
 #include "asterisk/cli.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/module.h"
+
+static void *do_cdr(void *data);
 
 /*! Default AMA flag for billing records (CDR's) */
 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
@@ -61,45 +64,24 @@
 
 struct ast_cdr_beitem {
 	char name[20];
+	char name_detail[20];
 	char desc[80];
 	ast_cdrbe be;
+	void *be_data;
+	int cancel_thread;
+	int waiting_cdrs;
+	pthread_t cdr_thread;
+	ast_cond_t cdr_pending_cond;
+	AST_LIST_HEAD(, ast_cdr) cdr_queue;
 	AST_RWLIST_ENTRY(ast_cdr_beitem) list;
 };
 
 static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
 
-struct ast_cdr_batch_item {
-	struct ast_cdr *cdr;
-	struct ast_cdr_batch_item *next;
-};
-
-static struct ast_cdr_batch {
-	int size;
-	struct ast_cdr_batch_item *head;
-	struct ast_cdr_batch_item *tail;
-} *batch = NULL;
-
-static struct sched_context *sched;
-static int cdr_sched = -1;
-static pthread_t cdr_thread = AST_PTHREADT_NULL;
-
-#define BATCH_SIZE_DEFAULT 100
-#define BATCH_TIME_DEFAULT 300
-#define BATCH_SCHEDULER_ONLY_DEFAULT 0
-#define BATCH_SAFE_SHUTDOWN_DEFAULT 1
+#define SAFE_SHUTDOWN_DEFAULT 1
 
 static int enabled;		/*! Is the CDR subsystem enabled ? */
-static int batchmode;
-static int batchsize;
-static int batchtime;
-static int batchscheduleronly;
-static int batchsafeshutdown;
-
-AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
-
-/* these are used to wake up the CDR thread when there's work to do */
-AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
-static ast_cond_t cdr_pending_cond;
+static int safeshutdown;
 
 int check_cdr_enabled()
 {
@@ -109,7 +91,7 @@
 /*! Register a CDR driver. Each registered CDR driver generates a CDR 
 	\return 0 on success, -1 on failure 
 */
-int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
+int ast_cdr_register(const char *name, const char *name_detail, const char *desc, ast_cdrbe be)
 {
 	struct ast_cdr_beitem *i = NULL;
 
@@ -123,8 +105,8 @@
 
 	AST_RWLIST_WRLOCK(&be_list);
 	AST_RWLIST_TRAVERSE(&be_list, i, list) {
-		if (!strcasecmp(name, i->name)) {
-			ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
+		if (!strcasecmp(name, i->name) && !strcasecmp(name_detail, i->name_detail)) {
+			ast_log(LOG_WARNING, "Already have a CDR backend called '%s - %s'\n", name, name_detail);
 			AST_RWLIST_UNLOCK(&be_list);
 			return -1;
 		}
@@ -134,9 +116,14 @@
 		return -1;
 
 	i->be = be;
+	i->cancel_thread = 0;
+	i->waiting_cdrs = 0;
 	ast_copy_string(i->name, name, sizeof(i->name));
+	ast_copy_string(i->name_detail, name_detail, sizeof(i->name_detail));
 	ast_copy_string(i->desc, desc, sizeof(i->desc));
-
+	ast_cond_init(&i->cdr_pending_cond, NULL);
+	if (ast_pthread_create_background(&i->cdr_thread, NULL, do_cdr, i) < 0)
+		ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
 	AST_RWLIST_INSERT_HEAD(&be_list, i, list);
 	AST_RWLIST_UNLOCK(&be_list);
 
@@ -144,17 +131,25 @@
 }
 
 /*! unregister a CDR driver */
-void ast_cdr_unregister(const char *name)
+void ast_cdr_unregister(const char *name, const chat *name_detail)
 {
 	struct ast_cdr_beitem *i = NULL;
 
 	AST_RWLIST_WRLOCK(&be_list);
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
-		if (!strcasecmp(name, i->name)) {
+		if (!strcasecmp(name, i->name) && !strcasecmp(name_detail, i->name_detail)) {
+			i->cancel_thread = 1;
+			/* signal the thread so it can exit */
+			ast_cond_signal(&i->cdr_pending_cond);
+			/* wait for thread to exit so we can clean up */
+			pthread_join(i->cdr_thread, NULL);
+			i->cdr_thread = AST_PTHREADT_NULL;
+			ast_cond_destroy(&i->cdr_pending_cond);
 			AST_RWLIST_REMOVE_CURRENT(&be_list, list);
-			ast_verb(2, "Unregistered '%s' CDR backend\n", name);
+			if (option_verbose > 1)
+				ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s - %s' CDR backend\n", name, name_detail);
 			ast_free(i);
-			break;
+                        break;
 		}
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END;
@@ -635,7 +630,7 @@
 
 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
 {
-	struct ast_cdr *cdr;
+	struct ast_cdr *cdr = chan->cdr;
 	int newflag = ast_cdr_amaflags2int(flag);
 	if (newflag) {
 		cdr->amaflags = newflag;
@@ -696,7 +691,7 @@
 {
 	char *chan;
 	struct ast_cdr_beitem *i;
-
+	
 	chan = S_OR(cdr->channel, "<unknown>");
 	check_post(cdr);
 	if (ast_tvzero(cdr->end))
@@ -708,7 +703,17 @@
 		return;
 	AST_RWLIST_RDLOCK(&be_list);
 	AST_RWLIST_TRAVERSE(&be_list, i, list) {
-		i->be(cdr);
+		AST_LIST_LOCK(&i->cdr_queue);
+		ast_debug(1, "cdr inserted into queue/list for: %s\n", i->name);
+		/* increment usecount of this cdr record */
+		ast_atomic_fetchadd_int(&cdr->usecount, +1);
+		//ast_update_use_count();
+		/* increment cdr counter for this queue */
+		ast_atomic_fetchadd_int(&i->waiting_cdrs, +1);
+		//ast_update_use_count();
+		AST_LIST_INSERT_TAIL(&i->cdr_queue, cdr, list);
+		ast_cond_signal(&i->cdr_pending_cond);
+		AST_LIST_UNLOCK(&i->cdr_queue);
 	}
 	AST_RWLIST_UNLOCK(&be_list);
 }
@@ -766,101 +771,8 @@
 	cdr->disposition = AST_CDR_NULL;
 }
 
-
-/*! \note Don't call without cdr_batch_lock */
-static void reset_batch(void)
-{
-	batch->size = 0;
-	batch->head = NULL;
-	batch->tail = NULL;
-}
-
-/*! \note Don't call without cdr_batch_lock */
-static int init_batch(void)
-{
-	/* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
-	if (!(batch = ast_malloc(sizeof(*batch))))
-		return -1;
-
-	reset_batch();
-
-	return 0;
-}
-
-static void *do_batch_backend_process(void *data)
-{
-	struct ast_cdr_batch_item *processeditem;
-	struct ast_cdr_batch_item *batchitem = data;
-
-	/* Push each CDR into storage mechanism(s) and free all the memory */
-	while (batchitem) {
-		post_cdr(batchitem->cdr);
-		ast_cdr_free(batchitem->cdr);
-		processeditem = batchitem;
-		batchitem = batchitem->next;
-		ast_free(processeditem);
-	}
-
-	return NULL;
-}
-
-void ast_cdr_submit_batch(int shutdown)
-{
-	struct ast_cdr_batch_item *oldbatchitems = NULL;
-	pthread_t batch_post_thread = AST_PTHREADT_NULL;
-
-	/* if there's no batch, or no CDRs in the batch, then there's nothing to do */
-	if (!batch || !batch->head)
-		return;
-
-	/* move the old CDRs aside, and prepare a new CDR batch */
-	ast_mutex_lock(&cdr_batch_lock);
-	oldbatchitems = batch->head;
-	reset_batch();
-	ast_mutex_unlock(&cdr_batch_lock);
-
-	/* if configured, spawn a new thread to post these CDRs,
-	   also try to save as much as possible if we are shutting down safely */
-	if (batchscheduleronly || shutdown) {
-		ast_debug(1, "CDR single-threaded batch processing begins now\n");
-		do_batch_backend_process(oldbatchitems);
-	} else {
-		if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
-			ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
-			do_batch_backend_process(oldbatchitems);
-		} else {
-			ast_debug(1, "CDR multi-threaded batch processing begins now\n");
-		}
-	}
-}
-
-static int submit_scheduled_batch(void *data)
-{
-	ast_cdr_submit_batch(0);
-	/* manually reschedule from this point in time */
-	cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
-	/* returning zero so the scheduler does not automatically reschedule */
-	return 0;
-}
-
-static void submit_unscheduled_batch(void)
-{
-	/* this is okay since we are not being called from within the scheduler */
-	if (cdr_sched > -1)
-		ast_sched_del(sched, cdr_sched);
-	/* schedule the submission to occur ASAP (1 ms) */
-	cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
-	/* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
-	ast_mutex_lock(&cdr_pending_lock);
-	ast_cond_signal(&cdr_pending_cond);
-	ast_mutex_unlock(&cdr_pending_lock);
-}
-
 void ast_cdr_detach(struct ast_cdr *cdr)
 {
-	struct ast_cdr_batch_item *newtail;
-	int curr;
-
 	if (!cdr)
 		return;
 
@@ -872,94 +784,76 @@
 		return;
 	}
 
-	/* post stuff immediately if we are not in batch mode, this is legacy behaviour */
-	if (!batchmode) {
-		post_cdr(cdr);
-		ast_cdr_free(cdr);
-		return;
-	}
-
-	/* otherwise, each CDR gets put into a batch list (at the end) */
-	ast_debug(1, "CDR detaching from this thread\n");
-
-	/* we'll need a new tail for every CDR */
-	if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
-		post_cdr(cdr);
-		ast_cdr_free(cdr);
-		return;
-	}
-
-	/* don't traverse a whole list (just keep track of the tail) */
-	ast_mutex_lock(&cdr_batch_lock);
-	if (!batch)
-		init_batch();
-	if (!batch->head) {
-		/* new batch is empty, so point the head at the new tail */
-		batch->head = newtail;
-	} else {
-		/* already got a batch with something in it, so just append a new tail */
-		batch->tail->next = newtail;
-	}
-	newtail->cdr = cdr;
-	batch->tail = newtail;
-	curr = batch->size++;
-	ast_mutex_unlock(&cdr_batch_lock);
-
-	/* if we have enough stuff to post, then do it */
-	if (curr >= (batchsize - 1))
-		submit_unscheduled_batch();
+	post_cdr(cdr);
 }
 
 static void *do_cdr(void *data)
 {
-	struct timespec timeout;
-	int schedms;
-	int numevents = 0;
-
+	struct ast_cdr_beitem *i = data;
+	struct ast_cdr *next = NULL, *cdr = NULL;
+	int be_res = 0;
+
+	ast_debug(1, "CDR Worker thread for %s started\n", i->name);
 	for (;;) {
-		struct timeval now;
-		schedms = ast_sched_wait(sched);
-		/* this shouldn't happen, but provide a 1 second default just in case */
-		if (schedms <= 0)
-			schedms = 1000;
-		now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
-		timeout.tv_sec = now.tv_sec;
-		timeout.tv_nsec = now.tv_usec * 1000;
-		/* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
-		ast_mutex_lock(&cdr_pending_lock);
-		ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
-		numevents = ast_sched_runq(sched);
-		ast_mutex_unlock(&cdr_pending_lock);
-		ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
-	}
-
+		/* We lock the cdr queue and see if one exists
+		if not we wait to be signaled*/
+		AST_LIST_LOCK(&i->cdr_queue);
+		if (AST_LIST_EMPTY(&i->cdr_queue) && !i->cancel_thread) {
+			ast_debug(1, "I guess the cdr queue for %s is empty, lets wait\n", i->name);
+			ast_cond_wait(&i->cdr_pending_cond, &i->cdr_queue.lock);
+		} else if (AST_LIST_EMPTY(&i->cdr_queue) && i->cancel_thread){
+			break;
+		}
+		next = AST_LIST_FIRST(&i->cdr_queue);
+		AST_LIST_HEAD_INIT_NOLOCK(&i->cdr_queue);
+		AST_LIST_UNLOCK(&i->cdr_queue);
+
+		while ((cdr = next)) {
+			/* Get the next cdr now so that we can
+			free our current structure later */
+			next = AST_LIST_NEXT(cdr, list);
+			ast_debug(1, "writing cdr to %s\n", i->name);
+			for (;;)
+			{
+				be_res = i->be(cdr, i->be_data);
+				if (be_res == AST_CDR_POST_OK) {
+					ast_debug(1, "cdr insert into %s ok!\n", i->name);
+					break;
+				} else if (be_res == AST_CDR_POST_RETRY) {
+					ast_debug(1, "cdr insert into %s temporarily failed, retrying\n", i->name);
+				} else if (be_res == AST_CDR_POST_FATAL){
+					ast_log(LOG_ERROR, "cdr insert failed in: %s, record lost\n",i->name);
+					break;
+				} else if (be_res == AST_CDR_POST_DISABLED) {
+					ast_debug(1, "cdr backend %s is registered, but disabled\n", i->name);
+					break;
+				}
+			}
+			/* depracate cdr counter for this queue */
+			ast_atomic_fetchadd_int(&i->waiting_cdrs, -1);
+			/* depracate uses of this cdr record
+			free if no one is using it*/
+			if (ast_atomic_fetchadd_int(&cdr->usecount, -1) == 1) {
+				ast_debug(1, "cdr record no longer in any queue, so lets free it\n");
+				ast_cdr_free(cdr);
+			}
+		}
+ 	}
+ 	ast_debug(1, "CDR Worker thread for %s stopped\n", i->name);
 	return NULL;
 }
 
 static int handle_cli_status(int fd, int argc, char *argv[])
 {
 	struct ast_cdr_beitem *beitem=NULL;
-	int cnt=0;
-	long nextbatchtime=0;
 
 	if (argc > 2)
 		return RESULT_SHOWUSAGE;
 
+	// what if there are no backends?
 	ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
-	ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
 	if (enabled) {
-		if (batchmode) {
-			if (batch)
-				cnt = batch->size;
-			if (cdr_sched > -1)
-				nextbatchtime = ast_sched_when(sched, cdr_sched);
-			ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
-			ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
-			ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, ESS(cnt));
-			ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
-			ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
-			ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, ESS(nextbatchtime));
-		}
+		ast_cli(fd, "CDR safe shut down: %s\n", safeshutdown ? "enabled" : "disabled");
 		AST_RWLIST_RDLOCK(&be_list);
 		AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
 			ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
@@ -969,24 +863,6 @@
 
 	return 0;
 }
-
-static int handle_cli_submit(int fd, int argc, char *argv[])
-{
-	if (argc > 2)
-		return RESULT_SHOWUSAGE;
-
-	submit_unscheduled_batch();
-	ast_cli(fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
-
-	return 0;
-}
-
-static struct ast_cli_entry cli_submit = {
-	{ "cdr", "submit", NULL },
-	handle_cli_submit, "Posts all pending batched CDR data",
-	"Usage: cdr submit\n"
-	"       Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
-};
 
 static struct ast_cli_entry cli_status = {
 	{ "cdr", "status", NULL },
@@ -999,109 +875,37 @@
 {
 	struct ast_config *config;
 	const char *enabled_value;
-	const char *batched_value;
-	const char *scheduleronly_value;
-	const char *batchsafeshutdown_value;
-	const char *size_value;
-	const char *time_value;
-	const char *end_before_h_value;
-	int cfg_size;
-	int cfg_time;
+	const char *safeshutdown_value;
 	int was_enabled;
-	int was_batchmode;
 	int res=0;
 
-	ast_mutex_lock(&cdr_batch_lock);
-
-	batchsize = BATCH_SIZE_DEFAULT;
-	batchtime = BATCH_TIME_DEFAULT;
-	batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
-	batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
+	safeshutdown = SAFE_SHUTDOWN_DEFAULT;
 	was_enabled = enabled;
-	was_batchmode = batchmode;
 	enabled = 1;
-	batchmode = 0;
-
-	/* don't run the next scheduled CDR posting while reloading */
-	if (cdr_sched > -1)
-		ast_sched_del(sched, cdr_sched);
 
 	if ((config = ast_config_load("cdr.conf"))) {
 		if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
 			enabled = ast_true(enabled_value);
 		}
-		if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
-			batchmode = ast_true(batched_value);
-		}
-		if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
-			batchscheduleronly = ast_true(scheduleronly_value);
-		}
-		if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
-			batchsafeshutdown = ast_true(batchsafeshutdown_value);
-		}
-		if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
-			if (sscanf(size_value, "%d", &cfg_size) < 1)
-				ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
-			else if (size_value < 0)
-				ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
-			else
-				batchsize = cfg_size;
-		}
-		if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
-			if (sscanf(time_value, "%d", &cfg_time) < 1)
-				ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
-			else if (time_value < 0)
-				ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
-			else
-				batchtime = cfg_time;
-		}
-		if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
-			ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
-	}
-
-	if (enabled && !batchmode) {
+		if ((safeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
+			safeshutdown = ast_true(safeshutdown_value);
+		}
+	}
+
+	if (enabled) {
 		ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
-	} else if (enabled && batchmode) {
-		cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
-		ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
 	} else {
 		ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
 	}
-
-	/* if this reload enabled the CDR batch mode, create the background thread
-	   if it does not exist */
-	if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
-		ast_cond_init(&cdr_pending_cond, NULL);
-		if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
-			ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
-			ast_sched_del(sched, cdr_sched);
-		} else {
-			ast_cli_register(&cli_submit);
-			ast_register_atexit(ast_cdr_engine_term);
-			res = 0;
-		}
-	/* if this reload disabled the CDR and/or batch mode and there is a background thread,
-	   kill it */
-	} else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
-		/* wake up the thread so it will exit */
-		pthread_cancel(cdr_thread);
-		pthread_kill(cdr_thread, SIGURG);
-		pthread_join(cdr_thread, NULL);
-		cdr_thread = AST_PTHREADT_NULL;
-		ast_cond_destroy(&cdr_pending_cond);
-		ast_cli_unregister(&cli_submit);
+	
+	if (was_enabled && !enabled) {
 		ast_unregister_atexit(ast_cdr_engine_term);
-		res = 0;
-		/* if leaving batch mode, then post the CDRs in the batch,
-		   and don't reschedule, since we are stopping CDR logging */
-		if (!batchmode && was_batchmode) {
-			ast_cdr_engine_term();
-		}
-	} else {
-		res = 0;
-	}
-
-	ast_mutex_unlock(&cdr_batch_lock);
+		ast_cli_unregister(&cli_status);
+	} else if (!was_enabled && enabled) {
+		ast_register_atexit(ast_cdr_engine_term);
+		ast_cli_register(&cli_status);
+	}
+
 	ast_config_destroy(config);
 
 	return res;
@@ -1109,35 +913,36 @@
 
 int ast_cdr_engine_init(void)
 {
-	int res;
-
-	sched = sched_context_create();
-	if (!sched) {
-		ast_log(LOG_ERROR, "Unable to create schedule context.\n");
-		return -1;
-	}
-
-	ast_cli_register(&cli_status);
-
-	res = do_reload();
-	if (res) {
-		ast_mutex_lock(&cdr_batch_lock);
-		res = init_batch();
-		ast_mutex_unlock(&cdr_batch_lock);
-	}
-
-	return res;
+	return do_reload();
 }
 
 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
    hanging up channels, and then again, after the channel hangup timeout expires */
 void ast_cdr_engine_term(void)
 {
-	ast_cdr_submit_batch(batchsafeshutdown);
+	struct ast_cdr_beitem *i = NULL;
+
+	if (safeshutdown) {
+		if (option_verbose > 1)
+			ast_verbose(VERBOSE_PREFIX_2 "Waiting for CDR Backends to shutdown..\n");
+		AST_RWLIST_WRLOCK(&be_list);
+		AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
+			i->cancel_thread = 1;
+			/* signal the thread so it can exit */
+			ast_cond_signal(&i->cdr_pending_cond);
+			/* wait for thread to exit so we can clean up */
+			pthread_join(i->cdr_thread, NULL);
+			i->cdr_thread = AST_PTHREADT_NULL;
+			ast_cond_destroy(&i->cdr_pending_cond);
+			AST_RWLIST_REMOVE_CURRENT(&be_list, list);
+			ast_free(i);
+		}
+		AST_RWLIST_TRAVERSE_SAFE_END;
+		AST_RWLIST_UNLOCK(&be_list);
+	}
 }
 
 int ast_cdr_engine_reload(void)
 {
 	return do_reload();
 }
-




More information about the asterisk-commits mailing list