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
This commit is contained in:
Joshua Colp
2016-05-05 11:14:00 -03:00
parent 94cd351ec4
commit d03e170ae7
5 changed files with 76 additions and 92 deletions

View File

@@ -21,6 +21,9 @@
#include "asterisk/pbx.h" #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 * \brief structure used for presence XML bodies
* *
@@ -51,6 +54,8 @@ struct ast_sip_exten_state_data {
char remote[PJSIP_MAX_URL_SIZE]; char remote[PJSIP_MAX_URL_SIZE];
/*! Optional subscription */ /*! Optional subscription */
struct ast_sip_subscription *sub; struct ast_sip_subscription *sub;
/*! A datastores container to persist datastores */
struct ao2_container *datastores;
/*! Allocation pool */ /*! Allocation pool */
pj_pool_t *pool; pj_pool_t *pool;
}; };

View 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); 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 * \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); 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 * \brief Register a subscription handler
* *

View 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"); 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) { if (datastore) {
return 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) { if (!datastore) {
return NULL; return NULL;
} }
datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state)); 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); ao2_ref(datastore, -1);
return NULL; return NULL;
} }
@@ -82,9 +82,9 @@ static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip
return datastore; 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; struct dialog_info_xml_state *state;
if (!datastore) { if (!datastore) {
@@ -108,11 +108,11 @@ static int dialog_info_generate_body_content(void *body, void *data)
unsigned int version; unsigned int version;
char version_str[32], sanitized[PJSIP_MAX_URL_SIZE]; char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
if (!local || !state_data->sub) { if (!local || !state_data->datastores) {
return -1; 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"); ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
return -1; return -1;
} }

View File

@@ -96,6 +96,8 @@ struct exten_state_publisher {
regex_t exten_regex; regex_t exten_regex;
/*! Publish client to use for sending publish messages */ /*! Publish client to use for sending publish messages */
struct ast_sip_outbound_publish_client *client; struct ast_sip_outbound_publish_client *client;
/*! Datastores container to hold persistent information */
struct ao2_container *datastores;
/*! Whether context filtering is active */ /*! Whether context filtering is active */
unsigned int context_filter; unsigned int context_filter;
/*! Whether extension filtering is active */ /*! 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.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.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.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) || if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
(info->exten_state == AST_EXTENSION_REMOVED)) { (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.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, ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
task_data->terminate); 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, ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
sizeof(exten_state_data->remote)); sizeof(exten_state_data->remote));
exten_state_data->sub = sip_sub; 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( exten_state_data->exten_state = ast_extension_state_extended(
NULL, exten_state_sub->context, exten_state_sub->exten, 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)); 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, res = ast_sip_pubsub_generate_body_content(publisher->body_type,
publisher->body_subtype, &gen_data, &body_text); publisher->body_subtype, &gen_data, &body_text);
pj_pool_reset(pool); pj_pool_reset(pool);
@@ -801,6 +808,7 @@ static void exten_state_publisher_destroy(void *obj)
} }
ao2_cleanup(publisher->client); ao2_cleanup(publisher->client);
ao2_cleanup(publisher->datastores);
} }
static int build_regex(regex_t *regex, const char *text) 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->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); publisher->client = ao2_bump(client);
ao2_lock(publishers); ao2_lock(publishers);

View 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) static void add_subscription(struct sip_subscription_tree *obj)
{ {
SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); 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 */ strcpy(sub->resource, resource); /* Safe */
sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp); sub->datastores = ast_datastores_alloc();
if (!sub->datastores) { if (!sub->datastores) {
destroy_subscription(sub); destroy_subscription(sub);
return NULL; 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; 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) 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); return ast_datastores_alloc_datastore(info, uid);
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;
} }
int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore) int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
{ {
ast_assert(datastore != NULL); return ast_datastores_add(subscription->datastores, datastore);
ast_assert(datastore->info != NULL);
ast_assert(!ast_strlen_zero(datastore->uid));
if (!ao2_link(subscription->datastores, datastore)) {
return -1;
}
return 0;
} }
struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name) 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) 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) int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
{ {
ast_assert(datastore != NULL); return ast_datastores_add(publication->datastores, datastore);
ast_assert(datastore->info != NULL);
ast_assert(!ast_strlen_zero(datastore->uid));
if (!ao2_link(publication->datastores, datastore)) {
return -1;
}
return 0;
} }
struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name) 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) 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); 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; return NULL;
} }
if (!(publication->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp))) { if (!(publication->datastores = ast_datastores_alloc())) {
ao2_ref(publication, -1); ao2_ref(publication, -1);
return NULL; return NULL;
} }