]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_pjsip_pubsub: Use common datastores container API. 78/2778/2
authorJoshua Colp <jcolp@digium.com>
Thu, 5 May 2016 14:14:00 +0000 (11:14 -0300)
committerJoshua Colp <jcolp@digium.com>
Mon, 9 May 2016 13:40:36 +0000 (10:40 -0300)
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

include/asterisk/res_pjsip_body_generator_types.h
include/asterisk/res_pjsip_pubsub.h
res/res_pjsip_dialog_info_body_generator.c
res/res_pjsip_exten_state.c
res/res_pjsip_pubsub.c

index aab14723860c4906fc92d67c8105fe7519c27b6e..f61534b1075ac735625b49cfb1cb4449fb5e5f6b 100644 (file)
@@ -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 @@ struct ast_sip_exten_state_data {
        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;
 };
index 4f9a7633afb916bfed872e7de0b09ea234baeadc..cb51db31ffe16c45d07e629ab899b76d3844fb42 100644 (file)
@@ -165,6 +165,20 @@ struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publicati
  */
 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
  */
@@ -517,6 +531,20 @@ struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscrip
  */
 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
  *
index 86f3986544ba89226c4d2e87145e6b505ea6f13f..d21af2ae61c2b19b4df336e46cf78a8387a7e786 100644 (file)
@@ -61,20 +61,20 @@ static void *dialog_info_allocate_body(void *data)
        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 @@ static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip
        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 @@ static int dialog_info_generate_body_content(void *body, void *data)
        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;
        }
index 76a88a29cf7e99c7d03b2a3a6aa807fa00e3af67..22fb69c29a58b492507a87d78af425cd0ebcab5e 100644 (file)
@@ -96,6 +96,8 @@ struct exten_state_publisher {
        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 @@ static struct notify_task_data *alloc_notify_task_data(const char *exten,
        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 @@ static int notify_task(void *obj)
        }
 
        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 @@ static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_su
        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 @@ static int exten_state_publisher_cb(void *data)
                }
                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 @@ static void exten_state_publisher_destroy(void *obj)
        }
 
        ao2_cleanup(publisher->client);
+       ao2_cleanup(publisher->datastores);
 }
 
 static int build_regex(regex_t *regex, const char *text)
@@ -895,6 +903,14 @@ static int publisher_start(struct ast_sip_outbound_publish *configuration, struc
                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);
index 755a154a5a49c058e586481e00ec8f7f13bb862c..10ffb19fcc155e6ae26877d3819f5734d8675c96 100644 (file)
@@ -1001,28 +1001,6 @@ static int build_resource_tree(struct ast_sip_endpoint *endpoint, const struct a
        }
 }
 
-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 @@ static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_s
        }
        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 @@ static int sip_subscription_accept(struct sip_subscription_tree *sub_tree, pjsip
        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);
 }
 
-int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
+struct ao2_container *ast_sip_subscription_get_datastores(const struct ast_sip_subscription *subscription)
 {
-       ast_assert(datastore != NULL);
-       ast_assert(datastore->info != NULL);
-       ast_assert(!ast_strlen_zero(datastore->uid));
+       return subscription->datastores;
+}
 
-       if (!ao2_link(publication->datastores, datastore)) {
-               return -1;
-       }
-       return 0;
+int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
+{
+       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 @@ static struct ast_sip_publication *sip_create_publication(struct ast_sip_endpoin
                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;
        }