[Freeswitch-svn] [commit] r7793 - freeswitch/trunk/src/mod/endpoints/mod_sofia

Freeswitch SVN brian at freeswitch.org
Wed Mar 5 15:31:20 EST 2008


Author: brian
Date: Wed Mar  5 15:31:18 2008
New Revision: 7793

Modified:
   freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c

Log:
support multiple subscriptions for sla goodies

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	Wed Mar  5 15:31:18 2008
@@ -265,8 +265,7 @@
 	if (session && (tech_pvt->profile->pflags & PFLAG_PRESENCE)) {
 		char *sql = switch_mprintf("delete from sip_dialogs where call_id='%q'", tech_pvt->call_id);
 		switch_assert(sql);
-		sofia_glue_execute_sql(tech_pvt->profile, SWITCH_FALSE, sql, tech_pvt->profile->ireg_mutex);
-		free(sql);
+		sofia_glue_execute_sql(tech_pvt->profile, &sql, SWITCH_TRUE);
 	}
 
 	if (tech_pvt->kick && (a_session = switch_core_session_locate(tech_pvt->kick))) {

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	Wed Mar  5 15:31:18 2008
@@ -88,12 +88,6 @@
 #include <sofia-sip/nea.h>
 #include <sofia-sip/msg_addr.h>
 
-typedef struct {
-	switch_bool_t master;
-	char *sql;
-} sofia_sql_job_t;
-
-
 typedef enum {
 	DTMF_2833,
 	DTMF_INFO,
@@ -509,7 +503,7 @@
 					 char const *phrase,
 					 nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]);
 
-void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex);
+void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t dyn);
 void sofia_glue_actually_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex);
 void sofia_reg_check_expire(sofia_profile_t *profile, time_t now);
 void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	Wed Mar  5 15:31:18 2008
@@ -73,8 +73,7 @@
 		
 		sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id);
 		switch_assert(sql != NULL);
-		sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-		free(sql);
+		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		nua_handle_destroy(nh);
 	}
 }
@@ -169,8 +168,7 @@
 
 			sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", user, host);
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Deleting registration for %s@%s\n", user, host);
-			sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL);
-			switch_safe_free(sql);
+			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		}
 	}
 
@@ -398,16 +396,13 @@
 		}
 
 		switch_mutex_lock(profile->ireg_mutex);
-		sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-		switch_safe_free(sql);
-
+		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
+		
 		sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','Registered', '%q', %ld, '%q')",
 							  call_id, from_user, from_host, contact_str, rpid, expires, user_agent);
 
 		if (sql) {
-			sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-			switch_safe_free(sql);
-			sql = NULL;
+			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Propagating registration for %s@%s->%s\n", from_user, from_host, contact_str);
 		}
 		switch_mutex_unlock(profile->ireg_mutex);
@@ -424,9 +419,8 @@
 	sofia_profile_t *profile = (sofia_profile_t *) obj;
 	uint32_t ireg_loops = 0;
 	uint32_t gateway_loops = 0;
-	void *pop;
-	sofia_sql_job_t *job;
 	int loops = 0;
+	uint32_t qsize;
 
 	ireg_loops = IREG_SECONDS;
 	gateway_loops = GATEWAY_SECONDS;
@@ -435,15 +429,19 @@
 	
 	switch_queue_create(&profile->sql_queue, 500000, profile->pool);
 	
-	while ((mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) || switch_queue_size(profile->sql_queue)) {
-		while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
-			job = (sofia_sql_job_t *) pop;
-			sofia_glue_actually_execute_sql(profile, job->master, job->sql, NULL);
-			free(job->sql);
-			free(job);
-			job = NULL;
+	qsize = switch_queue_size(profile->sql_queue);
+
+	while ((mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) || qsize) {
+		if (qsize) {
+			void *pop;
+			switch_mutex_lock(profile->ireg_mutex);
+			while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
+				sofia_glue_actually_execute_sql(profile, SWITCH_TRUE, (char *) pop, NULL);
+				free(pop);
+			}
+			switch_mutex_unlock(profile->ireg_mutex);
 		}
-		
+
 		if (++loops >= 100) {
 			if (++ireg_loops >= IREG_SECONDS) {
 				sofia_reg_check_expire(profile, switch_timestamp(NULL));
@@ -458,6 +456,7 @@
 		}
 
 		switch_yield(10000);
+		qsize = switch_queue_size(profile->sql_queue);
 	}
 	
 	sofia_clear_pflag_locked(profile, PFLAG_WORKER_RUNNING);
@@ -470,13 +469,20 @@
 {
 	switch_thread_t *thread;
 	switch_threadattr_t *thd_attr = NULL;
+	int x = 0;
 
 	switch_threadattr_create(&thd_attr, profile->pool);
 	switch_threadattr_detach_set(thd_attr, 1);
 	switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
 	switch_threadattr_priority_increase(thd_attr);
 	switch_thread_create(&thread, thd_attr, sofia_profile_worker_thread_run, profile, profile->pool);
-	switch_yield(1000000);
+
+	while(!sofia_test_pflag(profile, PFLAG_WORKER_RUNNING)) {
+		switch_yield(100000);
+		if (++x >= 100) {
+			break;
+		}
+	}
 }
 
 void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void *obj)
