[sr-dev] git:master: modules_k/presence Fix and improvement for subscribe updates

Anca Vamanu anca.vamanu at 1and1.ro
Tue Mar 6 17:40:46 CET 2012


Module: sip-router
Branch: master
Commit: 23cddf4220439be35ccb00c9056d802d32b9bc6f
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=23cddf4220439be35ccb00c9056d802d32b9bc6f

Author: Anca Vamanu <anca.vamanu at 1and1.ro>
Committer: Anca Vamanu <anca.vamanu at 1and1.ro>
Date:   Tue Mar  6 18:38:11 2012 +0200

modules_k/presence Fix and improvement for subscribe updates

	Fix: wrong Cseq for Notify for terminated Subscription in some DB
	modes
	Improvement: When DB mode is DB_ONLY do only one update in
	active_watchers when processing one Subscribe(instead of 2)

---

 modules_k/presence/hash.c      |    2 +-
 modules_k/presence/notify.c    |   47 +++------------------------------------
 modules_k/presence/subscribe.c |   47 ++++++++++++++++++++++-----------------
 modules_k/presence/subscribe.h |    5 +--
 4 files changed, 33 insertions(+), 68 deletions(-)

diff --git a/modules_k/presence/hash.c b/modules_k/presence/hash.c
index 87b0908..4208fd2 100644
--- a/modules_k/presence/hash.c
+++ b/modules_k/presence/hash.c
@@ -346,7 +346,7 @@ int update_shtable(shtable_t htable,unsigned int hash_code,
 	else
 	{
 		subs->local_cseq = ++s->local_cseq;
-		s->version = ++subs->version;
+		subs->version = ++s->version;
 	}
 	
 	if(strncmp(s->contact.s, subs->contact.s, subs->contact.len))
diff --git a/modules_k/presence/notify.c b/modules_k/presence/notify.c
index 4527b5a..52cb958 100644
--- a/modules_k/presence/notify.c
+++ b/modules_k/presence/notify.c
@@ -1150,13 +1150,13 @@ int get_subs_db(str* pres_uri, pres_ev_t* event, str* sender,
 		s.local_contact.len = s.local_contact.s?strlen(s.local_contact.s):0;
 		
 		s.event= event;
-		s.local_cseq = row_vals[cseq_col].val.int_val;
+		s.local_cseq = row_vals[cseq_col].val.int_val +1;
 		if(row_vals[expires_col].val.int_val < (int)time(NULL))
 		    s.expires = 0;
 		else
 		    s.expires = row_vals[expires_col].val.int_val -
 			(int)time(NULL);
-		s.version = row_vals[version_col].val.int_val;
+		s.version = row_vals[version_col].val.int_val +1;
 
 		s_new= mem_copy_subs(&s, PKG_MEM_TYPE);
 		if(s_new== NULL)
@@ -1182,46 +1182,6 @@ error:
 	return -1;
 }
 
-int update_in_list(subs_t* s, subs_t* s_array, int new_rec_no, int n)
-{
-	int i= 0;
-	subs_t* ls;
-
-	ls= s_array;
-	
-	while(i< new_rec_no)
-	{
-		i++;
-		ls= ls->next;
-	}
-
-	for(i = 0; i< n; i++)
-	{
-		if(ls== NULL)
-		{
-			LM_ERR("wrong records count\n");
-			return -1;
-		}
-		printf_subs(ls);
-		
-		if(ls->callid.len== s->callid.len &&
-		strncmp(ls->callid.s, s->callid.s, s->callid.len)== 0 &&
-		ls->to_tag.len== s->to_tag.len &&
-		strncmp(ls->to_tag.s, s->to_tag.s, s->to_tag.len)== 0 &&
-		ls->from_tag.len== s->from_tag.len &&
-		strncmp(ls->from_tag.s, s->from_tag.s, s->from_tag.len)== 0 )
-		{
-			ls->local_cseq= s->local_cseq;
-			ls->expires= s->expires- (int)time(NULL);
-			ls->version= s->version;
-			ls->status= s->status;
-			return 1;
-		}
-		ls= ls->next;
-	}
-	return -1;
-}
-
 subs_t* get_subs_dialog(str* pres_uri, pres_ev_t* event, str* sender)
 {
 	unsigned int hash_code;
@@ -1600,7 +1560,8 @@ int notify(subs_t* subs, subs_t * watcher_subs,str* n_body,int force_null_body)
 			}
 		}
 		/* if DB_ONLY mode or WRITE_THROUGH update in database */
