Browse code

- added some KEYs to presence tables to improve db query performance (table version increased) - did some cleanup (corrected some log messages, a bit of restructuring )

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

Anca Vamanu authored on 19/07/2007 14:34:45
Showing 12 changed files
... ...
@@ -293,7 +293,7 @@ if(method=="SUBSCRIBE")
293 293
 use openser;
294 294
 
295 295
 CREATE TABLE `presentity` (
296
-  `id` int(10) NOT NULL auto_increment,
296
+  `id` int(10)  NOT NULL auto_increment,
297 297
   `username` varchar(64) NOT NULL,
298 298
   `domain` varchar(124) NOT NULL,
299 299
   `event` varchar(64) NOT NULL,
... ...
@@ -301,10 +301,13 @@ CREATE TABLE `presentity` (
301 301
   `expires` int(11) NOT NULL,
302 302
   `received_time` int(11) NOT NULL,
303 303
   `body` text NOT NULL,
304
-  UNIQUE KEY udee_presentity (`username`,`domain`,`event`,`etag`),
305
-  PRIMARY KEY (id)
304
+  UNIQUE KEY `uk_id` (`id`),
305
+  UNIQUE KEY `udee_presentity` (`username`,`domain`,`event`, `etag`),
306
+  KEY `ude_presentity` (`username`,`domain`,`event`),
307
+  KEY `exp_presentity` (`expires`)
306 308
 ) ENGINE=MyISAM;
307 309
 
310
+
308 311
 CREATE TABLE `active_watchers` (
309 312
   `id` int(10) NOT NULL auto_increment,
310 313
   `pres_user` varchar(64) NOT NULL,
... ...
@@ -314,7 +317,7 @@ CREATE TABLE `active_watchers` (
314 317
   `from_user` varchar(64) NOT NULL,
315 318
   `from_domain` varchar(128) NOT NULL,
316 319
   `event` varchar(64) NOT NULL default 'presence',
317
-  `event_id` varchar(64),
320
+  `event_id` varchar(64) default NULL,
318 321
   `to_tag` varchar(128) NOT NULL,
319 322
   `from_tag` varchar(128) NOT NULL,
320 323
   `callid` varchar(128) NOT NULL,
... ...
@@ -329,8 +332,9 @@ CREATE TABLE `active_watchers` (
329 332
   `local_contact` varchar(255) NOT NULL,
330 333
   PRIMARY KEY  (`id`),
331 334
   UNIQUE KEY `tt_watchers` (`to_tag`),
332
-  KEY `due_activewatchers` (`to_domain`,`to_user`,`event`)
333
-) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
335
+  KEY `ude_active_watchers` (`pres_domain`,`pres_user`,`event`),
336
+  KEY `exp_active_watchers` (`expires`)
337
+) ENGINE=MyISAM;
334 338
 
335 339
 
336 340
 CREATE TABLE `watchers` (
... ...
@@ -35,12 +35,9 @@ int bind_presence(event_api_t* api)
35 35
 		LOG(L_ERR, "NOTIFIER:bind_notifier: Invalid parameter value\n");
36 36
 		return -1;
37 37
 	}
38
-	api->add_event = (add_event_t )find_export("add_event", 1, 0);
39
-	if(!api->add_event)
40
-	{
41
-		LOG(L_ERR, "NOTIFIER:bind_notifier: Can't bind add_event\n");
42
-		return -1;
43
-	}
38
+	
39
+	api->add_event = add_event;
40
+	api->contains_event= contains_event;
44 41
 
45 42
 	return 0;
46 43
 }
... ...
@@ -33,6 +33,7 @@
33 33
 
34 34
 typedef struct event_api {
35 35
 	add_event_t add_event;
36
+	contains_event_t contains_event;
36 37
 } event_api_t;
37 38
 
38 39
 int bind_presence(event_api_t* api);
... ...
@@ -2,7 +2,7 @@
2 2
 use openser;
3 3
 
4 4
 CREATE TABLE `presentity` (
5
-  `id` int(10) NOT NULL auto_increment,
5
+  `id` int(10)  NOT NULL auto_increment,
6 6
   `username` varchar(64) NOT NULL,
7 7
   `domain` varchar(124) NOT NULL,
8 8
   `event` varchar(64) NOT NULL,
... ...
@@ -10,10 +10,13 @@ CREATE TABLE `presentity` (
10 10
   `expires` int(11) NOT NULL,
11 11
   `received_time` int(11) NOT NULL,
12 12
   `body` text NOT NULL,
13
-  UNIQUE KEY udee_presentity (`username`,`domain`,`event`,`etag`),
14
-  PRIMARY KEY (id)
13
+  UNIQUE KEY `uk_id` (`id`),
14
+  UNIQUE KEY `udee_presentity` (`username`,`domain`,`event`, `etag`),
15
+  KEY `ude_presentity` (`username`,`domain`,`event`),
16
+  KEY `exp_presentity` (`expires`)
15 17
 ) ENGINE=MyISAM;
16 18
 
19
+
17 20
 CREATE TABLE `active_watchers` (
18 21
   `id` int(10) NOT NULL auto_increment,
19 22
   `pres_user` varchar(64) NOT NULL,
... ...
@@ -23,7 +26,7 @@ CREATE TABLE `active_watchers` (
23 26
   `from_user` varchar(64) NOT NULL,
24 27
   `from_domain` varchar(128) NOT NULL,
25 28
   `event` varchar(64) NOT NULL default 'presence',
26
-  `event_id` varchar(64),
29
+  `event_id` varchar(64) default NULL,
27 30
   `to_tag` varchar(128) NOT NULL,
28 31
   `from_tag` varchar(128) NOT NULL,
29 32
   `callid` varchar(128) NOT NULL,
... ...
@@ -38,8 +41,9 @@ CREATE TABLE `active_watchers` (
38 41
   `local_contact` varchar(255) NOT NULL,
39 42
   PRIMARY KEY  (`id`),
40 43
   UNIQUE KEY `tt_watchers` (`to_tag`),
41
-  KEY `due_activewatchers` (`to_domain`,`to_user`,`event`)
42
-) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
44
+  KEY `ude_active_watchers` (`pres_domain`,`pres_user`,`event`),
45
+  KEY `exp_active_watchers` (`expires`)
46
+) ENGINE=MyISAM;
43 47
 
44 48
 
45 49
 CREATE TABLE `watchers` (
... ...
@@ -211,3 +211,4 @@ void destroy_evlist()
211 211
 		shm_free(EvList);
212 212
     }
213 213
 }
214
+
... ...
@@ -93,6 +93,7 @@ struct ev
93 93
 	publ_handling_t  * evs_publ_handl;
94 94
 	subs_handling_t  * evs_subs_handl;
95 95
 	free_body_t* free_body;
96
+	int default_expires;
96 97
 	struct ev* wipeer;			
97 98
 	struct ev* next;
98 99
 	
... ...
@@ -113,6 +114,8 @@ typedef int (*add_event_t)(ev_t* event);
113 114
 
114 115
 ev_t* contains_event(str* name, str* param);
115 116
 
117
+typedef ev_t* (*contains_event_t) (str* name, str* param);
118
+
116 119
 void destroy_evlist();
117 120
 
118 121
 extern evlist_t* EvList;
... ...
@@ -742,7 +742,7 @@ dlg_t* build_dlg_t (str p_uri, subs_t* subs)
742 742
 	return td;
743 743
 
744 744
 error:
745
-	if(w_uri.s ==NULL)
745
+	if(w_uri.s)
746 746
 	{
747 747
 		pkg_free(w_uri.s);
748 748
 		w_uri.s= NULL;
... ...
@@ -59,8 +59,9 @@
59 59
 
60 60
 MODULE_VERSION
61 61
 
62
-#define S_TABLE_VERSION 1
63
-#define ACTWATCH_TABLE_VERSION 4
62
+#define S_TABLE_VERSION  1
63
+#define P_TABLE_VERSION  2
64
+#define ACTWATCH_TABLE_VERSION 5
64 65
 
65 66
 char *log_buf = NULL;
66 67
 static int clean_period=100;
... ...
@@ -91,6 +92,7 @@ struct sl_binds slb;
91 92
 
92 93
 static int mod_init(void);
93 94
 static int child_init(int);
95
+void destroy(void);
94 96
 int handle_publish(struct sip_msg*, char*, char*);
95 97
 int handle_subscribe(struct sip_msg*, char*, char*);
96 98
 int stored_pres_info(struct sip_msg* msg, char* pres_uri, char* s);
... ...
@@ -106,16 +108,12 @@ int expires_offset = 0;
106 108
 int default_expires = 3600;
107 109
 int max_expires = 3600;
108 110
 
109
-
110
-void destroy(void);
111
-
112 111
 static cmd_export_t cmds[]=
113 112
 {
114 113
 	{"handle_publish",		handle_publish,	     0,	   0,        REQUEST_ROUTE},
115 114
 	{"handle_publish",		handle_publish,	     1,fixup_presence,REQUEST_ROUTE},
116 115
 	{"handle_subscribe",	handle_subscribe,	 0,	   0,         REQUEST_ROUTE},
117 116
 	{"bind_presence",(cmd_function)bind_presence,1,    0,            0         },
118
-	{"add_event",    (cmd_function)add_event,    1,    0,            0         },
119 117
 	{0,						0,				     0,	   0,            0	       }	 
120 118
 };
121 119
 
... ...
@@ -225,10 +223,10 @@ static int mod_init(void)
225 223
 	_s.s = presentity_table;
226 224
 	_s.len = strlen(presentity_table);
227 225
 	 ver =  table_version(&pa_dbf, pa_db, &_s);
228
-	if(ver!=S_TABLE_VERSION)
226
+	if(ver!=P_TABLE_VERSION)
229 227
 	{
230 228
 		LOG(L_ERR,"PRESENCE:mod_init: Wrong version v%d for table <%s>,"
231
-				" need v%d\n", ver, _s.s, S_TABLE_VERSION);
229
+				" need v%d\n", ver, _s.s, P_TABLE_VERSION);
232 230
 		return -1;
233 231
 	}
234 232
 	
... ...
@@ -424,4 +422,6 @@ struct mi_root* refreshWatchers(struct mi_root* cmd, void* param)
424 422
 	}	
425 423
 	
426 424
 	return init_mi_tree(200, "OK", 2);
427
-}	
425
+}
426
+
427
+
... ...
@@ -82,7 +82,7 @@ int publ_send200ok(struct sip_msg *msg, int lexpire, str etag)
82 82
 	hdr_append.s = (char *)pkg_malloc( sizeof(char)*100);
