[asterisk-commits] mmichelson: branch mmichelson/subscription_abstraction r416577 - /team/mmiche...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 18 10:27:30 CDT 2014


Author: mmichelson
Date: Wed Jun 18 10:27:23 2014
New Revision: 416577

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=416577
Log:
Factor out some common code during subscription creation.


Modified:
    team/mmichelson/subscription_abstraction/res/res_pjsip_pubsub.c

Modified: team/mmichelson/subscription_abstraction/res/res_pjsip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/subscription_abstraction/res/res_pjsip_pubsub.c?view=diff&rev=416577&r1=416576&r2=416577
==============================================================================
--- team/mmichelson/subscription_abstraction/res/res_pjsip_pubsub.c (original)
+++ team/mmichelson/subscription_abstraction/res/res_pjsip_pubsub.c Wed Jun 18 10:27:23 2014
@@ -321,12 +321,12 @@
 AST_RWLIST_HEAD_STATIC(body_generators, ast_sip_pubsub_body_generator);
 AST_RWLIST_HEAD_STATIC(body_supplements, ast_sip_pubsub_body_supplement);
 
-static pjsip_evsub *sip_subscription_get_evsub(struct ast_sip_subscription *sub)
+static pjsip_evsub *sip_subscription_get_evsub(const struct ast_sip_subscription *sub)
 {
 	return sub->pants.real.evsub;
 }
 
-static pjsip_dialog *sip_subscription_get_dlg(struct ast_sip_subscription *sub)
+static pjsip_dialog *sip_subscription_get_dlg(const struct ast_sip_subscription *sub)
 {
 	return sub->pants.real.dlg;
 }
@@ -470,7 +470,7 @@
 }
 
 static struct ast_sip_subscription *notifier_create_subscription(const struct ast_sip_subscription_handler *handler,
-		struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
+		struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource);
 
 /*! \brief Callback function to perform the actual recreation of a subscription */
 static int subscription_persistence_recreate(void *obj, void *arg, int flags)
@@ -549,7 +549,7 @@
 
 	resp = handler->notifier->new_subscribe(endpoint, resource);
 	if (resp >= 200 && resp < 300) {
-		sub = notifier_create_subscription(handler, endpoint, &rdata);
+		sub = notifier_create_subscription(handler, endpoint, &rdata, resource);
 		sub->persistence = ao2_bump(persistence);
 		subscription_persistence_update(sub, &rdata);
 	} else {
@@ -761,26 +761,17 @@
 	.on_server_timeout = pubsub_on_server_timeout,
 };
 