-		if(subs_dbmode == DB_ONLY || subs_dbmode == WRITE_THROUGH)
+		if(subs->recv_event!=PRES_SUBSCRIBE_RECV &&
+				(subs_dbmode == DB_ONLY || subs_dbmode == WRITE_THROUGH))
 		{
 			LM_DBG("updating subscription to database\n");
 			if(update_subs_db(subs, LOCAL_TYPE)< 0)
diff --git a/modules_k/presence/subscribe.c b/modules_k/presence/subscribe.c
index 39f649f..7e45a9b 100644
--- a/modules_k/presence/subscribe.c
+++ b/modules_k/presence/subscribe.c
@@ -326,8 +326,8 @@ int insert_subs_db(subs_t* s, int type)
 
 int update_subs_db(subs_t* subs, int type)
 {
-	db_key_t query_cols[3], update_keys[4];
-	db_val_t query_vals[3], update_vals[4];
+	db_key_t query_cols[3], update_keys[6];
+	db_val_t query_vals[3], update_vals[6];
 	int n_update_cols= 0;
 	int n_query_cols = 0;
 
@@ -363,18 +363,18 @@ int update_subs_db(subs_t* subs, int type)
 		update_vals[n_update_cols].val.int_val = subs->remote_cseq; 
 		n_update_cols++;
 	}
-	else
-	{	
+	if(type & LOCAL_TYPE)
+	{
 		update_keys[n_update_cols] = &str_local_cseq_col;
 		update_vals[n_update_cols].type = DB1_INT;
 		update_vals[n_update_cols].nul = 0;
-		update_vals[n_update_cols].val.int_val = subs->local_cseq+ 1;
+		update_vals[n_update_cols].val.int_val = subs->local_cseq;
 		n_update_cols++;
 	
 		update_keys[n_update_cols] = &str_version_col;
 		update_vals[n_update_cols].type = DB1_INT;
 		update_vals[n_update_cols].nul = 0;
-		update_vals[n_update_cols].val.int_val = subs->version+ 1;
+		update_vals[n_update_cols].val.int_val = subs->version;
 		n_update_cols++;
 	}
 
@@ -425,6 +425,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 {
 	unsigned int hash_code;
 
+	LM_DBG("update subscription\n");
 	printf_subs(subs);
 
 	*sent_reply= 0;
@@ -489,7 +490,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 		if(subs_dbmode == DB_ONLY ||  subs_dbmode== WRITE_THROUGH)
 		{
 			/* update in database table */
-			if(update_subs_db(subs, REMOTE_TYPE)< 0)
+			if(update_subs_db(subs, REMOTE_TYPE|LOCAL_TYPE)< 0)
 			{
 				LM_ERR("updating subscription in database table\n");
 				goto error;
@@ -506,6 +507,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 				LM_DBG("inserting in shtable\n");
 				subs->db_flag = (subs_dbmode==WRITE_THROUGH)?WTHROUGHDB_FLAG:INSERTDB_FLAG;
 				hash_code= core_hash(&subs->pres_uri, &subs->event->name, shtable_size);
+				subs->version = 0;
 				if(insert_shtable(subs_htable,hash_code,subs)< 0)
 				{
 					LM_ERR("failed to insert new record in subs htable\n");
@@ -515,6 +517,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 
 			if(subs_dbmode == DB_ONLY || subs_dbmode == WRITE_THROUGH)
 			{
+				subs->version = 1;
 				if(insert_subs_db(subs, REMOTE_TYPE) < 0)
 				{
 					LM_ERR("failed to insert new record in database\n");
@@ -531,7 +534,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 		}
 	}
 
-/* reply_and_notify  */
+	/* reply_and_notify  */
 
 	if(subs->event->type & PUBL_TYPE)
 	{	
@@ -542,17 +545,17 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
 			goto error;
 		}
 		*sent_reply= 1;
-		
+
 		if(subs->expires!= 0 && subs->event->wipeer)
-		{	
+		{
 			LM_DBG("send Notify with winfo\n");
 			if(query_db_notify(&subs->pres_uri, subs->event->wipeer, subs)< 0)
 			{
 				LM_ERR("Could not send notify winfo\n");
 				goto error;
-			}	
+			}
 			if(subs->send_on_cback== 0)
-			{	
+			{
 				if(notify(subs, NULL, NULL, 0)< 0)
 				{
 					LM_ERR("Could not send notify\n");
@@ -723,6 +726,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
 		}
 		reason= subs.reason;
 	}
+	/* mark that the received event is a SUBSCRIBE message */
+	subs.recv_event = PRES_SUBSCRIBE_RECV;
 
 	/* call event specific subscription handling */
 	if(event->evs_subs_handl)
@@ -732,7 +737,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
 			LM_ERR("in event specific subscription handling\n");
 			goto error;
 		}
-	}	
+	}
 
 
 	/* if dialog initiation Subscribe - get subscription state */
@@ -740,7 +745,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
 	{
 		if(!event->req_auth) 
 			subs.status = ACTIVE_STATUS;
-		else   
+		else
 		{
 			/* query in watchers_table */
 			if(get_db_subs_auth(&subs, &found)< 0)
@@ -1077,8 +1082,8 @@ int extract_sdialog_info(subs_t* subs,struct sip_msg* msg, int mexp,
 	}
 	subs->from_tag = pfrom->tag_value;
 
-	subs->version = 0;
-	
+	subs->version = 1;
+
 	if((!scontact.s) || (scontact.len== 0))
 	{
 		if(ps_fill_local_contact(msg, &subs->local_contact)<0)
@@ -1171,7 +1176,7 @@ found_rec:
 	if(subs->pres_uri.s == NULL)
 		subs->pres_uri= pres_uri;
 
-	subs->version = s->version;
+	subs->version = s->version + 1;
 	subs->status= s->status;
 	if(s->reason.s && s->reason.len)
 	{
@@ -1198,7 +1203,7 @@ found_rec:
 		subs->record_route.len= s->record_route.len;
 	}
 
-	subs->local_cseq= s->local_cseq;
+	subs->local_cseq= s->local_cseq +1;
 
 	if(subs->remote_cseq<= s->remote_cseq)
 	{
@@ -1331,8 +1336,8 @@ int get_database_info(struct sip_msg* msg, subs_t* subs, int* reply_code, str* r
 		subs->reason.len= reason.len;
 	}
 
-	subs->local_cseq= row_vals[local_cseq_col].val.int_val;
-	subs->version= row_vals[version_col].val.int_val;
+	subs->local_cseq= row_vals[local_cseq_col].val.int_val + 1;
+	subs->version= row_vals[version_col].val.int_val + 1;
 
 	if(!EVENT_DIALOG_SLA(subs->event->evp))
 	{
@@ -1518,7 +1523,7 @@ void update_db_subs_timer_dbonly(void)
 		}
 		s.event= event;
 
-		s.local_cseq = row_vals[local_cseq_col].val.int_val;
+		s.local_cseq = row_vals[local_cseq_col].val.int_val +1;
 		s.expires = 0;
 
 		s_new= mem_copy_subs(&s, PKG_MEM_TYPE);
diff --git a/modules_k/presence/subscribe.h b/modules_k/presence/subscribe.h
index d76bf4f..05a8fa5 100644
--- a/modules_k/presence/subscribe.h
+++ b/modules_k/presence/subscribe.h
@@ -50,8 +50,7 @@ struct pres_ev;
    and reason is polite-block */
 #define POLITE_BLOCK_STATUS  5
 
-#define INTERNAL_UPDATE_FALSE	0
-#define INTERNAL_UPDATE_TRUE	1
+#define PRES_SUBSCRIBE_RECV		1
 
 struct subscription
 {
@@ -78,7 +77,7 @@ struct subscription
 	int send_on_cback;
 	int db_flag;
 	str* auth_rules_doc;
-	int internal_update_flag;
+	int recv_event;
 	struct subscription* next;
 
 };




More information about the sr-dev mailing list