[asterisk-commits] kmoore: branch kmoore/stasis-bridging-channel_events r388038 - in /team/kmoor...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed May 8 15:12:15 CDT 2013


Author: kmoore
Date: Wed May  8 15:12:05 2013
New Revision: 388038

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388038
Log:
Multiple revisions 388005,388008,388014

........
  r388005 | dlee | 2013-05-08 13:34:50 -0500 (Wed, 08 May 2013) | 21 lines
  
  Remove required type field from channel blobs
  
  When we first introduced the channel blob types, the JSON blobs were
  self identifying by a required "type" field in the JSON object
  itself. This, as it turns out, was a bad idea.
  
  When we introduced the message router, it was useless for routing based
  on the JSON type. And messages had two type fields to check: the
  stasis_message_type() of the message itself, plus the type field in the
  JSON blob (but only if it was a blob message).
  
  This patch corrects that mistake by removing the required type field
  from JSON blobs, and introducing first class stasis_message_type objects
  for the actual message type.
  
  Since we now will have a proliferation of message types, I introduced a
  few macros to help reduce the amount of boilerplate necessary to set
  them up.
  
  Review: https://reviewboard.asterisk.org/r/2509
........
  r388008 | mjordan | 2013-05-08 13:36:21 -0500 (Wed, 08 May 2013) | 10 lines
  
  Don't perform a realtime lookup with a NULL keyword
  
  Previously, a call to ast_load_realtime_multientry could get away with
  passing a NULL parameter to the function, even though it really isn't
  supposed to do that. After the change over to using ast_variable instead
  of variadic arguments, the realtime engine gets unhappy if you do this.
  
  This was always an unintended function call in app_directory anyway - now,
  we just don't call into the realtime function calls if we don't have anything
  to query on.
........
  r388014 | dlee | 2013-05-08 14:00:55 -0500 (Wed, 08 May 2013) | 1 line
  
  Fixed set-but-not-used warning caught by newer GCC
........

Merged revisions 388005,388008,388014 from file:///srv/subversion/repos/asterisk/trunk
........

Merged revisions 388017 from http://svn.asterisk.org/svn/asterisk/team/group/bridge_construction

Modified:
    team/kmoore/stasis-bridging-channel_events/   (props changed)
    team/kmoore/stasis-bridging-channel_events/apps/app_directory.c
    team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c
    team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis.h
    team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis_channels.h
    team/kmoore/stasis-bridging-channel_events/main/channel.c
    team/kmoore/stasis-bridging-channel_events/main/manager_channels.c
    team/kmoore/stasis-bridging-channel_events/main/stasis_channels.c
    team/kmoore/stasis-bridging-channel_events/res/res_stasis.c
    team/kmoore/stasis-bridging-channel_events/tests/test_stasis_channels.c
    team/kmoore/stasis-bridging-channel_events/tests/test_stasis_endpoints.c

Propchange: team/kmoore/stasis-bridging-channel_events/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Wed May  8 15:12:05 2013
@@ -1,1 +1,1 @@
-/team/group/bridge_construction:1-388013
+/team/group/bridge_construction:1-388036

Modified: team/kmoore/stasis-bridging-channel_events/apps/app_directory.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/apps/app_directory.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/apps/app_directory.c (original)
+++ team/kmoore/stasis-bridging-channel_events/apps/app_directory.c Wed May  8 15:12:05 2013
@@ -440,7 +440,7 @@
 static struct ast_config *realtime_directory(char *context)
 {
 	struct ast_config *cfg;
-	struct ast_config *rtdata;
+	struct ast_config *rtdata = NULL;
 	struct ast_category *cat;
 	struct ast_variable *var;
 	char *mailbox;
@@ -475,7 +475,7 @@
 			rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", "default", SENTINEL);
 			context = "default";
 		}
-	} else {
+	} else if (!ast_strlen_zero(context)) {
 		rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, SENTINEL);
 	}
 

Modified: team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c (original)
+++ team/kmoore/stasis-bridging-channel_events/apps/app_userevent.c Wed May  8 15:12:05 2013
@@ -117,7 +117,8 @@
 		}
 	}
 
-	msg = ast_channel_blob_create(chan, blob);
+	msg = ast_channel_blob_create(
+		chan, ast_channel_user_event_type(), blob);
 	if (!msg) {
 		return -1;
 	}