83 83
 	if(hdr_append.s == NULL)
84 84
 	{
85
-		LOG(L_ERR,"ERROR:publ_send200ok: unable to add lump_rl\n");
85
+		LOG(L_ERR,"ERROR:publ_send200ok: ERROR no more memory\n");
86 86
 		return -1;
87 87
 	}
88 88
 	hdr_append.s[0]='\0';
... ...
@@ -107,7 +107,7 @@ int publ_send200ok(struct sip_msg *msg, int lexpire, str etag)
107 107
 	hdr_append2.s = (char *)pkg_malloc( sizeof(char)*(16+etag.len) );
108 108
 	if(hdr_append2.s == NULL)
109 109
 	{
110
-		LOG(L_ERR,"PRESENCE:publ_send200ok:ERROR unable to add lump_rl\n");
110
+		LOG(L_ERR,"PRESENCE:publ_send200ok:ERROR no more memory\n");
111 111
 		return -1;
112 112
 	}
113 113
 	hdr_append2.s[0]='\0';
... ...
@@ -39,8 +39,6 @@
39 39
 #include "notify.h"
40 40
 #include "../../ip_addr.h"
41 41
 
42
-#define LCONTACT_BUF_SIZE 1024
43
-
44 42
 int get_database_info(struct sip_msg* msg, subs_t* subs, unsigned int* remote_cseq, int* error_ret);
