[svn-commits] kmoore: branch kmoore/cel_backend_refactor r395926 - in /team/kmoore/cel_back...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Jul 31 22:10:41 CDT 2013


Author: kmoore
Date: Wed Jul 31 22:10:39 2013
New Revision: 395926

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395926
Log:
First stage, tests still passing

Modified:
    team/kmoore/cel_backend_refactor/cel/cel_custom.c
    team/kmoore/cel_backend_refactor/cel/cel_manager.c
    team/kmoore/cel_backend_refactor/cel/cel_odbc.c
    team/kmoore/cel_backend_refactor/cel/cel_pgsql.c
    team/kmoore/cel_backend_refactor/cel/cel_radius.c
    team/kmoore/cel_backend_refactor/cel/cel_sqlite3_custom.c
    team/kmoore/cel_backend_refactor/cel/cel_tds.c
    team/kmoore/cel_backend_refactor/include/asterisk/cel.h
    team/kmoore/cel_backend_refactor/main/cel.c
    team/kmoore/cel_backend_refactor/tests/test_cel.c

Modified: team/kmoore/cel_backend_refactor/cel/cel_custom.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_custom.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_custom.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_custom.c Wed Jul 31 22:10:39 2013
@@ -64,7 +64,7 @@
 	AST_RWLIST_ENTRY(cel_config) list;
 };
 
-static struct ast_event_sub *event_sub = NULL;
+#define CUSTOM_BACKEND_NAME "CEL Custom CSV Logging"
 
 static AST_RWLIST_HEAD_STATIC(sinks, cel_config);
 
@@ -116,7 +116,7 @@
 	return res;
 }
 
-static void custom_log(const struct ast_event *event, void *userdata)
+static void custom_log(struct ast_event *event)
 {
 	struct ast_channel *dummy;
 	struct ast_str *str;
@@ -167,19 +167,15 @@
 
 static int unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
 
 	if (AST_RWLIST_WRLOCK(&sinks)) {
-		event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
-			NULL, AST_EVENT_IE_END);
 		ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
 		return -1;
 	}
 
 	free_config();
 	AST_RWLIST_UNLOCK(&sinks);
+	ast_cel_backend_unregister(CUSTOM_BACKEND_NAME);
 	return 0;
 }
 
@@ -193,8 +189,9 @@
 	load_config();
 	AST_RWLIST_UNLOCK(&sinks);
 
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
-		NULL, AST_EVENT_IE_END);
+	if (ast_cel_backend_register(CUSTOM_BACKEND_NAME, custom_log)) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
 	return AST_MODULE_LOAD_SUCCESS;
 }
 

Modified: team/kmoore/cel_backend_refactor/cel/cel_manager.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_manager.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_manager.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_manager.c Wed Jul 31 22:10:39 2013
@@ -57,12 +57,12 @@
 /*! \brief show_user_def is off by default */
 #define CEL_SHOW_USERDEF_DEFAULT	0
 
+#define MANAGER_BACKEND_NAME "Manager Event Logging"
+
 /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
 static unsigned char cel_show_user_def;
 
-static struct ast_event_sub *event_sub;
-
-static void manager_log(const struct ast_event *event, void *userdata)
+static void manager_log(struct ast_event *event)
 {
 	struct ast_tm timeresult;
 	char start_time[80] = "";
@@ -180,12 +180,9 @@
 
 	cel_show_user_def = new_cel_show_user_def;
 	if (enablecel && !newenablecel) {
-		if (event_sub) {
-			event_sub = ast_event_unsubscribe(event_sub);
-		}
+		ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
 	} else if (!enablecel && newenablecel) {
-		event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
-		if (!event_sub) {
+		if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
 			ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
 		}
 	}
@@ -196,9 +193,7 @@
 
 static int unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
+	ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
 	return 0;
 }
 

Modified: team/kmoore/cel_backend_refactor/cel/cel_odbc.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_odbc.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_odbc.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_odbc.c Wed Jul 31 22:10:39 2013
@@ -51,7 +51,8 @@
 #include "asterisk/module.h"
 
 #define	CONFIG	"cel_odbc.conf"
-static struct ast_event_sub *event_sub = NULL;
+
+#define ODBC_BACKEND_NAME "ODBC CEL backend"
 
 /*! \brief show_user_def is off by default */
 #define CEL_SHOW_USERDEF_DEFAULT	0
