[Asterisk-code-review] res pjsip pubsub: Use common datastores container API. (asterisk[master])

Joshua Colp asteriskteam at digium.com
Mon May 9 18:27:36 CDT 2016


Joshua Colp has submitted this change and it was merged.

Change subject: res_pjsip_pubsub: Use common datastores container API.
......................................................................


res_pjsip_pubsub: Use common datastores container API.

This migrates res_pjsip_pubsub over to using the newly
introduce common datastores management API instead of using
its own implementations for both subscriptions and
publications.

As well the extension state data now provides a generic
datastores container instead of a subscription. This allows
the dialog-info+xml body generator to work for both
subscriptions and publications.

ASTERISK-25999 #close

Change-Id: I773f9e4f35092da0f653566736a8647e8cfebef1
---
M include/asterisk/res_pjsip_body_generator_types.h
M include/asterisk/res_pjsip_pubsub.h
M res/res_pjsip_dialog_info_body_generator.c
M res/res_pjsip_exten_state.c
M res/res_pjsip_pubsub.c
5 files changed, 76 insertions(+), 92 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Mark Michelson: Looks good to me, approved
  George Joseph: Looks good to me, but someone else must approve
  Joshua Colp: Verified



diff --git a/include/asterisk/res_pjsip_body_generator_types.h b/include/asterisk/res_pjsip_body_generator_types.h
index aab1472..f61534b 100644
--- a/include/asterisk/res_pjsip_body_generator_types.h
+++ b/include/asterisk/res_pjsip_body_generator_types.h
@@ -21,6 +21,9 @@
 
 #include "asterisk/pbx.h"
 
+/*! \brief Forward declaration for ao2_container so full astobj2.h is not required */
+struct ao2_container;
+
 /*!
  * \brief structure used for presence XML bodies
  *
@@ -51,6 +54,8 @@
 	char remote[PJSIP_MAX_URL_SIZE];
 	/*! Optional subscription */
 	struct ast_sip_subscription *sub;
+	/*! A datastores container to persist datastores */
+	struct ao2_container *datastores;
 	/*! Allocation pool */
 	pj_pool_t *pool;
 };
diff --git a/include/asterisk/res_pjsip_pubsub.h b/include/asterisk/res_pjsip_pubsub.h
index 4f9a763..cb51db3 100644
--- a/include/asterisk/res_pjsip_pubsub.h
+++ b/include/asterisk/res_pjsip_pubsub.h
@@ -166,6 +166,20 @@
 void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name);
 
 /*!
+ * \brief Get the datastores container for a publication
+ *
+ * \param publication The publication to get the datastores container from
+ *
+ * \retval NULL datastores container not present
+ * \retval non-NULL datastores container
+ *
+ * \note The container is NOT returned with reference count bumped
+ *
+ * \since 14.0.0
+ */
+struct ao2_container *ast_sip_publication_get_datastores(const struct ast_sip_publication *publication);
+
+/*!
  * \brief Opaque structure representing an RFC 3265 SIP subscription
  */
 struct ast_sip_subscription;
