Browse code

presence: option to set priority for presentity documents

- xavp_cfg - new parameter to specify the name of xavp use to store
attributes for publish processing
- priority can be set inside xavp_cfg with $xavp(xavp_cfg=>priority)
- priority is stored in a new column inside database table presentity
for each publish that has the xavp set
- retrieve_order - new parameter to specify the order to retrieve the
records from database. Default value is 0 (retrieve by received_time
like so far). If set to 1, retrieve by priority value
- if xavp_cfg parameter is set but priority field inside it is not, then
inside the database is stored a value based on timestamp so the newest
records will have a higher value, preserving the old behaviour even
the retrieve_order=1

Daniel-Constantin Mierla authored on 26/03/2015 19:43:45
Showing 6 changed files
... ...
@@ -94,6 +94,7 @@ str str_id_col = str_init("id");
94 94
 str str_sender_col = str_init("sender");
95 95
 str str_updated_col = str_init("updated");
96 96
 str str_updated_winfo_col = str_init("updated_winfo");
97
+str str_priority_col = str_init("priority");
97 98
 
98 99
 int subset=0;
99 100
 
... ...
@@ -96,6 +96,7 @@ extern str str_id_col;
96 96
 extern str str_sender_col;
97 97
 extern str str_updated_col;
98 98
 extern str str_updated_winfo_col;
99
+extern str str_priority_col;
99 100
 
100 101
 void PRINT_DLG(FILE* out, dlg_t* _d);
101 102
 
... ...
@@ -149,6 +149,8 @@ int publ_cache_enabled = 1;
149 149
 int pres_waitn_time = 5;
150 150
 int pres_notifier_poll_rate = 10;
151 151
 int pres_notifier_processes = 1;
152
+str pres_xavp_cfg = {0};
153
+int pres_retrieve_order = 0;
152 154
 
153 155
 int db_table_lock_type = 1;
154 156
 db_locking_t db_table_lock = DB_LOCKING_WRITE;
... ...
@@ -208,6 +210,8 @@ static param_export_t params[]={
208 208
 	{ "db_table_lock_type",     INT_PARAM, &db_table_lock_type},
209 209
 	{ "local_log_level",        PARAM_INT, &pres_local_log_level},
210 210
 	{ "subs_remove_match",      PARAM_INT, &pres_subs_remove_match},
211
+	{ "xavp_cfg",               PARAM_STR, &pres_xavp_cfg},
212
+	{ "retrieve_order",         PARAM_INT, &pres_retrieve_order},
211 213
     {0,0,0}
212 214
 };
213 215
 
... ...
@@ -88,6 +88,8 @@ extern int pres_fetch_rows;
88 88
 extern int pres_waitn_time;
89 89
 extern int pres_notifier_poll_rate;
90 90
 extern int pres_notifier_processes;
91
+extern str pres_xavp_cfg;
92
+extern int pres_retrieve_order;
91 93
 
92 94
 extern int phtable_size;
93 95
 extern phtable_t* pres_htable;
... ...
@@ -35,8 +35,8 @@
35 35
 #include "../../hashes.h"
36 36
 #include "../../dprint.h"
37 37
 #include "../../mem/shm_mem.h"
38
+#include "../../xavp.h"
38 39
 #include "../../str.h"
39
-#include "../alias_db/alias_db.h"
40 40
 #include "../../data_lump_rpl.h"
41 41
 #include "presentity.h"
42 42
 #include "presence.h" 
... ...
@@ -46,6 +46,9 @@
46 46
 #include "utils_func.h"
47 47
 
48 48
 
49
+/* base priority value (20150101T000000) */
50
+#define PRES_PRIORITY_TBASE	1420070400
51
+
49 52
 xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
50 53
 													const char *ns);
51 54
 static str pu_200_rpl  = str_init("OK");
... ...
@@ -164,7 +167,31 @@ error:
164 164
 		pkg_free(hdr_append2.s);
165 165
 
166 166
 	return -1;