@@ -367,7 +368,7 @@
 				}																\
 			} while (0)
 
-static void odbc_log(const struct ast_event *event, void *userdata)
+static void odbc_log(struct ast_event *event)
 {
 	struct tables *tableptr;
 	struct columns *entry;
@@ -789,18 +790,12 @@
 
 static int unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
 	if (AST_RWLIST_WRLOCK(&odbc_tables)) {
-		event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
-		if (!event_sub) {
-			ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
-		}
 		ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
 		return -1;
 	}
 
+	ast_cel_backend_unregister(ODBC_BACKEND_NAME);
 	free_config();
 	AST_RWLIST_UNLOCK(&odbc_tables);
 	AST_RWLIST_HEAD_DESTROY(&odbc_tables);
@@ -814,13 +809,13 @@
 
 	if (AST_RWLIST_WRLOCK(&odbc_tables)) {
 		ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
-		return 0;
+		return AST_MODULE_LOAD_FAILURE;
 	}
 	load_config();
 	AST_RWLIST_UNLOCK(&odbc_tables);
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
-	if (!event_sub) {
+	if (ast_cel_backend_register(ODBC_BACKEND_NAME, odbc_log)) {
 		ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
+		return AST_MODULE_LOAD_FAILURE;
 	}
 	return AST_MODULE_LOAD_SUCCESS;
 }
@@ -829,7 +824,7 @@
 {
 	if (AST_RWLIST_WRLOCK(&odbc_tables)) {
 		ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
-		return -1;
+		return AST_MODULE_LOAD_FAILURE;
 	}
 
 	free_config();
@@ -838,7 +833,7 @@
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CEL backend",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, ODBC_BACKEND_NAME,
 	.load = load_module,
 	.unload = unload_module,
 	.reload = reload,

Modified: team/kmoore/cel_backend_refactor/cel/cel_pgsql.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_pgsql.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_pgsql.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_pgsql.c Wed Jul 31 22:10:39 2013
@@ -58,6 +58,8 @@
 
 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
 
+#define PGSQL_BACKEND_NAME "CEL PGSQL backend"
+
 static char *config = "cel_pgsql.conf";
 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
 static int connected = 0;
@@ -73,7 +75,6 @@
 
 static PGconn	*conn = NULL;
 static PGresult	*result = NULL;
-static struct ast_event_sub *event_sub = NULL;
 
 struct columns {
 	char *name;
@@ -113,7 +114,7 @@
 		} \
 	} while (0)
 
-static void pgsql_log(const struct ast_event *event, void *userdata)
+static void pgsql_log(struct ast_event *event)
 {
 	struct ast_tm tm;
 	char timestr[128];
@@ -346,10 +347,7 @@
 {
 	struct columns *current;
 	AST_RWLIST_WRLOCK(&psql_columns);
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-		event_sub = NULL;
-	}
+	ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
 	if (conn) {
 		PQfinish(conn);
 		conn = NULL;
@@ -561,9 +559,7 @@
 	process_my_load_module(cfg);
 	ast_config_destroy(cfg);
 
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
-
-	if (!event_sub) {
+	if (ast_cel_backend_unregister(PGSQL_BACKEND_NAME, pgsql_log)) {
 		ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
 		return AST_MODULE_LOAD_DECLINE;
 	}

Modified: team/kmoore/cel_backend_refactor/cel/cel_radius.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_radius.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_radius.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_radius.c Wed Jul 31 22:10:39 2013
@@ -84,7 +84,8 @@
 static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
 
 static rc_handle *rh = NULL;
-static struct ast_event_sub *event_sub = NULL;
+
+#define RADIUS_BACKEND_NAME 
 
 #define ADD_VENDOR_CODE(x,y) (rc_avpair_add(rh, send, x, &y, strlen(y), VENDOR_CODE))
 
@@ -174,7 +175,7 @@
 	return 0;
 }
 
-static void radius_log(const struct ast_event *event, void *userdata)
+static void radius_log(struct ast_event *event)
 {
 	int result = ERROR_RC;
 	VALUE_PAIR *send = NULL;
@@ -204,9 +205,7 @@
 
 static int unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
+	ast_cel_backend_unregister(RADIUS_BACKEND_NAME);
 	if (rh) {
 		rc_destroy(rh);
 		rh = NULL;
@@ -256,8 +255,7 @@
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, radius_log, "CEL Radius Logging", NULL, AST_EVENT_IE_END);
-	if (!event_sub) {
+	if (ast_cel_backend_register(RADIUS_BACKEND_NAME, radius_log)) {
 		rc_destroy(rh);
 		rh = NULL;
 		return AST_MODULE_LOAD_DECLINE;

Modified: team/kmoore/cel_backend_refactor/cel/cel_sqlite3_custom.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_sqlite3_custom.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_sqlite3_custom.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_sqlite3_custom.c Wed Jul 31 22:10:39 2013
@@ -57,6 +57,8 @@
 #include "asterisk/options.h"
 #include "asterisk/stringfields.h"
 
+#define SQLITE_BACKEND_NAME "CEL sqlite3 custom backend"
+
 AST_MUTEX_DEFINE_STATIC(lock);
 
 static const char config_file[] = "cel_sqlite3_custom.conf";
@@ -69,7 +71,6 @@
  * \bug Handling of this var is crash prone on reloads
  */
 static char *columns;
-static struct ast_event_sub *event_sub = NULL;
 
 struct values {
 	char *expression;
@@ -229,7 +230,7 @@
 	}
 }
 
-static void write_cel(const struct ast_event *event, void *userdata)
+static void write_cel(struct ast_event *event)
 {
 	char *error = NULL;
 	char *sql = NULL;
@@ -293,9 +294,7 @@
 
 static int unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
+	ast_cel_backend_unregister(SQLITE_BACKEND_NAME);
 
 	free_config();
 
@@ -339,8 +338,7 @@
 		}
 	}
 
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, write_cel, "CEL sqlite3 custom backend", NULL, AST_EVENT_IE_END);
-	if (!event_sub) {
+	if (ast_cel_backend_register(SQLITE_BACKEND_NAME, write_cel)) {
 		ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n");
 		free_config();
 		return AST_MODULE_LOAD_DECLINE;

Modified: team/kmoore/cel_backend_refactor/cel/cel_tds.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/cel/cel_tds.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/cel/cel_tds.c (original)
+++ team/kmoore/cel_backend_refactor/cel/cel_tds.c Wed Jul 31 22:10:39 2013
@@ -81,9 +81,9 @@
 
 #define DATE_FORMAT "%Y/%m/%d %T"
 
+#define TDS_BACKEND_NAME "CEL TDS logging backend"
+
 static char *config = "cel_tds.conf";
-
-static struct ast_event_sub *event_sub = NULL;
 
 struct cel_tds_config {
 	AST_DECLARE_STRING_FIELDS(
@@ -112,7 +112,7 @@
 static int mssql_connect(void);
 static int mssql_disconnect(void);
 
-static void tds_log(const struct ast_event *event, void *userdata)
+static void tds_log(struct ast_event *event)
 {
 	char start[80];
 	char *accountcode_ai, *clidnum_ai, *exten_ai, *context_ai, *clid_ai, *channel_ai, *app_ai, *appdata_ai, *uniqueid_ai, *linkedid_ai, *cidani_ai, *cidrdnis_ai, *ciddnid_ai, *peer_ai, *userfield_ai;
@@ -397,9 +397,7 @@
 
 static int tds_unload_module(void)
 {
-	if (event_sub) {
-		event_sub = ast_event_unsubscribe(event_sub);
-	}
+	ast_cel_backend_unregister(TDS_BACKEND_NAME);
 
 	if (settings) {
 		ast_mutex_lock(&tds_lock);
@@ -561,8 +559,7 @@
 	}
 
 	/* Register MSSQL CEL handler */
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, tds_log, "CEL TDS logging backend", NULL, AST_EVENT_IE_END);
-	if (!event_sub) {
+	if (ast_cel_backend_register(TDS_BACKEND_NAME, tds_log)) {
 		ast_log(LOG_ERROR, "Unable to register MSSQL CEL handling\n");
 		ast_string_field_free_memory(settings);
 		ast_free(settings);

Modified: team/kmoore/cel_backend_refactor/include/asterisk/cel.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/include/asterisk/cel.h?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/include/asterisk/cel.h (original)
+++ team/kmoore/cel_backend_refactor/include/asterisk/cel.h Wed Jul 31 22:10:39 2013
@@ -300,6 +300,45 @@
 		enum ast_cel_event_type event_type, const char *userdefevname,
 		struct ast_json *extra, const char *peer_name);
 
+/*!
+ * \brief CEL backend callback
+ */
+/*typedef int (*ast_cel_backend_cb)(struct ast_cel_event_record *cel);*/
+typedef void (*ast_cel_backend_cb)(struct ast_event *event);
+
+/*!
+ * \brief Register a CEL backend
+ *
+ * \param name Name of backend to register
+ * \param backend_callback Callback to register
+ *
+ * \retval zero on success
+ * \retval non-zero on failure
+ * \since 12
+ */
+int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback);
+
+/*!
+ * \brief Unregister a CEL backend
+ *
+ * \param name Name of backend to unregister
+ *
+ * \retval zero on success
+ * \retval non-zero on failure
+ * \since 12
+ */
+int ast_cel_backend_unregister(const char *name);
+
+#ifdef TEST_FRAMEWORK
+/*!
+ * \brief Inject an event to CEL backends
+ *
+ * \param event Event to inject
+ * \since 12
+ */
+void ast_cel_inject_event(struct ast_event *event);
+#endif
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/kmoore/cel_backend_refactor/main/cel.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/main/cel.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/main/cel.c (original)
+++ team/kmoore/cel_backend_refactor/main/cel.c Wed Jul 31 22:10:39 2013
@@ -147,11 +147,17 @@
 /*! Container for primary channel/bridge ID listing for 2 party bridges */
 static struct ao2_container *bridge_primaries;
 
+/*! The number of buckets into which primary channel uniqueids will be hashed */
+#define BRIDGE_PRIMARY_BUCKETS 251
+
 struct stasis_message_type *cel_generic_type(void);
 STASIS_MESSAGE_TYPE_DEFN(cel_generic_type);
 
-/*! The number of buckets into which primary channel uniqueids will be hashed */
-#define BRIDGE_PRIMARY_BUCKETS 251
+/*! Container for CEL backend information */
+static struct ao2_container *cel_backends;
+
+/*! The number of buckets into which backend names will be hashed */
+#define BACKEND_BUCKETS 13
 
 /*! Container for dial end multichannel blobs for holding on to dial statuses */
 static struct ao2_container *cel_dialstatus_store;
@@ -323,6 +329,45 @@
 	[AST_CEL_LOCAL_OPTIMIZE]   = "LOCAL_OPTIMIZE",
 };
 
+struct cel_backend {
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(name);	/*!< Name of this backend */
+	);
+	ast_cel_backend_cb callback;	/*!< Callback for this backend */
+};
+
+/*! \brief Backend destructor */
+static void cel_backend_dtor(void *obj)
+{
+	struct cel_backend *backend = obj;
+	ast_string_field_free_memory(backend);
+	backend->callback = NULL;
+}
+
+/*! \brief Hashing function for cel_backend */
+static int cel_backend_hash(const void *obj, int flags)
+{
+	const struct cel_backend *backend = obj;
+	const char *name = obj;
+	if (!(flags & OBJ_KEY)) {
+		name = backend->name;
+	}
+
+	return ast_str_hash(name);
+}
+
+/*! \brief Comparator function for cel_backend */
+static int cel_backend_cmp(void *obj, void *arg, int flags)
+{
+	struct cel_backend *backend1 = obj, *backend2 = arg;
+	const char *backend2_id = arg, *backend1_id = backend1->name;
+	if (!(flags & OBJ_KEY)) {
+		backend2_id = backend2->name;
+	}
+
+	return !strcmp(backend1_id, backend2_id) ? CMP_MATCH | CMP_STOP : 0;
+}
+
 struct bridge_assoc {
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(bridge_id);           /*!< UniqueID of the bridge */
@@ -451,18 +496,18 @@
 	return 0;
 }
 
-static void print_cel_sub(const struct ast_event *event, void *data)
-{
-	struct ast_cli_args *a = data;
-
-	ast_cli(a->fd, "CEL Event Subscriber: %s\n",
-			ast_event_get_ie_str(event, AST_EVENT_IE_DESCRIPTION));
+static int event_desc_cb(void *obj, void *arg, int flags)
+{
+	struct ast_cli_args *a = arg;
+	struct cel_backend *backend = obj;
+
+	ast_cli(a->fd, "CEL Event Subscriber: %s\n", backend->name);
+	return 0;
 }
 
 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	unsigned int i;
-	struct ast_event_sub *sub;
 	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
 
 	switch (cmd) {
@@ -506,14 +551,7 @@
 	}
 
 	ao2_callback(cfg->general->apps, OBJ_NODATA, print_app, a);
-
-	if (!(sub = ast_event_subscribe_new(AST_EVENT_SUB, print_cel_sub, a))) {
-		return CLI_FAILURE;
-	}
-	ast_event_sub_append_ie_uint(sub, AST_EVENT_IE_EVENTTYPE, AST_EVENT_CEL);
-	ast_event_report_subs(sub);
-	ast_event_sub_destroy(sub);
-	sub = NULL;
+	ao2_callback(cel_backends, OBJ_MULTIPLE | OBJ_NODATA, event_desc_cb, a);
 
 	return CLI_SUCCESS;
 }
@@ -668,6 +706,13 @@
 		AST_EVENT_IE_END);
 }
 
+static int cel_backend_send_cb(void *obj, void *arg, int flags)
+{
+	struct cel_backend *backend = obj;
+	backend->callback(arg);
+	return 0;
+}
+
 static int cel_report_event(struct ast_channel_snapshot *snapshot,
 		enum ast_cel_event_type event_type, const char *userdefevname,
 		struct ast_json *extra, const char *peer2_name)
@@ -711,10 +756,13 @@
 	}
 
 	ev = ast_cel_create_event(snapshot, event_type, userdefevname, extra, peer_name);
-	if (ev && ast_event_queue(ev)) {
-		ast_event_destroy(ev);
-		return -1;
-	}
+	if (!ev) {
+		return -1;
+	}
+
+	/* Distribute event to backends */
+	ao2_callback(cel_backends, OBJ_MULTIPLE | OBJ_NODATA, cel_backend_send_cb, ev);
+	ast_event_destroy(ev);
 
 	return 0;
 }
@@ -1540,6 +1588,11 @@
 		return -1;
 	}
 
+	cel_backends = ao2_container_alloc(BACKEND_BUCKETS, cel_backend_hash, cel_backend_cmp);
+	if (!cel_backends) {
+		return -1;
+	}
+
 	cel_aggregation_topic = stasis_topic_create("cel_aggregation_topic");
 	if (!cel_aggregation_topic) {
 		return -1;
@@ -1699,3 +1752,40 @@
 	ao2_ref(mod_cfg->general, +1);
 }
 
+int ast_cel_backend_unregister(const char *name)
+{
+	RAII_VAR(struct cel_backend *, backend, NULL, ao2_cleanup);
+
+	backend = ao2_find(cel_backends, name, OBJ_KEY | OBJ_UNLINK);
+	if (!backend) {
+		return -1;
+	}
+
+	return 0;
+}
+
+int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
+{
+	RAII_VAR(struct cel_backend *, backend, NULL, ao2_cleanup);
+	if (ast_strlen_zero(name)) {
+		return -1;
+	}
+
+	backend = ao2_alloc(sizeof(*backend), cel_backend_dtor);
+	if (!backend || ast_string_field_init(backend, 32)) {
+		return -1;
+	}
+
+	ast_string_field_set(backend, name, name);
+	backend->callback = backend_callback;
+	ao2_link(cel_backends, backend);
+
+	return 0;
+}
+
+#ifdef TEST_FRAMEWORK
+void ast_cel_inject_event(struct ast_event *event)
+{
+	ao2_callback(cel_backends, OBJ_MULTIPLE | OBJ_NODATA, cel_backend_send_cb, event);
+}
+#endif

Modified: team/kmoore/cel_backend_refactor/tests/test_cel.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/cel_backend_refactor/tests/test_cel.c?view=diff&rev=395926&r1=395925&r2=395926
==============================================================================
--- team/kmoore/cel_backend_refactor/tests/test_cel.c (original)
+++ team/kmoore/cel_backend_refactor/tests/test_cel.c Wed Jul 31 22:10:39 2013
@@ -55,6 +55,8 @@
 
 #define CHANNEL_TECH_NAME "CELTestChannel"
 
+#define TEST_BACKEND_NAME "CEL Test Logging"
+
 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
 static struct ast_cel_general_config *saved_config;
 
@@ -1535,9 +1537,6 @@
 	return AST_TEST_PASS;
 }
 