45 43
 
46 44
 static str su_200_rpl  = str_init("OK");
... ...
@@ -879,7 +877,6 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
879 877
 	str rtag_value;
880 878
 	subs_t subs;
881 879
 	static char buf[50];
882
-	static char cont_buf[LCONTACT_BUF_SIZE];
883 880
 	str rec_route= {0, 0};
884 881
 	int error_ret = -1;
885 882
 	int rt  = 0;
... ...
@@ -895,7 +892,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
895 892
 	db_val_t *row_vals ;
896 893
 	str status= {0, 0};
897 894
 	str reason= {0, 0};
898
-	str contact;
895
+	str* contact= NULL;
899 896
 	ev_t* event= NULL;
900 897
 	param_t* ev_param= NULL;
901 898
 	str ev_name;
... ...
@@ -1178,67 +1175,14 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
1178 1175
 	
1179 1176
 	if((!server_address.s) || (server_address.len== 0))
1180 1177
 	{
1181
-		str ip;
1182
-		char* proto;
1183
-		int port;
1184
-		int len;
1185
-
1186
-		memset(cont_buf, 0, LCONTACT_BUF_SIZE*sizeof(char));
1187
-		contact.s= cont_buf;
1188
-		contact.len= 0;
1189
-	
1190
-		if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
1191
-		{
1192
-			proto= "udp";
1193
-		}
1194
-		else
1195
-		if(msg->rcv.proto== PROTO_TLS )
1196
-		{
1197
-			proto= "tls";
1198
-		}
1199
-		else	
1200
-		if(msg->rcv.proto== PROTO_TCP)
1178
+		contact= get_local_contact(msg);
1179
+		if(contact== NULL)
1201 1180
 		{
1202
-			proto= "tcp";
1203
-		}
1204
-		else
1205
-		{
1206
-			LOG(L_ERR, "PRESENCE: handle_subscribe:ERROR unsupported proto\n");
1207
-			goto error;
1208
-		}	
1209
-		ip.s= ip_addr2a(&msg->rcv.dst_ip);
1210
-		if(ip.s== NULL)
1211
-		{
1212
-			LOG(L_ERR, "PRESENCE: handle_subscribe:ERROR while transforming ip_addr to ascii\n");
1181
+			LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR in function"
1182
+					" get_local_contact\n");
1213 1183
 			goto error;
1214 1184
 		}