167
-}	
167
+}
168
+
169
+/**
170
+ * get priority value for presence document
171
+ */
172
+unsigned int pres_get_priority(void)
173
+{
174
+	sr_xavp_t *vavp = NULL;
175
+	str vname = str_init("priority");
176
+
177
+	if(pres_xavp_cfg.s==NULL || pres_xavp_cfg.len<=0) {
178
+		return 0;
179
+	}
180
+
181
+	vavp = xavp_get_child_with_ival(&pres_xavp_cfg, &vname);
182
+	if(vavp!=NULL) {
183
+		return (unsigned int)vavp->val.v.i;
184
+	}
185
+
186
+	return (unsigned int)(time(NULL) - PRES_PRIORITY_TBASE);
187
+}
188
+
189
+/**
190
+ * create new presentity record
191
+ */
168 192
 presentity_t* new_presentity( str* domain,str* user,int expires, 
169 193
 		pres_ev_t* event, str* etag, str* sender)
170 194
 {
... ...
@@ -221,6 +248,7 @@ presentity_t* new_presentity( str* domain,str* user,int expires,
221 221
 	presentity->event= event;
222 222
 	presentity->expires = expires;
223 223
 	presentity->received_time= (int)time(NULL);
224
+	presentity->priority = pres_get_priority();
224 225
 	return presentity;
225 226
     
226 227
 error:
... ...
@@ -268,9 +296,9 @@ int check_if_dialog(str body, int *is_dialog)
268 268
 int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
269 269
 		int new_t, int* sent_reply, char* sphere)
270 270
 {
271
-	db_key_t query_cols[12], update_keys[8], result_cols[5];
272
-	db_op_t  query_ops[12];
273
-	db_val_t query_vals[12], update_vals[8];
271
+	db_key_t query_cols[13], update_keys[9], result_cols[6];
272
+	db_op_t  query_ops[13];
273
+	db_val_t query_vals[13], update_vals[9];
274 274
 	db1_res_t *result= NULL;
275 275
 	int n_query_cols = 0;
276 276
 	int n_update_cols = 0;
... ...
@@ -375,6 +403,12 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
375 375
 		query_vals[n_query_cols].nul = 0;
376 376
 		query_vals[n_query_cols].val.int_val = presentity->received_time;
377 377
 		n_query_cols++;
378
+
379
+		query_cols[n_query_cols] = &str_priority_col;
380
+		query_vals[n_query_cols].type = DB1_INT;
381
+		query_vals[n_query_cols].nul = 0;
382
+		query_vals[n_query_cols].val.int_val = presentity->priority;
383
+		n_query_cols++;
378 384
 		
379 385
 		if (presentity->expires != -1)
380 386
 		{
... ...
@@ -677,6 +711,12 @@ after_dialog_check:
677 677
 		update_vals[n_update_cols].val.int_val= presentity->received_time;
678 678
 		n_update_cols++;
679 679
 
680
+		update_keys[n_update_cols] = &str_priority_col;
681
+		update_vals[n_update_cols].type = DB1_INT;
682
+		update_vals[n_update_cols].nul = 0;
683
+		update_vals[n_update_cols].val.int_val= presentity->priority;
684
+		n_update_cols++;
685
+
680 686
 		if(body && body->s)
681 687
 		{
682 688
 			update_keys[n_update_cols] = &str_body_col;
... ...
@@ -1088,7 +1128,11 @@ char* get_sphere(str* pres_uri)
1088 1088
 		return NULL;
1089 1089
 	}
1090 1090
 
1091
-	query_str = str_received_time_col;
1091
+	if(pres_retrieve_order==1) {
1092
+		query_str = str_priority_col;
1093
+	} else {
1094
+		query_str = str_received_time_col;
1095
+	}
1092 1096
 	if (pa_dbf.query (pa_db, query_cols, 0, query_vals,
1093 1097
 		 result_cols, n_query_cols, n_result_cols, &query_str ,  &result) < 0) 
1094 1098
 	{
... ...
@@ -46,6 +46,7 @@ typedef struct presentity
46 46
 	str* sender;
47 47
 	time_t expires;
48 48
 	time_t received_time;
49
+	unsigned int priority;
49 50
 } presentity_t;
50 51
 
51 52
 /* create new presentity */