- added some KEYs to presence tables to improve db query performance (table version...
authorAnca Vamanu <anca@voice-system.ro>
Thu, 19 Jul 2007 14:34:45 +0000 (14:34 +0000)
committerAnca Vamanu <anca@voice-system.ro>
Thu, 19 Jul 2007 14:34:45 +0000 (14:34 +0000)
- 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

12 files changed:
modules_k/presence/README
modules_k/presence/bind_presence.c
modules_k/presence/bind_presence.h
modules_k/presence/doc/presence.sql
modules_k/presence/event_list.c
modules_k/presence/event_list.h
modules_k/presence/notify.c
modules_k/presence/presence.c
modules_k/presence/presentity.c
modules_k/presence/subscribe.c
modules_k/presence/utils_func.c
modules_k/presence/utils_func.h

index b34e230..c29a814 100644 (file)
@@ -293,7 +293,7 @@ if(method=="SUBSCRIBE")
 use openser;
 
 CREATE TABLE `presentity` (
-  `id` int(10) NOT NULL auto_increment,
+  `id` int(10)  NOT NULL auto_increment,
   `username` varchar(64) NOT NULL,
   `domain` varchar(124) NOT NULL,
   `event` varchar(64) NOT NULL,
@@ -301,10 +301,13 @@ CREATE TABLE `presentity` (
   `expires` int(11) NOT NULL,
   `received_time` int(11) NOT NULL,
   `body` text NOT NULL,
-  UNIQUE KEY udee_presentity (`username`,`domain`,`event`,`etag`),
-  PRIMARY KEY (id)
+  UNIQUE KEY `uk_id` (`id`),
+  UNIQUE KEY `udee_presentity` (`username`,`domain`,`event`, `etag`),
+  KEY `ude_presentity` (`username`,`domain`,`event`),
+  KEY `exp_presentity` (`expires`)
 ) ENGINE=MyISAM;
 
+
 CREATE TABLE `active_watchers` (
   `id` int(10) NOT NULL auto_increment,
   `pres_user` varchar(64) NOT NULL,
@@ -314,7 +317,7 @@ CREATE TABLE `active_watchers` (
   `from_user` varchar(64) NOT NULL,
   `from_domain` varchar(128) NOT NULL,
   `event` varchar(64) NOT NULL default 'presence',
-  `event_id` varchar(64),
+  `event_id` varchar(64) default NULL,
   `to_tag` varchar(128) NOT NULL,
   `from_tag` varchar(128) NOT NULL,
   `callid` varchar(128) NOT NULL,
@@ -329,8 +332,9 @@ CREATE TABLE `active_watchers` (
   `local_contact` varchar(255) NOT NULL,
   PRIMARY KEY  (`id`),
   UNIQUE KEY `tt_watchers` (`to_tag`),
-  KEY `due_activewatchers` (`to_domain`,`to_user`,`event`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
+  KEY `ude_active_watchers` (`pres_domain`,`pres_user`,`event`),
+  KEY `exp_active_watchers` (`expires`)
+) ENGINE=MyISAM;
 
 
 CREATE TABLE `watchers` (
index 620bae5..90b3796 100644 (file)
@@ -35,12 +35,9 @@ int bind_presence(event_api_t* api)
                LOG(L_ERR, "NOTIFIER:bind_notifier: Invalid parameter value\n");
                return -1;
        }
-       api->add_event = (add_event_t )find_export("add_event", 1, 0);
-       if(!api->add_event)
-       {
-               LOG(L_ERR, "NOTIFIER:bind_notifier: Can't bind add_event\n");
-               return -1;
-       }
+       
+       api->add_event = add_event;
+       api->contains_event= contains_event;
 
        return 0;
 }
index 240bef4..bedc356 100644 (file)
@@ -33,6 +33,7 @@
 
 typedef struct event_api {
        add_event_t add_event;
+       contains_event_t contains_event;
 } event_api_t;
 
 int bind_presence(event_api_t* api);
index 229acff..2edd6a8 100644 (file)
@@ -2,7 +2,7 @@
 use openser;
 
 CREATE TABLE `presentity` (
-  `id` int(10) NOT NULL auto_increment,
+  `id` int(10)  NOT NULL auto_increment,
   `username` varchar(64) NOT NULL,
   `domain` varchar(124) NOT NULL,
   `event` varchar(64) NOT NULL,
@@ -10,10 +10,13 @@ CREATE TABLE `presentity` (
   `expires` int(11) NOT NULL,
   `received_time` int(11) NOT NULL,
   `body` text NOT NULL,
-  UNIQUE KEY udee_presentity (`username`,`domain`,`event`,`etag`),
-  PRIMARY KEY (id)
+  UNIQUE KEY `uk_id` (`id`),
+  UNIQUE KEY `udee_presentity` (`username`,`domain`,`event`, `etag`),
+  KEY `ude_presentity` (`username`,`domain`,`event`),
+  KEY `exp_presentity` (`expires`)
 ) ENGINE=MyISAM;
 
+
 CREATE TABLE `active_watchers` (
   `id` int(10) NOT NULL auto_increment,
   `pres_user` varchar(64) NOT NULL,
@@ -23,7 +26,7 @@ CREATE TABLE `active_watchers` (
   `from_user` varchar(64) NOT NULL,
   `from_domain` varchar(128) NOT NULL,
   `event` varchar(64) NOT NULL default 'presence',
-  `event_id` varchar(64),
+  `event_id` varchar(64) default NULL,
   `to_tag` varchar(128) NOT NULL,
   `from_tag` varchar(128) NOT NULL,
   `callid` varchar(128) NOT NULL,
@@ -38,8 +41,9 @@ CREATE TABLE `active_watchers` (
   `local_contact` varchar(255) NOT NULL,
   PRIMARY KEY  (`id`),
   UNIQUE KEY `tt_watchers` (`to_tag`),
-  KEY `due_activewatchers` (`to_domain`,`to_user`,`event`)
-) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
+  KEY `ude_active_watchers` (`pres_domain`,`pres_user`,`event`),
+  KEY `exp_active_watchers` (`expires`)
+) ENGINE=MyISAM;
 
 
 CREATE TABLE `watchers` (
index 8960939..a7a9df7 100644 (file)
@@ -211,3 +211,4 @@ void destroy_evlist()
                shm_free(EvList);
     }
 }
+
index 1ee93b8..f2e1434 100644 (file)
@@ -93,6 +93,7 @@ struct ev
        publ_handling_t  * evs_publ_handl;
        subs_handling_t  * evs_subs_handl;
        free_body_t* free_body;
+       int default_expires;
        struct ev* wipeer;                      
        struct ev* next;
        
@@ -113,6 +114,8 @@ typedef int (*add_event_t)(ev_t* event);
 
 ev_t* contains_event(str* name, str* param);
 
+typedef ev_t* (*contains_event_t) (str* name, str* param);
+
 void destroy_evlist();
 
 extern evlist_t* EvList;
index f820069..97a148c 100644 (file)
@@ -742,7 +742,7 @@ dlg_t* build_dlg_t (str p_uri, subs_t* subs)
        return td;
 
 error:
-       if(w_uri.s ==NULL)
+       if(w_uri.s)
        {
                pkg_free(w_uri.s);
                w_uri.s= NULL;
index fbc6a82..194895b 100644 (file)
@@ -59,8 +59,9 @@
 
 MODULE_VERSION
 
-#define S_TABLE_VERSION 1
-#define ACTWATCH_TABLE_VERSION 4
+#define S_TABLE_VERSION  1
+#define P_TABLE_VERSION  2
+#define ACTWATCH_TABLE_VERSION 5
 
 char *log_buf = NULL;
 static int clean_period=100;
@@ -91,6 +92,7 @@ struct sl_binds slb;
 
 static int mod_init(void);
 static int child_init(int);
+void destroy(void);
 int handle_publish(struct sip_msg*, char*, char*);
 int handle_subscribe(struct sip_msg*, char*, char*);
 int stored_pres_info(struct sip_msg* msg, char* pres_uri, char* s);
@@ -106,16 +108,12 @@ int expires_offset = 0;
 int default_expires = 3600;
 int max_expires = 3600;
 
-
-void destroy(void);
-
 static cmd_export_t cmds[]=
 {
        {"handle_publish",              handle_publish,      0,    0,        REQUEST_ROUTE},
        {"handle_publish",              handle_publish,      1,fixup_presence,REQUEST_ROUTE},
        {"handle_subscribe",    handle_subscribe,        0,        0,         REQUEST_ROUTE},
        {"bind_presence",(cmd_function)bind_presence,1,    0,            0         },
-       {"add_event",    (cmd_function)add_event,    1,    0,            0         },
        {0,                                             0,                                   0,    0,            0             }         
 };
 
@@ -225,10 +223,10 @@ static int mod_init(void)
        _s.s = presentity_table;
        _s.len = strlen(presentity_table);
         ver =  table_version(&pa_dbf, pa_db, &_s);
-       if(ver!=S_TABLE_VERSION)
+       if(ver!=P_TABLE_VERSION)
        {
                LOG(L_ERR,"PRESENCE:mod_init: Wrong version v%d for table <%s>,"
-                               " need v%d\n", ver, _s.s, S_TABLE_VERSION);
+                               " need v%d\n", ver, _s.s, P_TABLE_VERSION);
                return -1;
        }
        
@@ -424,4 +422,6 @@ struct mi_root* refreshWatchers(struct mi_root* cmd, void* param)
        }       
        
        return init_mi_tree(200, "OK", 2);
-}      
+}
+
+
index c11c996..445b69a 100644 (file)
@@ -82,7 +82,7 @@ int publ_send200ok(struct sip_msg *msg, int lexpire, str etag)
        hdr_append.s = (char *)pkg_malloc( sizeof(char)*100);
        if(hdr_append.s == NULL)
        {
-               LOG(L_ERR,"ERROR:publ_send200ok: unable to add lump_rl\n");
+               LOG(L_ERR,"ERROR:publ_send200ok: ERROR no more memory\n");
                return -1;
        }
        hdr_append.s[0]='\0';
@@ -107,7 +107,7 @@ int publ_send200ok(struct sip_msg *msg, int lexpire, str etag)
        hdr_append2.s = (char *)pkg_malloc( sizeof(char)*(16+etag.len) );
        if(hdr_append2.s == NULL)
        {
-               LOG(L_ERR,"PRESENCE:publ_send200ok:ERROR unable to add lump_rl\n");
+               LOG(L_ERR,"PRESENCE:publ_send200ok:ERROR no more memory\n");
                return -1;
        }
        hdr_append2.s[0]='\0';
index 98c135b..0a1263d 100644 (file)
@@ -39,8 +39,6 @@
 #include "notify.h"
 #include "../../ip_addr.h"
 
-#define LCONTACT_BUF_SIZE 1024
-
 int get_database_info(struct sip_msg* msg, subs_t* subs, unsigned int* remote_cseq, int* error_ret);
 
 static str su_200_rpl  = str_init("OK");
@@ -879,7 +877,6 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        str rtag_value;
        subs_t subs;
        static char buf[50];
-       static char cont_buf[LCONTACT_BUF_SIZE];
        str rec_route= {0, 0};
        int error_ret = -1;
        int rt  = 0;
@@ -895,7 +892,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        db_val_t *row_vals ;
        str status= {0, 0};
        str reason= {0, 0};
-       str contact;
+       str* contact= NULL;
        ev_t* event= NULL;
        param_t* ev_param= NULL;
        str ev_name;
@@ -1178,67 +1175,14 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        
        if((!server_address.s) || (server_address.len== 0))
        {
-               str ip;
-               char* proto;
-               int port;
-               int len;
-
-               memset(cont_buf, 0, LCONTACT_BUF_SIZE*sizeof(char));
-               contact.s= cont_buf;
-               contact.len= 0;
-       
-               if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
-               {
-                       proto= "udp";
-               }
-               else
-               if(msg->rcv.proto== PROTO_TLS )
-               {
-                       proto= "tls";
-               }
-               else    
-               if(msg->rcv.proto== PROTO_TCP)
+               contact= get_local_contact(msg);
+               if(contact== NULL)
                {
-                       proto= "tcp";
-               }
-               else
-               {
-                       LOG(L_ERR, "PRESENCE: handle_subscribe:ERROR unsupported proto\n");
-                       goto error;
-               }       
-               ip.s= ip_addr2a(&msg->rcv.dst_ip);
-               if(ip.s== NULL)
-               {
-                       LOG(L_ERR, "PRESENCE: handle_subscribe:ERROR while transforming ip_addr to ascii\n");
+                       LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR in function"
+                                       " get_local_contact\n");
                        goto error;
                }
-               ip.len= strlen(ip.s);
-               port = msg->rcv.dst_port;
-
-               if(strncmp(ip.s, "sip:", 4)!=0)
-               {
-                       strncpy(contact.s, "sip:", 4);
-                       contact.len+= 4;
-               }       
-               strncpy(contact.s+contact.len, ip.s, ip.len);
-               contact.len += ip.len;
-               if(contact.len> LCONTACT_BUF_SIZE - 21)
-               {
-                       LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR buffer overflow\n");
-                       goto error;
-               }       
-               len= sprintf(contact.s+contact.len, ":%d;transport=" , port);
-               if(len< 0)
-               {
-                       LOG(L_ERR, "PRESENCE: handle_subscribe: ERROR in function sprintf\n");
-                       goto error;
-
-               }       
-               contact.len+= len;
-               strncpy(contact.s+ contact.len, proto, 3);
-               contact.len += 3;
-               
-               subs.local_contact= contact;
+               subs.local_contact= *contact;
        }
        else
                subs.local_contact= server_address;
@@ -1535,7 +1479,12 @@ after_status:
                if(subs.pres_domain.s)
                        pkg_free(subs.pres_domain.s);
        }