Modified: team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis.h?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis.h (original)
+++ team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis.h Wed May  8 15:12:05 2013
@@ -543,6 +543,43 @@
 /*! @{ */
 
 /*!
+ * \brief Boiler-plate removing macro for defining message types.
+ *
+ * \param name Name of message type.
+ * \since 12
+ */
+#define STASIS_MESSAGE_TYPE_DEFN(name)				\
+	static struct stasis_message_type *__ ## name;	\
+	struct stasis_message_type *name(void) {	\
+		ast_assert(__ ## name != NULL);		\
+		return __ ## name;			\
+	}
+
+/*!
+ * \brief Boiler-plate removing macro for initializing message types.
+ *
+ * \param name Name of message type.
+ * \return 0 if initialization is successful.
+ * \return Non-zero on failure.
+ * \since 12
+ */
+#define STASIS_MESSAGE_TYPE_INIT(name)				\
+	({						\
+	__ ## name = stasis_message_type_create(#name);	\
+	__ ## name ? 0 : -1;				\
+	})
+
+#define STASIS_MESSAGE_TYPE_CLEANUP(name)		\
+	({					\
+		ao2_cleanup(__ ## name);	\
+		__ ## name = NULL;		\
+	})
+
+/*! @} */
+
+/*! @{ */
+
+/*!
  * \brief Initialize the Stasis subsystem
  * \return 0 on success.
  * \return Non-zero on error.

Modified: team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis_channels.h?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis_channels.h (original)
+++ team/kmoore/stasis-bridging-channel_events/include/asterisk/stasis_channels.h Wed May  8 15:12:05 2013
@@ -70,8 +70,7 @@
  * \since 12
  * \brief Blob of data associated with a channel.
  *
- * The \c blob is actually a JSON object of structured data. It has a "type" field
- * which contains the type string describing this blob.
+ * This blob is actually shared amongst several \ref stasis_message_type's.
  */
 struct ast_channel_blob {
 	/*! Channel blob is associated with (or NULL for global/all channels) */
@@ -112,14 +111,6 @@
 
 /*!
  * \since 12
- * \brief Message type for \ref ast_channel_blob messages.
- *
- * \retval Message type for \ref ast_channel_blob messages.
- */
-struct stasis_message_type *ast_channel_blob_type(void);
-
-/*!
- * \since 12
  * \brief Generate a snapshot of the channel state. This is an ao2 object, so
  * ao2_cleanup() to deallocate.
  *
@@ -128,44 +119,35 @@
  * \retval pointer on success (must be ast_freed)
  * \retval NULL on error
  */
-struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan);
+struct ast_channel_snapshot *ast_channel_snapshot_create(
+	struct ast_channel *chan);
 
 /*!
  * \since 12
  * \brief Creates a \ref ast_channel_blob message.
  *
- * The \a blob JSON object requires a \c "type" field describing the blob. It
- * should also be treated as immutable and not modified after it is put into the
- * message.
- *
- * \param chan Channel blob is associated with, or NULL for global/all channels.
- * \param blob JSON object representing the data.
+ * The given \a blob should be treated as immutable and not modified after it is
+ * put into the message.
+ *
+ * \param chan Channel blob is associated with, or \c NULL for global/all channels.
+ * \param type Message type for this blob.
+ * \param blob JSON object representing the data, or \c NULL for no data. If
+ *             \c NULL, ast_json_null() is put into the object.
+ *
  * \return \ref ast_channel_blob message.
  * \return \c NULL on error
  */
 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
-					       struct ast_json *blob);
-
-/*!
- * \since 12
- * \brief Extracts the type field from a \ref ast_channel_blob.
- * Returned \c char* is still owned by \a obj
- * \param obj Channel blob object.
- * \return Type field value from the blob.
- * \return \c NULL on error.
- */
-const char *ast_channel_blob_json_type(struct ast_channel_blob *obj);
-
-/*!
- * \since 12
- * \brief Create a \ref ast_multi_channel_blob suitable for a \ref stasis_message
- *
- * \note Similar to a \ref ast_channel_blob, the \ref ast_multi_channel_blob requires
- * a \a blob JSON object containing a \c "type" field describing the blob. It
- * should also be treated as immutable and not modified after it is put into the
- * message.
- *
- * \param blob The JSON blob that defines the type of this \ref ast_multi_channel_blob
+	struct stasis_message_type *type, struct ast_json *blob);
+
+/*!
+ * \since 12
+ * \brief Create a \ref ast_multi_channel_blob suitable for a \ref stasis_message.
+ *
+ * The given \a blob should be treated as immutable and not modified after it is
+ * put into the message.
+ *
+ * \param blob The JSON blob that defines the data of this \ref ast_multi_channel_blob
  *
  * \return \ref ast_multi_channel_blob object
  * \return \c NULL on error
@@ -189,8 +171,7 @@
  * \retval NULL on error or not found for the role specified
  */
 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(
-					       struct ast_multi_channel_blob *obj,
-					       const char *role);
+	struct ast_multi_channel_blob *obj, const char *role);
 
 /*!
  * \since 12
@@ -212,8 +193,7 @@
  * \retval NULL on error or not found for the role specified
  */
 struct ao2_container *ast_multi_channel_blob_get_channels(
-					       struct ast_multi_channel_blob *obj,
-					       const char *role);
+	struct ast_multi_channel_blob *obj, const char *role);
 
 /*!
  * \since 12
@@ -225,17 +205,6 @@
  * \return \c NULL on error.
  */
 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj);
