[asterisk-commits] dlee: branch dlee/stasis-http r382819 - /team/dlee/stasis-http/res/stasis_http/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 11 16:52:20 CDT 2013


Author: dlee
Date: Mon Mar 11 16:52:16 2013
New Revision: 382819

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382819
Log:
Implemented GET /channels and /channels/{channel_id}

Modified:
    team/dlee/stasis-http/res/stasis_http/resource_channels.c

Modified: team/dlee/stasis-http/res/stasis_http/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/stasis_http/resource_channels.c?view=diff&rev=382819&r1=382818&r2=382819
==============================================================================
--- team/dlee/stasis-http/res/stasis_http/resource_channels.c (original)
+++ team/dlee/stasis-http/res/stasis_http/resource_channels.c Mon Mar 11 16:52:16 2013
@@ -35,10 +35,37 @@
 #include "asterisk/channel.h"
 #include "resource_channels.h"
 
+static void fill_error(struct stasis_http_response *response, int response_code,
+		       const char *response_text, const char *message)
+{
+	response->message = ast_json_pack("{s: s}", "message", message);
+	if (response->message) {
+		response->response_code = response_code;
+		response->response_text = response_text;
+	} else {
+		/* Allocation failed. Error sending the error; great */
+		response->response_code = 500;
+		response->response_text = "Internal Server Error";
+		response->message = ast_json_pack("{s: s}", "message", "Allocation failed");
+	}
+}
+
+static void fill_ok(struct stasis_http_response *response, struct ast_json *message)
+{
+	if (!message) {
+		fill_error(response, 500, "Internal Server Error", "Allocation failed");
+	}
+
+	response->response_code = 200;
+	response->response_text = "OK";
+	response->message = message;
+}
+
 void stasis_http_dial(struct ast_variable *headers, struct ast_dial_args *args, struct stasis_http_response *response)
 {
 	ast_log(LOG_ERROR, "TODO: stasis_http_dial\n");
 }
+
 void stasis_http_continue_in_dialplan(struct ast_variable *headers, struct ast_continue_in_dialplan_args *args, struct stasis_http_response *response)
 {
 	RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
@@ -48,27 +75,21 @@
 
 	chan = ast_channel_get_by_name(args->channel_id);
 	if (chan == NULL) {
-		response->response_code = 404;
-		response->response_text = "Not Found";
-		response->message = ast_json_pack("{s: s}", "message", "Channel not found");
+		fill_error(response, 404, "Not Found", "Channel not found");
 		return;
 	}
 
 	control = stasis_app_control_find_by_channel(chan);
 	if (control == NULL) {
-		response->response_code = 409;
-		response->response_text = "Conflict";
-		response->message = ast_json_pack("{s: s}", "message", "Channel not in Stasis application");
+		fill_error(response, 409, "Conflict", "Channel not in Stasis application");
 		return;
 	}
 
 	stasis_app_control_continue(control);
 
-	response->response_code = 200;
-	response->response_text = "OK";
-	response->message = ast_json_pack("{s: s}", "message", "Returned to dialplan");
-	ast_assert(response->message != NULL);
+	fill_ok(response, ast_json_pack("{s: s}", "message", "Returned to dialplan"));
 }
+
 void stasis_http_reject_channel(struct ast_variable *headers, struct ast_reject_channel_args *args, struct stasis_http_response *response)
 {
 	ast_log(LOG_ERROR, "TODO: stasis_http_reject_channel\n");
@@ -91,16 +112,75 @@
 }
 void stasis_http_get_channel(struct ast_variable *headers, struct ast_get_channel_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_channel\n");
+	RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+ 	struct ast_channel_snapshot *snapshot;
+
+	caching_topic = ast_channel_topic_all_cached();
+	if (!caching_topic) {
+		fill_error(response, 500, "Internal Server Error", "Message bus not initialized");
+		return;
+	}
+	ao2_ref(caching_topic, +1);
+
+	msg = stasis_cache_get(caching_topic, ast_channel_snapshot(), args->channel_id);
+	if (!msg) {
+		fill_error(response, 404, "Not Found", "Channel not found");
+		return;
+	}
+
+	snapshot = stasis_message_data(msg);
+	ast_assert(snapshot != NULL);
+
+	fill_ok(response, ast_channel_snapshot_to_json(snapshot));
 }
+
 void stasis_http_delete_channel(struct ast_variable *headers, struct ast_delete_channel_args *args, struct stasis_http_response *response)
 {
 	ast_log(LOG_ERROR, "TODO: stasis_http_delete_channel\n");
 }
+
 void stasis_http_get_channels(struct ast_variable *headers, struct ast_get_channels_args *args, struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_channels\n");
+	RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
+	struct ao2_iterator i;
+	void *obj;
+
+	caching_topic = ast_channel_topic_all_cached();
+	if (!caching_topic) {
+		fill_error(response, 500, "Internal Server Error", "Message bus not initialized");
+		return;
+	}
+	ao2_ref(caching_topic, +1);
+
+	snapshots = stasis_cache_dump(caching_topic, ast_channel_snapshot());
+	if (!snapshots) {
+		fill_error(response, 500, "Internal Server Error", "Allocation failed");
+		return;
+	}
+
+	message = ast_json_array_create();
+	if (!message) {
+		fill_error(response, 500, "Internal Server Error", "Allocation failed");
+		return;
+	}
+
+	i = ao2_iterator_init(snapshots, 0);
+	while ((obj = ao2_iterator_next(&i))) {
+		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
+		struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
+		if (ast_json_array_append(message, ast_channel_snapshot_to_json(snapshot))) {
+			fill_error(response, 500, "Internal Server Error", "Allocation failed");
+			return;
+		}
+	}
+	ao2_iterator_destroy(&i);
+
+	fill_ok(response, ast_json_ref(message));
 }
+
 void stasis_http_originate(struct ast_variable *headers, struct ast_originate_args *args, struct stasis_http_response *response)
 {
 	if (args->endpoint) {




More information about the asterisk-commits mailing list