Browse code

- ability to load destination URI flags from database at startup/reload - it preserves backward compatibility (if the table has old version structure, the module won't load the flags column)

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

Daniel-Constantin Mierla authored on 07/02/2008 13:00:51
Showing 3 changed files
... ...
@@ -65,6 +65,9 @@
65 65
 #include "dispatch.h"
66 66
 
67 67
 #define DS_TABLE_VERSION	1
68
+#define DS_TABLE_VERSION2	2
69
+
70
+static int _ds_table_version = DS_TABLE_VERSION;
68 71
 
69 72
 typedef struct _ds_dest
70 73
 {
... ...
@@ -406,7 +409,6 @@ void ds_disconnect_db(void)
406 409
 /*initialize and verify DB stuff*/
407 410
 int init_ds_db(void)
408 411
 {
409
-	int ver;
410 412
 	int ret;
411 413
 
412 414
 	if(ds_table_name.s == 0)
... ...
@@ -428,15 +430,16 @@ int init_ds_db(void)
428 430
 		return -1;
429 431
 	}
430 432
 	
431
-	ver = db_table_version(&ds_dbf, ds_db_handle, &ds_table_name);
432
-	if (ver < 0) 
433
+	_ds_table_version = db_table_version(&ds_dbf, ds_db_handle, &ds_table_name);
434
+	if (_ds_table_version < 0) 
433 435
 	{
434 436
 		LM_ERR("failed to query table version\n");
435 437
 		return -1;
436
-	} else if (ver != DS_TABLE_VERSION) {
437
-		LM_ERR("invalid table version (found %d , required %d)\n"
438
+	} else if (_ds_table_version != DS_TABLE_VERSION
439
+			&& _ds_table_version != DS_TABLE_VERSION2) {
440
+		LM_ERR("invalid table version (found %d , required %d or %d)\n"
438 441
 			"(use openser_mysql.sh reinstall)\n",
439
-			ver, DS_TABLE_VERSION );
442
+			_ds_table_version, DS_TABLE_VERSION, DS_TABLE_VERSION2 );
440 443
 		return -1;
441 444
 	}
442 445
 
... ...
@@ -452,13 +455,19 @@ int ds_load_db(void)
452 455
 {
453 456
 	int i, id, nr_rows, setn;
454 457
 	int flags;
458
+	int nrcols;
455 459
 	str uri;
456 460
 	db_res_t * res;
457 461
 	db_val_t * values;
458 462
 	db_row_t * rows;
459 463
 	
460
-	db_key_t query_cols[2] = {&ds_set_id_col, &ds_dest_uri_col};
464
+	db_key_t query_cols[3] = {&ds_set_id_col, &ds_dest_uri_col,
465
+								&ds_dest_flags_col};
461 466
 	
467
+	nrcols = 2;
468
+	if(_ds_table_version == DS_TABLE_VERSION2)
469
+		nrcols = 3;
470
+
462 471
 	if( (*crt_idx) != (*next_idx))
463 472
 	{
464 473
 		LM_WARN("load command already generated, aborting reload...\n");
... ...
@@ -477,7 +486,7 @@ int ds_load_db(void)
477 486
 	}
478 487
 
479 488
 	/*select the whole table and all the columns*/
480
-	if(ds_dbf.query(ds_db_handle,0,0,0,query_cols, 0, 2, 0, &res) < 0)
489
+	if(ds_dbf.query(ds_db_handle,0,0,0,query_cols,0,nrcols,0,&res) < 0)
481 490
 	{
482 491
 		LM_ERR("error while querying database\n");
483 492
 		return -1;
... ...
@@ -487,7 +496,7 @@ int ds_load_db(void)
487 496
 	rows 	= RES_ROWS(res);
488 497
 	if(nr_rows == 0)
489 498
 	{
490
-		LM_WARN("no dispatching data in the db, use an empty destination set\n");
499
+		LM_WARN("no dispatching data in the db -- empty destination set\n");
491 500
 		ds_dbf.free_result(ds_db_handle, res);
492 501
 		return 0;
493 502
 	}
... ...
@@ -504,6 +513,8 @@ int ds_load_db(void)
504 513
 		uri.s = VAL_STR(values+1).s;
505 514
 		uri.len = strlen(uri.s);
506 515
 		flags = 0;
516
+		if(nrcols==3)
517
+			flags = VAL_INT(values+2);
507 518
 
508 519
 		if(add_dest2list(id, uri, flags, *next_idx, &setn) != 0)
509 520
 			goto err2;
... ...
@@ -52,6 +52,7 @@ extern str ds_db_url;
52 52
 extern str ds_table_name;
53 53
 extern str ds_set_id_col;
54 54
 extern str ds_dest_uri_col;
55
+extern str ds_dest_flags_col;
55 56
 
56 57
 typedef struct _ds_param
57 58
 {
... ...
@@ -56,7 +56,7 @@ MODULE_VERSION
56 56
 
57 57
 #define DS_SET_ID_COL		"setid"
58 58
 #define DS_DEST_URI_COL		"destination"
59
-#define DS_FLAGS_COL		"flags"
59
+#define DS_DEST_FLAGS_COL	"flags"
60 60
 #define DS_TABLE_NAME 		"dispatcher"
61 61
 
62 62
 /** parameters */
... ...
@@ -81,10 +81,11 @@ str ds_ping_from   = {"sip:dispatcher@localhost", 24};
81 81
 static int ds_ping_interval = 0;
82 82
 
83 83
 /*db */
84
-str ds_db_url       = {NULL, 0};
85
-str ds_set_id_col   = str_init(DS_SET_ID_COL);
86
-str ds_dest_uri_col = str_init(DS_DEST_URI_COL);
87
-str ds_table_name   = str_init(DS_TABLE_NAME);
84
+str ds_db_url         = {NULL, 0};
85
+str ds_set_id_col     = str_init(DS_SET_ID_COL);
86
+str ds_dest_uri_col   = str_init(DS_DEST_URI_COL);
87
+str ds_dest_flags_col = str_init(DS_DEST_FLAGS_COL);
88
+str ds_table_name     = str_init(DS_TABLE_NAME);
88 89
 
89 90
 
90 91
 /** module functions */
... ...
@@ -130,6 +131,7 @@ static param_export_t params[]={
130 131
 	{"table_name", 	    STR_PARAM, &ds_table_name.s},
131 132
 	{"setid_col",       STR_PARAM, &ds_set_id_col.s},
132 133
 	{"destination_col", STR_PARAM, &ds_dest_uri_col.s},
134
+	{"flags_col",       STR_PARAM, &ds_dest_flags_col.s},
133 135
 	{"force_dst",       INT_PARAM, &ds_force_dst},
134 136
 	{"flags",           INT_PARAM, &ds_flags},
135 137
 	{"use_default",     INT_PARAM, &ds_use_default},
... ...
@@ -173,19 +175,17 @@ struct module_exports exports= {
173 175
  */
174 176
 static int mod_init(void)
175 177
 {
176
-	if (ds_db_url.s)
177
-		ds_db_url.len     = strlen(ds_db_url.s);
178
-	ds_table_name.len = strlen(ds_table_name.s);
178
+	pv_spec_t avp_spec;
179
+
179 180
 	if (dst_avp_param.s)
180
-	dst_avp_param.len = strlen(dst_avp_param.s);
181
+		dst_avp_param.len = strlen(dst_avp_param.s);
181 182
 	if (grp_avp_param.s)
182
-	grp_avp_param.len = strlen(grp_avp_param.s);
183
+		grp_avp_param.len = strlen(grp_avp_param.s);
183 184
 	if (cnt_avp_param.s)
184
-	cnt_avp_param.len = strlen(cnt_avp_param.s);	
185
+		cnt_avp_param.len = strlen(cnt_avp_param.s);	
185 186
 	if (ds_ping_from.s) ds_ping_from.len = strlen(ds_ping_from.s);
186 187
 	if (ds_ping_method.s) ds_ping_method.len = strlen(ds_ping_method.s);
187 188
 
188
-	pv_spec_t avp_spec;
189 189
 	LM_DBG("initializing ...\n");
190 190
 
191 191
 	if(init_data()!= 0)
... ...
@@ -193,6 +193,12 @@ static int mod_init(void)
193 193
 
194 194
 	if(ds_db_url.s)
195 195
 	{
196
+		ds_db_url.len     = strlen(ds_db_url.s);
197
+		ds_table_name.len = strlen(ds_table_name.s);
198
+		ds_set_id_col.len     = strlen(ds_set_id_col.s);
199
+		ds_dest_uri_col.len   = strlen(ds_dest_uri_col.s);
200
+		ds_dest_flags_col.len = strlen(ds_dest_flags_col.s);
201
+
196 202
 		if(init_ds_db()!= 0)
197 203
 		{
198 204
 			LM_ERR("could not initiate a connect to the database\n");
... ...
@@ -212,13 +218,15 @@ static int mod_init(void)
212 218
 		if (pv_parse_spec(&dst_avp_param, &avp_spec)==0
213 219
 				|| avp_spec.type!=PVT_AVP)
214 220
 		{
215
-			LM_ERR("malformed or non AVP %.*s AVP definition\n", dst_avp_param.len, dst_avp_param.s);
221
+			LM_ERR("malformed or non AVP %.*s AVP definition\n",
222
+					dst_avp_param.len, dst_avp_param.s);
216 223
 			return -1;
217 224
 		}
218 225
 
219 226
 		if(pv_get_avp_name(0, &(avp_spec.pvp), &dst_avp_name, &dst_avp_type)!=0)
220 227
 		{
221
-			LM_ERR("[%.*s]- invalid AVP definition\n", dst_avp_param.len, dst_avp_param.s);
228
+			LM_ERR("[%.*s]- invalid AVP definition\n", dst_avp_param.len,
229
+					dst_avp_param.s);
222 230
 			return -1;
223 231
 		}
224 232
 	} else {
... ...
@@ -230,13 +238,15 @@ static int mod_init(void)
230 238
 		if (pv_parse_spec(&grp_avp_param, &avp_spec)==0
231 239
 				|| avp_spec.type!=PVT_AVP)
232 240
 		{
233
-			LM_ERR("malformed or non AVP %.*s AVP definition\n", grp_avp_param.len, grp_avp_param.s);
241
+			LM_ERR("malformed or non AVP %.*s AVP definition\n",
242
+					grp_avp_param.len, grp_avp_param.s);
234 243
 			return -1;
235 244
 		}
236 245
 
237 246
 		if(pv_get_avp_name(0, &(avp_spec.pvp), &grp_avp_name, &grp_avp_type)!=0)
238 247
 		{
239
-			LM_ERR("[%.*s]- invalid AVP definition\n", grp_avp_param.len, grp_avp_param.s);
248
+			LM_ERR("[%.*s]- invalid AVP definition\n", grp_avp_param.len,
249
+					grp_avp_param.s);
240 250
 			return -1;
241 251
 		}
242 252
 	} else {
... ...
@@ -248,13 +258,15 @@ static int mod_init(void)
248 258
 		if (pv_parse_spec(&cnt_avp_param, &avp_spec)==0
249 259
 				|| avp_spec.type!=PVT_AVP)
250 260
 		{
251
-			LM_ERR("malformed or non AVP %.*s AVP definition\n", cnt_avp_param.len, cnt_avp_param.s);
261
+			LM_ERR("malformed or non AVP %.*s AVP definition\n",
262
+					cnt_avp_param.len, cnt_avp_param.s);
252 263
 			return -1;
253 264
 		}
254 265
 
255 266
 		if(pv_get_avp_name(0, &(avp_spec.pvp), &cnt_avp_name, &cnt_avp_type)!=0)
256 267
 		{
257
-			LM_ERR("[%.*s]- invalid AVP definition\n", cnt_avp_param.len, cnt_avp_param.s);
268
+			LM_ERR("[%.*s]- invalid AVP definition\n", cnt_avp_param.len,
269
+					cnt_avp_param.s);
258 270
 			return -1;
259 271
 		}
260 272
 	} else {