1215
-		ip.len= strlen(ip.s);
1216
-		port = msg->rcv.dst_port;
1217
-
1218
-		if(strncmp(ip.s, "sip:", 4)!=0)
1219
-		{
1220
-			strncpy(contact.s, "sip:", 4);
1221
-			contact.len+= 4;
1222
-		}	
1223
-		strncpy(contact.s+contact.len, ip.s, ip.len);
1224
-		contact.len += ip.len;
1225
-		if(contact.len> LCONTACT_BUF_SIZE - 21)
1226
-		{
1227
-			LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR buffer overflow\n");
1228
-			goto error;
1229
-		}	
1230
-		len= sprintf(contact.s+contact.len, ":%d;transport=" , port);
1231
-		if(len< 0)
1232
-		{
1233
-			LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR in function sprintf\n");
1234
-			goto error;
1235
-
1236
-		}	
1237
-		contact.len+= len;
1238
-		strncpy(contact.s+ contact.len, proto, 3);
1239
-		contact.len += 3;
1240
-		
1241
-		subs.local_contact= contact;
1185
+		subs.local_contact= *contact;
1242 1186
 	}
1243 1187
 	else
1244 1188
 		subs.local_contact= server_address;
... ...
@@ -1535,7 +1479,12 @@ after_status:
1535 1479
 		if(subs.pres_domain.s)
1536 1480
 			pkg_free(subs.pres_domain.s);
1537 1481
 	}
1538
-
1482
+	if(contact)
1483
+	{	
1484
+		if(contact->s)
1485
+			pkg_free(contact->s);
1486
+		pkg_free(contact);
1487
+	}
1539 1488
 	return 1;
1540 1489
 
1541 1490
 bad_event:
... ...
@@ -1564,6 +1513,13 @@ error:
1564 1513
 		if(subs.pres_domain.s)
1565 1514
 			pkg_free(subs.pres_domain.s);
1566 1515
 	}
1516
+	if(contact)
1517
+	{	
1518
+		if(contact->s)
1519
+			pkg_free(contact->s);
1520
+		pkg_free(contact);
1521
+	}
1522
+
1567 1523
 	return error_ret;
1568 1524
 
1569 1525
 }
... ...
@@ -106,7 +106,7 @@ int reply_bad_event(struct sip_msg * msg)
106 106
 	hdr_append.len = sprintf(hdr_append.s, "Allow-Events: ");
107 107
 	if(hdr_append.len < 0)
108 108
 	{
109
-		LOG(L_ERR, "PRESENCE:handle_publish: ERROR unsuccessful sprintf\n");
109
+		LOG(L_ERR, "PRESENCE:reply_bad_event: ERROR unsuccessful sprintf\n");
110 110
 		return -1;
111 111
 	}
112 112
 
... ...
@@ -127,13 +127,13 @@ int reply_bad_event(struct sip_msg * msg)
127 127
 		
128 128
 	if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