-/*! Subscription for CEL events */
-static struct ast_event_sub *event_sub = NULL;
-
 /*! Container for astobj2 duplicated ast_events */
 static struct ao2_container *cel_received_events = NULL;
 
@@ -1639,24 +1638,13 @@
 	return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
 }
 
-static void test_sub(const struct ast_event *event, void *data)
+static void test_sub(struct ast_event *event)
 {
 	struct ast_event *event_dup = ao2_dup_event(event);
-	const char *sync_tag;
 	SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
 
 	if (!event_dup) {
 		return;
-	}
-
-	sync_tag = ast_event_get_ie_str(event, AST_EVENT_IE_SERVICE);
-	if (sync_tag) {
-		if (!strcmp(sync_tag, "SYNC")) {
-			/* trigger things */
-			SCOPED_MUTEX(lock, &sync_lock);
-			ast_cond_signal(&sync_out);
-			return;
-		}
 	}
 
 	/* save the event for later processing */
@@ -1680,7 +1668,6 @@
  */
 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
 {
-	ast_assert(event_sub == NULL);
 	ast_assert(cel_received_events == NULL);
 	ast_assert(cel_expected_events == NULL);
 
@@ -1697,8 +1684,9 @@
 	cel_expected_events = ao2_container_alloc(1, NULL, NULL);
 
 	/* start the CEL event callback */
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, test_sub, "CEL Test Logging",
-		NULL, AST_EVENT_IE_END);
+	if (ast_cel_backend_register(TEST_BACKEND_NAME, test_sub)) {
+		return -1;
+	}
 	return 0;
 }
 
