[asterisk-commits] kmoore: branch kmoore/stasis-cel_refactoring r388228 - in /team/kmoore/stasis...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 9 17:06:30 CDT 2013


Author: kmoore
Date: Thu May  9 17:06:29 2013
New Revision: 388228

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388228
Log:
Initial pass of CEL refactoring work, compiles

Modified:
    team/kmoore/stasis-cel_refactoring/include/asterisk/stasis_channels.h
    team/kmoore/stasis-cel_refactoring/main/cel.c
    team/kmoore/stasis-cel_refactoring/main/channel.c
    team/kmoore/stasis-cel_refactoring/main/stasis_channels.c

Modified: team/kmoore/stasis-cel_refactoring/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-cel_refactoring/include/asterisk/stasis_channels.h?view=diff&rev=388228&r1=388227&r2=388228
==============================================================================
--- team/kmoore/stasis-cel_refactoring/include/asterisk/stasis_channels.h (original)
+++ team/kmoore/stasis-cel_refactoring/include/asterisk/stasis_channels.h Thu May  9 17:06:29 2013
@@ -50,10 +50,20 @@
 		AST_STRING_FIELD(data);			/*!< Data passed to current application */
 		AST_STRING_FIELD(context);		/*!< Dialplan: Current extension context */
 		AST_STRING_FIELD(exten);		/*!< Dialplan: Current extension number */
+		AST_STRING_FIELD(bridged_name);		/*!< The name of the channel bridged to this one */
+		AST_STRING_FIELD(bridged_id);		/*!< The uniqueid of the channel bridged to this one */
 		AST_STRING_FIELD(caller_name);		/*!< Caller ID Name */
 		AST_STRING_FIELD(caller_number);	/*!< Caller ID Number */
+		AST_STRING_FIELD(caller_ani);		/*!< Caller ID ANI Number */
+		AST_STRING_FIELD(caller_rdnis);		/*!< Caller ID RDNIS Number */
+		AST_STRING_FIELD(caller_dnid);		/*!< Caller ID DNID Number */
 		AST_STRING_FIELD(connected_name);	/*!< Connected Line Name */
 		AST_STRING_FIELD(connected_number);	/*!< Connected Line Number */
+		/*!
+		 * The value of the DIALSTATUS channel variable.
+		 * This is necessary to keep separately from manager_vars for CEL purposes.
+		 */
+		AST_STRING_FIELD(dialstatus);
 	);
 
 	struct timeval creationtime;	/*!< The time of channel creation */

Modified: team/kmoore/stasis-cel_refactoring/main/cel.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-cel_refactoring/main/cel.c?view=diff&rev=388228&r1=388227&r2=388228
==============================================================================
--- team/kmoore/stasis-cel_refactoring/main/cel.c (original)
+++ team/kmoore/stasis-cel_refactoring/main/cel.c Thu May  9 17:06:29 2013
@@ -55,6 +55,11 @@
 #include "asterisk/config.h"
 #include "asterisk/cli.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/stasis_message_router.h"
+#include "asterisk/stasis_channels.h"
+
+/*! Message router for channel state */
+static struct stasis_message_router *cel_channel_state_router;
 
 /*! Is the CEL subsystem enabled ? */
 static unsigned char cel_enabled;
@@ -372,21 +377,101 @@
 	return S_OR(cel_ama_flags[flag], "Unknown");
 }
 
