[svn-commits] mmichelson: branch mmichelson/pubsub_bodies r405857 - /team/mmichelson/pubsub...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jan 17 13:51:53 CST 2014


Author: mmichelson
Date: Fri Jan 17 13:51:48 2014
New Revision: 405857

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=405857
Log:
Add body generators for various PIDF document types.


Modified:
    team/mmichelson/pubsub_bodies/res/res_pjsip_pidf.c

Modified: team/mmichelson/pubsub_bodies/res/res_pjsip_pidf.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pubsub_bodies/res/res_pjsip_pidf.c?view=diff&rev=405857&r1=405856&r2=405857
==============================================================================
--- team/mmichelson/pubsub_bodies/res/res_pjsip_pidf.c (original)
+++ team/mmichelson/pubsub_bodies/res/res_pjsip_pidf.c Fri Jan 17 13:51:48 2014
@@ -33,6 +33,7 @@
 #include "asterisk/module.h"
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_exten_state.h"
+#include "asterisk/res_pjsip_pubsub.h"
 
 enum state {
 	NOTIFY_OPEN,
@@ -171,13 +172,6 @@
 	create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
 }
 
-static void release_pool(void *obj)
-{
-	pj_pool_t *pool = obj;
-
-	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-}
-
 /*!
  * \internal
  * \brief Convert angle brackets in input into escaped forms suitable for XML
@@ -214,55 +208,105 @@
 	}
 }
 
+struct exten_state_body_generator_data {
+	struct ast_sip_exten_state_data *exten_data;
+	const char *local;
+	const char *remote;
+	pj_pool_t *pool;
+};
+
+static int exten_state_body_generator_data_init(struct ast_sip_exten_state_data *exten_data,
+		const char *local, const char *remote,
+		struct exten_state_body_generator_data *gen_data)
+{
+	gen_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+			"pidf", 1024, 1024);
+
+	if (!gen_data->pool) {
+		return -1;
+	}
+
+	gen_data->local = local;
+	gen_data->remote = remote;
+	gen_data->exten_data = exten_data;
+
+	return 0;
+}
+
+static void exten_state_body_generator_data_release(struct exten_state_body_generator_data *gen_data)
+{
+	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), gen_data->pool);
+}
+
+static int pidf_generate_body_content(void *body, void *data)
+{
+	pjpidf_tuple *tuple;
+	pj_str_t note, id, contact, priority;
+	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
+	int local_state;
+	char sanitized[PJSIP_MAX_URL_SIZE];
+	pjpidf_pres *pres = body;
+	struct exten_state_body_generator_data *gen_data = data;
+
+	exten_state_to_str(gen_data->exten_data->exten_state, &statestring, &pidfstate,
+			   &pidfnote, &local_state);
+
+	add_non_standard(gen_data->pool, pres, pidfstate);
+
+	if (!pjpidf_pres_add_note(gen_data->pool, pres, pj_cstr(&note, pidfnote))) {
+		ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
+		return -1;
+	}
+
+	if (!(tuple = pjpidf_pres_add_tuple(gen_data->pool, pres,
+					pj_cstr(&id, gen_data->exten_data->exten)))) {
+		ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
+		return -1;
+	}
+
+	sanitize_xml(gen_data->remote, sanitized, sizeof(sanitized));
+	pjpidf_tuple_set_contact(gen_data->pool, tuple, pj_cstr(&contact, sanitized));
+	pjpidf_tuple_set_contact_prio(gen_data->pool, tuple, pj_cstr(&priority, "1"));
+	pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
+				     (pidfstate[0] == 'b') || (local_state != NOTIFY_CLOSED));
+
+	return 0;
+}
+
 static int pidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
 				const char *remote, struct ast_str **body_text)
 {
 	pjpidf_pres *pres;
-	pjpidf_tuple *tuple;
-	pj_str_t entity, note, id, contact, priority;
-	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
-	int local_state, size;
-	char sanitized[PJSIP_MAX_URL_SIZE];
-
-	RAII_VAR(pj_pool_t *, pool,
-		 pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
-					 "pidf", 1024, 1024), release_pool);
-
-	exten_state_to_str(data->exten_state, &statestring, &pidfstate,
-			   &pidfnote, &local_state);
-
-	if (!(pres = pjpidf_create(pool, pj_cstr(&entity, local)))) {
+	pj_str_t entity;
+	int size;
+	struct exten_state_body_generator_data gen_data;
+	int res = 0;
+
+	if (exten_state_body_generator_data_init(data, local, remote, &gen_data)) {
+		ast_log(LOG_WARNING, "Unable to allocate resources for generating PIDF body\n");
+		return -1;
+	}
+
+	if (!(pres = pjpidf_create(gen_data.pool, pj_cstr(&entity, local)))) {
 		ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
-		return -1;
-	}
-
-	add_non_standard(pool, pres, pidfstate);
-
-	if (!pjpidf_pres_add_note(pool, pres, pj_cstr(&note, pidfnote))) {
-		ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
-		return -1;
-	}
-
-	if (!(tuple = pjpidf_pres_add_tuple(pool, pres, pj_cstr(&id, data->exten)))) {
-		ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
-		return -1;
-	}
-
-	sanitize_xml(remote, sanitized, sizeof(sanitized));
-	pjpidf_tuple_set_contact(pool, tuple, pj_cstr(&contact, sanitized));
-	pjpidf_tuple_set_contact_prio(pool, tuple, pj_cstr(&priority, "1"));
-	pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
-				     (pidfstate[0] == 'b') || (local_state != NOTIFY_CLOSED));
+		res = -1;
+		goto end;
+	}
+
+	ast_sip_pubsub_generate_body_content("application", "pidf+xml", pres, &gen_data);
 
 	if (!(size = pjpidf_print(pres, ast_str_buffer(*body_text),
 				  ast_str_size(*body_text)))) {
 		ast_log(LOG_WARNING, "PIDF body text too large\n");
-		return -1;
+		res = -1;
+		goto end;
 	}
 	*(ast_str_buffer(*body_text) + size) = '\0';
 	ast_str_update(*body_text);
 
-	return 0;
+end:
+	exten_state_body_generator_data_release(&gen_data);
+	return res;
 }
 
 static struct ast_sip_exten_state_provider pidf_xml_provider = {
@@ -273,61 +317,90 @@
 	.create_body = pidf_xml_create_body
 };
 
+static struct ast_sip_pubsub_body_generator pidf_body_generator = {
+	.type = "application",
+	.subtype = "pidf+xml",
+	.generator_type = AST_SIP_PUBSUB_BODY_GENERATOR_PRIMARY,
+	.generate_body_content = pidf_generate_body_content,
+};
+
+static int xpidf_generate_body_content(void *body, void *data)
+{
+	pjxpidf_pres *pres = body;
+	struct exten_state_body_generator_data *gen_data = data;
+	static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
+	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
+	pj_xml_attr *attr;
+	int local_state;
+	pj_str_t name, uri;
+
+	exten_state_to_str(gen_data->exten_data->exten_state, &statestring, &pidfstate,
+			   &pidfnote, &local_state);
+
+	attr = find_node_attr(gen_data->pool, pres, "atom", "id");
+	pj_strdup2(gen_data->pool, &attr->value, gen_data->exten_data->exten);
+
+	attr = find_node_attr(gen_data->pool, pres, "address", "uri");
+
+	uri.ptr = (char*) pj_pool_alloc(gen_data->pool, strlen(gen_data->remote) + STR_ADDR_PARAM.slen);
+	pj_strcpy2( &uri, gen_data->remote);
+	pj_strcat( &uri, &STR_ADDR_PARAM);
+	pj_strdup(gen_data->pool, &attr->value, &uri);
+
+	create_attr(gen_data->pool, pj_xml_find_node(pres, pj_cstr(&name, "address")),
+		    "priority", "0.80000");
+
+	attr = find_node_attr(gen_data->pool, pres, "status", "status");
+	pj_strdup2(gen_data->pool, &attr->value,
+		   (local_state ==  NOTIFY_OPEN) ? "open" :
+		   (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
+
+	attr = find_node_attr(gen_data->pool, pres, "msnsubstatus", "substatus");
+	pj_strdup2(gen_data->pool, &attr->value,
+		   (local_state == NOTIFY_OPEN) ? "online" :
+		   (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
+
+	return 0;
+}
+
 static int xpidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
 				 const char *remote, struct ast_str **body_text)
 {
-	static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
 	pjxpidf_pres *pres;
-	pj_xml_attr *attr;
-	pj_str_t name, uri;
-	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
-	int local_state, size;
-
-	RAII_VAR(pj_pool_t *, pool,
-		 pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
-					 "pidf", 1024, 1024), release_pool);
-
-	exten_state_to_str(data->exten_state, &statestring, &pidfstate,
-			   &pidfnote, &local_state);
-
-	if (!(pres = pjxpidf_create(pool, pj_cstr(&name, local)))) {
-		ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
-		return -1;
-	}
-
-	attr = find_node_attr(pool, pres, "atom", "id");
-	pj_strdup2(pool, &attr->value, data->exten);
-
-	attr = find_node_attr(pool, pres, "address", "uri");
-
-	uri.ptr = (char*) pj_pool_alloc(pool, strlen(remote) + STR_ADDR_PARAM.slen);
-	pj_strcpy2( &uri, remote);
-	pj_strcat( &uri, &STR_ADDR_PARAM);
-	pj_strdup(pool, &attr->value, &uri);
-
-	create_attr(pool, pj_xml_find_node(pres, pj_cstr(&name, "address")),
-		    "priority", "0.80000");
-
-	attr = find_node_attr(pool, pres, "status", "status");
-	pj_strdup2(pool, &attr->value,
-		   (local_state ==  NOTIFY_OPEN) ? "open" :
-		   (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
-
-	attr = find_node_attr(pool, pres, "msnsubstatus", "substatus");
-	pj_strdup2(pool, &attr->value,
-		   (local_state == NOTIFY_OPEN) ? "online" :
-		   (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
+	int size;
+	struct exten_state_body_generator_data gen_data;
+	int res = 0;
+	pj_str_t name;
+
+	if (exten_state_body_generator_data_init(data, local, remote, &gen_data)) {
+		ast_log(LOG_WARNING, "Unable to allocate resources for generating xpidf XML body\n");
+		res = -1;
+		goto end;
+	}
+
+	if (!(pres = pjxpidf_create(gen_data.pool, pj_cstr(&name, local)))) {
+		exten_state_body_generator_data_release(&gen_data);
+		ast_log(LOG_WARNING, "Unable to create XPIDF presence\n");
+		res = -1;
+		goto end;
+	}
+
+	ast_sip_pubsub_generate_body_content("application", "xpidf+xml", pres, &gen_data);
 
 	if (!(size = pjxpidf_print(pres, ast_str_buffer(*body_text),
 				  ast_str_size(*body_text)))) {
 		ast_log(LOG_WARNING, "XPIDF body text too large\n");
-		return -1;
+		exten_state_body_generator_data_release(&gen_data);
+		res = -1;
+		goto end;
 	}
 
 	*(ast_str_buffer(*body_text) + size) = '\0';
 	ast_str_update(*body_text);
 
-	return 0;
+end:
+	exten_state_body_generator_data_release(&gen_data);
+	return res;
 }
 
 static struct ast_sip_exten_state_provider xpidf_xml_provider = {
@@ -338,6 +411,13 @@
 	.create_body = xpidf_xml_create_body
 };
 
+static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
+	.type = "application",
+	.subtype = "xpidf+xml",
+	.generator_type = AST_SIP_PUBSUB_BODY_GENERATOR_PRIMARY,
+	.generate_body_content = xpidf_generate_body_content,
+};
+
 static struct ast_sip_exten_state_provider cpim_pidf_xml_provider = {
 	.event_name = "presence",
 	.type = "application",
@@ -346,22 +426,32 @@
 	.create_body = xpidf_xml_create_body,
 };
 
+static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
+	.type = "application",
+	.subtype = "cpim-pidf+xml",
+	.generator_type = AST_SIP_PUBSUB_BODY_GENERATOR_PRIMARY,
+	.generate_body_content = xpidf_generate_body_content,
+};
+
 static int load_module(void)
 {
 	if (ast_sip_register_exten_state_provider(&pidf_xml_provider)) {
 		ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
 			pidf_xml_provider.event_name, pidf_xml_provider.body_type);
 	}
+	ast_sip_pubsub_register_body_generator(&pidf_body_generator);
 
 	if (ast_sip_register_exten_state_provider(&xpidf_xml_provider)) {
 		ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
 			xpidf_xml_provider.event_name, xpidf_xml_provider.body_type);
 	}
+	ast_sip_pubsub_register_body_generator(&xpidf_body_generator);
 
 	if (ast_sip_register_exten_state_provider(&cpim_pidf_xml_provider)) {
 		ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
 			cpim_pidf_xml_provider.event_name, cpim_pidf_xml_provider.body_type);
 	}
+	ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator);
 
 	return AST_MODULE_LOAD_SUCCESS;
 }




More information about the svn-commits mailing list