[asterisk-commits] dlee: branch dlee/json_main r382803 - in /team/dlee/json_main: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 11 12:26:23 CDT 2013


Author: dlee
Date: Mon Mar 11 12:26:19 2013
New Revision: 382803

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382803
Log:
Extracted manager channel message handling to its own file

Added:
    team/dlee/json_main/main/manager/
    team/dlee/json_main/main/manager/channels.c   (with props)
Modified:
    team/dlee/json_main/include/asterisk/manager.h
    team/dlee/json_main/main/manager.c

Modified: team/dlee/json_main/include/asterisk/manager.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/include/asterisk/manager.h?view=diff&rev=382803&r1=382802&r2=382803
==============================================================================
--- team/dlee/json_main/include/asterisk/manager.h (original)
+++ team/dlee/json_main/include/asterisk/manager.h Mon Mar 11 12:26:19 2013
@@ -316,4 +316,9 @@
  */
 struct ast_datastore *astman_datastore_find(struct mansession *s, const struct ast_datastore_info *info, const char *uid);
 
+/*!
+ * \brief Initialize support for AMI channel events.
+ */
+void manager_channels_init(void);
+
 #endif /* _ASTERISK_MANAGER_H */

Modified: team/dlee/json_main/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/main/manager.c?view=diff&rev=382803&r1=382802&r2=382803
==============================================================================
--- team/dlee/json_main/main/manager.c (original)
+++ team/dlee/json_main/main/manager.c Mon Mar 11 12:26:19 2013
@@ -1127,8 +1127,6 @@
 	{{ "module", "unload", NULL }},
 	{{ "restart", "gracefully", NULL }},
 };
-
-static struct stasis_subscription *channel_state_sub;
 
 static void acl_change_event_cb(const struct ast_event *event, void *userdata);
 
@@ -7446,202 +7444,6 @@
 	AST_RWLIST_UNLOCK(&channelvars);
 }
 