-
-/*!
- * \since 12
- * \brief Extracts the type field from a \ref ast_multi_channel_blob.
- * Returned \c char* is still owned by \a obj
- *
- * \param obj Channel blob object.
- * \return Type field value from the blob.
- * \return \c NULL on error.
- */
-const char *ast_multi_channel_blob_get_type(struct ast_multi_channel_blob *obj);
 
 /*!
  * \since 12
@@ -250,8 +219,7 @@
  * \ref ast_multi_channel_blob object
  */
 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj,
-					       const char *role,
-					       struct ast_channel_snapshot *snapshot);
+	const char *role, struct ast_channel_snapshot *snapshot);
 
 /*!
  * \since 12
@@ -271,6 +239,46 @@
  * \retval A stasis message type
  */
 struct stasis_message_type *ast_channel_dial_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for when a variable is set on a channel.
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_varset_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for when a custom user event is sent on a channel.
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_user_event_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for when a hangup is requested on a channel.
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_hangup_request_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for when DTMF begins on a channel.
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_dtmf_begin_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for when DTMF ends on a channel.
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_dtmf_end_type(void);
 
 /*!
  * \since 12

Modified: team/kmoore/stasis-bridging-channel_events/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/main/channel.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/main/channel.c (original)
+++ team/kmoore/stasis-bridging-channel_events/main/channel.c Wed May  8 15:12:05 2013
@@ -1369,11 +1369,12 @@
 }
 
 /*! \internal \brief Publish a channel blob message */
