Browse code

- fixed a bug when restoring info from database - fixed a memory leak bug

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2156 689a6050-402a-0410-94f2-e92a70836424

Anca Vamanu authored on 07/05/2007 10:21:06
Showing 8 changed files
... ...
@@ -107,7 +107,7 @@ int pua_add_events()
107 107
 
108 108
 }	
109 109
 
110
-int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
110
+int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple_param)
111 111
 {
112 112
 
113 113
 	xmlDocPtr doc= NULL;
... ...
@@ -117,8 +117,8 @@ int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
117 117
 	char buf[50];
118 118
 	str* body= NULL;
119 119
 	int alloc_tuple= 0;
120
+	str* tuple= NULL;
120 121
 
121
-	tuple= NULL;
122 122
 	doc= xmlParseMemory(publ->body->s, publ->body->len );
123 123
 	if(doc== NULL)
124 124
 	{
... ...
@@ -135,6 +135,8 @@ int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
135 135
 	tuple_id= xmlNodeGetAttrContentByName(node, "id");
136 136
 	if(tuple_id== NULL)
137 137
 	{
138
+		tuple= *(tuple_param);
139
+
138 140
 		if(tuple== NULL)	// generate a tuple_id
139 141
 		{
140 142
 			tuple_id= buf;
... ...
@@ -155,7 +157,12 @@ int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
155 157
 			}
156 158
 			memcpy(tuple->s, tuple_id, tuple_id_len);
157 159
 			tuple->len= tuple_id_len;
160
+
161
+			*tuple_param= tuple;
158 162
 			alloc_tuple= 1;
163
+
164
+			DBG("PUA: pres_process_body: allocated tuple_id\n\n");
165
+
159 166
 		}
160 167
 		else
161 168
 		{
... ...
@@ -256,7 +263,7 @@ error:
256 263
 
257 264
 }	
258 265
 
259
-int bla_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
266
+int bla_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
260 267
 {
261 268
 	xmlNodePtr node= NULL;
262 269
 	xmlDocPtr doc= NULL;
... ...
@@ -320,7 +327,7 @@ error:
320 327
 	return -1;
321 328
 }
322 329
 
323
-int mwi_process_body(publ_info_t* publ, str** fin_body, int ver, str* tuple)
330
+int mwi_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
324 331
 {
325 332
 	*fin_body= publ->body;
326 333
 	return 0;
... ...
@@ -35,8 +35,8 @@
35 35
  * */
36 36
 int pua_add_events();
37 37
 
38
-int pres_process_body(struct publ_info* publ, str** fin_body, int ver, str* tuple);
39
-int bla_process_body (struct publ_info* publ, str** fin_body, int ver, str* tuple);
40
-int mwi_process_body (struct publ_info* publ, str** fin_body, int ver, str* tuple);
38
+int pres_process_body(struct publ_info* publ, str** fin_body, int ver, str** tuple);
39
+int bla_process_body (struct publ_info* publ, str** fin_body, int ver, str** tuple);
40
+int mwi_process_body (struct publ_info* publ, str** fin_body, int ver, str** tuple);
41 41
 
42 42
 #endif
... ...
@@ -33,7 +33,7 @@
33 33
 struct publ_info;
34 34
 
35 35
 typedef int (evs_process_body_t)(struct publ_info* , str** final_body, 
36
-		 int ver, str* tuple);
36
+		 int ver, str** tuple);
37 37
 
38 38
 typedef struct pua_event
39 39
 {
... ...
@@ -35,7 +35,19 @@
35 35
 #include "pua.h"
36 36
 #include "send_publish.h"
37 37
 
38
-
38
+void print_ua_pres(ua_pres_t* p)
39
+{
40
+	DBG("PUA:print_ua_pres: \n");
41
+	DBG("\tpres_uri= %.*s   len= %d\n", p->pres_uri->len, p->pres_uri->s, p->pres_uri->len);
42
+	if(p->watcher_uri)
43
+	{	
44
+		DBG("\twatcher_uri= %.*s  len= %d\n", p->watcher_uri->len, p->watcher_uri->s, p->watcher_uri->len);
45
+		DBG("\tcall_id= %.*s   len= %d\n", p->call_id.len, p->call_id.s, p->call_id.len);
46
+	}	
47
+	else
48
+		DBG("\tetag= %.*s - len= %d\n", p->etag.len, p->etag.s, p->etag.len);
49
+	DBG("\texpires= %ld\n", p->expires- (int)time(NULL));
50
+}	
39 51
 htable_t* new_htable()
40 52
 {
41 53
 	htable_t* H= NULL;
... ...
@@ -182,8 +194,8 @@ void update_htable(ua_pres_t* presentity,time_t desired_expires,
182 194
 
183 195
 	p->expires= expires+ (int)time(NULL);
184 196
 	p->desired_expires= desired_expires;
185
-	if(p->db_flag& NO_UPDATEDB_FLAG)
186
-		p->db_flag= UPDATEDB_FLAG;
197
+		
198
+	p->db_flag= UPDATEDB_FLAG;
187 199
 
188 200
 	if(p->watcher_uri)
189 201
 		p->cseq ++;
... ...
@@ -107,6 +107,9 @@ void destroy_htable();
107 107
 int is_dialog(ua_pres_t* dialog);
108 108
 ua_pres_t* get_dialog(ua_pres_t* dialog, unsigned int hash_code);
109 109
 
110
+/* for degug */
111
+void print_ua_pres(ua_pres_t* p);
112
+
110 113
 typedef int  (*query_dialog_t)(ua_pres_t* presentity);
111 114
 
112 115
 static inline int get_event_flag(str* event)
... ...
@@ -365,11 +365,10 @@ int db_restore()
365 365
 		row = &res->rows[i];
366 366
 		row_vals = ROW_VALUES(row);
367 367
 	
368
-		pres_uri.s= row_vals[0].val.str_val.s;
368
+		pres_uri.s= (char*)row_vals[puri_col].val.string_val;
369 369
 		pres_uri.len = strlen(pres_uri.s);
370
-
371
-		pres_id.s= row_vals[1].val.str_val.s;
372
-		pres_id.len = strlen(pres_id.s);
370
+		
371
+		DBG("PUA: db_restore: pres_uri= %.*s\n", pres_uri.len, pres_uri.s);
373 372
 
374 373
 		memset(&etag,			 0, sizeof(str));
375 374
 		memset(&tuple_id,		 0, sizeof(str));
... ...
@@ -378,43 +377,49 @@ int db_restore()
378 377
 		memset(&to_tag,			 0, sizeof(str));
379 378
 		memset(&from_tag,		 0, sizeof(str));
380 379
 		memset(&record_route,	 0, sizeof(str));
380
+		memset(&pres_id,         0, sizeof(str));
381 381
 	
382
-		if(row_vals[etag_col].val.str_val.s)
382
+		pres_id.s= (char*)row_vals[pid_col].val.string_val;
383
+		if(pres_id.s)
384
+			pres_id.len = strlen(pres_id.s);
385
+
386
+		if(row_vals[etag_col].val.string_val)
383 387
 		{
384
-			etag.s= row_vals[etag_col].val.str_val.s;
388
+			etag.s= (char*)row_vals[etag_col].val.string_val;
385 389
 			etag.len = strlen(etag.s);
386 390
 	
387
-			tuple_id.s= row_vals[tuple_col].val.str_val.s;
391
+			tuple_id.s= (char*)row_vals[tuple_col].val.string_val;
388 392
 			tuple_id.len = strlen(tuple_id.s);
389 393
 		}
390 394
 
391
-		if(row_vals[watcher_col].val.str_val.s)
395
+		if(row_vals[watcher_col].val.string_val)
392 396
 		{	
393
-			watcher_uri.s= row_vals[watcher_col].val.str_val.s;
397
+			watcher_uri.s= (char*)row_vals[watcher_col].val.string_val;
394 398
 			watcher_uri.len = strlen(watcher_uri.s);
395 399
 	
396
-			call_id.s= row_vals[callid_col].val.str_val.s;
400
+			call_id.s= (char*)row_vals[callid_col].val.string_val;
397 401
 			call_id.len = strlen(call_id.s);
398 402
 
399
-			to_tag.s= row_vals[totag_col].val.str_val.s;
403
+			to_tag.s= (char*)row_vals[totag_col].val.string_val;
400 404
 			to_tag.len = strlen(to_tag.s);
401 405
 
402
-			from_tag.s= row_vals[fromtag_col].val.str_val.s;
406
+			from_tag.s= (char*)row_vals[fromtag_col].val.string_val;
403 407
 			from_tag.len = strlen(from_tag.s);
404 408
 
405
-			if(row_vals[record_route_col].val.str_val.s)
409
+			if(row_vals[record_route_col].val.string_val)
406 410
 			{
407
-				record_route.s= row_vals[record_route_col].val.str_val.s;
411
+				record_route.s= (char*)row_vals[record_route_col].val.string_val;
408 412
 				record_route.len= strlen(record_route.s);
409 413
 			}	
410 414
 		}
411 415
 		
412
-		size= sizeof(ua_pres_t)+ sizeof(str)+ pres_uri.len+ pres_id.len;
413
-		if(etag.len)
414
-			size+= etag.len+ tuple_id.len;
415
-		else
416
+		size= sizeof(ua_pres_t)+ sizeof(str)+ pres_uri.len+ pres_id.len+
417
+					tuple_id.len;
418
+		
419
+		if(watcher_uri.s)
416 420
 			size+= sizeof(str)+ watcher_uri.len+ call_id.len+ to_tag.len+
417 421
 				from_tag.len+ record_route.len;
422
+		
418 423
 		p= (ua_pres_t*)shm_malloc(size);
419 424
 		if(p== NULL)
420 425
 		{
... ...
@@ -430,30 +435,24 @@ int db_restore()
430 435
 		memcpy(p->pres_uri->s, pres_uri.s, pres_uri.len);
431 436
 		p->pres_uri->len= pres_uri.len;
432 437
 		size+= pres_uri.len;
433
-
434
-		p->id.s= (char*)p + size;
435
-		memcpy(p->id.s, pres_id.s, pres_id.len);
436
-		p->id.len= pres_id.len;
437
-		size+= pres_id.len;
438
-
439
-		p->expires= row_vals[expires_col].val.int_val;
440
-		p->flag|=	row_vals[flag_col].val.int_val;
441
-		p->db_flag|= INSERTDB_FLAG;
442
-
443
-		if(etag.len)
438
+		
439
+		if(pres_id.s)
440
+		{	
441
+			p->id.s= (char*)p + size;
442
+			memcpy(p->id.s, pres_id.s, pres_id.len);
443
+			p->id.len= pres_id.len;
444
+			size+= pres_id.len;
445
+		}
446
+		if(tuple_id.s && tuple_id.len)
444 447
 		{
445
-			p->etag.s= (char*)p+ size;
446
-			memcpy(p->etag.s, etag.s, etag.len);
447
-			p->etag.len= etag.len;
448
-			size+= etag.len;
449
-			
450 448
 			p->tuple_id.s= (char*)p + size;
451 449
 			memcpy(p->tuple_id.s, tuple_id.s, tuple_id.len);
452 450
 			p->tuple_id.len= tuple_id.len;
453 451
 			size+= tuple_id.len;
454
-		}
455
-		else
456
-		{
452
+		}	
453
+
454
+		if(watcher_uri.s && watcher_uri.len)
455
+		{	
457 456
 			p->watcher_uri= (str*)((char*)p+ size);
458 457
 			size+= sizeof(str);
459 458
 
... ...
@@ -486,10 +485,30 @@ int db_restore()
486 485
 			}	
487 486
 			p->cseq= row_vals[cseq_col].val.int_val;
488 487
 		}
488
+		
489 489
 		p->event= row_vals[event_col].val.int_val;
490
+		p->expires= row_vals[expires_col].val.int_val;
491
+		p->flag|=	row_vals[flag_col].val.int_val;
492
+		p->db_flag|= INSERTDB_FLAG;
490 493
 
494
+		memset(&p->etag, 0, sizeof(str));
495
+		if(etag.s && etag.len)
496
+		{
497
+			/* alloc separately */
498
+			p->etag.s= (char*)shm_malloc(etag.len* sizeof(char));
499
+			if(p->etag.s==  NULL)
500
+			{
501
+				LOG(L_ERR, "pua:db_restore:ERROR while aloocating memory\n");
502
+				goto error;
503
+			}	
504
+			memcpy(p->etag.s, etag.s, etag.len);
505
+			p->etag.len= etag.len;
506
+		}
507
+
508
+		print_ua_pres(p);
491 509
 		insert_htable(p);
492 510
 	}
511
+
493 512
 	if(res)
494 513
 	{
495 514
 		pua_dbf.free_result(pua_db, res);
... ...
@@ -527,6 +546,7 @@ void hashT_clean(unsigned int ticks,void *param)
527 546
 		p= HashT->p_records[i].entity->next;
528 547
 		while(p)
529 548
 		{	
549
+			print_ua_pres(p);
530 550
 			if(p->expires- update_period < now )
531 551
 			{
532 552
 				if((p->desired_expires> p->expires + min_expires) || 
... ...
@@ -589,7 +609,7 @@ int update_pua(ua_pres_t* p, unsigned int hash_code)
589 609
 			LOG(L_ERR, "PUA: update_pua: ERROR while constructing publ callback param\n");
590 610
 			goto error;
591 611
 		}	
592
-		result= tmb.t_request(&met,						/* Type of the message */
612
+		result= tmb.t_request(&met,				/* Type of the message */
593 613
 				p->pres_uri,					/* Request-URI */
594 614
 				p->pres_uri,					/* To */
595 615
 				p->pres_uri,					/* From */
... ...
@@ -843,7 +863,7 @@ void db_update(unsigned int ticks,void *param)
843 863
 						}
844 864
 						pua_dbf.free_result(pua_db, res);
845 865
 						res= NULL;
846
-
866
+						break;		
847 867
 					}
848 868
 					else
849 869
 					{
... ...
@@ -854,8 +874,8 @@ void db_update(unsigned int ticks,void *param)
854 874
 						}
855 875
 						DBG("PUA:db_update: UPDATEDB_FLAG and no record"
856 876
 								" found\n");
877
+						p->db_flag= INSERTDB_FLAG;
857 878
 					}	
858
-					break;		
859 879
 				}
860 880
 				
861 881
 				case INSERTDB_FLAG:
... ...
@@ -902,7 +922,7 @@ void db_update(unsigned int ticks,void *param)
902 922
 			lock_release(&HashT->p_records[i].lock);	
903 923
 	}
904 924
 
905
-	db_vals[0].val.int_val= (int)time(NULL);
925
+	db_vals[0].val.int_val= (int)time(NULL)- 10;
906 926
 	db_ops[0]= OP_LT;
907 927
 	if(pua_dbf.delete(pua_db, db_cols, db_ops, db_vals, 1) < 0)
908 928
 	{
... ...
@@ -920,21 +940,20 @@ treq_cbparam_t* build_uppubl_cbparam(ua_pres_t* pres)
920 940
 	treq_cbparam_t* cb_param= NULL;
921 941
 	
922 942
 	size= sizeof(treq_cbparam_t)+ sizeof(str)*2+ (pres->pres_uri->len+ 
923
-		+ pres->content_type.len+ pres->id.len+ pres->etag.len+
924
-		pres->tuple_id.len+ 1)*sizeof(char); 
943
+		+ pres->id.len+ pres->etag.len+ pres->tuple_id.len)*sizeof(char); 
925 944
 
926
-	DBG("PUA: send_publish: before allocating size= %d\n", size);
945
+	DBG("PUA: build_uppubl_cbparam: before allocating size= %d\n", size);
927 946
 	cb_param= (treq_cbparam_t*)shm_malloc(size);
928 947
 	if(cb_param== NULL)
929 948
 	{
930
-		LOG(L_ERR, "PUA: send_publish: ERROR no more share memory while"
949
+		LOG(L_ERR, "PUA: build_uppubl_cbparam: ERROR no more share memory while"
931 950
 				" allocating cb_param - size= %d\n", size);
932 951
 		return NULL;
933 952
 	}
934 953
 	memset(cb_param, 0, size);
935 954
 	memset(&cb_param->publ, 0, sizeof(publ_info_t));
936 955
 	size =  sizeof(treq_cbparam_t);
937
-	DBG("PUA: send_publish: size= %d\n", size);
956
+	DBG("PUA: build_uppubl_cbparam: size= %d\n", size);
938 957
 
939 958
 	cb_param->publ.pres_uri = (str*)((char*)cb_param + size);
940 959
 	size+= sizeof(str);
... ...
@@ -966,7 +985,7 @@ treq_cbparam_t* build_uppubl_cbparam(ua_pres_t* pres)
966 985
 	cb_param->tuple_id.len= pres->tuple_id.len;
967 986
 	size+= pres->tuple_id.len;
968 987
 	
969
-	DBG("PUA:send_publish: after allocating: size= %d\n", size);
988
+	DBG("PUA:build_uppubl_cbparam: after allocating: size= %d\n", size);
970 989
 	cb_param->publ.event= pres->event;
971 990
 	cb_param->publ.source_flag|= pres->flag;
972 991
 	cb_param->publ.expires= pres->expires;
... ...
@@ -979,14 +998,8 @@ ua_pres_t* build_upsubs_cbparam(ua_pres_t* p)
979 998
 	ua_pres_t* hentity;
980 999
 	int size;
981 1000
 
982
-	size= sizeof(ua_pres_t)+ sizeof(str)+ (p->pres_uri->len+ 1)*sizeof(char);
1001
+	size= sizeof(ua_pres_t)+ sizeof(str)*2+ (p->pres_uri->len+ p->watcher_uri->len)*sizeof(char);
983 1002
 	
984
-	if(p->watcher_uri)
985
-		size+= sizeof(str)+ p->watcher_uri->len*sizeof(char);
986
-	else
987
-	if(p->id.s && p->id.len)
988
-		size+=  p->id.len*sizeof(char);
989
-
990 1003
 	hentity= (ua_pres_t*)shm_malloc(size);
991 1004
 	if(hentity== NULL)
992 1005
 	{
... ...
@@ -1006,23 +1019,13 @@ ua_pres_t* build_upsubs_cbparam(ua_pres_t* p)
1006 1019
 	hentity->pres_uri->len= p->pres_uri->len;
1007 1020
 	size+= p->pres_uri->len;
1008 1021
 		
1009
-	if(p->watcher_uri)
1010
-	{
1011
-		hentity->watcher_uri=(str*)((char*)hentity+ size);
1012
-		size+= sizeof(str);
1013
-		hentity->watcher_uri->s= (char*)hentity+ size;
1014
-		memcpy(hentity->watcher_uri->s, p->watcher_uri->s,p->watcher_uri->len);
1015
-		hentity->watcher_uri->len= p->watcher_uri->len;
1016
-		size+= p->watcher_uri->len;
1017
-	}	
1018
-	else
1019
-	if(p->id.s && p->id.len)
1020
-	{	
1021
-		hentity->id.s = ((char*)hentity+ size);
1022
-		memcpy(hentity->id.s, p->id.s, p->id.len);
1023
-		hentity->id.len= p->id.len;
1024
-		size+= p->id.len;
1025
-	}
1022
+	hentity->watcher_uri=(str*)((char*)hentity+ size);
1023
+	size+= sizeof(str);
1024
+	hentity->watcher_uri->s= (char*)hentity+ size;
1025
+	memcpy(hentity->watcher_uri->s, p->watcher_uri->s,p->watcher_uri->len);
1026
+	hentity->watcher_uri->len= p->watcher_uri->len;
1027
+	size+= p->watcher_uri->len;
1028
+	
1026 1029
 	hentity->flag|= p->flag;
1027 1030
 	
1028 1031
 	return hentity;
... ...
@@ -317,6 +317,8 @@ void publ_cback_func(struct cell *t, int type, struct tmcb_params *ps)
317 317
 		goto error;
318 318
 	}	
319 319
 	memset(presentity, 0, size);
320
+	memset(&presentity->etag, 0, sizeof(str));
321
+
320 322
 	size= sizeof(ua_pres_t);
321 323
 	presentity->pres_uri= (str*)((char*)presentity+ size);
322 324
 	size+= sizeof(str);
... ...
@@ -507,7 +509,7 @@ insert:
507 509
 
508 510
 	if(publ->body && publ->body->s)
509 511
 	{
510
-		ret_code= ev->process_body(publ, &body, ver, tuple_id);
512
+		ret_code= ev->process_body(publ, &body, ver, &tuple_id );
511 513
 		if( ret_code< 0 || body== NULL)
512 514
 		{
513 515
 			LOG(L_ERR, "PUA:send_publish: ERROR while processing body\n");
... ...
@@ -516,6 +518,9 @@ insert:
516 518
 			goto error;
517 519
 		}
518 520
 	}
521
+	if(tuple_id)
522
+		DBG("\n\nPUA:send_publish: tuple_id= %.*s\n\n", tuple_id->len, tuple_id->s  );
523
+	
519 524
 	/* construct the callback parameter */
520 525
 
521 526
 	size= sizeof(treq_cbparam_t)+ sizeof(str)+ (publ->pres_uri->len+ 
... ...
@@ -58,7 +58,7 @@ typedef struct treq_cbparam
58 58
 
59 59
 typedef int (*send_publish_t)(publ_info_t* publ);
60 60
 int send_publish( publ_info_t* publ );
61
-void print_ua_pres(ua_pres_t* p);
61
+
62 62
 void publ_cback_func(struct cell *t, int type, struct tmcb_params *ps);
63 63
 str* publ_build_hdr(int expires, pua_event_t* event, str* content_type, str* etag,
64 64
 		str* extra_headers, int is_body);