@@ -544,6 +550,8 @@
 				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("presence")),
 				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("dialog")),
 				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("call-info")),
+				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("sla")),
+				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("include-session-description")),
 				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("presence.winfo")),
 				   TAG_IF((profile->pflags & PFLAG_PRESENCE), NUTAG_ALLOW_EVENTS("message-summary")),
 				   SIPTAG_SUPPORTED_STR("100rel, precondition, timer"), SIPTAG_USER_AGENT_STR(profile->user_agent), TAG_END());
@@ -599,11 +607,12 @@
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting thread for %s\n", profile->name);
 
 	profile->started = switch_timestamp(NULL);
-	switch_yield(1000000);
 
 	sofia_set_pflag_locked(profile, PFLAG_RUNNING);
 	launch_sofia_worker_thread(profile);
 
+	switch_yield(1000000);
+
 	while (mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING) && sofia_test_pflag(profile, PFLAG_WORKER_RUNNING)) {
 		su_root_step(profile->s_root, 1000);
 	}
@@ -1489,15 +1498,13 @@
 
 					switch_assert(sql);
 					
-					sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-					free(sql);
+					sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 				}
 			} else if (status == 200 && (profile->pflags & PFLAG_PRESENCE)) { 
 				char *sql = NULL;
 				sql = switch_mprintf("update sip_dialogs set state='%s' where uuid='%s';\n", astate, switch_core_session_get_uuid(session));
 				switch_assert(sql); 
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex); 
-				free(sql);
+				sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); 
 			}
 		}
 	}
@@ -2873,8 +2880,7 @@
 								 );
 			
 			switch_assert(sql);
-			sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-			free(sql);
+			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		}
 		
 		return;

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	Wed Mar  5 15:31:18 2008
@@ -2443,27 +2443,34 @@
 }
 
 
-void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex)
+void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t dyn)
 {
 	switch_status_t status = SWITCH_STATUS_FALSE;
-	sofia_sql_job_t *job = NULL;
+	char *d_sql = NULL, *sql;
+
+	switch_assert(sqlp && *sqlp);
+	sql = *sqlp;
 	
 	if (profile->sql_queue) {
-		switch_zmalloc(job, sizeof(*job));
+		if (dyn) {
+			d_sql = sql;
+		} else {
+			d_sql = strdup(sql);
+		}
 
-		job->sql = strdup(sql);
-		switch_assert(job->sql);
-		job->master = master;
-		
-		status = switch_queue_trypush(profile->sql_queue, job);
+		switch_assert(d_sql);
+		status = switch_queue_trypush(profile->sql_queue, d_sql);
 	}
 	
-	if (status != SWITCH_STATUS_SUCCESS) {
-		if (job) {
-			free(job->sql);
-			free(job);
+	if (status == SWITCH_STATUS_SUCCESS) {
+		if (dyn) {
+			*sqlp = NULL;
+		}
+	} else {
+		if (!dyn) {
+			switch_safe_free(d_sql);
 		}
-		sofia_glue_actually_execute_sql(profile, master, sql, mutex);
+		sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
 	}
 
 }

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	Wed Mar  5 15:31:18 2008
@@ -461,9 +461,10 @@
 			
 
 			if (!switch_strlen_zero((char *)helper.stream.data)) {
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, helper.stream.data, profile->ireg_mutex);
+				char *ssql = (char *)helper.stream.data;
+				sofia_glue_execute_sql(profile, &ssql, SWITCH_TRUE);
+				helper.stream.data = NULL;
 			}
-			switch_safe_free(helper.stream.data);
 		}
 	}
 	switch_mutex_unlock(mod_sofia_globals.hash_mutex);
@@ -781,7 +782,7 @@
 			ct = "application/dialog-info+xml";
 		}
 		
-		if (astate && uuid && helper && helper->stream.data) {
+		if (!switch_strlen_zero(astate) && !switch_strlen_zero(uuid) && helper && helper->stream.data) {
 			helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where uuid='%s';\n", astate, uuid);
 		}
 
@@ -1131,20 +1132,23 @@
 		exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
 		exp = (long) switch_timestamp(NULL) + exp_raw;
 
-		switch_mutex_lock(profile->ireg_mutex);
+		if (sofia_test_pflag(profile, PFLAG_MULTIREG)) {
+			sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id);
+		} else {
+			sql = switch_mprintf("delete from sip_subscriptions where "
+								 "proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'",
+								 proto,
+								 from_user,
+								 from_host,
+								 to_user,
+								 to_host, event
+								 );
+		}
 
-		sql = switch_mprintf("delete from sip_subscriptions where "
-							 "proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'",
-							 proto,
-							 from_user,
-							 from_host,
-							 to_user,
-							 to_host, event
-							 );
 