-static void publish_channel_blob(struct ast_channel *chan, struct ast_json *blob)
+static void publish_channel_blob(struct ast_channel *chan,
+	struct stasis_message_type *type, struct ast_json *blob)
 {
 	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
 	if (blob) {
-		message = ast_channel_blob_create(chan, blob);
+		message = ast_channel_blob_create(chan, type, blob);
 	}
 	if (message) {
 		stasis_publish(ast_channel_topic(chan), message);
@@ -1383,7 +1384,6 @@
 /*! \brief Queue a hangup frame for channel */
 int ast_queue_hangup(struct ast_channel *chan)
 {
-	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
 	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
 	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
 	int res;
@@ -1391,8 +1391,7 @@
 	/* Yeah, let's not change a lock-critical value without locking */
 	ast_channel_lock(chan);
 	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
-	blob = ast_json_pack("{s: s}", "type", "hangup_request");
-	publish_channel_blob(chan, blob);
+	publish_channel_blob(chan, ast_channel_hangup_request_type(), NULL);
 
 	res = ast_queue_frame(chan, &f);
 	ast_channel_unlock(chan);
@@ -1417,10 +1416,9 @@
 	if (cause < 0) {
 		f.data.uint32 = ast_channel_hangupcause(chan);
 	}
-	blob = ast_json_pack("{s: s, s: i}",
-			     "type", "hangup_request",
+	blob = ast_json_pack("{s: i}",
 			     "cause", cause);
-	publish_channel_blob(chan, blob);
+	publish_channel_blob(chan, ast_channel_hangup_request_type(), blob);
 
 	res = ast_queue_frame(chan, &f);
 	ast_channel_unlock(chan);
@@ -2729,11 +2727,10 @@
 
 	ast_channel_lock(chan);
 	res = ast_softhangup_nolock(chan, cause);
-	blob = ast_json_pack("{s: s, s: i, s: b}",
-			     "type", "hangup_request",
+	blob = ast_json_pack("{s: i, s: b}",
 			     "cause", cause,
 			     "soft", 1);
-	publish_channel_blob(chan, blob);
+	publish_channel_blob(chan, ast_channel_hangup_request_type(), blob);
 	ast_channel_unlock(chan);
 
 	return res;
@@ -3740,15 +3737,14 @@
 	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
 	char digit_str[] = { digit, '\0' };
 
-	blob = ast_json_pack("{ s: s, s: s, s: s }",
-		"type", "dtmf_begin",
+	blob = ast_json_pack("{ s: s, s: s }",
 		"digit", digit_str,
 		"direction", dtmf_direction_to_string(direction));
 	if (!blob) {
 		return;
 	}
 
-	publish_channel_blob(chan, blob);
+	publish_channel_blob(chan, ast_channel_dtmf_begin_type(), blob);
 }
 
 static void send_dtmf_end_event(struct ast_channel *chan,
@@ -3757,8 +3753,7 @@
 	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
 	char digit_str[] = { digit, '\0' };
 
-	blob = ast_json_pack("{ s: s, s: s, s: s, s: i }",
-		"type", "dtmf_end",
+	blob = ast_json_pack("{ s: s, s: s, s: i }",
 		"digit", digit_str,
 		"direction", dtmf_direction_to_string(direction),
 		"duration_ms", duration_ms);
@@ -3766,7 +3761,7 @@
 		return;
 	}
 
-	publish_channel_blob(chan, blob);
+	publish_channel_blob(chan, ast_channel_dtmf_end_type(), blob);
 }
 
 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)

Modified: team/kmoore/stasis-bridging-channel_events/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/main/manager_channels.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/main/manager_channels.c (original)
+++ team/kmoore/stasis-bridging-channel_events/main/manager_channels.c Wed May  8 15:12:05 2013
@@ -434,8 +434,10 @@
 	}
 }
 
-static void channel_varset(struct ast_channel_blob *obj)
-{
+static void channel_varset_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
 	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"));
@@ -518,8 +520,10 @@
 	return 0;
 }
 
-static void channel_userevent(struct ast_channel_blob *obj)
-{
+static void channel_user_event_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
 	RAII_VAR(struct ast_str *, body, NULL, ast_free);
 	const char *eventname;
@@ -553,8 +557,11 @@
 		      ast_str_buffer(channel_event_string), eventname, ast_str_buffer(body));
 }
 
-static void channel_hangup_request(struct ast_channel_blob *obj)
-{
+static void channel_hangup_request_cb(void *data,
+	struct stasis_subscription *sub, struct stasis_topic *topic,
+	struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
 	RAII_VAR(struct ast_str *, extra, NULL, ast_free);
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
 	struct ast_json *cause;
@@ -590,8 +597,10 @@
 		      ast_str_buffer(extra));
 }
 
-static void channel_dtmf_begin(struct ast_channel_blob *obj)
-{
+static void channel_dtmf_begin_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
 	const char *digit =
 		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
@@ -629,8 +638,10 @@
 		digit, direction);
 }
 
