Browse code

modules_k/presence: added db_only mode support for presentity table

- This enhancement was made by Paul Pankhurst at Crocodile RCS

pd authored on 19/10/2011 10:57:36
Showing 3 changed files
... ...
@@ -140,7 +140,7 @@ int timeout_rm_subs = 1;
140 140
 int send_fast_notify = 1;
141 141
 
142 142
 int phtable_size= 9;
143
-phtable_t* pres_htable;
143
+phtable_t* pres_htable=NULL;
144 144
 
145 145
 static cmd_export_t cmds[]=
146 146
 {
... ...
@@ -330,22 +330,25 @@ static int mod_init(void)
330 330
 		}
331 331
 	}
332 332
 
333
-	if(phtable_size< 1)
334
-		phtable_size= 256;
335
-	else
336
-		phtable_size= 1<< phtable_size;
333
+	if(dbmode != DB_ONLY)
334
+	{	
335
+		if(phtable_size< 1)
336
+			phtable_size= 256;
337
+		else
338
+			phtable_size= 1<< phtable_size;
337 339
 
338
-	pres_htable= new_phtable();
339
-	if(pres_htable== NULL)
340
-	{
341
-		LM_ERR("initializing presentity hash table\n");
342
-		return -1;
343
-	}
340
+		pres_htable= new_phtable();
341
+		if(pres_htable== NULL)
342
+		{
343
+			LM_ERR("initializing presentity hash table\n");
344
+			return -1;
345
+		}
344 346
 
345
-	if(pres_htable_restore()< 0)
346
-	{
347
-		LM_ERR("filling in presentity hash table from database\n");
348
-		return -1;
347
+		if(pres_htable_restore()< 0)
348
+		{
349
+			LM_ERR("filling in presentity hash table from database\n");
350
+			return -1;
351
+		}
349 352
 	}
350 353
 
351 354
 	startup_time = (int) time(NULL);
... ...
@@ -366,9 +369,14 @@ static int mod_init(void)
366 366
 	/* for legacy, we also keep the fallback2db parameter, but make sure for consistency */
367 367
 	if(fallback2db)
368 368
 	{
369
-		dbmode = DB_FALLBACK;
369
+		if (dbmode == DB_ONLY) 
370
+			LM_ERR( "fallback2db ignored as in DB_ONLY mode\n" );
371
+		else
372
+			dbmode = DB_FALLBACK;
370 373
 	}
371 374
 
375
+	if (dbmode == DB_ONLY)
376
+		LM_INFO( "Database mode set to DB_ONLY\n" );
372 377
 	return 0;
373 378
 }
374 379
 
... ...
@@ -751,7 +759,7 @@ int pres_update_status(subs_t subs, str reason, db_key_t* query_cols,
751 751
 		}
752 752
 		/* save in the list all affected dialogs */
753 753
 		/* if status switches to terminated -> delete dialog */