+		switch_mutex_lock(profile->ireg_mutex);
 		switch_assert(sql != NULL);
-		sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-		free(sql);
+		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 
 		if (sub_state == nua_substate_terminated) {
 			sstr = switch_mprintf("terminated");
@@ -1162,8 +1166,7 @@
 								 contact_str, call_id, full_from, full_via, exp, full_agent, accept);
 			
 			switch_assert(sql != NULL);
-			sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-			free(sql);
+			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 
 			sstr = switch_mprintf("active;expires=%ld", exp_raw);
 			if (status < 200) {
@@ -1295,8 +1298,7 @@
 				if ((sql =
 					 switch_mprintf("update sip_registrations set status='%q',rpid='%q' where sip_user='%q' and sip_host='%q'",
 									note_txt, rpid, from_user, from_host))) {
-					sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-					switch_safe_free(sql);
+					sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 				}
 
 				event_type = sip_header_as_string(profile->home, (void *) sip->sip_event);

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	Wed Mar  5 15:31:18 2008
@@ -210,6 +210,7 @@
 void sofia_reg_check_expire(sofia_profile_t *profile, time_t now)
 {
 	char sql[1024];
+	char *psql = sql;
 
 #ifdef SWITCH_HAVE_ODBC
     if (profile->odbc_dsn) {
@@ -245,19 +246,19 @@
 	} else {
 		switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0");
 	}
-	sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL);
+	sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE);
 	if (now) {
 		switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld", (long) now);
 	} else {
 		switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0");
 	}
-	sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL);
+	sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE);
 	if (now) {
 		switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0 and expires <= %ld", (long) now);
 	} else {
 		switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires > 0");
 	}
-	sofia_glue_execute_sql(profile, SWITCH_TRUE, sql, NULL);
+	sofia_glue_execute_sql(profile, &psql, SWITCH_FALSE);
 
 	if (now) {
 		switch_snprintf(sql, sizeof(sql), "select * from sip_registrations where status like '%%NATHACK%%'");
@@ -317,13 +318,10 @@
 	switch_uuid_get(&uuid);
 	switch_uuid_format(uuid_str, &uuid);
 
-	switch_mutex_lock(profile->ireg_mutex);
 	sql = switch_mprintf("insert into sip_authentication (nonce, expires) values('%q', %ld)",
 						 uuid_str, switch_timestamp(NULL) + profile->nonce_ttl);
 	switch_assert(sql != NULL);
-	sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-	switch_safe_free(sql);
-	switch_mutex_unlock(profile->ireg_mutex);
+	sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 
 	auth_str =
 		switch_mprintf("Digest realm=\"%q\", nonce=\"%q\",%s algorithm=MD5, qop=\"auth\"", realm, uuid_str, stale ? " stale=\"true\"," : "");
@@ -521,8 +519,7 @@
 			sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, to_host);
 		}
 		switch_mutex_lock(profile->ireg_mutex);
-		sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-		switch_safe_free(sql);
+		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		
 		sql = switch_mprintf("insert into sip_registrations values ('%q', '%q','%q','%q','%q', '%q', %ld, '%q')", call_id,
 							 to_user, to_host, contact_str, reg_desc,
@@ -530,9 +527,7 @@
 
 		
 		if (sql) {
-			sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
-			switch_safe_free(sql);
-			sql = NULL;
+			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		}
 		switch_mutex_unlock(profile->ireg_mutex);
 
@@ -573,29 +568,21 @@
 			if ((p = strchr(icontact + 4, ':'))) {
 				*p = '\0';
 			}
-			if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q' and contact like '%%%q%%'", to_user, to_host, icontact))) {
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-				switch_safe_free(sql);
-				sql = NULL;
+			if ((sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id))) {
+				sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 			}
 			
 			if ((sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id))) {
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-				switch_safe_free(sql);
-				sql = NULL;
+				sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 			}
 			switch_safe_free(icontact);
 		} else {
 			if ((sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q'", to_user, to_host))) {
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-				switch_safe_free(sql);
-				sql = NULL;
+				sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 			}
 			
 			if ((sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, to_host))) {
-				sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, profile->ireg_mutex);
-				switch_safe_free(sql);
-				sql = NULL;
+				sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 			}
 		}
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_OUT) == SWITCH_STATUS_SUCCESS) {
@@ -654,19 +641,25 @@
 	if (!sip || !sip->sip_request || !sip->sip_request->rq_method_name) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received an invalid packet!\n");
 		nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
-		return;
+		goto end;
 	}
 
 	if (!(sip->sip_contact && sip->sip_contact->m_url)) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO CONTACT!\n");
 		nua_respond(nh, 400, "Missing Contact Header", TAG_END());
-		return;
+		goto end;
 	}
 
 	sofia_reg_handle_register(nua, profile, nh, sip, REG_REGISTER, key, sizeof(key), &v_event);
+
 	if (v_event) {
 		switch_event_fire(&v_event);
 	}
+
+ end:	
+
+	nua_handle_destroy(nh);
+
 }
 
 



More information about the Freeswitch-svn mailing list