asterisk/res/res_pjsip_pidf_eyebeam_body_supplement.c
Mark Michelson f55abe9cf1 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
........

Merged revisions 407016 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@407030 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-01-31 22:27:07 +00:00

114 lines
3.5 KiB
C

/*
* asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2014, Digium, Inc.
*
* Mark Michelson <mmichelson@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*** MODULEINFO
<depend>pjproject</depend>
<depend>res_pjsip</depend>
<depend>res_pjsip_pubsub</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_simple.h>
#include <pjlib.h>
#include "asterisk/module.h"
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_pubsub.h"
#include "asterisk/res_pjsip_presence_xml.h"
#include "asterisk/res_pjsip_body_generator_types.h"
/*!
* \internal
* \brief Adds non standard elements to the xml body
*
* This is some code that was part of the original chan_sip implementation
* that is not part of the RFC 3863 definition, but we are keeping available
* for backward compatability. The original comment stated that Eyebeam
* supports this format.
*/
static void add_eyebeam(pj_pool_t *pool, pj_xml_node *node, const char *pidfstate)
{
static const char *XMLNS_PP = "xmlns:pp";
static const char *XMLNS_PERSON = "urn:ietf:params:xml:ns:pidf:person";
static const char *XMLNS_ES = "xmlns:es";
static const char *XMLNS_RPID_STATUS = "urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status";
static const char *XMLNS_EP = "xmlns:ep";
static const char *XMLNS_RPID_PERSON = "urn:ietf:params:xml:ns:pidf:rpid:rpid-person";
pj_xml_node *person = ast_sip_presence_xml_create_node(pool, node, "pp:person");
pj_xml_node *status = ast_sip_presence_xml_create_node(pool, person, "status");
if (pidfstate[0] != '-') {
pj_xml_node *activities = ast_sip_presence_xml_create_node(pool, status, "ep:activities");
size_t str_size = sizeof("ep:") + strlen(pidfstate);
activities->content.ptr = pj_pool_alloc(pool, str_size);
activities->content.slen = pj_ansi_snprintf(activities->content.ptr, str_size,
"ep:%s", pidfstate);
}
ast_sip_presence_xml_create_attr(pool, node, XMLNS_PP, XMLNS_PERSON);
ast_sip_presence_xml_create_attr(pool, node, XMLNS_ES, XMLNS_RPID_STATUS);
ast_sip_presence_xml_create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
}
static int pidf_supplement_body(void *body, void *data)
{
pjpidf_pres *pres = body;
struct ast_sip_exten_state_data *state_data = data;
char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
enum ast_sip_pidf_state local_state;
ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
&pidfstate, &pidfnote, &local_state);
add_eyebeam(state_data->pool, pres, pidfstate);
return 0;
}
static struct ast_sip_pubsub_body_supplement pidf_supplement = {
.type = "application",
.subtype = "pidf+xml",
.supplement_body = pidf_supplement_body,
};
static int load_module(void)
{
if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
return 0;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Eyebeam supplement",
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
);