-static void channel_dtmf_end(struct ast_channel_blob *obj)
-{
+static void channel_dtmf_end_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
 	const char *digit =
 		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
@@ -675,33 +686,10 @@
 }
 
 /*!
- * \brief Callback processing messages on the channel topic.
- */
-static void channel_blob_cb(void *data, struct stasis_subscription *sub,
-			    struct stasis_topic *topic,
-			    struct stasis_message *message)
-{
-	struct ast_channel_blob *obj = stasis_message_data(message);
-
-	if (strcmp("varset", ast_channel_blob_json_type(obj)) == 0) {
-		channel_varset(obj);
-	} else if (strcmp("userevent", ast_channel_blob_json_type(obj)) == 0) {
-		channel_userevent(obj);
-	} else if (strcmp("hangup_request", ast_channel_blob_json_type(obj)) == 0) {
-		channel_hangup_request(obj);
-	} else if (strcmp("dtmf_begin", ast_channel_blob_json_type(obj)) == 0) {
-		channel_dtmf_begin(obj);
-	} else if (strcmp("dtmf_end", ast_channel_blob_json_type(obj)) == 0) {
-		channel_dtmf_end(obj);
-	}
-}
-
-/*!
  * \brief Callback processing messages for channel dialing
  */
 static void channel_dial_cb(void *data, struct stasis_subscription *sub,
-				struct stasis_topic *topic,
-				struct stasis_message *message)
+	struct stasis_topic *topic, struct stasis_message *message)
 {
 	struct ast_multi_channel_blob *obj = stasis_message_data(message);
 	const char *dialstatus;
@@ -710,11 +698,6 @@
 	struct ast_channel_snapshot *peer;
 	RAII_VAR(struct ast_str *, caller_event_string, NULL, ast_free);
 	RAII_VAR(struct ast_str *, peer_event_string, NULL, ast_free);
-
-	if (strcmp("dial", ast_multi_channel_blob_get_type(obj))) {
-		ast_assert(0);
-		return;
-	}
 
 	caller = ast_multi_channel_blob_get_channel(obj, "caller");
 	peer = ast_multi_channel_blob_get_channel(obj, "peer");
@@ -785,8 +768,28 @@
 					 NULL);
 
 	ret |= stasis_message_router_add(channel_state_router,
-					 ast_channel_blob_type(),
-					 channel_blob_cb,
+					 ast_channel_varset_type(),
+					 channel_varset_cb,
+					 NULL);
+
+	ret |= stasis_message_router_add(channel_state_router,
+					 ast_channel_user_event_type(),
+					 channel_user_event_cb,
+					 NULL);
+
+	ret |= stasis_message_router_add(channel_state_router,
+					 ast_channel_dtmf_begin_type(),
+					 channel_dtmf_begin_cb,
+					 NULL);
+
+	ret |= stasis_message_router_add(channel_state_router,
+					 ast_channel_dtmf_end_type(),
+					 channel_dtmf_end_cb,
+					 NULL);
+
+	ret |= stasis_message_router_add(channel_state_router,
+					 ast_channel_hangup_request_type(),
+					 channel_hangup_request_cb,
 					 NULL);
 
 	ret |= stasis_message_router_add(channel_state_router,

Modified: team/kmoore/stasis-bridging-channel_events/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/main/stasis_channels.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/main/stasis_channels.c (original)
+++ team/kmoore/stasis-bridging-channel_events/main/stasis_channels.c Wed May  8 15:12:05 2013
@@ -38,35 +38,23 @@
 
 #define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7
 
-/*! \brief Message type for channel snapshot messages */
-static struct stasis_message_type *channel_snapshot_type;
-
-/*! \brief Message type for channel blob messages */
-static struct stasis_message_type *channel_blob_type;
-
-/*! \brief Message type for channel dial messages */
-static struct stasis_message_type *channel_dial_type;
+/*!
+ * @{ \brief Define channel message types.
+ */
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_snapshot_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_dial_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type);
+/*! @} */
 
 /*! \brief Topic for all channels */
 struct stasis_topic *channel_topic_all;
 
 /*! \brief Caching topic for all channels */
 struct stasis_caching_topic *channel_topic_all_cached;
-
-struct stasis_message_type *ast_channel_dial_type(void)
-{
-	return channel_dial_type;
-}
-
-struct stasis_message_type *ast_channel_blob_type(void)
-{
-	return channel_blob_type;
-}
-
-struct stasis_message_type *ast_channel_snapshot_type(void)
-{
-	return channel_snapshot_type;
-}
 
 struct stasis_topic *ast_channel_topic_all(void)
 {
@@ -221,18 +209,13 @@
 }
 
 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
-					       struct ast_json *blob)
+	struct stasis_message_type *type, struct ast_json *blob)
 {
 	RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-	struct ast_json *type;
-
-	ast_assert(blob != NULL);
-
-	type = ast_json_object_get(blob, "type");
-	if (type == NULL) {
-		ast_log(LOG_ERROR, "Invalid ast_channel_blob; missing type field\n");
-		return NULL;
+
+	if (blob == NULL) {
+		blob = ast_json_null();
 	}
 
 	obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
@@ -249,22 +232,13 @@
 
 	obj->blob = ast_json_ref(blob);
 
-	msg = stasis_message_create(ast_channel_blob_type(), obj);
+	msg = stasis_message_create(type, obj);
 	if (!msg) {
 		return NULL;
 	}
 
 	ao2_ref(msg, +1);
 	return msg;
-}
-
-const char *ast_channel_blob_json_type(struct ast_channel_blob *obj)
-{
-	if (obj == NULL) {
-		return NULL;
-	}
-
-	return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
 }
 
 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
@@ -319,17 +293,10 @@
 	RAII_VAR(struct ast_multi_channel_blob *, obj,
 			ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
 			ao2_cleanup);
-	struct ast_json *type;
 
 	ast_assert(blob != NULL);
 
 	if (!obj) {
-		return NULL;
-	}
-
-	type = ast_json_object_get(blob, "type");
-	if (type == NULL) {
-		ast_log(LOG_ERROR, "Invalid ast_multi_channel_blob; missing type field\n");
 		return NULL;
 	}
 
@@ -423,15 +390,6 @@
 	return obj->blob;
 }
 
-const char *ast_multi_channel_blob_get_type(struct ast_multi_channel_blob *obj)
-{
-	if (!obj) {
-		return NULL;
-	}
-
-	return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
-}
-
 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
 {
 	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
@@ -449,7 +407,8 @@
 		return;
 	}
 