-
+       if(contact)
+       {       
+               if(contact->s)
+                       pkg_free(contact->s);
+               pkg_free(contact);
+       }
        return 1;
 
 bad_event:
@@ -1564,6 +1513,13 @@ error:
                if(subs.pres_domain.s)
                        pkg_free(subs.pres_domain.s);
        }
+       if(contact)
+       {       
+               if(contact->s)
+                       pkg_free(contact->s);
+               pkg_free(contact);
+       }
+
        return error_ret;
 
 }
index ec31b3a..fd4ba51 100644 (file)
@@ -106,7 +106,7 @@ int reply_bad_event(struct sip_msg * msg)
        hdr_append.len = sprintf(hdr_append.s, "Allow-Events: ");
        if(hdr_append.len < 0)
        {
-               LOG(L_ERR, "PRESENCE:handle_publish: ERROR unsuccessful sprintf\n");
+               LOG(L_ERR, "PRESENCE:reply_bad_event: ERROR unsuccessful sprintf\n");
                return -1;
        }
 
@@ -127,13 +127,13 @@ int reply_bad_event(struct sip_msg * msg)
                
        if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
        {
-               LOG(L_ERR,"PRESENCE: handle_publish:ERROR unable to add lump_rl\n");
+               LOG(L_ERR,"PRESENCE: reply_bad_event:ERROR unable to add lump_rl\n");
                return -1;
        }
 
        if (slb.reply(msg, 489, &pu_489_rpl) == -1)
        {
-               LOG(L_ERR, "PRESENCE: handle_publish: Error while sending reply\n");
+               LOG(L_ERR, "PRESENCE: reply_bad_event: Error while sending reply\n");
                return -1;
        }
        return 0;