@@ -1856,29 +1844,6 @@
 	return 0;
 }
 
-static struct ast_event *create_sync_event(void)
-{
-	struct ast_event *event_dup;
-	RAII_VAR(struct ast_event *, event, ao2_callback(cel_expected_events, 0, NULL, NULL), ao2_cleanup);
-	uint16_t event_len;
-
-	if (!event) {
-		return NULL;
-	}
-
-	event_len = ast_event_get_size(event);
-
-	event_dup = ast_calloc(1, event_len);
-	if (!event_dup) {
-		return NULL;
-	}
-
-	memcpy(event_dup, event, event_len);
-	ast_event_append_ie_str(&event_dup, AST_EVENT_IE_SERVICE, "SYNC");
-
-	return event_dup;
-}
-
 /*!
  * \internal \brief Callback function called after each test executes.
  * In addition to cleanup, this function also performs verification
@@ -1887,35 +1852,15 @@
  */
 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
 {
-	struct ast_event *sync;
 	RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
 	RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
-	ast_assert(event_sub != NULL);
 	ast_assert(cel_received_events != NULL);
 	ast_assert(cel_expected_events != NULL);
 
 	do_sleep();
 
-	/* sync with the event system */
-	sync = create_sync_event();
-	ast_test_validate(test, sync != NULL);
-	if (ast_event_queue(sync)) {
-		ast_event_destroy(sync);
-		ast_test_validate(test, NULL);
-	} else {
-		struct timeval start = ast_tvnow();
-		struct timespec end = {
-			.tv_sec = start.tv_sec + 15,
-			.tv_nsec = start.tv_usec * 1000
-		};
-
-		SCOPED_MUTEX(lock, &sync_lock);
-		ast_cond_timedwait(&sync_out, &sync_lock, &end);
-	}
-
 	/* stop the CEL event callback and clean up storage structures*/
-	ast_event_unsubscribe(event_sub);
-	event_sub = NULL;
+	ast_cel_backend_unregister(TEST_BACKEND_NAME);
 
 	/* cleaned up by RAII_VAR's */
 	cel_expected_events = NULL;




More information about the svn-commits mailing list