[asterisk-commits] dlee: branch dlee/stasis-http r382895 - /team/dlee/stasis-http/apps/app_stasis.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 12 13:00:11 CDT 2013


Author: dlee
Date: Tue Mar 12 13:00:07 2013
New Revision: 382895

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382895
Log:
less wonky

Modified:
    team/dlee/stasis-http/apps/app_stasis.c

Modified: team/dlee/stasis-http/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/app_stasis.c?view=diff&rev=382895&r1=382894&r2=382895
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c (original)
+++ team/dlee/stasis-http/apps/app_stasis.c Tue Mar 12 13:00:07 2013
@@ -60,6 +60,9 @@
 	</application>
  ***/
 
+/*! Time to wait for a frame in the application */
+#define MAX_WAIT_MS 200
+
 /*! \brief Maximum number of arguments for the Stasis dialplan application */
 #define MAX_ARGS 128
 
@@ -230,7 +233,7 @@
 	 */
 	int continue_to_dialplan:1;
 	/*! Uniqueid of the associated channel */
-	char channel_uniqueid[];
+	char channel_id[];
 };
 
 static struct stasis_app_control *control_create(const char *uniqueid)
@@ -246,9 +249,44 @@
 
 	control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
 
-	strncpy(control->channel_uniqueid, uniqueid, size - sizeof(*control));
+	strncpy(control->channel_id, uniqueid, size - sizeof(*control));
 
 	return control;
+}
+
+static void *exec_command(struct stasis_app_control *control,
+			  struct stasis_app_command *command)
+{
+        ao2_lock(control);
+	ao2_ref(command, +1);
+	ao2_link(control->command_queue, command);
+        ao2_unlock(control);
+
+	return wait_for_command(command);
+}
+
+/*! AO2 hash function for \ref stasis_app_control */
+static int control_hash(const void *obj, const int flags)
+{
+	const struct stasis_app_control *control = obj;
+	const char *id = flags & OBJ_KEY ? obj : control->channel_id;
+
+	return ast_str_hash(id);
+}
+
+/*! AO2 comparison function for \ref stasis_app_control */
+static int control_compare(void *lhs, void *rhs, int flags)
+{
+	const struct stasis_app_control *lhs_control = lhs;
+	const struct stasis_app_control *rhs_control = rhs;
+	const char *rhs_name =
+		flags & OBJ_KEY ? rhs : rhs_control->channel_id;
+
+	if (strcmp(lhs_control->channel_id, rhs_name) == 0) {
+		return CMP_MATCH | CMP_STOP;
+	} else {
+		return 0;
+	}
 }
 
 struct stasis_app_control *stasis_app_control_find_by_channel(
@@ -298,8 +336,10 @@
 static int OK = 0;
 static int FAIL = -1;
 
-static void *__app_control_answer(struct stasis_app_control *control, struct ast_channel *chan, void *data)
-{
+static void *__app_control_answer(struct stasis_app_control *control,
+				  struct ast_channel *chan, void *data)
+{
+	ast_debug(3, "%s: Answering", control->channel_id);
 	return __ast_answer(chan, 0, 1) == 0 ? &OK : &FAIL;
 }
 
@@ -308,10 +348,10 @@
 	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
 	int *retval;
 
-	SCOPED_AO2LOCK(lock, control);
+	ast_debug(3, "%s: Sending answer command\n", control->channel_id);
 
 	command = command_create(__app_control_answer, NULL);
-	retval = wait_for_command(command);
+	retval = exec_command(control, command);
 
 	if (*retval != 0) {
 		ast_log(LOG_WARNING, "Failed to answer channel");
@@ -340,15 +380,21 @@
 		return NULL;
 	}
 
-	r = ast_json_object_set(message, "event", ast_json_string_create(event_name));
+	r = ast_json_object_set(message,
+				"event", ast_json_string_create(event_name));
 	if (r != 0) {
 		ast_log(LOG_ERROR, "Failed to add event_name to message\n");
 		return NULL;
 	}
 
 	if (channel_info) {
-		if (ast_json_object_set(message, "channel_info", ast_channel_snapshot_to_json(channel_info)) != 0) {
-			ast_log(LOG_ERROR, "Failed to add channel info to event\n");
+		r = ast_json_object_set(
+			message,
+			"channel_info",
+			ast_channel_snapshot_to_json(channel_info));
+		if (r != 0) {
+			ast_log(LOG_ERROR,
+				"Failed to add channel info to event\n");
 			return NULL;
 		}
 	}
@@ -530,21 +576,46 @@
 		return res;
 	}
 
-	while (!hungup && !control_continue_test_and_reset(control) && ast_waitfor(chan, -1) > -1) {
+	while (1) {
 		RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
+		int r;
+
+		if (hungup) {
+			ast_debug(3, "%s: Hangup\n",
+				  ast_channel_uniqueid(chan));
+			break;
+		}
+
+		if (control_continue_test_and_reset(control)) {
+			ast_debug(3, "%s: Continue\n",
+				  ast_channel_uniqueid(chan));
+			break;
+		}
+
+		r = ast_waitfor(chan, MAX_WAIT_MS);
+
+		if (r < 0) {
+			ast_debug(3, "%s: Poll error\n",
+				  ast_channel_uniqueid(chan));
+			break;
+		}
 
 		dispatch_commands(control, chan);
+
+		if (r == 0) {
+			continue;
+		}
 
 		f = ast_read(chan);
 		if (!f) {
-			ast_debug(3, "%s: No more frames. Must be done, I guess.\n", ast_channel_uniqueid(chan));
+			ast_debug(3, "%s: No more frames. Must be done, I guess.\n",
+				  ast_channel_uniqueid(chan));
 			break;
 		}
 
 		switch (f->frametype) {
 		case AST_FRAME_CONTROL:
 			if (f->subclass.integer == AST_CONTROL_HANGUP) {
-				ast_debug(3, "%s: Received hangup\n", ast_channel_uniqueid(chan));
 				hungup = 1;
 			}
 			break;
@@ -628,12 +699,14 @@
 {
 	int r = 0;
 
-	__apps_registry = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
+	__apps_registry =
+		ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
 	if (__apps_registry == NULL) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
-	__app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS, app_hash, app_compare);
+	__app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS,
+					     control_hash, control_compare);
 	if (__app_controls == NULL) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
@@ -656,7 +729,8 @@
 	return r;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis dialplan application",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS,
+		"Stasis dialplan application",
 		.load = load_module,
 		.unload = unload_module,
 		.nonoptreq = "res_json");




More information about the asterisk-commits mailing list