-/* XXX TODO There is a lot of repeated code between the two subscription creation functions. */
-
-static struct ast_sip_subscription *notifier_create_subscription(const struct ast_sip_subscription_handler *handler,
-		struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
+static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_subscription_handler *handler,
+		struct ast_sip_endpoint *endpoint, const char *resource, enum ast_sip_subscription_role role)
 {
 	struct ast_sip_subscription *sub;
-	pjsip_dialog *dlg;
-	struct subscription_persistence *persistence;
-	size_t resource_size;
-	pjsip_sip_uri *request_uri;
-	struct pjsip_evsub *evsub;
-
-	request_uri = pjsip_uri_get_uri(rdata->msg_info.msg->line.req.uri);
-	resource_size = pj_strlen(&request_uri->user) + 1;
-
-	sub = ao2_alloc(sizeof(*sub) + resource_size, subscription_destructor);
+
+	sub = ao2_alloc(sizeof(*sub) + strlen(resource) + 1, subscription_destructor);
 	if (!sub) {
 		return NULL;
 	}
-	ast_copy_pj_str(sub->resource, &request_uri->user, resource_size);
+	strcpy(sub->resource, resource); /* Safe */
+
 	sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
 	if (!sub->datastores) {
 		ao2_ref(sub, -1);
@@ -791,10 +782,39 @@
 		ao2_ref(sub, -1);
 		return NULL;
 	}
+	sub->role = role;
+	sub->type = SIP_SUBSCRIPTION_REAL;
+	sub->endpoint = ao2_bump(endpoint);
+	sub->handler = handler;
+
+	return sub;
+}
+
+static void subscription_setup_dialog(struct ast_sip_subscription *sub, pjsip_dialog *dlg)
+{
+	/* We keep a reference to the dialog until our subscription is destroyed. See
+	 * the subscription_destructor for more details
+	 */
+	pjsip_dlg_inc_session(dlg, &pubsub_module);
+	sub->pants.real.dlg = dlg;
+	ast_sip_dialog_set_serializer(dlg, sub->serializer);
+	pjsip_evsub_set_mod_data(sip_subscription_get_evsub(sub), pubsub_module.id, sub);
+}
+
+static struct ast_sip_subscription *notifier_create_subscription(const struct ast_sip_subscription_handler *handler,
+		struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource)
+{
+	struct ast_sip_subscription *sub;
+	pjsip_dialog *dlg;
+	struct subscription_persistence *persistence;
+
+	sub = allocate_subscription(handler, endpoint, resource, AST_SIP_NOTIFIER);
+	if (!sub) {
+		return NULL;
+	}
+
 	sub->body_generator = ast_sip_mod_data_get(rdata->endpt_info.mod_data,
 			pubsub_module.id, MOD_DATA_BODY_GENERATOR);
-	sub->role = AST_SIP_NOTIFIER;
-	sub->type = SIP_SUBSCRIPTION_REAL;
 	dlg = ast_sip_create_dialog_uas(endpoint, rdata);
 	if (!dlg) {
 		ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
@@ -814,17 +834,8 @@
 		dlg->remote.cseq = persistence->cseq;
 	}
 
-	pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &evsub);
-	sub->pants.real.evsub = evsub;
-	/* We keep a reference to the dialog until our subscription is destroyed. See
-	 * the subscription_destructor for more details
-	 */
-	pjsip_dlg_inc_session(dlg, &pubsub_module);
-	sub->pants.real.dlg = dlg;
-	ast_sip_dialog_set_serializer(dlg, sub->serializer);
-	pjsip_evsub_set_mod_data(sip_subscription_get_evsub(sub), pubsub_module.id, sub);
-	sub->endpoint = ao2_bump(endpoint);
-	sub->handler = handler;
+	pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &sub->pants.real.evsub);
+	subscription_setup_dialog(sub, dlg);
 
 	add_subscription(sub);
 	return sub;
@@ -838,22 +849,11 @@
 	struct ast_sip_contact *contact;
 	pj_str_t event;
 
+	sub = allocate_subscription(handler, endpoint, resource, AST_SIP_SUBSCRIBER);
 	if (!sub) {
 		return NULL;
 	}
-	strcpy(sub->resource, resource); /* Safe */
-	sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
-	if (!sub->datastores) {
-		ao2_ref(sub, -1);
-		return NULL;
-	}
-	sub->serializer = ast_sip_create_serializer();
-	if (!sub->serializer) {
-		ao2_ref(sub, -1);
-		return NULL;
-	}
-	sub->role = AST_SIP_SUBSCRIBER;
-	sub->type = SIP_SUBSCRIPTION_REAL;
+
 	contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
 	if (!contact || ast_strlen_zero(contact->uri)) {
 		ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
@@ -872,16 +872,7 @@
 
 	pj_cstr(&event, handler->event_name);
 	pjsip_evsub_create_uac(dlg, &pubsub_cb, &event, 0, &sub->pants.real.evsub);
-	/* We keep a reference to the dialog until our subscription is destroyed. See
-	 * the subscription_destructor for more details
-	 */
-	pjsip_dlg_inc_session(dlg, &pubsub_module);
-	sub->pants.real.dlg = dlg;
-	ast_sip_dialog_set_serializer(dlg, sub->serializer);
-	pjsip_evsub_set_mod_data(sip_subscription_get_evsub(sub), pubsub_module.id, sub);
-	ao2_ref(endpoint, +1);
-	sub->endpoint = endpoint;
-	sub->handler = handler;
+	subscription_setup_dialog(sub, dlg);
 
 	add_subscription(sub);
 
@@ -1329,7 +1320,7 @@
 		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
 		return PJ_TRUE;
 	}
-	sub = notifier_create_subscription(handler, endpoint, rdata);
+	sub = notifier_create_subscription(handler, endpoint, rdata, resource);
 	if (!sub) {
 		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
 	} else {




More information about the asterisk-commits mailing list