-	msg = ast_channel_blob_create(chan, ast_json_ref(blob));
+	msg = ast_channel_blob_create(chan, ast_channel_varset_type(),
+		ast_json_ref(blob));
 
 	if (!msg) {
 		return;
@@ -522,12 +481,13 @@
 
 void ast_stasis_channels_shutdown(void)
 {
-	ao2_cleanup(channel_snapshot_type);
-	channel_snapshot_type = NULL;
-	ao2_cleanup(channel_blob_type);
-	channel_blob_type = NULL;
-	ao2_cleanup(channel_dial_type);
-	channel_dial_type = NULL;
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
 	ao2_cleanup(channel_topic_all);
 	channel_topic_all = NULL;
 	channel_topic_all_cached = stasis_caching_unsubscribe(channel_topic_all_cached);
@@ -535,9 +495,14 @@
 
 void ast_stasis_channels_init(void)
 {
-	channel_snapshot_type = stasis_message_type_create("ast_channel_snapshot");
-	channel_blob_type = stasis_message_type_create("ast_channel_blob");
-	channel_dial_type = stasis_message_type_create("ast_channel_dial");
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
+	STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
+
 	channel_topic_all = stasis_topic_create("ast_channel_topic_all");
 	channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
 }

Modified: team/kmoore/stasis-bridging-channel_events/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/res/res_stasis.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/res/res_stasis.c (original)
+++ team/kmoore/stasis-bridging-channel_events/res/res_stasis.c Wed May  8 15:12:05 2013
@@ -78,9 +78,6 @@
 
 struct ao2_container *__app_controls;
 
-/*! \brief Container for handlers for channel blob messages */
-struct ao2_container *channel_blob_handlers;
-
 /*! \brief Message router for the channel caching topic */
 struct stasis_message_router *channel_router;
 
@@ -371,20 +368,6 @@
 /*! \brief Typedef for blob handler callbacks */
 typedef struct ast_json *(*channel_blob_handler_cb)(struct ast_channel_blob *);
 
-/*! \brief AO2 refcounted object linking channel blob json type to its handler callback */
-struct blob_handler {
-	char *type;
-	channel_blob_handler_cb handler;
-};
-
-static void blob_handler_dtor(void *obj)
-{
-	struct blob_handler *handler = obj;
-
-	ast_free(handler->type);
-	handler->type = NULL;
-}
-
 static int OK = 0;
 static int FAIL = -1;
 
@@ -653,27 +636,10 @@
 	ao2_callback(apps, OBJ_NODATA, app_send_cb, msg);
 }
 
-static struct ast_json *handle_channel_blob(struct ast_channel_blob *obj)
-{
-	RAII_VAR(struct blob_handler *, handler, NULL, ao2_cleanup);
-	const char *handler_type = ast_channel_blob_json_type(obj);
-
-	handler = ao2_find(channel_blob_handlers, handler_type, OBJ_KEY);
-	if (!handler) {
-		return NULL;
-	}
-
-	return handler->handler(obj);
-}
-
-static void sub_blob_handler(void *data,
-		struct stasis_subscription *sub,
-		struct stasis_topic *topic,
-		struct stasis_message *message)
+static void generic_blob_handler(struct ast_channel_blob *obj, channel_blob_handler_cb handler_cb)
 {
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
-	struct ast_channel_blob *obj = stasis_message_data(message);
 
 	if (!obj->snapshot) {
 		return;
@@ -684,7 +650,7 @@
 		return;
 	}
 
-	msg = handle_channel_blob(obj);
+	msg = handler_cb(obj);
 	if (!msg) {
 		return;
 	}
@@ -900,7 +866,6 @@
 	}
 }
 
-/* To simplify events, we'll only generate on DTMF end (dtmf_end type) */
 static struct ast_json *handle_blob_dtmf(struct ast_channel_blob *obj)
 {
 	RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
@@ -925,49 +890,56 @@
 	return stasis_json_event_channel_dtmf_received_create(obj->snapshot, extra);
 }
 
+/* To simplify events, we'll only generate on DTMF end (dtmf_end type) */
+static void sub_dtmf_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
+	generic_blob_handler(obj, handle_blob_dtmf);
+}
+
 static struct ast_json *handle_blob_userevent(struct ast_channel_blob *obj)
 {
 	return stasis_json_event_channel_userevent_create(obj->snapshot, obj->blob);
 }
 