-/*!
- * \brief Generate the AMI message body from a channel snapshot
- * \internal
- *
- * \param snapshot the channel snapshot for which to generate an AMI message body
- *
- * \retval NULL on error
- * \retval ast_str* on success (must be ast_freed by caller)
- */
-static struct ast_str *manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
-{
-	struct ast_str *out = ast_str_create(1024);
-	int res = 0;
-	if (!out) {
-		return NULL;
-	}
-	res = ast_str_set(&out, 0,
-		"Channel: %s\r\n"
-		"ChannelState: %d\r\n"
-		"ChannelStateDesc: %s\r\n"
-		"CallerIDNum: %s\r\n"
-		"CallerIDName: %s\r\n"
-		"ConnectedLineNum: %s\r\n"
-		"ConnectedLineName: %s\r\n"
-		"AccountCode: %s\r\n"
-		"Context: %s\r\n"
-		"Exten: %s\r\n"
-		"Priority: %d\r\n"
-		"Uniqueid: %s\r\n"
-		"Cause: %d\r\n"
-		"Cause-txt: %s\r\n",
-		snapshot->name,
-		snapshot->state,
-		ast_state2str(snapshot->state),
-		snapshot->caller_number,
-		snapshot->caller_name,
-		snapshot->connected_number,
-		snapshot->connected_name,
-		snapshot->accountcode,
-		snapshot->context,
-		snapshot->exten,
-		snapshot->priority,
-		snapshot->uniqueid,
-		snapshot->hangupcause,
-		ast_cause2str(snapshot->hangupcause));
-
-	if (!res) {
-		return NULL;
-	}
-
-	return out;
-}
-
-static void channel_snapshot_update(struct stasis_cache_update *update)
-{
-	struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
-	struct ast_channel_snapshot *new_snapshot = stasis_message_data(update->new_snapshot);
-	int is_hungup;
-	char *manager_event = NULL;
-
-	if (!new_snapshot) {
-		/* Ignore cache clearing events; we'll see the hangup first */
-		return;
-	}
-
-	is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
-
-	if (!old_snapshot) {
-		manager_event = "Newchannel";
-	}
-
-	if (old_snapshot && old_snapshot->state != new_snapshot->state) {
-		manager_event = "Newstate";
-	}
-
-	if (old_snapshot && is_hungup) {
-		manager_event = "Hangup";
-	}
-
-	if (manager_event) {
-		RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
-
-		channel_event_string = manager_build_channel_state_string(new_snapshot);
-		if (channel_event_string) {
-			manager_event(EVENT_FLAG_CALL, manager_event, "%s", ast_str_buffer(channel_event_string));
-		}
-	}
-}
-
-static void channel_varset(struct ast_channel_blob *obj)
-{
-	const char *variable = ast_json_string_get(ast_json_object_get(obj->blob, "variable"));
-	const char *value = ast_json_string_get(ast_json_object_get(obj->blob, "value"));
-	const char *name;
-	const char *uniqueid;
-
-	if (obj->snapshot) {
-		name = obj->snapshot->name;
-		uniqueid = obj->snapshot->uniqueid;
-	} else {
-		name = "none";
-		uniqueid = "none";
-	}
-
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a variable is set to a particular value.</synopsis>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_DIALPLAN, "VarSet",
-		      "Channel: %s\r\n"
-		      "Variable: %s\r\n"
-		      "Value: %s\r\n"
-		      "Uniqueid: %s\r\n",
-		      name, variable, value, uniqueid);
-}
-
-static void channel_userevent(struct ast_channel_blob *obj)
-{
-	const char *uniqueid;
-	const char *eventname;
-	const char *body;
-
-	uniqueid = obj->snapshot->uniqueid;
-	eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
-	body = ast_json_string_get(ast_json_object_get(obj->blob, "body"));
-
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>A user defined event raised from the dialplan.</synopsis>
-			<parameter name="UserEvent">
-				<para>The event name, as specified in the dialplan.</para>
-			</parameter>
-			<see-also>
-			<ref type="application">UserEvent</ref>
-			</see-also>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_USER, "UserEvent",
-		      "UserEvent: %s\r\n"
-		      "Uniqueid: %s\r\n"
-		      "%s",
-		      eventname, uniqueid, body);
-}
-
-static void channel_newexten(struct ast_channel_snapshot *snapshot)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a channel enters a new context, extension, priority.</synopsis>
-			<syntax>
-				<parameter name="Application">
-					<para>The application about to be executed.</para>
-				</parameter>
-				<parameter name="AppData">
-					<para>The data to be passed to the application.</para>
-				</parameter>
-			</syntax>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_DIALPLAN, "Newexten",
-		      "Channel: %s\r\n"
-		      "Context: %s\r\n"
-		      "Extension: %s\r\n"
-		      "Priority: %d\r\n"
-		      "Application: %s\r\n"
-		      "AppData: %s\r\n"
-		      "Uniqueid: %s\r\n",
-		      snapshot->name, snapshot->context, snapshot->exten, snapshot->priority, snapshot->appl, snapshot->data, snapshot->uniqueid);
-}
-
-/*!
- * \brief Callback processing messages on the channel topic.
- */
-static void channel_message_cb(void *data, struct stasis_subscription *sub,
-			       struct stasis_topic *topic, struct stasis_message *message)
-{
-	if (stasis_message_type(message) == stasis_cache_update()) {
-		struct stasis_cache_update *update = stasis_message_data(message);
-		if (ast_channel_snapshot() == update->type) {
-			channel_snapshot_update(update);
-		}
-	} else if (stasis_message_type(message) == ast_channel_blob()) {
-		struct ast_channel_blob *obj = stasis_message_data(message);
-
-		if (strcmp("varset", ast_channel_blob_type(obj)) == 0) {
-			channel_varset(obj);
-		} else if (strcmp("userevent", ast_channel_blob_type(obj)) == 0) {
-			channel_userevent(obj);
-		}
-	} else if (stasis_message_type(message) == ast_pbx_newexten()) {
-		struct ast_channel_snapshot *snapshot = stasis_message_data(message);
-		channel_newexten(snapshot);
-	}
-}
-
 /*! \internal \brief Free a user record.  Should already be removed from the list */
 static void manager_free_user(struct ast_manager_user *user)
 {
@@ -7664,9 +7466,6 @@
 static void manager_shutdown(void)
 {
 	struct ast_manager_user *user;
-
-	stasis_unsubscribe(channel_state_sub);
-	channel_state_sub = NULL;
 
 	if (registered) {
 		ast_manager_unregister("Ping");
@@ -7759,11 +7558,7 @@
 
 	manager_enabled = 0;
 
-	if (!channel_state_sub) {
-		channel_state_sub = stasis_subscribe(
-			stasis_caching_get_topic(ast_channel_topic_all_cached()),
-			channel_message_cb, NULL);
-	}
+	manager_channels_init();
 
 	if (!registered) {
 		/* Register default actions */

Added: team/dlee/json_main/main/manager/channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/json_main/main/manager/channels.c?view=auto&rev=382803
==============================================================================
--- team/dlee/json_main/main/manager/channels.c (added)
+++ team/dlee/json_main/main/manager/channels.c Mon Mar 11 12:26:19 2013
@@ -1,0 +1,247 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief The Asterisk Management Interface - AMI (channel event handling)
+ *
+ * \author David M. Lee, II <dlee at digium.com>
+ *
+ * AMI generated many per-channel and global-channel events by converting Stasis
+ * messages to AMI events. It makes sense to simply put them into a single file.
+ */
+
+#include "asterisk/manager.h"
+
+static struct stasis_subscription *channel_state_sub;
+
+/*** DOCUMENTATION
+ ***/
+
+/*!
+ * \brief Generate the AMI message body from a channel snapshot
+ * \internal
+ *
+ * \param snapshot the channel snapshot for which to generate an AMI message body
+ *
+ * \retval NULL on error
+ * \retval ast_str* on success (must be ast_freed by caller)
+ */
+static struct ast_str *manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
+{
+	struct ast_str *out = ast_str_create(1024);
+	int res = 0;
+	if (!out) {
+		return NULL;
+	}
+	res = ast_str_set(&out, 0,
+		"Channel: %s\r\n"
+		"ChannelState: %d\r\n"
+		"ChannelStateDesc: %s\r\n"
+		"CallerIDNum: %s\r\n"
+		"CallerIDName: %s\r\n"
+		"ConnectedLineNum: %s\r\n"
+		"ConnectedLineName: %s\r\n"
+		"AccountCode: %s\r\n"
+		"Context: %s\r\n"
+		"Exten: %s\r\n"
+		"Priority: %d\r\n"
+		"Uniqueid: %s\r\n"
+		"Cause: %d\r\n"
+		"Cause-txt: %s\r\n",
+		snapshot->name,
+		snapshot->state,
+		ast_state2str(snapshot->state),
+		snapshot->caller_number,
+		snapshot->caller_name,
+		snapshot->connected_number,
+		snapshot->connected_name,
+		snapshot->accountcode,
+		snapshot->context,
+		snapshot->exten,
+		snapshot->priority,
+		snapshot->uniqueid,
+		snapshot->hangupcause,
+		ast_cause2str(snapshot->hangupcause));
+
+	if (!res) {
+		return NULL;
+	}
+
+	return out;
+}
+
+static void channel_snapshot_update(struct stasis_cache_update *update)
+{
+	struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
+	struct ast_channel_snapshot *new_snapshot = stasis_message_data(update->new_snapshot);
+	int is_hungup;
+	char *manager_event = NULL;
+
+	if (!new_snapshot) {
+		/* Ignore cache clearing events; we'll see the hangup first */
+		return;
+	}
+
+	is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
+
+	if (!old_snapshot) {
+		manager_event = "Newchannel";
+	}
+
+	if (old_snapshot && old_snapshot->state != new_snapshot->state) {
+		manager_event = "Newstate";
+	}
+
+	if (old_snapshot && is_hungup) {
+		manager_event = "Hangup";
+	}
+
+	if (manager_event) {
+		RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+
+		channel_event_string = manager_build_channel_state_string(new_snapshot);
+		if (channel_event_string) {
+			manager_event(EVENT_FLAG_CALL, manager_event, "%s", ast_str_buffer(channel_event_string));
+		}
+	}
+}
+
+static void channel_varset(struct ast_channel_blob *obj)
+{
+	const char *variable = ast_json_string_get(ast_json_object_get(obj->blob, "variable"));
+	const char *value = ast_json_string_get(ast_json_object_get(obj->blob, "value"));
+	const char *name;
+	const char *uniqueid;
+
+	if (obj->snapshot) {
+		name = obj->snapshot->name;
+		uniqueid = obj->snapshot->uniqueid;
+	} else {
+		name = "none";
+		uniqueid = "none";
+	}
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a variable is set to a particular value.</synopsis>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DIALPLAN, "VarSet",
+		      "Channel: %s\r\n"
+		      "Variable: %s\r\n"
+		      "Value: %s\r\n"
+		      "Uniqueid: %s\r\n",
+		      name, variable, value, uniqueid);
+}
+
+static void channel_userevent(struct ast_channel_blob *obj)
+{
+	const char *uniqueid;
+	const char *eventname;
+	const char *body;
+
+	uniqueid = obj->snapshot->uniqueid;
+	eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
+	body = ast_json_string_get(ast_json_object_get(obj->blob, "body"));
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>A user defined event raised from the dialplan.</synopsis>
+			<parameter name="UserEvent">
+				<para>The event name, as specified in the dialplan.</para>
+			</parameter>
+			<see-also>
+			<ref type="application">UserEvent</ref>
+			</see-also>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_USER, "UserEvent",
+		      "UserEvent: %s\r\n"
+		      "Uniqueid: %s\r\n"
+		      "%s",
+		      eventname, uniqueid, body);
+}
+
+static void channel_newexten(struct ast_channel_snapshot *snapshot)
+{
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a channel enters a new context, extension, priority.</synopsis>
+			<syntax>
+				<parameter name="Application">
+					<para>The application about to be executed.</para>
+				</parameter>
+				<parameter name="AppData">
+					<para>The data to be passed to the application.</para>
+				</parameter>
+			</syntax>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DIALPLAN, "Newexten",
+		      "Channel: %s\r\n"
+		      "Context: %s\r\n"
+		      "Extension: %s\r\n"
+		      "Priority: %d\r\n"
+		      "Application: %s\r\n"
+		      "AppData: %s\r\n"
+		      "Uniqueid: %s\r\n",
+		      snapshot->name, snapshot->context, snapshot->exten, snapshot->priority, snapshot->appl, snapshot->data, snapshot->uniqueid);
+}
+
+/*!
+ * \brief Callback processing messages on the channel topic.
+ */
+static void channel_message_cb(void *data, struct stasis_subscription *sub,
+			       struct stasis_topic *topic, struct stasis_message *message)
+{
+	if (stasis_message_type(message) == stasis_cache_update()) {
+		struct stasis_cache_update *update = stasis_message_data(message);
+		if (ast_channel_snapshot() == update->type) {
+			channel_snapshot_update(update);
+		}
+	} else if (stasis_message_type(message) == ast_channel_blob()) {
+		struct ast_channel_blob *obj = stasis_message_data(message);
+
+		if (strcmp("varset", ast_channel_blob_type(obj)) == 0) {
+			channel_varset(obj);
+		} else if (strcmp("userevent", ast_channel_blob_type(obj)) == 0) {
+			channel_userevent(obj);
+		}
+	} else if (stasis_message_type(message) == ast_pbx_newexten()) {
+		struct ast_channel_snapshot *snapshot = stasis_message_data(message);
+		channel_newexten(snapshot);
+	}
+}
+
+static void manager_channels_shutdown(void)
+{
+	stasis_unsubscribe(channel_state_sub);
+	channel_state_sub = NULL;
+}
+
+void manager_channels_init(void)
+{
+	ast_register_atexit(manager_channels_shutdown);
+
+	if (!channel_state_sub) {
+		channel_state_sub = stasis_subscribe(
+			stasis_caching_get_topic(ast_channel_topic_all_cached()),
+			channel_message_cb, NULL);
+	}
+}

Propchange: team/dlee/json_main/main/manager/channels.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/json_main/main/manager/channels.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/json_main/main/manager/channels.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list