+static int report_event_snapshot(struct ast_channel_snapshot *snapshot,
+		enum ast_cel_event_type event_type, const char *userdefevname,
+		const char *extra)
+{
+	struct timeval eventtime;
+	struct ast_event *ev;
+	char *linkedid = ast_strdupa(snapshot->linkedid);
+
+	/* Make sure a reload is not occurring while we're checking to see if this
+	 * is an event that we care about.  We could lose an important event in this
+	 * process otherwise. */
+	ast_mutex_lock(&reload_lock);
+
+	/* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
+	 * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
+	if (cel_enabled && ast_cel_track_event(AST_CEL_LINKEDID_END) && event_type == AST_CEL_CHANNEL_START && linkedid) {
+		if (ast_cel_linkedid_ref(linkedid)) {
+			ast_mutex_unlock(&reload_lock);
+			return -1;
+		}
+	}
+
+	if (!cel_enabled || !ast_cel_track_event(event_type)) {
+		ast_mutex_unlock(&reload_lock);
+		return 0;
+	}
+
+	if (event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END) {
+		char *app;
+		if (!(app = ao2_find(appset, (char *) snapshot->appl, OBJ_POINTER))) {
+			ast_mutex_unlock(&reload_lock);
+			return 0;
+		}
+		ao2_ref(app, -1);
+	}
+
+	ast_mutex_unlock(&reload_lock);
+
+	if (!userdefevname) {
+		userdefevname = "";
+	}
+
+	if (!extra) {
+		extra = "";
+	}
+
+	eventtime = ast_tvnow();
+
+	ev = ast_event_new(AST_EVENT_CEL,
+		AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
+		AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
+		AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
+		AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, userdefevname,
+		AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_name,
+		AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_number,
+		AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_ani,
+		AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_rdnis,
+		AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_dnid,
+		AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, snapshot->exten,
+		AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, snapshot->context,
+		AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->name,
+		AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->appl,
+		AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, snapshot->data,
+		AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, snapshot->amaflags,
+		AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, snapshot->accountcode,
+		AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peeraccount,
+		AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->uniqueid,
+		AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->linkedid,
+		AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->userfield,
+		AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, extra,
+		AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, snapshot->bridged_name,
+		AST_EVENT_IE_END);
+
+	if (ev && ast_event_queue(ev)) {
+		ast_event_destroy(ev);
+		return -1;
+	}
+
+	return 0;
+}
+
 /* called whenever a channel is destroyed or a linkedid is changed to
  * potentially emit a CEL_LINKEDID_END event */
-void ast_cel_check_retire_linkedid(struct ast_channel *chan)
-{
-	const char *linkedid = ast_channel_linkedid(chan);
+static void check_retire_linkedid(struct ast_channel_snapshot *snapshot)
+{
 	char *lid;
 
 	/* make sure we need to do all this work */
 
-	if (ast_strlen_zero(linkedid) || !ast_cel_track_event(AST_CEL_LINKEDID_END)) {
-		return;
-	}
-
-	if (!(lid = ao2_find(linkedids, (void *) linkedid, OBJ_POINTER))) {
-		ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n", linkedid);
+	if (ast_strlen_zero(snapshot->linkedid) || !ast_cel_track_event(AST_CEL_LINKEDID_END)) {
+		return;
+	}
+
+	if (!(lid = ao2_find(linkedids, (void *) snapshot->linkedid, OBJ_POINTER))) {
+		ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n", snapshot->linkedid);
 		return;
 	}
 
@@ -394,7 +479,7 @@
 	 * before unreffing the channel we have a refcount of 3, we're done. Unlink and report. */
 	if (ao2_ref(lid, -1) == 3) {
 		ao2_unlink(linkedids, lid);
-		ast_cel_report_event(chan, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
+		report_event_snapshot(snapshot, AST_CEL_LINKEDID_END, NULL, NULL);
 	}
 	ao2_ref(lid, -1);
 }
@@ -719,8 +804,111 @@
 #define lid_hash app_hash
 #define lid_cmp app_cmp
 
+/*! \brief Typedef for callbacks that get called on channel snapshot updates */
+typedef void (*cel_snapshot_monitor)(
+	struct ast_channel_snapshot *old_snapshot,
+	struct ast_channel_snapshot *new_snapshot);
+
+/*! \brief Handle channel state changes */
+static void cel_channel_state_change(
+	struct ast_channel_snapshot *old_snapshot,
+	struct ast_channel_snapshot *new_snapshot)
+{
+	int is_hungup, was_hungup;
+
+	if (!new_snapshot) {
+		report_event_snapshot(old_snapshot, AST_CEL_CHANNEL_END, NULL, NULL);
+		check_retire_linkedid(old_snapshot);
+		return;
+	}
+
+	if (!old_snapshot) {
+		report_event_snapshot(new_snapshot, AST_CEL_CHANNEL_START, NULL, NULL);
+		return;
+	}
+
+	was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
+	is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
+
+	if (!was_hungup && is_hungup) {
+		RAII_VAR(struct ast_str *, extra_str, ast_str_create(128), ast_free);
+		ast_str_set(&extra_str, 0, "%d,%s,%s", new_snapshot->hangupcause, new_snapshot->hangupsource, new_snapshot->dialstatus);
+		report_event_snapshot(new_snapshot, AST_CEL_HANGUP, NULL, ast_str_buffer(extra_str));
+		return;
+	}
+
+	if (old_snapshot->state != new_snapshot->state && new_snapshot->state == AST_STATE_UP) {
+		report_event_snapshot(new_snapshot, AST_CEL_ANSWER, NULL, NULL);
+		return;
+	}
+}
+
+static void cel_channel_linkedid_change(
+	struct ast_channel_snapshot *old_snapshot,
+	struct ast_channel_snapshot *new_snapshot)
+{
+	if (strcmp(old_snapshot->linkedid, new_snapshot->linkedid)) {
+		check_retire_linkedid(old_snapshot);
+	}
+}
+
+static void cel_channel_app_change(
+	struct ast_channel_snapshot *old_snapshot,
+	struct ast_channel_snapshot *new_snapshot)
+{
+	/* No Newexten event on cache clear */
+	if (!new_snapshot || !old_snapshot
+		|| !strcmp(old_snapshot->appl, new_snapshot->appl)) {
+		return;
+	}
+
+	/* old snapshot has an application, end it */
+	if (!ast_strlen_zero(old_snapshot->appl)) {
+		/* AST_CEL_APP_END */
+		return;
+	}
+
+	/* new snapshot has an application, start it */
+	if (!ast_strlen_zero(old_snapshot->appl)) {
+		/* AST_CEL_APP_START */
+		return;
+	}
+}
+
+cel_snapshot_monitor cel_monitors[] = {
+	cel_channel_state_change,
+	cel_channel_app_change,
+	cel_channel_linkedid_change,
+};
+
+static void cel_channel_snapshot_update(void *data, struct stasis_subscription *sub,
+				    struct stasis_topic *topic,
+				    struct stasis_message *message)
+{
+	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	struct stasis_cache_update *update;
+	struct ast_channel_snapshot *old_snapshot;
+	struct ast_channel_snapshot *new_snapshot;
+	size_t i;
+
+	update = stasis_message_data(message);
+
+	if (ast_channel_snapshot_type() != update->type) {
+		return;
+	}
+
+	old_snapshot = stasis_message_data(update->old_snapshot);
+	new_snapshot = stasis_message_data(update->new_snapshot);
+
+	for (i = 0; i < ARRAY_LEN(cel_monitors); ++i) {
+		cel_monitors[i](old_snapshot, new_snapshot);
+	}
+}
+
 static void ast_cel_engine_term(void)
 {
+	stasis_message_router_unsubscribe(cel_channel_state_router);
+	cel_channel_state_router = NULL;
 	if (appset) {
 		ao2_ref(appset, -1);
 		appset = NULL;
@@ -734,6 +922,7 @@
 
 int ast_cel_engine_init(void)
 {
+	int ret = 0;
 	if (!(appset = ao2_container_alloc(NUM_APP_BUCKETS, app_hash, app_cmp))) {
 		return -1;
 	}
@@ -750,6 +939,26 @@
 		return -1;
 	}
 
+	cel_channel_state_router = stasis_message_router_create(
+		stasis_caching_get_topic(ast_channel_topic_all_cached()));
+
+	if (!cel_channel_state_router) {
+		return -1;
+	}
+
+	ret |= stasis_message_router_add(cel_channel_state_router,
+					 stasis_cache_update_type(),
+					 cel_channel_snapshot_update,
+					 NULL);
+
+	/* If somehow we failed to add any routes, just shut down the whole
+	 * thing and fail it.
+	 */
+	if (ret) {
+		ast_cel_engine_term();
+		return -1;
+	}
+
 	ast_register_atexit(ast_cel_engine_term);
 
 	return 0;

Modified: team/kmoore/stasis-cel_refactoring/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-cel_refactoring/main/channel.c?view=diff&rev=388228&r1=388227&r2=388228
==============================================================================
--- team/kmoore/stasis-cel_refactoring/main/channel.c (original)
+++ team/kmoore/stasis-cel_refactoring/main/channel.c Thu May  9 17:06:29 2013
@@ -1060,14 +1060,6 @@
 		ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
 	}
 
-	/*
-	 * This is the last place the channel constructor can fail.
-	 *
-	 * The destructor takes advantage of this fact to ensure that the
-	 * AST_CEL_CHANNEL_END is not posted if we have not posted the
-	 * AST_CEL_CHANNEL_START yet.
-	 */
-
 	if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
 		return ast_channel_unref(tmp);
 	}
@@ -1153,7 +1145,6 @@
 	ast_cdr_start(ast_channel_cdr(tmp));
 
 	ast_atomic_fetchadd_int(&chancount, +1);
-	ast_cel_report_event(tmp, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
 
 	headp = ast_channel_varshead(tmp);
 	AST_LIST_HEAD_INIT_NOLOCK(headp);
@@ -2371,11 +2362,6 @@
 	char device_name[AST_CHANNEL_NAME];
 	struct ast_callid *callid;
 
-	if (ast_channel_internal_is_finalized(chan)) {
-		ast_cel_report_event(chan, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
-		ast_cel_check_retire_linkedid(chan);
-	}
-
 	ast_pbx_hangup_handler_destroy(chan);
 
 	ast_channel_lock(chan);
@@ -2790,8 +2776,6 @@
 /*! \brief Hangup a channel */
 int ast_hangup(struct ast_channel *chan)
 {
-	char extra_str[64]; /* used for cel logging below */
-
 	ast_autoservice_stop(chan);
 
 	ast_channel_lock(chan);
@@ -2866,9 +2850,6 @@
 	ast_channel_generatordata_set(chan, NULL);
 	ast_channel_generator_set(chan, NULL);
 
-	snprintf(extra_str, sizeof(extra_str), "%d,%s,%s", ast_channel_hangupcause(chan), ast_channel_hangupsource(chan), S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
-	ast_cel_report_event(chan, AST_CEL_HANGUP, NULL, extra_str, NULL);
-
 	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
 		ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
 			"is blocked by thread %ld in procedure %s!  Expect a failure\n",
@@ -2934,11 +2915,9 @@
 		if (cdr_answer) {
 			ast_cdr_answer(ast_channel_cdr(chan));
 		}
-		ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
 		ast_channel_unlock(chan);
 		break;
 	case AST_STATE_UP:
-		ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
 		/* Calling ast_cdr_answer when it it has previously been called
 		 * is essentially a no-op, so it is safe.
 		 */
@@ -4083,8 +4062,6 @@
 				} else {
 					/* Answer the CDR */
 					ast_setstate(chan, AST_STATE_UP);
-					/* removed a call to ast_cdr_answer(chan->cdr) from here. */
-					ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
 				}
 			} else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
 				read_action_payload = f->data.ptr;
@@ -6653,7 +6630,6 @@
 		return;
 	}
 
-	ast_cel_check_retire_linkedid(chan);
 	ast_channel_linkedid_set(chan, linkedid);
 	ast_cel_linkedid_ref(linkedid);
 }

Modified: team/kmoore/stasis-cel_refactoring/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-cel_refactoring/main/stasis_channels.c?view=diff&rev=388228&r1=388227&r2=388228
==============================================================================
--- team/kmoore/stasis-cel_refactoring/main/stasis_channels.c (original)
+++ team/kmoore/stasis-cel_refactoring/main/stasis_channels.c Thu May  9 17:06:29 2013
@@ -35,6 +35,7 @@
 #include "asterisk/stasis.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/pbx.h"
 
 #define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7
 
@@ -102,6 +103,7 @@
 struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
 {
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+	struct ast_channel *bridged;
 
 	snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor);
 	if (!snapshot || ast_string_field_init(snapshot, 1024)) {
@@ -125,10 +127,28 @@
 	ast_string_field_set(snapshot, context, ast_channel_context(chan));
 	ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
 
+	bridged = ast_bridged_channel(chan);
+	if (bridged) {
+		ast_channel_ref(bridged);
+		ast_channel_lock(bridged);
+		ast_string_field_set(snapshot, bridged_name, ast_channel_name(bridged));
+		ast_string_field_set(snapshot, bridged_id, ast_channel_uniqueid(bridged));
+		ast_channel_unlock(bridged);
+		ast_channel_unref(bridged);
+		bridged = NULL;
+	}
+
 	ast_string_field_set(snapshot, caller_name,
 		S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
 	ast_string_field_set(snapshot, caller_number,
 		S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
+
+	ast_string_field_set(snapshot, caller_ani,
+		S_COR(ast_channel_caller(chan)->ani.number.valid, ast_channel_caller(chan)->ani.number.str, ""));
+	ast_string_field_set(snapshot, caller_rdnis,
+		S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, ""));
+	ast_string_field_set(snapshot, caller_dnid,
+		S_OR(ast_channel_dialed(chan)->number.str, ""));
 
 	ast_string_field_set(snapshot, connected_name,
 		S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
@@ -143,6 +163,8 @@
 	snapshot->flags = *ast_channel_flags(chan);
 	snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
 
+	ast_string_field_set(snapshot, dialstatus,
+		S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
 	snapshot->manager_vars = ast_channel_get_manager_vars(chan);
 
 	ao2_ref(snapshot, +1);




More information about the asterisk-commits mailing list