+static void sub_userevent_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
+	generic_blob_handler(obj, handle_blob_userevent);
+}
+
 static struct ast_json *handle_blob_hangup_request(struct ast_channel_blob *obj)
 {
 	return stasis_json_event_channel_hangup_request_create(obj->snapshot, obj->blob);
 }
 
+static void sub_hangup_request_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
+	generic_blob_handler(obj, handle_blob_hangup_request);
+}
+
 static struct ast_json *handle_blob_varset(struct ast_channel_blob *obj)
 {
 	return stasis_json_event_channel_varset_create(obj->snapshot, obj->blob);
 }
 
-static void register_blob_handler(const char *blob_type, channel_blob_handler_cb blob_type_handler_cb)
-{
-	RAII_VAR(struct blob_handler *, handler, ao2_alloc(sizeof(*handler), blob_handler_dtor), ao2_cleanup);
-
-	if (!handler) {
-		return;
-	}
-
-	handler->handler = blob_type_handler_cb;
-	handler->type = ast_strdup(blob_type);
-	if (!handler->type) {
-		return;
-	}
-
-	ao2_link(channel_blob_handlers, handler);
-}
-
-static int blob_handler_hash(const void *obj, const int flags)
-{
-	const char *handler_type = (flags & OBJ_KEY) ? obj : ((struct blob_handler*) obj)->type;
-	return ast_str_case_hash(handler_type);
-}
-
-static int blob_handler_cmp(void *obj, void *arg, int flags)
-{
-	struct blob_handler *opt1 = obj, *opt2 = arg;
-	const char *handler_type = (flags & OBJ_KEY) ? arg : opt2->type;
-	return strcasecmp(opt1->type, handler_type) ? 0 : CMP_MATCH | CMP_STOP;
+static void sub_varset_handler(void *data,
+		struct stasis_subscription *sub,
+		struct stasis_topic *topic,
+		struct stasis_message *message)
+{
+	struct ast_channel_blob *obj = stasis_message_data(message);
+	generic_blob_handler(obj, handle_blob_varset);
 }
 
 static int load_module(void)
@@ -986,23 +958,16 @@
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
-	channel_blob_handlers = ao2_container_alloc(BLOB_HANDLER_BUCKETS, blob_handler_hash, blob_handler_cmp);
-	if (!channel_blob_handlers) {
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
-	register_blob_handler("userevent", handle_blob_userevent);
-	register_blob_handler("hangup_request", handle_blob_hangup_request);
-	register_blob_handler("varset", handle_blob_varset);
-	register_blob_handler("dtmf_end", handle_blob_dtmf);
-
 	channel_router = stasis_message_router_create(stasis_caching_get_topic(ast_channel_topic_all_cached()));
 	if (!channel_router) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
 	r |= stasis_message_router_add(channel_router, stasis_cache_update_type(), sub_snapshot_handler, NULL);
-	r |= stasis_message_router_add(channel_router, ast_channel_blob_type(), sub_blob_handler, NULL);
+	r |= stasis_message_router_add(channel_router, ast_channel_user_event_type(), sub_userevent_handler, NULL);
+	r |= stasis_message_router_add(channel_router, ast_channel_varset_type(), sub_varset_handler, NULL);
+	r |= stasis_message_router_add(channel_router, ast_channel_dtmf_begin_type(), sub_dtmf_handler, NULL);
+	r |= stasis_message_router_add(channel_router, ast_channel_hangup_request_type(), sub_hangup_request_handler, NULL);
 	if (r) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
@@ -1016,9 +981,6 @@
 
 	stasis_message_router_unsubscribe(channel_router);
 	channel_router = NULL;
-
-	ao2_cleanup(channel_blob_handlers);
-	channel_blob_handlers = NULL;
 
 	ao2_cleanup(__apps_registry);
 	__apps_registry = NULL;

Modified: team/kmoore/stasis-bridging-channel_events/tests/test_stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/tests/test_stasis_channels.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/tests/test_stasis_channels.c (original)
+++ team/kmoore/stasis-bridging-channel_events/tests/test_stasis_channels.c Wed May  8 15:12:05 2013
@@ -54,6 +54,7 @@
 AST_TEST_DEFINE(channel_blob_create)
 {
 	struct ast_channel_blob *blob;
+	RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
 	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
@@ -70,36 +71,71 @@
 		break;
 	}
 
+	type = stasis_message_type_create("test-type");
 	chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
 	json = ast_json_pack("{s: s}",
-		     "type", "test");
-	bad_json = ast_json_pack("{s: s}",
-		     "bad_key", "test");
+		     "foo", "bar");
 
 	/* Off nominal creation */
-	ast_test_validate(test, NULL == ast_channel_blob_create(NULL, bad_json));
-	ast_test_validate(test, NULL == ast_channel_blob_create(chan, bad_json));
+	ast_test_validate(test, NULL == ast_channel_blob_create(chan, NULL, json));
 
 	/* Test for single channel */
-	msg = ast_channel_blob_create(chan, json);
+	msg = ast_channel_blob_create(chan, type, json);
 	ast_test_validate(test, NULL != msg);
 	blob = stasis_message_data(msg);
 	ast_test_validate(test, NULL != blob);
 	ast_test_validate(test, NULL != blob->snapshot);
 	ast_test_validate(test, NULL != blob->blob);
-	ast_test_validate(test, 0 == strcmp(ast_channel_blob_json_type(blob), "test"));
+	ast_test_validate(test, type == stasis_message_type(msg));
 
 	ast_test_validate(test, 1 == ao2_ref(msg, 0));
 	ao2_cleanup(msg);
 
 	/* Test for global channels */
-	msg = ast_channel_blob_create(NULL, json);
+	msg = ast_channel_blob_create(NULL, type, json);
 	ast_test_validate(test, NULL != msg);
 	blob = stasis_message_data(msg);
 	ast_test_validate(test, NULL != blob);
 	ast_test_validate(test, NULL == blob->snapshot);
 	ast_test_validate(test, NULL != blob->blob);
-	ast_test_validate(test, 0 == strcmp(ast_channel_blob_json_type(blob), "test"));
+	ast_test_validate(test, type == stasis_message_type(msg));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(null_blob)
+{
+	struct ast_channel_blob *blob;
+	RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = test_category;
+		info->summary = "Test creation of ast_channel_blob objects";
+		info->description = "Test creation of ast_channel_blob objects";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	type = stasis_message_type_create("test-type");
+	chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
+	json = ast_json_pack("{s: s}",
+		     "foo", "bar");
+
+	/* Test for single channel */
+	msg = ast_channel_blob_create(chan, type, NULL);
+	ast_test_validate(test, NULL != msg);
+	blob = stasis_message_data(msg);
+	ast_test_validate(test, NULL != blob);
+	ast_test_validate(test, NULL != blob->snapshot);
+	ast_test_validate(test, ast_json_null() == blob->blob);
+	ast_test_validate(test, type == stasis_message_type(msg));
 
 	return AST_TEST_PASS;
 }
@@ -122,17 +158,11 @@
 	}
 
 	json = ast_json_pack("{s: s}",
-		     "type", "test");
-	bad_json = ast_json_pack("{s: s}",
-		     "bad_key", "test");
-
-	/* Off nominal creation */
-	ast_test_validate(test, NULL == ast_multi_channel_blob_create(bad_json));
+		     "foo", "bar");
 
 	/* Test for single channel */
 	blob = ast_multi_channel_blob_create(json);
 	ast_test_validate(test, NULL != blob);
