[asterisk-commits] dlee: branch dlee/endpoints r386611 - in /team/dlee/endpoints: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 26 09:20:16 CDT 2013


Author: dlee
Date: Fri Apr 26 09:20:11 2013
New Revision: 386611

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=386611
Log:
GETs work. Surprisingly

Modified:
    team/dlee/endpoints/include/asterisk/stasis_endpoints.h
    team/dlee/endpoints/main/stasis_endpoints.c
    team/dlee/endpoints/res/stasis_http/resource_endpoints.c

Modified: team/dlee/endpoints/include/asterisk/stasis_endpoints.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/include/asterisk/stasis_endpoints.h?view=diff&rev=386611&r1=386610&r2=386611
==============================================================================
--- team/dlee/endpoints/include/asterisk/stasis_endpoints.h (original)
+++ team/dlee/endpoints/include/asterisk/stasis_endpoints.h Fri Apr 26 09:20:11 2013
@@ -121,13 +121,14 @@
  * name.
  *
  * \param tech Name of the endpoint's technology.
- * \param name Name of the endpoint.
+ * \param resource Resource name of the endpoint.
  * \return Snapshot of the endpoint with the given name.
  * \return \c NULL if endpoint is not found, or on error.
  * \since 12
  */
 struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech,
-	const char *name);
+	const char *resource
+);
 
 /*! @} */
 

Modified: team/dlee/endpoints/main/stasis_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/main/stasis_endpoints.c?view=diff&rev=386611&r1=386610&r2=386611
==============================================================================
--- team/dlee/endpoints/main/stasis_endpoints.c (original)
+++ team/dlee/endpoints/main/stasis_endpoints.c Fri Apr 26 09:20:11 2013
@@ -59,8 +59,26 @@
 struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech,
 	const char *name)
 {
-	ast_assert(0); // TODO
-	return NULL;
+	RAII_VAR(char *, id, NULL, ast_free);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_endpoint_snapshot *snapshot;
+
+	ast_asprintf(&id, "%s/%s", tech, name);
+	if (!id) {
+		return NULL;
+	}
+
+	msg = stasis_cache_get(ast_endpoint_topic_all_cached(),
+		ast_endpoint_snapshot_type(), id);
+	if (!msg) {
+		return NULL;
+	}
+
+	snapshot = stasis_message_data(msg);
+	ast_assert(snapshot != NULL);
+
+	ao2_ref(snapshot, +1);
+	return snapshot;
 }
 
 /*!
@@ -127,7 +145,8 @@
 	ast_assert(channel_array != NULL);
 	for (i = 0; i < snapshot->current_channels; ++i) {
 		v = ast_json_array_append(channel_array,
-			ast_json_string_create(snapshot->channel_ids[i]));
+			ast_json_stringf("channel:%s",
+				snapshot->channel_ids[i]));
 		if (v != 0) {
 			return NULL;
 		}

Modified: team/dlee/endpoints/res/stasis_http/resource_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/res/stasis_http/resource_endpoints.c?view=diff&rev=386611&r1=386610&r2=386611
==============================================================================
--- team/dlee/endpoints/res/stasis_http/resource_endpoints.c (original)
+++ team/dlee/endpoints/res/stasis_http/resource_endpoints.c Fri Apr 26 09:20:11 2013
@@ -83,11 +83,75 @@
 	struct ast_get_endpoints_by_tech_args *args,
 	struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_endpoints_by_tech\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 *, json, NULL, ast_json_unref);
+	struct ao2_iterator i;
+	void *obj;
+
+	// TODO - if tech isn't a recognized type of endpoint, it should 404
+
+	caching_topic = ast_endpoint_topic_all_cached();
+	if (!caching_topic) {
+		stasis_http_response_error(
+			response, 500, "Internal Server Error",
+			"Message bus not initialized");
+		return;
+	}
+	ao2_ref(caching_topic, +1);
+
+	snapshots = stasis_cache_dump(caching_topic, ast_endpoint_snapshot_type());
+	if (!snapshots) {
+		stasis_http_response_alloc_failed(response);
+		return;
+	}
+
+	json = ast_json_array_create();
+	if (!json) {
+		stasis_http_response_alloc_failed(response);
+		return;
+	}
+
+	i = ao2_iterator_init(snapshots, 0);
+	while ((obj = ao2_iterator_next(&i))) {
+		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
+		struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
+		int r;
+
+		if (strcmp(args->tech, snapshot->tech) != 0) {
+			continue;
+		}
+
+		r = ast_json_array_append(
+			json, ast_endpoint_snapshot_to_json(snapshot));
+		if (r != 0) {
+			stasis_http_response_alloc_failed(response);
+			return;
+		}
+	}
+	ao2_iterator_destroy(&i);
+
+	stasis_http_response_ok(response, ast_json_ref(json));
 }
 void stasis_http_get_endpoint(struct ast_variable *headers,
 	struct ast_get_endpoint_args *args,
 	struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_endpoint\n");
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
+	if (!snapshot) {
+		stasis_http_response_error(response, 404, "Not Found",
+			"Endpoint not found");
+		return;
+	}
+
+	json = ast_endpoint_snapshot_to_json(snapshot);
+	if (!json) {
+		stasis_http_response_alloc_failed(response);
+		return;
+	}
+
+	stasis_http_response_ok(response, ast_json_ref(json));
 }




More information about the asterisk-commits mailing list