@@ -518,6 +532,20 @@
 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name);
 
 /*!
+ * \brief Get the datastores container for a subscription
+ *
+ * \param subscription The subscription to get the datastores container from
+ *
+ * \retval NULL datastores container not present
+ * \retval non-NULL datastores container
+ *
+ * \note The container is NOT returned with reference count bumped
+ *
+ * \since 14.0.0
+ */
+struct ao2_container *ast_sip_subscription_get_datastores(const struct ast_sip_subscription *subscription);
+
+/*!
  * \brief Register a subscription handler
  *
  * \retval 0 Handler was registered successfully
diff --git a/res/res_pjsip_dialog_info_body_generator.c b/res/res_pjsip_dialog_info_body_generator.c
index 86f3986..d21af2a 100644
--- a/res/res_pjsip_dialog_info_body_generator.c
+++ b/res/res_pjsip_dialog_info_body_generator.c
@@ -61,20 +61,20 @@
 	return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
 }
 
-static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip_subscription *sub)
+static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ao2_container *datastores)
 {
-	struct ast_datastore *datastore = ast_sip_subscription_get_datastore(sub, "dialog-info+xml");
+	struct ast_datastore *datastore = ast_datastores_find(datastores, "dialog-info+xml");
 
 	if (datastore) {
 		return datastore;
 	}
 
-	datastore = ast_sip_subscription_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
+	datastore = ast_datastores_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
 	if (!datastore) {
 		return NULL;
 	}
 	datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
-	if (!datastore->data || ast_sip_subscription_add_datastore(sub, datastore)) {
+	if (!datastore->data || ast_datastores_add(datastores, datastore)) {
 		ao2_ref(datastore, -1);
 		return NULL;
 	}
@@ -82,9 +82,9 @@
 	return datastore;
 }
 
-static unsigned int dialog_info_xml_get_version(struct ast_sip_subscription *sub, unsigned int *version)
+static unsigned int dialog_info_xml_get_version(struct ao2_container *datastores, unsigned int *version)
 {
-	struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(sub);
+	struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(datastores);
 	struct dialog_info_xml_state *state;
 
 	if (!datastore) {
@@ -108,11 +108,11 @@
 	unsigned int version;
 	char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
 
-	if (!local || !state_data->sub) {
+	if (!local || !state_data->datastores) {
 		return -1;
 	}
 
-	if (dialog_info_xml_get_version(state_data->sub, &version)) {
+	if (dialog_info_xml_get_version(state_data->datastores, &version)) {
 		ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
 		return -1;
 	}
diff --git a/res/res_pjsip_exten_state.c b/res/res_pjsip_exten_state.c
index 76a88a2..22fb69c 100644
--- a/res/res_pjsip_exten_state.c
+++ b/res/res_pjsip_exten_state.c
@@ -96,6 +96,8 @@
 	regex_t exten_regex;
 	/*! Publish client to use for sending publish messages */
 	struct ast_sip_outbound_publish_client *client;
+	/*! Datastores container to hold persistent information */
+	struct ao2_container *datastores;
 	/*! Whether context filtering is active */
 	unsigned int context_filter;
 	/*! Whether extension filtering is active */
@@ -271,6 +273,7 @@
 	task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
 	task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
 	task_data->exten_state_data.sub = exten_state_sub->sip_sub;
+	task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(exten_state_sub->sip_sub);
 
 	if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
 	    (info->exten_state == AST_EXTENSION_REMOVED)) {
@@ -311,6 +314,7 @@
 	}
 
 	task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
+	task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(task_data->exten_state_sub->sip_sub);
 
 	ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
 			task_data->terminate);
@@ -498,6 +502,7 @@
 	ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
 			sizeof(exten_state_data->remote));
 	exten_state_data->sub = sip_sub;
+	exten_state_data->datastores = ast_sip_subscription_get_datastores(sip_sub);
 
 	exten_state_data->exten_state = ast_extension_state_extended(
 			NULL, exten_state_sub->context, exten_state_sub->exten,
@@ -658,6 +663,8 @@
 		}
 		ast_copy_string(pub_data->exten_state_data.remote, uri, sizeof(pub_data->exten_state_data.remote));
 
+		pub_data->exten_state_data.datastores = publisher->datastores;
+
 		res = ast_sip_pubsub_generate_body_content(publisher->body_type,
 			publisher->body_subtype, &gen_data, &body_text);
 		pj_pool_reset(pool);
@@ -801,6 +808,7 @@
 	}
 
 	ao2_cleanup(publisher->client);
+	ao2_cleanup(publisher->datastores);
 }
 
 static int build_regex(regex_t *regex, const char *text)
@@ -895,6 +903,14 @@
 		publisher->exten_filter = 1;
 	}
 
+	publisher->datastores = ast_datastores_alloc();
+	if (!publisher->datastores) {
+		ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not create datastores container\n",
+			name);
+		ao2_ref(publisher, -1);
+		return -1;
+	}
+
 	publisher->client = ao2_bump(client);
 
 	ao2_lock(publishers);
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index 755a154..10ffb19 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -1001,28 +1001,6 @@
 	}
 }
 