index 216deb6..6a2bbc4 100644 (file)
@@ -37,6 +37,8 @@
 #include "../../str.h"
 #include "../../parser/msg_parser.h"
 
+#define LCONTACT_BUF_SIZE 1024
+
 static inline int uandd_to_uri(str user,  str domain, str *out)
 {
        int size;
@@ -68,6 +70,89 @@ static inline int uandd_to_uri(str user,  str domain, str *out)
        return 0;
 }
 
+static inline str* get_local_contact(struct sip_msg* msg)
+{
+       str ip;
+       char* proto;
+       int port;
+       int len;
+       str* contact;
+
+       contact= (str*)pkg_malloc(sizeof(str));
+       if(contact== NULL)
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact: ERROR No more memory\n");
+               return NULL;
+       }
+       contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE* sizeof(char));
+       if(contact->s== NULL)
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact: ERROR No more memory\n");
+               pkg_free(contact);
+               return NULL;
+       }
+
+       memset(contact->s, 0, LCONTACT_BUF_SIZE*sizeof(char));
+       contact->len= 0;
+       
+       if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
+               proto= "udp";
+       else
+       if(msg->rcv.proto== PROTO_TLS )
+                       proto= "tls";
+       else    
+       if(msg->rcv.proto== PROTO_TCP)
+               proto= "tcp";
+       else
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact:ERROR unsupported proto\n");
+               pkg_free(contact->s);
+               pkg_free(contact);
+               return NULL;
+       }       
+       
+       ip.s= ip_addr2a(&msg->rcv.dst_ip);
+       if(ip.s== NULL)
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact:ERROR while transforming ip_addr to ascii\n");
+               pkg_free(contact->s);
+               pkg_free(contact);
+               return NULL;
+       }
+       ip.len= strlen(ip.s);
+       port = msg->rcv.dst_port;
+
+       if(strncmp(ip.s, "sip:", 4)!=0)
+       {
+               strncpy(contact->s, "sip:", 4);
+               contact->len+= 4;
+       }       
+       strncpy(contact->s+contact->len, ip.s, ip.len);
+       contact->len += ip.len;
+       if(contact->len> LCONTACT_BUF_SIZE - 21)
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact: ERROR buffer overflow\n");
+               pkg_free(contact->s);
+               pkg_free(contact);
+               return NULL;
+
+       }       
+       len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
+       if(len< 0)
+       {
+               LOG(L_ERR, "PRESENCE: get_local_contact: ERROR in function sprintf\n");
+               pkg_free(contact->s);
+               pkg_free(contact);
+               return NULL;
+       }       
+       contact->len+= len;
+       strncpy(contact->s+ contact->len, proto, 3);
+       contact->len += 3;
+       
+       return contact;
+       
+}
+
 //str* int_to_str(long int n);
 
 int a_to_i (char *s,int len);