[asterisk-commits] mmichelson: branch 12 r407016 - in /branches/12: include/asterisk/ res/ res/r...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Jan 31 16:11:22 CST 2014
Author: mmichelson
Date: Fri Jan 31 16:11:18 2014
New Revision: 407016
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=407016
Log:
Decouple subscription handling from NOTIFY/PUBLISH body generation.
When the PJSIP pubsub framework was created, subscription handlers were required
to state what event they handled along with what body types they knew how to
generate. While this serves well when implementing a base RFC, it has problems
when trying to extend the body to support non-standard or proprietary body
elements. The code also was NOTIFY-specific, meaning that when the time comes
that we start writing code to send out PUBLISH requests with MWI or presence
bodies, we would likely find ourselves duplicating code that had previously been
written.
This changeset introduces the concept of body generators and body supplements. A
body generator is responsible for allocating a native structure for a given body
type, providing the primary body content, converting the native structure to a
string, and deallocating resources. A body supplement takes the primary body
content (the native structure, not a string) generated by the body generator and
adds nonstandard elements to the body. With these elements living in their own
module, it becomes easy to extend our support for body types and to re-use
resources when sending a PUBLISH request.
Body generators and body supplements register themselves with the pubsub core,
similar to how subscription and publish handlers had done. Now, subscription
handlers do not need to know what type of body content they generate, but they
still need to inform the pubsub core about what the default body type for a
given event package is. The pubsub core keeps track of what body generators and
body supplements have been registered. When a SUBSCRIBE arrives, the pubsub core
will check that there is a subscription handler for the event in the SUBSCRIBE,
then it will check that there is a body generator that can provide the content
specified in the Accept header(s).
Because of the nature of body generators and supplements, it means
res_pjsip_exten_state and res_pjsip_mwi have been completely gutted. They no
longer worry about body types, instead calling
ast_sip_pubsub_generate_body_content() when they need to generate a NOTIFY body.
Review: https://reviewboard.asterisk.org/r/3150
Added:
branches/12/include/asterisk/res_pjsip_body_generator_types.h
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_body_generator_types.h
branches/12/res/res_pjsip/presence_xml.c
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c
branches/12/res/res_pjsip_mwi_body_generator.c
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/res/res_pjsip_mwi_body_generator.c
branches/12/res/res_pjsip_pidf_body_generator.c
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c
branches/12/res/res_pjsip_pidf_eyebeam_body_supplement.c
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_eyebeam_body_supplement.c
branches/12/res/res_pjsip_xpidf_body_generator.c
- copied unchanged from r407013, team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c
Removed:
branches/12/include/asterisk/res_pjsip_exten_state.h
branches/12/res/res_pjsip_pidf.c
Modified:
branches/12/include/asterisk/res_pjsip_pubsub.h
branches/12/res/res_pjsip_exten_state.c
branches/12/res/res_pjsip_mwi.c
branches/12/res/res_pjsip_pubsub.c
branches/12/res/res_pjsip_pubsub.exports.in
Modified: branches/12/include/asterisk/res_pjsip_pubsub.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/res_pjsip_pubsub.h?view=diff&rev=407016&r1=407015&r2=407016
==============================================================================
--- branches/12/include/asterisk/res_pjsip_pubsub.h (original)
+++ branches/12/include/asterisk/res_pjsip_pubsub.h Fri Jan 31 16:11:18 2014
@@ -226,23 +226,22 @@
struct ast_sip_subscription_handler {
/*! The name of the event this handler deals with */
const char *event_name;
- /*! The types of body this handler accepts */
+ /*! The types of body this handler accepts.
+ *
+ * \note This option has no bearing when the handler is used in the
+ * notifier role. When in a subscriber role, this header is used to
+ * populate the Accept: header of an outbound SUBSCRIBE request
+ */
const char *accept[AST_SIP_MAX_ACCEPT];
/*!
- * \brief Indicates if this handler can be used as a default handler for an event type.
+ * \brief Default body type defined for the event package this handler handles.
*
* Typically, a SUBSCRIBE request will contain one or more Accept headers that tell
* what format they expect the body of NOTIFY requests to use. However, every event
* package is required to define a default body format type to be used if a SUBSCRIBE
* request for the event contains no Accept header.
- *
- * If this value is non-zero, then this handler provides the default body format for
- * the event package and can handle SUBSCRIBES with no Accept headers present.
- * If this value is zero, then this handler provides an alternative body format
- * from the default for the event package and cannot handle SUBSCRIBEs with no
- * Accept header.
- */
- unsigned int handles_default_accept;
+ */
+ const char *default_accept;
/*!
* \brief Called when a subscription is to be destroyed
*
@@ -537,4 +536,176 @@
*/
void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler);
+/*!
+ * \brief Pubsub body generator
+ *
+ * A body generator is responsible for taking Asterisk content
+ * and converting it into a body format to be placed in an outbound
+ * SIP NOTIFY or PUBLISH request.
+ */
+struct ast_sip_pubsub_body_generator {
+ /*!
+ * \brief Content type
+ * In "plain/text", "plain" is the type
+ */
+ const char *type;
+ /*!
+ * \brief Content subtype
+ * In "plain/text", "text" is the subtype
+ */
+ const char *subtype;
+ /*!
+ * \brief allocate body structure.
+ *
+ * Body generators will have this method called when a NOTIFY
+ * or PUBLISH body needs to be created. The type returned depends on
+ * the type of content being produced for the body. The data parameter
+ * is provided by the subscription handler and will vary between different
+ * event types.
+ *
+ * \param data The subscription data provided by the event handler
+ * \retval non-NULL The allocated body
+ * \retval NULL Failure
+ */
+ void *(*allocate_body)(void *data);
+ /*!
+ * \brief Add content to the body of a SIP request
+ *
+ * The body of the request has already been allocated by the body generator's
+ * allocate_body callback.
+ *
+ * \param body The body of the SIP request. The type is determined by the
+ * content type.
+ * \param data The subscription data used to populate the body. The type is
+ * determined by the content type.
+ */
+ int (*generate_body_content)(void *body, void *data);
+ /*!
+ * \brief Convert the body to a string.
+ *
+ * \param body The request body.
+ * \param str The converted string form of the request body
+ */
+ void (*to_string)(void *body, struct ast_str **str);
+ /*!
+ * \brief Deallocate resources created for the body
+ *
+ * Optional callback to destroy resources allocated for the
+ * message body.
+ *
+ * \param body Body to be destroyed
+ */
+ void (*destroy_body)(void *body);
+ AST_LIST_ENTRY(ast_sip_pubsub_body_generator) list;
+};
+
+/*!
+ * \brief Body supplement
+ *
+ * Body supplements provide additions to bodies not already
+ * provided by body generators. This may include proprietary
+ * extensions, optional content, or other nonstandard fare.
+ */
+struct ast_sip_pubsub_body_supplement {
+ /*!
+ * \brief Content type
+ * In "plain/text", "plain" is the type
+ */
+ const char *type;
+ /*!
+ * \brief Content subtype
+ * In "plain/text", "text" is the subtype
+ */
+ const char *subtype;
+ /*!
+ * \brief Add additional content to a SIP request body.
+ *
+ * A body generator will have already allocated a body and populated
+ * it with base data for the event. The supplement's duty is, if desired,
+ * to extend the body to have optional data beyond what a base RFC specifies.
+ *
+ * \param body The body of the SIP request. The type is determined by the
+ * body generator that allocated the body.
+ * \param data The subscription data used to populate the body. The type is
+ * determined by the content type.
+ */
+ int (*supplement_body)(void *body, void *data);
+ AST_LIST_ENTRY(ast_sip_pubsub_body_supplement) list;
+};
+
+/*!
+ * \since 13.0.0
+ * \brief Generate body content for a PUBLISH or NOTIFY
+ *
+ * This function takes a pre-allocated body and calls into registered body
+ * generators in order to fill in the body with appropriate details.
+ * The primary body generator will be called first, followed by the
+ * supplementary body generators
+ *
+ * \param content_type The content type of the body
+ * \param content_subtype The content subtype of the body
+ * \param data The data associated with body generation.
+ * \param[out] str The string representation of the generated body
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+int ast_sip_pubsub_generate_body_content(const char *content_type,
+ const char *content_subtype, void *data, struct ast_str **str);
+
+/*!
+ * \since 13.0.0
+ * \brief Register a body generator with the pubsub core.
+ *
+ * This may fail if an attempt is made to register a primary body supplement
+ * for a given content type if a primary body supplement for that content type
+ * has already been registered.
+ *
+ * \param generator Body generator to register
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator);
+
+/*!
+ * \since 13.0.0
+ * \brief Unregister a body generator with the pubsub core.
+ *
+ * \param generator Body generator to unregister
+ */
+void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator);
+
+/*!
+ * \since 13.0.0
+ * \brief Register a body generator with the pubsub core.
+ *
+ * This may fail if an attempt is made to register a primary body supplement
+ * for a given content type if a primary body supplement for that content type
+ * has already been registered.
+ *
+ * \param generator Body generator to register
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
+
+/*!
+ * \since 13.0.0
+ * \brief Unregister a body generator with the pubsub core.
+ *
+ * \param generator Body generator to unregister
+ */
+void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement);
+
+/*!
+ * \since 13.0.0
+ * \brief Get the body type used for this subscription
+ */
+const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub);
+
+/*!
+ * \since 13.0.0
+ * \brief Get the body subtype used for this subscription
+ */
+const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub);
+
#endif /* RES_PJSIP_PUBSUB_H */
Modified: branches/12/res/res_pjsip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip_exten_state.c?view=diff&rev=407016&r1=407015&r2=407016
==============================================================================
--- branches/12/res/res_pjsip_exten_state.c (original)
+++ branches/12/res/res_pjsip_exten_state.c Fri Jan 31 16:11:18 2014
@@ -31,7 +31,7 @@
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_pubsub.h"
-#include "asterisk/res_pjsip_exten_state.h"
+#include "asterisk/res_pjsip_body_generator_types.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/astobj2.h"
@@ -41,43 +41,6 @@
#define BODY_SIZE 1024
#define EVENT_TYPE_SIZE 50
-AST_RWLIST_HEAD_STATIC(providers, ast_sip_exten_state_provider);
-
-/*!
- * \internal
- * \brief Find a provider based on the given accept body type.
- */
-static struct ast_sip_exten_state_provider *provider_by_type(const char *type)
-{
- struct ast_sip_exten_state_provider *i;
- SCOPED_LOCK(lock, &providers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
- AST_RWLIST_TRAVERSE_SAFE_BEGIN(&providers, i, next) {
- if (!strcmp(i->body_type, type)) {
- return i;
- }
- }
- AST_RWLIST_TRAVERSE_SAFE_END;
- return NULL;
-}
-
-/*!
- * \internal
- * \brief Find a provider based on the given accept body types.
- */
-static struct ast_sip_exten_state_provider *provider_by_types(const char *event_name,
- char **types, int count)
-{
- int i;
- struct ast_sip_exten_state_provider *res;
- for (i = 0; i < count; ++i) {
- if ((res = provider_by_type(types[i])) &&
- !strcmp(event_name, res->event_name)) {
- return res;
- }
- }
- return NULL;
-}
-
/*!
* \brief A subscription for extension state
*
@@ -90,12 +53,6 @@
int id;
/*! The SIP subscription */
struct ast_sip_subscription *sip_sub;
- /*! The name of the event the subscribed to */
- char event_name[EVENT_TYPE_SIZE];
- /*! The number of body types */
- int body_types_count;
- /*! The subscription body types */
- char **body_types;
/*! Context in which subscription looks for updates */
char context[AST_MAX_CONTEXT];
/*! Extension within the context to receive updates from */
@@ -104,40 +61,36 @@
enum ast_extension_states last_exten_state;
};
+#define DEFAULT_PRESENCE_BODY "application/pidf+xml"
+
+static void subscription_shutdown(struct ast_sip_subscription *sub);
+static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint,
+ pjsip_rx_data *rdata);
+static void resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
+ struct ast_sip_subscription_response_data *response_data);
+static void subscription_timeout(struct ast_sip_subscription *sub);
+static void subscription_terminated(struct ast_sip_subscription *sub,
+ pjsip_rx_data *rdata);
+static void to_ami(struct ast_sip_subscription *sub,
+ struct ast_str **buf);
+
+struct ast_sip_subscription_handler presence_handler = {
+ .event_name = "presence",
+ .accept = { DEFAULT_PRESENCE_BODY, },
+ .default_accept = DEFAULT_PRESENCE_BODY,
+ .subscription_shutdown = subscription_shutdown,
+ .new_subscribe = new_subscribe,
+ .resubscribe = resubscribe,
+ .subscription_timeout = subscription_timeout,
+ .subscription_terminated = subscription_terminated,
+ .to_ami = to_ami,
+};
+
static void exten_state_subscription_destructor(void *obj)
{
struct exten_state_subscription *sub = obj;
- int i;
-
- for (i = 0; i < sub->body_types_count; ++i) {
- ast_free(sub->body_types[i]);
- }
-
- ast_free(sub->body_types);
+
ao2_cleanup(sub->sip_sub);
-}
-
-/*!
- * \internal
- * \brief Copies the body types the message wishes to subscribe to.
- */
-static void copy_body_types(pjsip_rx_data *rdata,
- struct exten_state_subscription *exten_state_sub)
-{
- int i;
- pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)
- pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
-
- exten_state_sub->body_types_count = hdr->count;
- exten_state_sub->body_types = ast_malloc(hdr->count * sizeof(char*));
-
- for (i = 0; i < hdr->count; ++i) {
- exten_state_sub->body_types[i] =
- ast_malloc(hdr->values[i].slen * sizeof(char*) + 1);
-
- ast_copy_string(exten_state_sub->body_types[i],
- pj_strbuf(&hdr->values[i]), hdr->values[i].slen + 1);
- }
}
/*!
@@ -157,11 +110,6 @@
static struct exten_state_subscription *exten_state_subscription_alloc(
struct ast_sip_endpoint *endpoint, enum ast_sip_subscription_role role, pjsip_rx_data *rdata)
{
- static const pj_str_t event_name = { "Event", 5 };
- pjsip_event_hdr *hdr = (pjsip_event_hdr*)pjsip_msg_find_hdr_by_name(
- rdata->msg_info.msg, &event_name, NULL);
-
- struct ast_sip_exten_state_provider *provider;
RAII_VAR(struct exten_state_subscription *, exten_state_sub,
ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor), ao2_cleanup);
@@ -169,19 +117,8 @@
return NULL;
}
- ast_copy_pj_str(exten_state_sub->event_name, &hdr->event_type,
- sizeof(exten_state_sub->event_name));
-
- copy_body_types(rdata, exten_state_sub);
- if (!(provider = provider_by_types(exten_state_sub->event_name,
- exten_state_sub->body_types,
- exten_state_sub->body_types_count))) {
- ast_log(LOG_WARNING, "Unable to locate subscription handler\n");
- return NULL;
- }
-
if (!(exten_state_sub->sip_sub = ast_sip_create_subscription(
- provider->handler, role, endpoint, rdata))) {
+ &presence_handler, role, endpoint, rdata))) {
ast_log(LOG_WARNING, "Unable to create SIP subscription for endpoint %s\n",
ast_sorcery_object_get_id(endpoint));
return NULL;
@@ -204,27 +141,13 @@
pj_str_t reason_str;
const pj_str_t *reason_str_ptr = NULL;
pjsip_tx_data *tdata;
- pjsip_dialog *dlg;
- char local[PJSIP_MAX_URL_SIZE], remote[PJSIP_MAX_URL_SIZE];
struct ast_sip_body body;
- struct ast_sip_exten_state_provider *provider = provider_by_types(
- exten_state_sub->event_name, exten_state_sub->body_types,
- exten_state_sub->body_types_count);
-
- if (!provider) {
- ast_log(LOG_ERROR, "Unable to locate provider for subscription\n");
- return;
- }
-
- body.type = provider->type;
- body.subtype = provider->subtype;
-
- dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
- ast_copy_pj_str(local, &dlg->local.info_str, sizeof(local));
- ast_copy_pj_str(remote, &dlg->remote.info_str, sizeof(remote));
-
- if (provider->create_body(exten_state_data, local, remote, &body_text)) {
+ body.type = ast_sip_subscription_get_body_type(exten_state_sub->sip_sub);
+ body.subtype = ast_sip_subscription_get_body_subtype(exten_state_sub->sip_sub);
+
+ if (ast_sip_pubsub_generate_body_content(body.type, body.subtype,
+ exten_state_data, &body_text)) {
ast_log(LOG_ERROR, "Unable to create body on NOTIFY request\n");
return;
}
@@ -262,13 +185,19 @@
{
RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup);
char *subtype = NULL, *message = NULL;
-
+ pjsip_dialog *dlg;
struct ast_sip_exten_state_data exten_state_data = {
.exten = exten_state_sub->exten,
.presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
exten_state_sub->exten, &subtype, &message),
};
+ dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
+ ast_copy_pj_str(exten_state_data.local, &dlg->local.info_str,
+ sizeof(exten_state_data.local));
+ ast_copy_pj_str(exten_state_data.remote, &dlg->remote.info_str,
+ sizeof(exten_state_data.remote));
+
if ((exten_state_data.exten_state = ast_extension_state_extended(
NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) {
@@ -277,8 +206,12 @@
return;
}
+ exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+ "exten_state", 1024, 1024);
+
exten_state_data.device_state_info = info;
create_send_notify(exten_state_sub, reason, evsub_state, &exten_state_data);
+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data.pool);
}
struct notify_task_data {
@@ -300,6 +233,7 @@
{
struct notify_task_data *task_data =
ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
+ struct pjsip_dialog *dlg;
if (!task_data) {
ast_log(LOG_WARNING, "Unable to create notify task data\n");
@@ -319,6 +253,12 @@
if (task_data->exten_state_data.device_state_info) {
ao2_ref(task_data->exten_state_data.device_state_info, +1);
}
+
+ dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
+ ast_copy_pj_str(task_data->exten_state_data.local, &dlg->local.info_str,
+ sizeof(task_data->exten_state_data.local));
+ ast_copy_pj_str(task_data->exten_state_data.remote, &dlg->remote.info_str,
+ sizeof(task_data->exten_state_data.remote));
if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
(info->exten_state == AST_EXTENSION_REMOVED)) {
@@ -334,9 +274,16 @@
{
RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
+ /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
+ task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+ "exten_state", 1024, 1024);
+
create_send_notify(task_data->exten_state_sub, task_data->evsub_state ==
PJSIP_EVSUB_STATE_TERMINATED ? "noresource" : NULL,
task_data->evsub_state, &task_data->exten_state_data);
+
+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
+ task_data->exten_state_data.pool);
return 0;
}
@@ -527,107 +474,19 @@
exten_state_sub->last_exten_state));
}
-#define DEFAULT_PRESENCE_BODY "application/pidf+xml"
-
-/*!
- * \internal
- * \brief Create and register a subscription handler.
- *
- * Creates a subscription handler that can be registered with the pub/sub
- * framework for the given event_name and accept value.
- */
-static struct ast_sip_subscription_handler *create_and_register_handler(
- const char *event_name, const char *accept)
-{
- struct ast_sip_subscription_handler *handler =
- ao2_alloc(sizeof(*handler), NULL);
-
- if (!handler) {
- return NULL;
- }
-
- handler->event_name = event_name;
- handler->accept[0] = accept;
- if (!strcmp(accept, DEFAULT_PRESENCE_BODY)) {
- handler->handles_default_accept = 1;
- }
-
- handler->subscription_shutdown = subscription_shutdown;
- handler->new_subscribe = new_subscribe;
- handler->resubscribe = resubscribe;
- handler->subscription_timeout = subscription_timeout;
- handler->subscription_terminated = subscription_terminated;
- handler->to_ami = to_ami;
-
- if (ast_sip_register_subscription_handler(handler)) {
+static int load_module(void)
+{
+ if (ast_sip_register_subscription_handler(&presence_handler)) {
ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
- handler->event_name);
- ao2_cleanup(handler);
- return NULL;
- }
-
- return handler;
-}
-
-int ast_sip_register_exten_state_provider(struct ast_sip_exten_state_provider *obj)
-{
- if (ast_strlen_zero(obj->type)) {
- ast_log(LOG_WARNING, "Type not specified on provider for event %s\n",
- obj->event_name);
- return -1;
- }
-
- if (ast_strlen_zero(obj->subtype)) {
- ast_log(LOG_WARNING, "Subtype not specified on provider for event %s\n",
- obj->event_name);
- return -1;
- }
-
- if (!obj->create_body) {
- ast_log(LOG_WARNING, "Body handler not specified on provide for event %s\n",
- obj->event_name);
- return -1;
- }
-
- if (!(obj->handler = create_and_register_handler(obj->event_name, obj->body_type))) {
- ast_log(LOG_WARNING, "Handler could not be registered for provider event %s\n",
- obj->event_name);
- return -1;
- }
-
- /* scope to avoid mix declarations */
- {
- SCOPED_LOCK(lock, &providers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
- AST_RWLIST_INSERT_TAIL(&providers, obj, next);
- ast_module_ref(ast_module_info->self);
- }
-
- return 0;
-}
-
-void ast_sip_unregister_exten_state_provider(struct ast_sip_exten_state_provider *obj)
-{
- struct ast_sip_exten_state_provider *i;
- SCOPED_LOCK(lock, &providers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
- AST_RWLIST_TRAVERSE_SAFE_BEGIN(&providers, i, next) {
- if (i == obj) {
- ast_sip_unregister_subscription_handler(i->handler);
- ao2_cleanup(i->handler);
- AST_RWLIST_REMOVE_CURRENT(next);
- ast_module_unref(ast_module_info->self);
- break;
- }
- }
- AST_RWLIST_TRAVERSE_SAFE_END;
-}
-
-static int load_module(void)
-{
+ presence_handler.event_name);
+ return AST_MODULE_LOAD_DECLINE;
+ }
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
+ ast_sip_unregister_subscription_handler(&presence_handler);
return 0;
}
Modified: branches/12/res/res_pjsip_mwi.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip_mwi.c?view=diff&rev=407016&r1=407015&r2=407016
==============================================================================
--- branches/12/res/res_pjsip_mwi.c (original)
+++ branches/12/res/res_pjsip_mwi.c Fri Jan 31 16:11:18 2014
@@ -31,6 +31,7 @@
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_pubsub.h"
+#include "asterisk/res_pjsip_body_generator_types.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/astobj2.h"
@@ -43,6 +44,10 @@
#define STASIS_BUCKETS 13
#define MWI_BUCKETS 53
+
+#define MWI_TYPE "application"
+#define MWI_SUBTYPE "simple-message-summary"
+
static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
pjsip_rx_data *rdata);
@@ -58,8 +63,8 @@
static struct ast_sip_subscription_handler mwi_handler = {
.event_name = "message-summary",
- .accept = { "application/simple-message-summary", },
- .handles_default_accept = 1,
+ .accept = { MWI_TYPE"/"MWI_SUBTYPE, },
+ .default_accept = MWI_TYPE"/"MWI_SUBTYPE,
.subscription_shutdown = mwi_subscription_shutdown,
.new_subscribe = mwi_new_subscribe,
.resubscribe = mwi_resubscribe,
@@ -223,17 +228,11 @@
return strcmp(mwi_sub1->id, mwi_sub2->id) ? 0 : CMP_MATCH;
}
-struct message_accumulator {
- int old_msgs;
- int new_msgs;
- const char *reason;
-};
-
static int get_message_count(void *obj, void *arg, int flags)
{
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
struct mwi_stasis_subscription *mwi_stasis = obj;
- struct message_accumulator *counter = arg;
+ struct ast_sip_message_accumulator *counter = arg;
struct ast_mwi_state *mwi_state;
msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), mwi_stasis->mailbox);
@@ -366,11 +365,7 @@
static void send_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason)
{
const pj_str_t *reason_str_ptr = NULL;
- static pjsip_media_type mwi_type = {
- .type = { "application", 11 },
- .subtype = { "simple-message-summary", 22 },
- };
- struct message_accumulator counter = {
+ struct ast_sip_message_accumulator counter = {
.old_msgs = 0,
.new_msgs = 0,
};
@@ -378,6 +373,13 @@
pjsip_tx_data *tdata;
pj_str_t reason_str;
pj_str_t pj_body;
+ const char *type = sub->is_solicited ?
+ ast_sip_subscription_get_body_type(sub->sip_sub) :
+ MWI_TYPE;
+ const char *subtype = sub->is_solicited ?
+ ast_sip_subscription_get_body_subtype(sub->sip_sub) :
+ MWI_SUBTYPE;
+ pjsip_media_type mwi_type = { { 0,}, };
ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
@@ -385,11 +387,17 @@
pj_cstr(&reason_str, reason);
reason_str_ptr = &reason_str;
}
- ast_str_append(&body, 0, "Messages-Waiting: %s\r\n", counter.new_msgs ? "yes" : "no");
- ast_str_append(&body, 0, "Voice-Message: %d/%d (0/0)\r\n", counter.new_msgs, counter.old_msgs);
+
+ if (ast_sip_pubsub_generate_body_content(type, subtype, &counter, &body)) {
+ ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
+ return;
+ }
+
pj_cstr(&pj_body, ast_str_buffer(body));
-
- ast_debug(5, "Sending %s MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
+ pj_cstr(&mwi_type.type, type);
+ pj_cstr(&mwi_type.subtype, subtype);
+
+ ast_debug(5, "Sending %s MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
sub->is_solicited ? "solicited" : "unsolicited", sub->id, counter.new_msgs,
counter.old_msgs);
Modified: branches/12/res/res_pjsip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip_pubsub.c?view=diff&rev=407016&r1=407015&r2=407016
==============================================================================
--- branches/12/res/res_pjsip_pubsub.c (original)
+++ branches/12/res/res_pjsip_pubsub.c Fri Jan 31 16:11:18 2014
@@ -81,6 +81,8 @@
.on_rx_request = pubsub_on_rx_request,
};
+#define MOD_DATA_BODY_GENERATOR "sub_body_generator"
+
static const pj_str_t str_event_name = { "Event", 5 };
/*! \brief Scheduler used for automatically expiring publications */
@@ -195,6 +197,8 @@
pjsip_evsub *evsub;
/*! The underlying PJSIP dialog */
pjsip_dialog *dlg;
+ /*! Body generaator for NOTIFYs */
+ struct ast_sip_pubsub_body_generator *body_generator;
/*! Next item in the list */
AST_LIST_ENTRY(ast_sip_subscription) next;
};
@@ -205,6 +209,9 @@
};
AST_RWLIST_HEAD_STATIC(subscriptions, ast_sip_subscription);
+
+AST_RWLIST_HEAD_STATIC(body_generators, ast_sip_pubsub_body_generator);
+AST_RWLIST_HEAD_STATIC(body_supplements, ast_sip_pubsub_body_supplement);
static void add_subscription(struct ast_sip_subscription *obj)
{
@@ -402,6 +409,8 @@
ao2_ref(sub, -1);
return NULL;
}
+ sub->body_generator = ast_sip_mod_data_get(rdata->endpt_info.mod_data,
+ pubsub_module.id, MOD_DATA_BODY_GENERATOR);
sub->role = role;
if (role == AST_SIP_NOTIFIER) {
dlg = ast_sip_create_dialog_uas(endpoint, rdata);
@@ -631,31 +640,35 @@
ast_module_ref(ast_module_info->self);
}
-static int sub_handler_exists_for_event_name(const char *event_name)
+static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name)
{
struct ast_sip_subscription_handler *iter;
SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
if (!strcmp(iter->event_name, event_name)) {
- return 1;
- }
- }
- return 0;
+ break;
+ }
+ }
+ return iter;
}
int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
{
+ pj_str_t event;
pj_str_t accept[AST_SIP_MAX_ACCEPT];
+ struct ast_sip_subscription_handler *existing;
int i;
if (ast_strlen_zero(handler->event_name)) {
- ast_log(LOG_ERROR, "No event package specifief for subscription handler. Cannot register\n");
+ ast_log(LOG_ERROR, "No event package specified for subscription handler. Cannot register\n");
return -1;
}
- if (ast_strlen_zero(handler->accept[0])) {
- ast_log(LOG_ERROR, "Subscription handler must supply at least one 'Accept' format\n");
+ existing = find_sub_handler_for_event_name(handler->event_name);
+ if (existing) {
+ ast_log(LOG_ERROR, "Unable to register subscription handler for event %s."
+ "A handler is already registered\n", handler->event_name);
return -1;
}
@@ -663,19 +676,12 @@
pj_cstr(&accept[i], handler->accept[i]);
}
- if (!sub_handler_exists_for_event_name(handler->event_name)) {
- pj_str_t event;
-
- pj_cstr(&event, handler->event_name);
-
- if (!strcmp(handler->event_name, "message-summary")) {
- pjsip_mwi_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
- } else {
- pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept);
- }
+ pj_cstr(&event, handler->event_name);
+
+ if (!strcmp(handler->event_name, "message-summary")) {
+ pjsip_mwi_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
} else {
- pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module, PJSIP_H_ACCEPT, NULL,
- i, accept);
+ pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept);
}
sub_add_handler(handler);
@@ -696,48 +702,52 @@
AST_RWLIST_TRAVERSE_SAFE_END;
}
-static struct ast_sip_subscription_handler *find_sub_handler(const char *event, char accept[AST_SIP_MAX_ACCEPT][64], size_t num_accept)
-{
- struct ast_sip_subscription_handler *iter;
- int match = 0;
- SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
- AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
- int i;
- int j;
- if (strcmp(event, iter->event_name)) {
- ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
- continue;
- }
- ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
- if (!num_accept && iter->handles_default_accept) {
- /* The SUBSCRIBE contained no Accept headers, and this subscription handler
- * provides the default body type, so it's a match!
- */
+static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype(const char *content_type,
+ const char *content_subtype)
+{
+ struct ast_sip_pubsub_body_generator *iter;
+ SCOPED_LOCK(lock, &body_generators, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+
+ AST_LIST_TRAVERSE(&body_generators, iter, list) {
+ if (!strcmp(iter->type, content_type) &&
+ !strcmp(iter->subtype, content_subtype)) {
break;
}
- for (i = 0; i < num_accept; ++i) {
- for (j = 0; j < num_accept; ++j) {
- if (ast_strlen_zero(iter->accept[i])) {
- ast_debug(3, "Breaking because subscription handler has run out of 'accept' types\n");
- break;
- }
- if (!strcmp(accept[j], iter->accept[i])) {
- ast_debug(3, "Accept headers match: %s = %s\n", accept[j], iter->accept[i]);
- match = 1;
- break;
- }
- ast_debug(3, "Accept %s does not match %s\n", accept[j], iter->accept[i]);
- }
- if (match) {
- break;
- }
- }
- if (match) {
+ };
+
+ return iter;
+}
+
+static struct ast_sip_pubsub_body_generator *find_body_generator_accept(const char *accept)
+{
+ char *accept_copy = ast_strdupa(accept);
+ char *subtype = accept_copy;
+ char *type = strsep(&subtype, "/");
+
+ if (ast_strlen_zero(type) || ast_strlen_zero(subtype)) {
+ return NULL;
+ }
+
+ return find_body_generator_type_subtype(type, subtype);
+}
+
+static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
+ size_t num_accept)
+{
+ int i;
+ struct ast_sip_pubsub_body_generator *generator = NULL;
+
+ for (i = 0; i < num_accept; ++i) {
+ generator = find_body_generator_accept(accept[i]);
+ if (generator) {
+ ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]);
break;
- }
- }
-
- return iter;
+ } else {
+ ast_debug(3, "No body generator found for accept type %s\n", accept[i]);
+ }
+ }
+
+ return generator;
}
static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata)
@@ -751,6 +761,7 @@
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
struct ast_sip_subscription *sub;
size_t num_accept_headers;
+ struct ast_sip_pubsub_body_generator *generator;
endpoint = ast_pjsip_rdata_get_endpoint(rdata);
ast_assert(endpoint != NULL);
@@ -778,24 +789,38 @@
}
ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
- accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
- if (accept_header) {
- int i;
-
- for (i = 0; i < accept_header->count; ++i) {
- ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
- }
- num_accept_headers = accept_header->count;
- } else {
- num_accept_headers = 0;
- }
-
- handler = find_sub_handler(event, accept, num_accept_headers);
+ handler = find_sub_handler_for_event_name(event);
if (!handler) {
ast_log(LOG_WARNING, "No registered subscribe handler for event %s\n", event);
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
return PJ_TRUE;
}
+
+ accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
+ if (accept_header) {
+ int i;
+
+ for (i = 0; i < accept_header->count; ++i) {
+ ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
+ }
+ num_accept_headers = accept_header->count;
+ } else {
+ /* If a SUBSCRIBE contains no Accept headers, then we must assume that
+ * the default accept type for the event package is to be used.
+ */
+ ast_copy_string(accept[0], handler->default_accept, sizeof(accept[0]));
+ num_accept_headers = 1;
+ }
+
+ generator = find_body_generator(accept, num_accept_headers);
+ if (!generator) {
+ pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
+ return PJ_TRUE;
+ }
+
+ ast_sip_mod_data_set(rdata->tp_info.pool, rdata->endpt_info.mod_data,
+ pubsub_module.id, MOD_DATA_BODY_GENERATOR, generator);
+
sub = handler->new_subscribe(endpoint, rdata);
if (!sub) {
pjsip_transaction *trans = pjsip_rdata_get_tsx(rdata);
@@ -1063,6 +1088,137 @@
return pjsip_tsx_send_msg(tsx, tdata);
}
+int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
+{
+ struct ast_sip_pubsub_body_generator *existing;
+ pj_str_t accept;
+ pj_size_t accept_len;
+
+ existing = find_body_generator_type_subtype(generator->type, generator->subtype);
+ if (existing) {
+ ast_log(LOG_WARNING, "Cannot register body generator of %s/%s."
+ "One is already registered.\n", generator->type, generator->subtype);
+ return -1;
+ }
+
+ AST_RWLIST_WRLOCK(&body_generators);
+ AST_LIST_INSERT_HEAD(&body_generators, generator, list);
+ AST_RWLIST_UNLOCK(&body_generators);
+
+ /* Lengths of type and subtype plus space for a slash. pj_str_t is not
+ * null-terminated, so there is no need to allocate for the extra null
+ * byte
+ */
+ accept_len = strlen(generator->type) + strlen(generator->subtype) + 1;
+
+ accept.ptr = alloca(accept_len);
+ accept.slen = accept_len;
+ /* Safe use of sprintf */
+ sprintf(accept.ptr, "%s/%s", generator->type, generator->subtype);
+ pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module,
+ PJSIP_H_ACCEPT, NULL, 1, &accept);
+
+ return 0;
+}
+
+void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
+{
+ struct ast_sip_pubsub_body_generator *iter;
+ SCOPED_LOCK(lock, &body_generators, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_generators, iter, list) {
+ if (iter == generator) {
+ AST_LIST_REMOVE_CURRENT(list);
+ break;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END;
+}
+
+int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
+{
+ AST_RWLIST_WRLOCK(&body_supplements);
+ AST_RWLIST_INSERT_TAIL(&body_supplements, supplement, list);
+ AST_RWLIST_UNLOCK(&body_supplements);
+
+ return 0;
+}
+
+void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
+{
+ struct ast_sip_pubsub_body_supplement *iter;
+ SCOPED_LOCK(lock, &body_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_supplements, iter, list) {
+ if (iter == supplement) {
+ AST_LIST_REMOVE_CURRENT(list);
+ break;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END;
+}
+
+const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub)
+{
+ return sub->body_generator->type;
+}
+
+const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub)
+{
+ return sub->body_generator->subtype;
+}
+
+int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype,
+ void *data, struct ast_str **str)
+{
+ struct ast_sip_pubsub_body_supplement *supplement;
+ struct ast_sip_pubsub_body_generator *generator;
+ int res;
+ void *body;
+
+ generator = find_body_generator_type_subtype(type, subtype);
+ if (!generator) {
+ ast_log(LOG_WARNING, "Unable to find a body generator for %s/%s\n",
+ type, subtype);
+ return -1;
+ }
+
+ body = generator->allocate_body(data);
+ if (!body) {
+ ast_log(LOG_WARNING, "Unable to allocate a NOTIFY body of type %s/%s\n",
+ type, subtype);
+ return -1;
+ }
+
+ if (generator->generate_body_content(body, data)) {
+ res = -1;
+ goto end;
+ }
+
+ AST_RWLIST_RDLOCK(&body_supplements);
+ AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) {
+ if (!strcmp(generator->type, supplement->type) &&
+ !strcmp(generator->subtype, supplement->subtype)) {
+ res = supplement->supplement_body(body, data);
+ if (res) {
+ break;
+ }
+ }
+ }
+ AST_RWLIST_UNLOCK(&body_supplements);
+
+ if (!res) {
+ generator->to_string(body, str);
+ }
+
+end:
+ if (generator->destroy_body) {
+ generator->destroy_body(body);
+ }
+
+ return res;
+}
+
[... 25 lines stripped ...]
More information about the asterisk-commits
mailing list