-static int datastore_hash(const void *obj, int flags)
-{
-	const struct ast_datastore *datastore = obj;
-	const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
-
-	ast_assert(uid != NULL);
-
-	return ast_str_hash(uid);
-}
-
-static int datastore_cmp(void *obj, void *arg, int flags)
-{
-	const struct ast_datastore *datastore1 = obj;
-	const struct ast_datastore *datastore2 = arg;
-	const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
-
-	ast_assert(datastore1->uid != NULL);
-	ast_assert(uid2 != NULL);
-
-	return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
-}
-
 static void add_subscription(struct sip_subscription_tree *obj)
 {
 	SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
@@ -1086,7 +1064,7 @@
 	}
 	strcpy(sub->resource, resource); /* Safe */
 
-	sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
+	sub->datastores = ast_datastores_alloc();
 	if (!sub->datastores) {
 		destroy_subscription(sub);
 		return NULL;
@@ -2323,92 +2301,49 @@
 	return pjsip_evsub_accept(sub_tree->evsub, rdata, response, &res_hdr) == PJ_SUCCESS ? 0 : -1;
 }
 
-static void subscription_datastore_destroy(void *obj)
-{
-	struct ast_datastore *datastore = obj;
-
-	/* Using the destroy function (if present) destroy the data */
-	if (datastore->info->destroy != NULL && datastore->data != NULL) {
-		datastore->info->destroy(datastore->data);
-		datastore->data = NULL;
-	}
-
-	ast_free((void *) datastore->uid);
-	datastore->uid = NULL;
-}
-
 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
 {
-	RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
-	char uuid_buf[AST_UUID_STR_LEN];
-	const char *uid_ptr = uid;
-
-	if (!info) {
-		return NULL;
-	}
-
-	datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
-	if (!datastore) {
-		return NULL;
-	}
-
-	datastore->info = info;
-	if (ast_strlen_zero(uid)) {
-		/* They didn't provide an ID so we'll provide one ourself */
-		uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
-	}
-
-	datastore->uid = ast_strdup(uid_ptr);
-	if (!datastore->uid) {
-		return NULL;
-	}
-
-	ao2_ref(datastore, +1);
-	return datastore;
+	return ast_datastores_alloc_datastore(info, uid);
 }
 
 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
 {
-	ast_assert(datastore != NULL);
-	ast_assert(datastore->info != NULL);
-	ast_assert(!ast_strlen_zero(datastore->uid));
-
-	if (!ao2_link(subscription->datastores, datastore)) {
-		return -1;
-	}
-	return 0;
+	return ast_datastores_add(subscription->datastores, datastore);
 }
 
 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
 {
-	return ao2_find(subscription->datastores, name, OBJ_KEY);
+	return ast_datastores_find(subscription->datastores, name);
 }
 
 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
 {
-	ao2_find(subscription->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
+	ast_datastores_remove(subscription->datastores, name);
+}
+
+struct ao2_container *ast_sip_subscription_get_datastores(const struct ast_sip_subscription *subscription)
+{
+	return subscription->datastores;
 }
 
 int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
 {
-	ast_assert(datastore != NULL);
-	ast_assert(datastore->info != NULL);
-	ast_assert(!ast_strlen_zero(datastore->uid));
-
-	if (!ao2_link(publication->datastores, datastore)) {
-		return -1;
-	}
-	return 0;
+	return ast_datastores_add(publication->datastores, datastore);
 }
 
 struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name)
 {
-	return ao2_find(publication->datastores, name, OBJ_KEY);
+	return ast_datastores_find(publication->datastores, name);
 }
 
 void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name)
 {
-	ao2_callback(publication->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
+	ast_datastores_remove(publication->datastores, name);
+}
+
+struct ao2_container *ast_sip_publication_get_datastores(const struct ast_sip_publication *publication)
+{
+	return publication->datastores;
 }
 
 AST_RWLIST_HEAD_STATIC(publish_handlers, ast_sip_publish_handler);
@@ -2830,7 +2765,7 @@
 		return NULL;
 	}
 
-	if (!(publication->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp))) {
+	if (!(publication->datastores = ast_datastores_alloc())) {
 		ao2_ref(publication, -1);
 		return NULL;
 	}

-- 
To view, visit https://gerrit.asterisk.org/2778
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I773f9e4f35092da0f653566736a8647e8cfebef1
Gerrit-PatchSet: 2
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>



More information about the asterisk-code-review mailing list