129 129
 	{
130
-		LOG(L_ERR,"PRESENCE: handle_publish:ERROR unable to add lump_rl\n");
130
+		LOG(L_ERR,"PRESENCE: reply_bad_event:ERROR unable to add lump_rl\n");
131 131
 		return -1;
132 132
 	}
133 133
 
134 134
 	if (slb.reply(msg, 489, &pu_489_rpl) == -1)
135 135
 	{
136
-		LOG(L_ERR, "PRESENCE: handle_publish: Error while sending reply\n");
136
+		LOG(L_ERR, "PRESENCE: reply_bad_event: Error while sending reply\n");
137 137
 		return -1;
138 138
 	}
139 139
 	return 0;
... ...
@@ -37,6 +37,8 @@
37 37
 #include "../../str.h"
38 38
 #include "../../parser/msg_parser.h"
39 39
 
40
+#define LCONTACT_BUF_SIZE 1024
41
+
40 42
 static inline int uandd_to_uri(str user,  str domain, str *out)
41 43
 {
42 44
 	int size;
... ...
@@ -68,6 +70,89 @@ static inline int uandd_to_uri(str user,  str domain, str *out)
68 70
 	return 0;
69 71
 }
70 72
 
73
+static inline str* get_local_contact(struct sip_msg* msg)
74
+{
75
+	str ip;
76
+	char* proto;
77
+	int port;
78
+	int len;
79
+	str* contact;
80
+
81
+	contact= (str*)pkg_malloc(sizeof(str));
82
+	if(contact== NULL)
83
+	{
84
+		LOG(L_ERR, "PRESENCE: get_local_contact: ERROR No more memory\n");
85
+		return NULL;
86
+	}
87
+	contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE* sizeof(char));
88
+	if(contact->s== NULL)
89
+	{
90
+		LOG(L_ERR, "PRESENCE: get_local_contact: ERROR No more memory\n");
91
+		pkg_free(contact);
92
+		return NULL;
93
+	}
94
+
95
+	memset(contact->s, 0, LCONTACT_BUF_SIZE*sizeof(char));
96
+	contact->len= 0;
97
+	
98
+	if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
99
+		proto= "udp";
100
+	else
101
+	if(msg->rcv.proto== PROTO_TLS )
102
+			proto= "tls";
103
+	else	
104
+	if(msg->rcv.proto== PROTO_TCP)
105
+		proto= "tcp";
106
+	else
107
+	{
108
+		LOG(L_ERR, "PRESENCE: get_local_contact:ERROR unsupported proto\n");
109
+		pkg_free(contact->s);
110
+		pkg_free(contact);
111
+		return NULL;
112
+	}	
113
+	
114
+	ip.s= ip_addr2a(&msg->rcv.dst_ip);
115
+	if(ip.s== NULL)
116
+	{
117
+		LOG(L_ERR, "PRESENCE: get_local_contact:ERROR while transforming ip_addr to ascii\n");
118
+		pkg_free(contact->s);
119
+		pkg_free(contact);
120
+		return NULL;
121
+	}
122
+	ip.len= strlen(ip.s);
123
+	port = msg->rcv.dst_port;
124
+
125
+	if(strncmp(ip.s, "sip:", 4)!=0)
126
+	{
127
+		strncpy(contact->s, "sip:", 4);
128
+		contact->len+= 4;
129
+	}	
130
+	strncpy(contact->s+contact->len, ip.s, ip.len);
131
+	contact->len += ip.len;
132
+	if(contact->len> LCONTACT_BUF_SIZE - 21)
133
+	{
134
+		LOG(L_ERR, "PRESENCE: get_local_contact: ERROR buffer overflow\n");
135
+		pkg_free(contact->s);
136
+		pkg_free(contact);
137
+		return NULL;
138
+
139
+	}	
140
+	len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
141
+	if(len< 0)
142
+	{
143
+		LOG(L_ERR, "PRESENCE: get_local_contact: ERROR in function sprintf\n");
144
+		pkg_free(contact->s);
145
+		pkg_free(contact);
146
+		return NULL;
147
+	}	
148
+	contact->len+= len;
149
+	strncpy(contact->s+ contact->len, proto, 3);
150
+	contact->len += 3;
151
+	
152
+	return contact;
153
+	
154
+}
155
+
71 156
 //str* int_to_str(long int n);
72 157
 
73 158
 int a_to_i (char *s,int len);