[asterisk-commits] dlee: branch dlee/endpoints r386604 - in /team/dlee/endpoints: channels/ incl...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 25 23:09:42 CDT 2013


Author: dlee
Date: Thu Apr 25 23:09:38 2013
New Revision: 386604

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=386604
Log:
GET endpoints works. Surprisingly.

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

Modified: team/dlee/endpoints/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/channels/chan_sip.c?view=diff&rev=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/channels/chan_sip.c (original)
+++ team/dlee/endpoints/channels/chan_sip.c Thu Apr 25 23:09:38 2013
@@ -5259,8 +5259,6 @@
 {
 	ast_debug(3, "Destroying SIP peer %s\n", peer->name);
 
-	printf("%s(%s)\n", __func__, peer->name);
-
 	/*
 	 * Remove any mailbox event subscriptions for this peer before
 	 * we destroy anything.  An event subscription callback may be
@@ -30726,8 +30724,6 @@
 		if (!(peer->the_mark))
 			firstpass = 0;
 	} else {
-		printf("%s(%s)\n", __func__, name);
-
 		if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct"))) {
 			return NULL;
 		}

Modified: team/dlee/endpoints/include/asterisk/endpoints.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/include/asterisk/endpoints.h?view=diff&rev=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/include/asterisk/endpoints.h (original)
+++ team/dlee/endpoints/include/asterisk/endpoints.h Thu Apr 25 23:09:38 2013
@@ -34,11 +34,15 @@
  * \since 12
  */
 enum ast_endpoint_state {
+	/*! The endpoint state is not known. */
+	AST_ENDPOINT_UNKNOWN,
 	/*! The endpoint is not available. */
 	AST_ENDPOINT_OFFLINE,
 	/*! The endpoint is available. */
 	AST_ENDPOINT_ONLINE,
 };
+
+const char *ast_endpoint_state_to_string(enum ast_endpoint_state state);
 
 /*!
  * \brief Opaque struct representing an endpoint.
@@ -53,7 +57,7 @@
 /*!
  * \brief Create an endpoint struct.
  *
- * The endpoint is created with a state of OFFLINE and max_channels of -1
+ * The endpoint is created with a state of UNKNOWN and max_channels of -1
  * (unlimited). While \ref ast_endpoint is AO2 managed, you have to
  * shut it down with ast_endpoint_shutdown() to clean up references from
  * subscriptions.

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=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/include/asterisk/stasis_endpoints.h (original)
+++ team/dlee/endpoints/include/asterisk/stasis_endpoints.h Thu Apr 25 23:09:38 2013
@@ -132,6 +132,16 @@
 /*! @} */
 
 /*!
+ * \brief Build a JSON object from a \ref ast_endpoint_snapshot.
+ *
+ * \param snapshot Endpoint snapshot.
+ * \return JSON object representing endpoint snapshot.
+ * \return \c NULL on error
+ */
+struct ast_json *ast_endpoint_snapshot_to_json(
+	const struct ast_endpoint_snapshot *snapshot);
+
+/*!
  * \brief Initialization function for endpoint stasis support.
  *
  * \return 0 on success.

Modified: team/dlee/endpoints/main/endpoints.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/endpoints/main/endpoints.c?view=diff&rev=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/main/endpoints.c (original)
+++ team/dlee/endpoints/main/endpoints.c Thu Apr 25 23:09:38 2013
@@ -64,6 +64,19 @@
 	struct ao2_container *channel_ids;
 };
 
+const char *ast_endpoint_state_to_string(enum ast_endpoint_state state)
+{
+	switch (state) {
+	case AST_ENDPOINT_UNKNOWN:
+		return "unknown";
+	case AST_ENDPOINT_OFFLINE:
+		return "offline";
+	case AST_ENDPOINT_ONLINE:
+		return "online";
+	}
+	return "?";
+}
+
 static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
 {
 	RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
@@ -105,11 +118,15 @@
 {
 	struct ast_endpoint *endpoint = data;
 	struct ast_channel_snapshot *snapshot = stasis_message_data(message);
-
-	ast_assert(endpoint != NULL);
+	RAII_VAR(char *, existing_id, NULL, ao2_cleanup);
+
+	ast_assert(endpoint != NULL);
+	ast_assert(snapshot != NULL);
 
 	ao2_lock(endpoint);
-	if (!ao2_find(endpoint->channel_ids, snapshot->uniqueid, OBJ_NODATA)) {
+	existing_id = ao2_find(endpoint->channel_ids, snapshot->uniqueid,
+		OBJ_POINTER);
+	if (!existing_id) {
 		ast_str_container_add(endpoint->channel_ids,
 			snapshot->uniqueid);
 	}
@@ -125,10 +142,10 @@
 	struct stasis_cache_clear *clear = stasis_message_data(message);
 
 	ast_assert(endpoint != NULL);
+	ast_assert(clear != NULL);
 
 	ao2_lock(endpoint);
-	ao2_find(endpoint->channel_ids, clear->id,
-		OBJ_NODATA | OBJ_UNLINK);
+	ao2_find(endpoint->channel_ids, clear->id, OBJ_POINTER | OBJ_NODATA | OBJ_UNLINK);
 	ao2_unlock(endpoint);
 	endpoint_publish_snapshot(endpoint);
 }
@@ -160,7 +177,7 @@
 	}
 
 	endpoint->max_channels = -1;
-	endpoint->state = AST_ENDPOINT_OFFLINE;
+	endpoint->state = AST_ENDPOINT_UNKNOWN;
 
 	if (ast_string_field_init(endpoint, 80) != 0) {
 		return NULL;
@@ -271,6 +288,8 @@
 {
 	RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
 	int channel_count;
+	struct ao2_iterator i;
+	void *obj;
 	SCOPED_AO2LOCK(lock, endpoint);
 
 	channel_count = ao2_container_count(endpoint->channel_ids);
@@ -291,6 +310,13 @@
 	snapshot->state = endpoint->state;
 	snapshot->max_channels = endpoint->max_channels;
 
+	i = ao2_iterator_init(endpoint->channel_ids, 0);
+	while ((obj = ao2_iterator_next(&i))) {
+		RAII_VAR(char *, channel_id, obj, ao2_cleanup);
+		snapshot->channel_ids[snapshot->current_channels++] =
+			channel_id;
+	}
+
 	ao2_ref(snapshot, +1);
 	return snapshot;
 }

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=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/main/stasis_endpoints.c (original)
+++ team/dlee/endpoints/main/stasis_endpoints.c Thu Apr 25 23:09:38 2013
@@ -97,6 +97,45 @@
 	endpoint_topic_all_cached = NULL;
 }
 
+struct ast_json *ast_endpoint_snapshot_to_json(
+	const struct ast_endpoint_snapshot *snapshot)
+{
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *channel_array;
+	int v;
+	int i;
+
+	json = ast_json_pack("{s: s, s: s, s: s, s: []}",
+		"technology", snapshot->tech,
+		"resource", snapshot->resource,
+		"state", ast_endpoint_state_to_string(snapshot->state),
+		"channels");
+
+	if (json == NULL) {
+		return NULL;
+	}
+
+	if (snapshot->max_channels != -1) {
+		v = ast_json_object_set(json, "max_channels",
+			ast_json_integer_create(snapshot->max_channels));
+		if (v != 0) {
+			return NULL;
+		}
+	}
+
+	channel_array = ast_json_object_get(json, "channels");
+	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]));
+		if (v != 0) {
+			return NULL;
+		}
+	}
+
+	return ast_json_ref(json);
+}
+
 int ast_endpoint_stasis_init(void)
 {
 	ast_register_atexit(endpoints_stasis_shutdown);

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=386604&r1=386603&r2=386604
==============================================================================
--- team/dlee/endpoints/res/stasis_http/resource_endpoints.c (original)
+++ team/dlee/endpoints/res/stasis_http/resource_endpoints.c Thu Apr 25 23:09:38 2013
@@ -29,15 +29,65 @@
 
 #include "resource_endpoints.h"
 
-void stasis_http_get_endpoints(struct ast_variable *headers, struct ast_get_endpoints_args *args, struct stasis_http_response *response)
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis.h"
+#include "asterisk/stasis_endpoints.h"
+
+void stasis_http_get_endpoints(struct ast_variable *headers,
+	struct ast_get_endpoints_args *args,
+	struct stasis_http_response *response)
 {
-	ast_log(LOG_ERROR, "TODO: stasis_http_get_endpoints\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;
+
+	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 = 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_endpoints_by_tech(struct ast_variable *headers, struct ast_get_endpoints_by_tech_args *args, struct stasis_http_response *response)
+void stasis_http_get_endpoints_by_tech(struct ast_variable *headers,
+	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");
 }
-void stasis_http_get_endpoint(struct ast_variable *headers, struct ast_get_endpoint_args *args, struct stasis_http_response *response)
+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");
 }




More information about the asterisk-commits mailing list