-	ast_test_validate(test, 0 == strcmp(ast_multi_channel_blob_get_type(blob), "test"));
 	ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
 
 	return AST_TEST_PASS;
@@ -266,6 +296,7 @@
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(channel_blob_create);
+	AST_TEST_UNREGISTER(null_blob);
 	AST_TEST_UNREGISTER(multi_channel_blob_create);
 	AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
 	AST_TEST_UNREGISTER(channel_snapshot_json);
@@ -276,6 +307,7 @@
 static int load_module(void)
 {
 	AST_TEST_REGISTER(channel_blob_create);
+	AST_TEST_REGISTER(null_blob);
 	AST_TEST_REGISTER(multi_channel_blob_create);
 	AST_TEST_REGISTER(multi_channel_blob_snapshots);
 	AST_TEST_REGISTER(channel_snapshot_json);

Modified: team/kmoore/stasis-bridging-channel_events/tests/test_stasis_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-bridging-channel_events/tests/test_stasis_endpoints.c?view=diff&rev=388038&r1=388037&r2=388038
==============================================================================
--- team/kmoore/stasis-bridging-channel_events/tests/test_stasis_endpoints.c (original)
+++ team/kmoore/stasis-bridging-channel_events/tests/test_stasis_endpoints.c Wed May  8 15:12:05 2013
@@ -112,6 +112,7 @@
 	ast_endpoint_set_state(uut, AST_ENDPOINT_OFFLINE);
 	actual_count = stasis_message_sink_wait_for_count(sink, 1,
 		STASIS_SINK_DEFAULT_WAIT);

[... 13 lines stripped ...]



More information about the asterisk-commits mailing list