754
-		if(update_pw_dialogs(&subs, subs.db_flag, subs_array)< 0)
754
+		if(dbmode != DB_ONLY && update_pw_dialogs(&subs, subs.db_flag, subs_array)< 0)
755 755
 		{
756 756
 			LM_ERR( "extracting dialogs from [watcher]=%.*s@%.*s to"
757 757
 				" [presentity]=%.*s\n",	subs.from_user.len, subs.from_user.s,
... ...
@@ -336,8 +336,9 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
336 336
 	if(new_t) 
337 337
 	{
338 338
 		/* insert new record in hash_table */
339
-	
340
-		if(insert_phtable(&pres_uri, presentity->event->evp->type, sphere)< 0)
339
+
340
+		if ( dbmode != DB_ONLY && 
341
+			insert_phtable(&pres_uri, presentity->event->evp->type, sphere)< 0)
341 342
 		{
342 343
 			LM_ERR("inserting record in hash table\n");
343 344
 			goto error;
... ...
@@ -489,7 +490,8 @@ after_dialog_check:
489 489
 
490 490
 				/* delete from hash table */
491 491
 	
492
-				if(delete_phtable(&pres_uri, presentity->event->evp->type)< 0)
492
+				if(dbmode != DB_ONLY && 
493
+					delete_phtable(&pres_uri, presentity->event->evp->type)< 0)
493 494
 				{
494 495
 					LM_ERR("deleting record from hash table\n");
495 496
 					goto error;
... ...
@@ -584,7 +586,8 @@ after_dialog_check:
584 584
 				if(sphere_enable && 
585 585
 						presentity->event->evp->type== EVENT_PRESENCE)
586 586
 				{
587
-					if(update_phtable(presentity, pres_uri, *body)< 0)
587
+					if(dbmode != DB_ONLY && 
588
+						update_phtable(presentity, pres_uri, *body)< 0)
588 589
 					{
589 590
 						LM_ERR("failed to update sphere for presentity\n");
590 591
 						goto error;
... ...
@@ -695,6 +698,12 @@ int pres_htable_restore(void)
695 695
 	event_t ev;
696 696
 	char* sphere= NULL;
697 697
 
698
+	if ( dbmode == DB_ONLY )
699
+	{
700
+		LM_ERR( "Can't restore when dbmode is DB_ONLY\n" );
701
+		return(-1);
702
+	} 
703
+
698 704
 	result_cols[user_col= n_result_cols++]= &str_username_col;
699 705
 	result_cols[domain_col= n_result_cols++]= &str_domain_col;
700 706
 	result_cols[event_col= n_result_cols++]= &str_event_col;
... ...
@@ -871,30 +880,32 @@ char* get_sphere(str* pres_uri)
871 871
 	if(!sphere_enable)
872 872
 		return NULL;
873 873
 
874
-	/* search in hash table*/
875
-	hash_code= core_hash(pres_uri, NULL, phtable_size);
874
+	if ( dbmode != DB_ONLY )
875
+	{
876
+		/* search in hash table*/
877
+		hash_code= core_hash(pres_uri, NULL, phtable_size);
876 878
 
877
-	lock_get(&pres_htable[hash_code].lock);
879
+		lock_get(&pres_htable[hash_code].lock);
878 880
 
879
-	p= search_phtable(pres_uri, EVENT_PRESENCE, hash_code);
881
+		p= search_phtable(pres_uri, EVENT_PRESENCE, hash_code);
880 882
 
881
-	if(p)
882
-	{
883
-		if(p->sphere)
883
+		if(p)
884 884
 		{
885
-			sphere= (char*)pkg_malloc(strlen(p->sphere)* sizeof(char));
886
-			if(sphere== NULL)
885
+			if(p->sphere)
887 886
 			{
888
-				lock_release(&pres_htable[hash_code].lock);
889
-				ERR_MEM(PKG_MEM_STR);
887
+				sphere= (char*)pkg_malloc(strlen(p->sphere)* sizeof(char));
888
+				if(sphere== NULL)
889
+				{
890
+					lock_release(&pres_htable[hash_code].lock);
891
+					ERR_MEM(PKG_MEM_STR);
892
+				}
893
+				strcpy(sphere, p->sphere);
890 894
 			}
891
-			strcpy(sphere, p->sphere);
895
+			lock_release(&pres_htable[hash_code].lock);
896
+			return sphere;
892 897
 		}
893 898
 		lock_release(&pres_htable[hash_code].lock);
894
-		return sphere;
895 899
 	}
896
-	lock_release(&pres_htable[hash_code].lock);
897
-
898 900
 
899 901
 	/* if record not found and subscriptions are held also in database, query database*/
900 902
 	if(dbmode == DB_MEMORY_ONLY)
... ...
@@ -187,7 +187,7 @@ void msg_presentity_clean(unsigned int ticks,void *param)
187 187
 		}
188 188
 		
189 189
 		/* delete from hash table */
190
-		if(delete_phtable(&p[i].uri, ev.type)< 0)
190
+		if(dbmode != DB_ONLY && delete_phtable(&p[i].uri, ev.type)< 0)
191 191
 		{
192 192
 			LM_ERR("deleting from pres hash table\n");
193 193
 			free_event_params(ev.params.list, PKG_MEM_TYPE);